summaryrefslogtreecommitdiffstats
path: root/java/com/android/voicemail/impl/mail/Address.java
blob: 3a7a8660712c42fc94464fe431b668915952fc7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.voicemail.impl.mail;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.VisibleForTesting;
import android.text.Html;
import android.text.TextUtils;
import android.text.util.Rfc822Token;
import android.text.util.Rfc822Tokenizer;
import com.android.voicemail.impl.mail.utils.LogUtils;
import java.util.ArrayList;
import java.util.regex.Pattern;
import org.apache.james.mime4j.codec.EncoderUtil;
import org.apache.james.mime4j.decoder.DecoderUtil;

/**
 * This class represent email address.
 *
 * <p>RFC822 email address may have following format. "name" <address> (comment) "name" <address>
 * name <address> address Name and comment part should be MIME/base64 encoded in header if
 * necessary.
 */
public class Address implements Parcelable {
  public static final String ADDRESS_DELIMETER = ",";
  /** Address part, in the form local_part@domain_part. No surrounding angle brackets. */
  private String mAddress;

  /**
   * Name part. No surrounding double quote, and no MIME/base64 encoding. This must be null if
   * Address has no name part.
   */
  private String mPersonal;

  /**
   * When personal is set, it will return the first token of the personal string. Otherwise, it will
   * return the e-mail address up to the '@' sign.
   */
  private String mSimplifiedName;

  // Regex that matches address surrounded by '<>' optionally. '^<?([^>]+)>?$'
  private static final Pattern REMOVE_OPTIONAL_BRACKET = Pattern.compile("^<?([^>]+)>?$");
  // Regex that matches personal name surrounded by '""' optionally. '^"?([^"]+)"?$'
  private static final Pattern REMOVE_OPTIONAL_DQUOTE = Pattern.compile("^\"?([^\"]*)\"?$");
  // Regex that matches escaped character '\\([\\"])'
  private static final Pattern UNQUOTE = Pattern.compile("\\\\([\\\\\"])");

  // TODO: LOCAL_PART and DOMAIN_PART_PART are too permissive and can be improved.
  // TODO: Fix this to better constrain comments.
  /** Regex for the local part of an email address. */
  private static final String LOCAL_PART = "[^@]+";
  /** Regex for each part of the domain part, i.e. the thing between the dots. */
  private static final String DOMAIN_PART_PART = "[[\\w][\\d]\\-\\(\\)\\[\\]]+";
  /** Regex for the domain part, which is two or more {@link #DOMAIN_PART_PART} separated by . */
  private static final String DOMAIN_PART = "(" + DOMAIN_PART_PART + "\\.)+" + DOMAIN_PART_PART;

  /** Pattern to check if an email address is valid. */
  private static final Pattern EMAIL_ADDRESS =
      Pattern.compile("\\A" + LOCAL_PART + "@" + DOMAIN_PART + "\\z");

  private static final Address[] EMPTY_ADDRESS_ARRAY = new Address[0];

  // delimiters are chars that do not appear in an email address, used by fromHeader
  private static final char LIST_DELIMITER_EMAIL = '\1';
  private static final char LIST_DELIMITER_PERSONAL = '\2';

  private static final String LOG_TAG = "Email Address";

  @VisibleForTesting
  public Address(String address) {
    setAddress(address);
  }

  public Address(String address, String personal) {
    setPersonal(personal);
    setAddress(address);
  }

  /**
   * Returns a simplified string for this e-mail address. When a name is known, it will return the
   * first token of that name. Otherwise, it will return the e-mail address up to the '@' sign.
   */
  public String getSimplifiedName() {
    if (mSimplifiedName == null) {
      if (TextUtils.isEmpty(mPersonal) && !TextUtils.isEmpty(mAddress)) {
        int atSign = mAddress.indexOf('@');
        mSimplifiedName = (atSign != -1) ? mAddress.substring(0, atSign) : "";
      } else if (!TextUtils.isEmpty(mPersonal)) {

        // TODO: use Contacts' NameSplitter for more reliable first-name extraction

        int end = mPersonal.indexOf(' ');
        while (end > 0 && mPersonal.charAt(end - 1) == ',') {
          end--;
        }
        mSimplifiedName = (end < 1) ? mPersonal : mPersonal.substring(0, end);

      } else {
        LogUtils.w(LOG_TAG, "Unable to get a simplified name");
        mSimplifiedName = "";
      }
    }
    return mSimplifiedName;
  }

