2 * Copyright (C) 2003,2005,2008 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_idna.h"
27 /* The low-level interface we use. */
31 /* check whether an address is an IDN */
33 static int check_idn (ADDRESS *ap)
37 if (!ap || !ap->mailbox)
43 for (p = strchr (ap->mailbox, '@'); p && *p; p = strchr (p, '.'))
44 if (ascii_strncasecmp (++p, "xn--", 4) == 0)
54 static int mutt_idna_to_local (const char *in, char **out, int flags)
58 if (!option (OPTUSEIDN))
64 /* Is this the right function? Interesting effects with some bad identifiers! */
65 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
68 /* we don't want charset-hook effects, so we set flags to 0 */
69 if (mutt_convert_string (out, "utf-8", Charset, 0) == -1)
73 * make sure that we can convert back and come out with the same
77 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0)
81 char *tmp = safe_strdup (*out);
83 /* we don't want charset-hook effects, so we set flags to 0 */
84 if (mutt_convert_string (&tmp, Charset, "utf-8", 0) == -1)
86 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
88 if (!irrev && ascii_strcasecmp (t2, in))
90 dprint (1, (debugfile, "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
105 FREE (out); /* __FREE_CHECKED__ */
106 *out = safe_strdup (in);
110 static int mutt_local_to_idna (const char *in, char **out)
113 char *tmp = safe_strdup (in);
122 /* we don't want charset-hook effects, so we set flags to 0 */
123 if (mutt_convert_string (&tmp, Charset, "utf-8", 0) == -1)
125 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
131 FREE (out); /* __FREE_CHECKED__ */
132 *out = safe_strdup (in);
137 /* higher level functions */
139 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
141 static char *buff = NULL;
144 mutt_str_replace (&buff, mbx);
146 p = strchr (buff, '@');
155 int mutt_addrlist_to_idna (ADDRESS *a, char **err)
157 char *user = NULL, *domain = NULL;
164 for (; a; a = a->next)
168 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
171 if (mutt_local_to_idna (domain, &tmp) < 0)
175 *err = safe_strdup (domain);
179 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
180 sprintf (a->mailbox, "%s@%s", NONULL(user), NONULL(tmp)); /* __SPRINTF_CHECKED__ */
193 int mutt_addrlist_to_local (ADDRESS *a)
198 for (; a; a = a->next)
204 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
206 if (mutt_idna_to_local (domain, &tmp, 0) == 0)
208 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
209 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
219 /* convert just for displaying purposes */
220 const char *mutt_addr_for_display (ADDRESS *a)
222 static char *buff = NULL;
224 /* user and domain will be either allocated or reseted to the NULL in
225 * the mbox_to_udomain(), but for safety... */
233 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
235 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0)
241 safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
242 sprintf (buff, "%s@%s", NONULL(user), NONULL(tmp)); /* __SPRINTF_CHECKED__ */
247 /* Convert an ENVELOPE structure */
249 void mutt_env_to_local (ENVELOPE *e)
251 mutt_addrlist_to_local (e->return_path);
252 mutt_addrlist_to_local (e->from);
253 mutt_addrlist_to_local (e->to);
254 mutt_addrlist_to_local (e->cc);
255 mutt_addrlist_to_local (e->bcc);
256 mutt_addrlist_to_local (e->reply_to);
257 mutt_addrlist_to_local (e->mail_followup_to);
260 /* Note that `a' in the `env->a' expression is macro argument, not
261 * "real" name of an `env' compound member. Real name will be substituted
262 * by preprocessor at the macro-expansion time.
264 #define H_TO_IDNA(a) \
265 if (mutt_addrlist_to_idna (env->a, err) && !e) \
267 if (tag) *tag = #a; e = 1; err = NULL; \
270 int mutt_env_to_idna (ENVELOPE *env, char **tag, char **err)
273 H_TO_IDNA(return_path);
279 H_TO_IDNA(mail_followup_to);
285 #endif /* HAVE_LIBIDN */