1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.phone.common.mail.store.imap;
17
18import com.android.phone.common.mail.utils.LogUtils;
19
20import java.util.ArrayList;
21
22/**
23 * Utility methods for use with IMAP.
24 */
25public class ImapUtility {
26    public static final String TAG = "ImapUtility";
27    /**
28     * Apply quoting rules per IMAP RFC,
29     * quoted          = DQUOTE *QUOTED-CHAR DQUOTE
30     * QUOTED-CHAR     = <any TEXT-CHAR except quoted-specials> / "\" quoted-specials
31     * quoted-specials = DQUOTE / "\"
32     *
33     * This is used primarily for IMAP login, but might be useful elsewhere.
34     *
35     * NOTE:  Not very efficient - you may wish to preflight this, or perhaps it should check
36     * for trouble chars before calling the replace functions.
37     *
38     * @param s The string to be quoted.
39     * @return A copy of the string, having undergone quoting as described above
40     */
41    public static String imapQuoted(String s) {
42
43        // First, quote any backslashes by replacing \ with \\
44        // regex Pattern:  \\    (Java string const = \\\\)
45        // Substitute:     \\\\  (Java string const = \\\\\\\\)
46        String result = s.replaceAll("\\\\", "\\\\\\\\");
47
48        // Then, quote any double-quotes by replacing " with \"
49        // regex Pattern:  "    (Java string const = \")
50        // Substitute:     \\"  (Java string const = \\\\\")
51        result = result.replaceAll("\"", "\\\\\"");
52
53        // return string with quotes around it
54        return "\"" + result + "\"";
55    }
56
57    /**
58     * Gets all of the values in a sequence set per RFC 3501. Any ranges are expanded into a
59     * list of individual numbers. If the set is invalid, an empty array is returned.
60     * <pre>
61     * sequence-number = nz-number / "*"
62     * sequence-range  = sequence-number ":" sequence-number
63     * sequence-set    = (sequence-number / sequence-range) *("," sequence-set)
64     * </pre>
65     */
66    public static String[] getImapSequenceValues(String set) {
67        ArrayList<String> list = new ArrayList<String>();
68        if (set != null) {
69            String[] setItems = set.split(",");
70            for (String item : setItems) {
71                if (item.indexOf(':') == -1) {
72                    // simple item
73                    try {
74                        Integer.parseInt(item); // Don't need the value; just ensure it's valid
75                        list.add(item);
76                    } catch (NumberFormatException e) {
77                        LogUtils.d(TAG, "Invalid UID value", e);
78                    }
79                } else {
80                    // range
81                    for (String rangeItem : getImapRangeValues(item)) {
82                        list.add(rangeItem);
83                    }
84                }
85            }
86        }
87        String[] stringList = new String[list.size()];
88        return list.toArray(stringList);
89    }
90
91    /**
92     * Expand the given number range into a list of individual numbers. If the range is not valid,
93     * an empty array is returned.
94     * <pre>
95     * sequence-number = nz-number / "*"
96     * sequence-range  = sequence-number ":" sequence-number
97     * sequence-set    = (sequence-number / sequence-range) *("," sequence-set)
98     * </pre>
99     */
100    public static String[] getImapRangeValues(String range) {
101        ArrayList<String> list = new ArrayList<String>();
102        try {
103            if (range != null) {
104                int colonPos = range.indexOf(':');
105                if (colonPos > 0) {
106                    int first  = Integer.parseInt(range.substring(0, colonPos));
107                    int second = Integer.parseInt(range.substring(colonPos + 1));
108                    if (first < second) {
109                        for (int i = first; i <= second; i++) {
110                            list.add(Integer.toString(i));
111                        }
112                    } else {
113                        for (int i = first; i >= second; i--) {
114                            list.add(Integer.toString(i));
115                        }
116                    }
117                }
118            }
119        } catch (NumberFormatException e) {
120            LogUtils.d(TAG, "Invalid range value", e);
121        }
122        String[] stringList = new String[list.size()];
123        return list.toArray(stringList);
124    }
125}