2 * Copyright (C) 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 #include "mutt_idna.h"
27 /* The low-level interface we use. */
31 int mutt_idna_to_local (const char *in, char **out, int flags)
33 *out = safe_strdup (in);
37 int mutt_local_to_idna (const char *in, char **out)
39 *out = safe_strdup (in);
45 int mutt_idna_to_local (const char *in, char **out, int flags)
49 if (!option (OPTUSEIDN))
55 /* Is this the right function? Interesting effects with some bad identifiers! */
56 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
58 if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
62 * make sure that we can convert back and come out with the same
66 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
69 char *tmp = safe_strdup (*out);
71 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
73 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
75 if (!irrev && ascii_strcasecmp (t2, in)) {
78 "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
94 *out = safe_strdup (in);
98 int mutt_local_to_idna (const char *in, char **out)
101 char *tmp = safe_strdup (in);
110 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
112 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
118 *out = safe_strdup (in);
126 /* higher level functions */
128 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
135 p = strchr (mbx, '@');
138 *user = safe_calloc ((p - mbx + 1), sizeof (mbx[0]));
139 strfcpy (*user, mbx, (p - mbx + 1));
140 *domain = safe_strdup (p + 1);
144 int mutt_addrlist_to_idna (ADDRESS * a, char **err)
146 char *user = NULL, *domain = NULL;
153 for (; a; a = a->next) {
156 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
159 if (mutt_local_to_idna (domain, &tmp) < 0) {
162 *err = safe_strdup (domain);
165 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
166 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
180 int mutt_addrlist_to_local (ADDRESS * a)
185 for (; a; a = a->next) {
188 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
191 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
192 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
193 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
204 /* convert just for displaying purposes */
205 const char *mutt_addr_for_display (ADDRESS * a)
207 static char *buff = NULL;
210 /* user and domain will be either allocated or reseted to the NULL in
211 * the mbox_to_udomain(), but for safety... */
217 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
219 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
226 safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
227 sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
234 /* Convert an ENVELOPE structure */
236 void mutt_env_to_local (ENVELOPE * e)
238 mutt_addrlist_to_local (e->return_path);
239 mutt_addrlist_to_local (e->from);
240 mutt_addrlist_to_local (e->to);
241 mutt_addrlist_to_local (e->cc);
242 mutt_addrlist_to_local (e->bcc);
243 mutt_addrlist_to_local (e->reply_to);
244 mutt_addrlist_to_local (e->mail_followup_to);
247 /* Note that `a' in the `env->a' expression is macro argument, not
248 * "real" name of an `env' compound member. Real name will be substituted
249 * by preprocessor at the macro-expansion time.
251 #define H_TO_IDNA(a) \
252 if (mutt_addrlist_to_idna (env->a, err) && !e) \
254 if (tag) *tag = #a; e = 1; err = NULL; \
257 int mutt_env_to_idna (ENVELOPE * env, char **tag, char **err)
261 H_TO_IDNA (return_path);
266 H_TO_IDNA (reply_to);
267 H_TO_IDNA (mail_followup_to);