  public static synchronized Address getEmailAddress(String rawAddress) {
    if (TextUtils.isEmpty(rawAddress)) {
      return null;
    }
    String name, address;
    final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(rawAddress);
    if (tokens.length > 0) {
      final String tokenizedName = tokens[0].getName();
      name = tokenizedName != null ? Html.fromHtml(tokenizedName.trim()).toString() : "";
      address = Html.fromHtml(tokens[0].getAddress()).toString();
    } else {
      name = "";
      address = rawAddress == null ? "" : Html.fromHtml(rawAddress).toString();
    }
    return new Address(address, name);
  }

  public String getAddress() {
    return mAddress;
  }

  public void setAddress(String address) {
    mAddress = REMOVE_OPTIONAL_BRACKET.matcher(address).replaceAll("$1");
  }

  /**
   * Get name part as UTF-16 string. No surrounding double quote, and no MIME/base64 encoding.
   *
   * @return Name part of email address. Returns null if it is omitted.
   */
  public String getPersonal() {
    return mPersonal;
  }

  /**
   * Set personal part from UTF-16 string. Optional surrounding double quote will be removed. It
   * will be also unquoted and MIME/base64 decoded.
   *
   * @param personal name part of email address as UTF-16 string. Null is acceptable.
   */
  public void setPersonal(String personal) {
    mPersonal = decodeAddressPersonal(personal);
  }

  /**
   * Decodes name from UTF-16 string. Optional surrounding double quote will be removed. It will be
   * also unquoted and MIME/base64 decoded.
   *
   * @param personal name part of email address as UTF-16 string. Null is acceptable.
   */
  public static String decodeAddressPersonal(String personal) {
    if (personal != null) {
      personal = REMOVE_OPTIONAL_DQUOTE.matcher(personal).replaceAll("$1");
      personal = UNQUOTE.matcher(personal).replaceAll("$1");
      personal = DecoderUtil.decodeEncodedWords(personal);
      if (personal.length() == 0) {
        personal = null;
      }
    }
    return personal;
  }

