summaryrefslogtreecommitdiff
path: root/kpimutils/email.h
blob: bd48ed849029bd866522906eac833ae7d446df24 (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
/*  -*- mode: C++; c-file-style: "gnu" -*-

  This file is part of the kpimutils library.
  Copyright (c) 2004 Matt Douhan <matt@fruitsalad.org>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/
/**
  @file
  This file is part of the KDEPIM Utilities library and provides
  static methods for email address validation.

  @brief
  Email address validation methods.

  @author Matt Douhan \<matt@fruitsalad.org\>
 */

#ifndef KPIMUTILS_EMAIL_H
#define KPIMUTILS_EMAIL_H

#include <KDE/KUrl>

#include <QtCore/QStringList>
#include <QtCore/QByteArray>

#include "kpimutils_export.h"

namespace KPIMUtils {

  /**
    @defgroup emailvalidation Email Validation Functions

    This collection of methods that can validate email addresses as supplied
    by the user (typically, user input from a text box). There are also
    functions for splitting an RFC2822 address into its component parts.

    @{
  */

  /**
    Email validation result. The only 'success' code in
    this enumeration is AddressOK; all the other values
    indicate some specific problem with the address which
    is being validated.

    Result type for splitAddress(), isValidAddress()
    and isValidSimpleAddress().
  */
  enum EmailParseResult {
    AddressOk,          /**< Email is valid */
    AddressEmpty,       /**< The address is empty */
    UnexpectedEnd,      /**< Something is unbalanced */
    UnbalancedParens,   /**< Unbalanced ( ) */
    MissingDomainPart,  /**< No domain in address */
    UnclosedAngleAddr,  /**< \< with no matching \> */
    UnopenedAngleAddr,  /**< \> with no preceding \< */
    TooManyAts,         /**< More than one \@ in address */
    UnexpectedComma,    /**< Comma not allowed here */
    TooFewAts,          /**< Missing \@ in address */
    MissingLocalPart,   /**< No address specified, only domain */
    UnbalancedQuote,    /**< Quotes (single or double) not matched */
    NoAddressSpec,
    DisallowedChar,     /**< An invalid character detected in address */
    InvalidDisplayName  /**< An invalid displayname detected in address */
  };

  /** Split a comma separated list of email addresses.

  @param aStr a single string representing a list of addresses
  @return a list of strings, where each string is one address
  from the original list
  */
  KPIMUTILS_EXPORT
  QStringList splitAddressList( const QString &aStr );

  /**
    Splits the given address into display name, email address and comment.
    Returns AddressOk if no error was encountered. Otherwise an appropriate
    error code is returned. In case of an error the values of displayName,
    addrSpec and comment are undefined.

    @param address      a single email address,
    example: Joe User (comment1) <joe.user@example.org> (comment2)
    @param displayName  only out: the display-name of the email address, i.e.
    "Joe User" in the example; in case of an error the
    return value is undefined
    @param addrSpec     only out: the addr-spec, i.e. "joe.user@example.org"
    in the example; in case of an error the return value is undefined
    @param comment      only out: the space-separated comments, i.e.
    "comment1 comment2" in the example; in case of an
    error the return value is undefined

    @return             AddressOk if no error was encountered. Otherwise an
    appropriate error code is returned.
  */
  KPIMUTILS_EXPORT
  EmailParseResult splitAddress( const QByteArray &address,
                                 QByteArray &displayName,
                                 QByteArray &addrSpec,
                                 QByteArray &comment );

  /**
    This is an overloaded member function, provided for convenience.
    It behaves essentially like the above function.

    Splits the given address into display name, email address and comment.
    Returns AddressOk if no error was encountered. Otherwise an appropriate
    error code is returned. In case of an error the values of displayName,
    addrSpec and comment are undefined.

    @param address      a single email address,
    example: Joe User (comment1) <joe.user@example.org> (comment2)
    @param displayName  only out: the display-name of the email address, i.e.
    "Joe User" in the example; in case of an error the
    return value is undefined
    @param addrSpec     only out: the addr-spec, i.e. "joe.user@example.org"
    in the example; in case of an error the return value is undefined
    @param comment      only out: the space-separated comments, i.e.
    "comment1 comment2" in the example; in case of an
    error the return value is undefined

    @return             AddressOk if no error was encountered. Otherwise an
    appropriate error code is returned.
  */
  KPIMUTILS_EXPORT
  EmailParseResult splitAddress( const QString &address,
                                 QString &displayName,
                                 QString &addrSpec,
                                 QString &comment );

  /**
    Validates an email address in the form of "Joe User" <joe@example.org>.
    Returns AddressOk if no error was encountered. Otherwise an appropriate
    error code is returned.

    @param aStr         a single email address,
    example: Joe User (comment1) <joe.user@example.org>
    @return             AddressOk if no error was encountered. Otherwise an
    appropriate error code is returned.
  */
  KPIMUTILS_EXPORT
  EmailParseResult isValidAddress( const QString &aStr );

  /**
    Validates a list of email addresses, and also allow aliases and
    distribution lists to be expanded before validation.

    @param aStr         a string containing a list of email addresses.
    @param badAddr      a string to hold the address that was faulty.

    @return AddressOk if no error was encountered. Otherwise an
    appropriate error code is returned.
  */
  KPIMUTILS_EXPORT
  EmailParseResult isValidAddressList( const QString &aStr,
                                       QString &badAddr );

  /**
    Translate the enum errorcodes from emailParseResult
    into i18n'd strings that can be used for msg boxes.

    @param errorCode an @em error code returned from one of the
    email validation functions. Do not pass
    AddressOk as a value, since that will yield
    a misleading error message

    @return human-readable and already translated message describing
    the validation error.
  */
  KPIMUTILS_EXPORT
  QString emailParseResultToString( EmailParseResult errorCode );

  /**
    Validates an email address in the form of joe@example.org.
    Returns true if no error was encountered.
    This method should be used when the input field should not
    allow a "full" email address with comments and other special
    cases that normally are valid in an email address.

    @param aStr         a single email address,
    example: joe.user@example.org

    @return             true if no error was encountered.

    @note This method differs from calling isValidAddress()
    and checking that that returns AddressOk in two ways:
    it is faster, and it does @em not allow fancy addresses.
  */
  KPIMUTILS_EXPORT
  bool isValidSimpleAddress( const QString &aStr );

  /**
    Returns a i18n string to be used in msgboxes. This allows for error
    messages to be the same across the board.

    @return             An i18n ready string for use in msgboxes.
  */

  KPIMUTILS_EXPORT
  QString simpleEmailAddressErrorMsg();

  /** @}  */

  /** @defgroup emailextraction Email Extraction Functions
      @{
  */

  /**
    Returns the pure email address (addr-spec in RFC2822) of the given address
    (mailbox in RFC2822).

    @param address  an email address, e.g. "Joe User <joe.user@example.org>"
    @return         the addr-spec of @a address, i.e. joe.user@example.org
    in the example
  */
  KPIMUTILS_EXPORT
  QByteArray extractEmailAddress( const QByteArray & address );

  /**
    This is an overloaded member function, provided for convenience.
    It behaves essentially like the above function.

    Returns the pure email address (addr-spec in RFC2822) of the given
    address (mailbox in RFC2822).

    @param address  an email address, e.g. "Joe User <joe.user@example.org>"
    @return         the addr-spec of @a address, i.e. joe.user@example.org
    in the example
  */
  KPIMUTILS_EXPORT
  QString extractEmailAddress( const QString & address );

  /**
    Returns the pure email address (addr-spec in RFC2822) of the first
    email address of a list of addresses.

    @param addresses an email address, e.g. "Joe User <joe.user@example.org>"
    @return          the addr-spec of @a addresses, i.e. joe.user@example.org
    in the example
  */
  KPIMUTILS_EXPORT
  QByteArray firstEmailAddress( const QByteArray & addresses );

  /**
    This is an overloaded member function, provided for convenience.
    It behaves essentially like the above function.

    Returns the pure email address (addr-spec in RFC2822) of the first
    email address of a list of addresses.

    @param addresses an email address, e.g. "Joe User <joe.user@example.org>"
    @return          the addr-spec of @a addresses, i.e. joe.user@example.org
    in the example
  */
  KPIMUTILS_EXPORT
  QString firstEmailAddress( const QString & addresses );

  /**
    Return email address and name from string.
    Examples:
      "Stefan Taferner <taferner@example.org>" returns "taferner@example.org"
      and "Stefan Taferner". "joe@example.com" returns "joe@example.com"
      and "". Note that this only returns the first address.

    Also note that the return value is true if both the name and the
    mail are not empty: this does NOT tell you if mail contains a
    valid email address or just some rubbish.

    @param aStr  an email address, e.g "Joe User <joe.user@example.org>"
    @param name  only out: returns the displayname, "Joe User" in the example
    @param mail  only out: returns the email address "joe.user@example.org"
    in the example

    @return true if both name and email address are not empty
  */
  KPIMUTILS_EXPORT
  bool extractEmailAddressAndName( const QString &aStr, QString &mail,
                                   QString &name );

  /**
    Compare two email addresses. If matchName is false, it just checks
    the email address, and returns true if this matches. If matchName
    is true, both the name and the email must be the same.

    @param email1  the first email address to use for comparison
    @param email2  the second email address to use for comparison
    @param matchName  if set to true email address and displayname must match

    @return true if the comparison matches true in all other cases
  */
  KPIMUTILS_EXPORT
  bool compareEmail( const QString &email1, const QString &email2,
                     bool matchName );

  /**
    Returns a normalized address built from the given parts. The normalized
    address is of one the following forms:
      - displayName (comment) &lt;addrSpec&gt;
      - displayName &lt;addrSpec&gt;
      - comment &lt;addrSpec&gt;
      - addrSpec

    @param displayName  the display name of the address
    @param addrSpec     the actual email address (addr-spec in RFC 2822)
    @param comment      a comment

    @return             a normalized address built from the given parts
  */
  KPIMUTILS_EXPORT
  QString normalizedAddress( const QString &displayName,
                             const QString &addrSpec,
                             const QString &comment );

  /** @} */

  /** @defgroup emailidn Email IDN (punycode) handling
      @{
  */

  /**
    Decodes the punycode domain part of the given addr-spec if it's an IDN.

    @param addrSpec  a pure 7-bit email address (addr-spec in RFC2822)
    @return          the email address with Unicode domain
  */
  KPIMUTILS_EXPORT
  QString fromIdn( const QString &addrSpec );

  /**
    Encodes the domain part of the given addr-spec in punycode if it's an IDN.

    @param addrSpec  a pure email address with Unicode domain
    @return          the email address with domain in punycode
  */
  KPIMUTILS_EXPORT
  QString toIdn( const QString &addrSpec );

  /**
    Normalizes all email addresses in the given list and decodes all IDNs.

    @param addresses  a list of email addresses with punycoded IDNs
    @return           the email addresses in normalized form with Unicode IDNs
  */
  KPIMUTILS_EXPORT
  QString normalizeAddressesAndDecodeIdn( const QString &addresses );

  /**
    Normalizes all email addresses in the given list and encodes all IDNs
    in punycode.

    @param str  a list of email addresses
    @return     the email addresses in normalized form
  */
  KPIMUTILS_EXPORT
  QString normalizeAddressesAndEncodeIdn( const QString &str );

  /** @} */

  /** @ingroup emailextraction

    Add quote characters around the given string if it contains a
    character that makes that necessary, in an email name, such as ",".

    @param str  a string that may need quoting
    @return     the string quoted if necessary
  */
  KPIMUTILS_EXPORT
  QString quoteNameIfNecessary( const QString &str );

  /**
    * Creates a valid mailto: URL from the given mailbox.
    * @param mailbox The mailbox, which means the display name and the address specification, for
    *                example "Thomas McGuire" <thomas@domain.com>. The display name is optional.
    * @return a valid mailto: URL for the given mailbox.
    * @since 4.4.3
    */
  KPIMUTILS_EXPORT
  KUrl encodeMailtoUrl( const QString &mailbox );

  /**
    * Extracts the mailbox out of the mailto: URL.
    * @param mailtoUrl the URL with the mailto protocol, which contains the mailbox to be extracted
    * @return the mailbox, which means the display name and the address specification.
    * @since 4.4.3
    */
  KPIMUTILS_EXPORT
  QString decodeMailtoUrl( const KUrl &mailtoUrl );

} // namespace

#endif