2 * Copyright notice from original mutt:
3 * Copyright (C) 2003 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
16 #include "mutt_idna.h"
18 /* The low-level interface we use. */
22 int mutt_idna_to_local (const char *in, char **out, int flags)
24 *out = safe_strdup (in);
28 int mutt_local_to_idna (const char *in, char **out)
30 *out = safe_strdup (in);
36 int mutt_idna_to_local (const char *in, char **out, int flags)
40 if (!option (OPTUSEIDN))
46 /* Is this the right function? Interesting effects with some bad identifiers! */
47 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
49 if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
53 * make sure that we can convert back and come out with the same
57 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
60 char *tmp = safe_strdup (*out);
62 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
64 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
66 if (!irrev && ascii_strcasecmp (t2, in)) {
69 "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
85 *out = safe_strdup (in);
89 int mutt_local_to_idna (const char *in, char **out)
92 char *tmp = safe_strdup (in);
101 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
103 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
109 *out = safe_strdup (in);
117 /* higher level functions */
119 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
126 p = strchr (mbx, '@');
129 *user = safe_calloc ((p - mbx + 1), sizeof (mbx[0]));
130 strfcpy (*user, mbx, (p - mbx + 1));
131 *domain = safe_strdup (p + 1);
135 int mutt_addrlist_to_idna (ADDRESS * a, char **err)
137 char *user = NULL, *domain = NULL;
144 for (; a; a = a->next) {
147 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
150 if (mutt_local_to_idna (domain, &tmp) < 0) {
153 *err = safe_strdup (domain);
156 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
157 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
171 int mutt_addrlist_to_local (ADDRESS * a)
176 for (; a; a = a->next) {
179 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
182 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
183 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
184 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
195 /* convert just for displaying purposes */
196 const char *mutt_addr_for_display (ADDRESS * a)
198 static char *buff = NULL;
201 /* user and domain will be either allocated or reseted to the NULL in
202 * the mbox_to_udomain(), but for safety... */
208 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
210 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
217 safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
218 sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
225 /* Convert an ENVELOPE structure */
227 void mutt_env_to_local (ENVELOPE * e)
229 mutt_addrlist_to_local (e->return_path);
230 mutt_addrlist_to_local (e->from);
231 mutt_addrlist_to_local (e->to);
232 mutt_addrlist_to_local (e->cc);
233 mutt_addrlist_to_local (e->bcc);
234 mutt_addrlist_to_local (e->reply_to);
235 mutt_addrlist_to_local (e->mail_followup_to);
238 /* Note that `a' in the `env->a' expression is macro argument, not
239 * "real" name of an `env' compound member. Real name will be substituted
240 * by preprocessor at the macro-expansion time.
242 #define H_TO_IDNA(a) \
243 if (mutt_addrlist_to_idna (env->a, err) && !e) \
245 if (tag) *tag = #a; e = 1; err = NULL; \
248 int mutt_env_to_idna (ENVELOPE * env, char **tag, char **err)
252 H_TO_IDNA (return_path);
257 H_TO_IDNA (reply_to);
258 H_TO_IDNA (mail_followup_to);