  /**
   * This method is used to check that all the addresses that the user entered in a list (e.g. To:)
   * are valid, so that none is dropped.
   */
  @VisibleForTesting
  public static boolean isAllValid(String addressList) {
    // This code mimics the parse() method below.
    // I don't know how to better avoid the code-duplication.
    if (addressList != null && addressList.length() > 0) {
      Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addressList);
      for (int i = 0, length = tokens.length; i < length; ++i) {
        Rfc822Token token = tokens[i];
        String address = token.getAddress();
        if (!TextUtils.isEmpty(address) && !isValidAddress(address)) {
          return false;
        }
      }
    }
    return true;
  }

  /**
   * Parse a comma-delimited list of addresses in RFC822 format and return an array of Address
   * objects.
   *
   * @param addressList Address list in comma-delimited string.
   * @return An array of 0 or more Addresses.
   */
  public static Address[] parse(String addressList) {
    if (addressList == null || addressList.length() == 0) {
      return EMPTY_ADDRESS_ARRAY;
    }
    Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(addressList);
    ArrayList<Address> addresses = new ArrayList<Address>();
    for (int i = 0, length = tokens.length; i < length; ++i) {
      Rfc822Token token = tokens[i];
      String address = token.getAddress();
      if (!TextUtils.isEmpty(address)) {
        if (isValidAddress(address)) {
          String name = token.getName();
          if (TextUtils.isEmpty(name)) {
            name = null;
          }
          addresses.add(new Address(address, name));
        }
      }
    }
    return addresses.toArray(new Address[addresses.size()]);
  }

  /** Checks whether a string email address is valid. E.g. name@domain.com is valid. */
  @VisibleForTesting
  static boolean isValidAddress(final String address) {
    return EMAIL_ADDRESS.matcher(address).find();
  }

  @Override
  public boolean equals(Object o) {
    if (o instanceof Address) {
      // It seems that the spec says that the "user" part is case-sensitive,
      // while the domain part in case-insesitive.
      // So foo@yahoo.com and Foo@yahoo.com are different.
      // This may seem non-intuitive from the user POV, so we
      // may re-consider it if it creates UI trouble.
      // A problem case is "replyAll" sending to both
      // a@b.c and to A@b.c, which turn out to be the same on the server.
      // Leave unchanged for now (i.e. case-sensitive).
      return getAddress().equals(((Address) o).getAddress());
    }
    return super.equals(o);
  }

  @Override
  public int hashCode() {
    return getAddress().hashCode();
  }

  /**
   * Get human readable address string. Do not use this for email header.
   *
   * @return Human readable address string. Not quoted and not encoded.
   */
  @Override
  public String toString() {
    if (mPersonal != null && !mPersonal.equals(mAddress)) {
      if (mPersonal.matches(".*[\\(\\)<>@,;:\\\\\".\\[\\]].*")) {
        return ensureQuotedString(mPersonal) + " <" + mAddress + ">";
      } else {
        return mPersonal + " <" + mAddress + ">";
      }
    } else {
      return mAddress;
    }
  }

  /**
   * Ensures that the given string starts and ends with the double quote character. The string is
   * not modified in any way except to add the double quote character to start and end if it's not
   * already there.
   *
   * <p>sample -> "sample" "sample" -> "sample" ""sample"" -> "sample" "sample"" -> "sample"
   * sa"mp"le -> "sa"mp"le" "sa"mp"le" -> "sa"mp"le" (empty string) -> "" " -> ""
   */
  private static String ensureQuotedString(String s) {
    if (s == null) {
      return null;
    }
    if (!s.matches("^\".*\"$")) {
      return "\"" + s + "\"";
    } else {
      return s;
    }
  }

  /**
   * Get human readable comma-delimited address string.
   *
   * @param addresses Address array
   * @return Human readable comma-delimited address string.
   */
  @VisibleForTesting
  public static String toString(Address[] addresses) {
    return toString(addresses, ADDRESS_DELIMETER);
  }

  /**
   * Get human readable address strings joined with the specified separator.
   *
   * @param addresses Address array
   * @param separator Separator
   * @return Human readable comma-delimited address string.
   */
  public static String toString(Address[] addresses, String separator) {
    if (addresses == null || addresses.length == 0) {
      return null;
    }
    if (addresses.length == 1) {
      return addresses[0].toString();
    }
    StringBuilder sb = new StringBuilder(addresses[0].toString());
    for (int i = 1; i < addresses.length; i++) {
      sb.append(separator);
      // TODO: investigate why this .trim() is needed.
      sb.append(addresses[i].toString().trim());
    }
    return sb.toString();
  }

  /**
   * Get RFC822/MIME compatible address string.
   *
   * @return RFC822/MIME compatible address string. It may be surrounded by double quote or quoted
   *     and MIME/base64 encoded if necessary.
   */
  public String toHeader() {
    if (mPersonal != null) {
      return EncoderUtil.encodeAddressDisplayName(mPersonal) + " <" + mAddress + ">";
    } else {
      return mAddress;
    }
  }

  /**
   * Get RFC822/MIME compatible comma-delimited address string.
   *
   * @param addresses Address array
   * @return RFC822/MIME compatible comma-delimited address string. it may be surrounded by double
   *     quoted or quoted and MIME/base64 encoded if necessary.
   */
  public static String toHeader(Address[] addresses) {
    if (addresses == null || addresses.length == 0) {
      return null;
    }
    if (addresses.length == 1) {
      return addresses[0].toHeader();
    }
    StringBuilder sb = new StringBuilder(addresses[0].toHeader());
    for (int i = 1; i < addresses.length; i++) {
      // We need space character to be able to fold line.
      sb.append(", ");
      sb.append(addresses[i].toHeader());
    }
    return sb.toString();
  }

  /**
   * Get Human friendly address string.
   *
   * @return the personal part of this Address, or the address part if the personal part is not
   *     available
   */
  @VisibleForTesting
  public String toFriendly() {
    if (mPersonal != null && mPersonal.length() > 0) {
      return mPersonal;
    } else {
      return mAddress;
    }
  }

  /**
   * Creates a comma-delimited list of addresses in the "friendly" format (see toFriendly() for
   * details on the per-address conversion).
   *
   * @param addresses Array of Address[] values
   * @return A comma-delimited string listing all of the addresses supplied. Null if source was null
   *     or empty.
   */
  @VisibleForTesting
  public static String toFriendly(Address[] addresses) {
    if (addresses == null || addresses.length == 0) {
      return null;
    }
    if (addresses.length == 1) {
      return addresses[0].toFriendly();
    }
    StringBuilder sb = new StringBuilder(addresses[0].toFriendly());
    for (int i = 1; i < addresses.length; i++) {
      sb.append(", ");
      sb.append(addresses[i].toFriendly());
    }
    return sb.toString();
  }

  /** Returns exactly the same result as Address.toString(Address.fromHeader(addressList)). */
  @VisibleForTesting
  public static String fromHeaderToString(String addressList) {
    return toString(fromHeader(addressList));
  }

  /** Returns exactly the same result as Address.toHeader(Address.parse(addressList)). */
  @VisibleForTesting
  public static String parseToHeader(String addressList) {
    return Address.toHeader(Address.parse(addressList));
  }

  /**
   * Returns null if the addressList has 0 addresses, otherwise returns the first address. The same
   * as Address.fromHeader(addressList)[0] for non-empty list. This is an utility method that offers
   * some performance optimization opportunities.
   */
  @VisibleForTesting
  public static Address firstAddress(String addressList) {
    Address[] array = fromHeader(addressList);
    return array.length > 0 ? array[0] : null;
  }

  /**
   * This method exists to convert an address list formatted in a deprecated legacy format to the
   * standard RFC822 header format. {@link #fromHeader(String)} is capable of reading the legacy
   * format and the RFC822 format. {@link #toHeader()} always produces the RFC822 format.
   *
   * <p>This implementation is brute-force, and could be replaced with a more efficient version if
   * desired.
   */
  public static String reformatToHeader(String addressList) {
    return toHeader(fromHeader(addressList));
  }

  /**
   * @param addressList a CSV of RFC822 addresses or the deprecated legacy string format
   * @return array of addresses parsed from <code>addressList</code>
   */
  @VisibleForTesting
  public static Address[] fromHeader(String addressList) {
    if (addressList == null || addressList.length() == 0) {
      return EMPTY_ADDRESS_ARRAY;
    }
    // IF we're CSV, just parse
    if ((addressList.indexOf(LIST_DELIMITER_PERSONAL) == -1)
        && (addressList.indexOf(LIST_DELIMITER_EMAIL) == -1)) {
      return Address.parse(addressList);
    }
    // Otherwise, do backward-compatible unpack
    ArrayList<Address> addresses = new ArrayList<Address>();
    int length = addressList.length();
    int pairStartIndex = 0;
    int pairEndIndex;

    /* addressEndIndex is only re-scanned (indexOf()) when a LIST_DELIMITER_PERSONAL
       is used, not for every email address; i.e. not for every iteration of the while().
       This reduces the theoretical complexity from quadratic to linear,
       and provides some speed-up in practice by removing redundant scans of the string.
    */
    int addressEndIndex = addressList.indexOf(LIST_DELIMITER_PERSONAL);

    while (pairStartIndex < length) {
      pairEndIndex = addressList.indexOf(LIST_DELIMITER_EMAIL, pairStartIndex);
      if (pairEndIndex == -1) {
        pairEndIndex = length;
      }
      Address address;
      if (addressEndIndex == -1 || pairEndIndex <= addressEndIndex) {
        // in this case the DELIMITER_PERSONAL is in a future pair,
        // so don't use personal, and don't update addressEndIndex
        address = new Address(addressList.substring(pairStartIndex, pairEndIndex), null);
      } else {
        address =
            new Address(
                addressList.substring(pairStartIndex, addressEndIndex),
                addressList.substring(addressEndIndex + 1, pairEndIndex));
        // only update addressEndIndex when we use the LIST_DELIMITER_PERSONAL
        addressEndIndex = addressList.indexOf(LIST_DELIMITER_PERSONAL, pairEndIndex + 1);
      }
      addresses.add(address);
      pairStartIndex = pairEndIndex + 1;
    }
    return addresses.toArray(new Address[addresses.size()]);
  }

  public static final Creator<Address> CREATOR =
      new Creator<Address>() {
        @Override
        public Address createFromParcel(Parcel parcel) {
          return new Address(parcel);
        }

        @Override
        public Address[] newArray(int size) {
          return new Address[size];
        }
      };

  public Address(Parcel in) {
    setPersonal(in.readString());
    setAddress(in.readString());
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel out, int flags) {
    out.writeString(mPersonal);
    out.writeString(mAddress);
  }
}