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"
22 /* The low-level interface we use. */
26 int mutt_idna_to_local (const char *in, char **out, int flags)
28 *out = safe_strdup (in);
32 int mutt_local_to_idna (const char *in, char **out)
34 *out = safe_strdup (in);
40 int mutt_idna_to_local (const char *in, char **out, int flags)
44 if (!option (OPTUSEIDN))
50 /* Is this the right function? Interesting effects with some bad identifiers! */
51 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
53 if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
57 * make sure that we can convert back and come out with the same
61 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
64 char *tmp = safe_strdup (*out);
66 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
68 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
70 if (!irrev && ascii_strcasecmp (t2, in)) {
73 "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
89 *out = safe_strdup (in);
93 int mutt_local_to_idna (const char *in, char **out)
96 char *tmp = safe_strdup (in);
105 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
107 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
113 *out = safe_strdup (in);
121 /* higher level functions */
123 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
130 p = strchr (mbx, '@');
133 *user = safe_calloc ((p - mbx + 1), sizeof (mbx[0]));
134 strfcpy (*user, mbx, (p - mbx + 1));
135 *domain = safe_strdup (p + 1);
139 int mutt_addrlist_to_idna (ADDRESS * a, char **err)
141 char *user = NULL, *domain = NULL;
148 for (; a; a = a->next) {
151 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
154 if (mutt_local_to_idna (domain, &tmp) < 0) {
157 *err = safe_strdup (domain);
160 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
161 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
175 int mutt_addrlist_to_local (ADDRESS * a)
180 for (; a; a = a->next) {
183 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
186 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
187 safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
188 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
199 /* convert just for displaying purposes */
200 const char *mutt_addr_for_display (ADDRESS * a)
202 static char *buff = NULL;
205 /* user and domain will be either allocated or reseted to the NULL in
206 * the mbox_to_udomain(), but for safety... */
212 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
214 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
221 safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
222 sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
229 /* Convert an ENVELOPE structure */
231 void mutt_env_to_local (ENVELOPE * e)
233 mutt_addrlist_to_local (e->return_path);
234 mutt_addrlist_to_local (e->from);
235 mutt_addrlist_to_local (e->to);
236 mutt_addrlist_to_local (e->cc);
237 mutt_addrlist_to_local (e->bcc);
238 mutt_addrlist_to_local (e->reply_to);
239 mutt_addrlist_to_local (e->mail_followup_to);
242 /* Note that `a' in the `env->a' expression is macro argument, not
243 * "real" name of an `env' compound member. Real name will be substituted
244 * by preprocessor at the macro-expansion time.
246 #define H_TO_IDNA(a) \
247 if (mutt_addrlist_to_idna (env->a, err) && !e) \
249 if (tag) *tag = #a; e = 1; err = NULL; \
252 int mutt_env_to_idna (ENVELOPE * env, char **tag, char **err)
256 H_TO_IDNA (return_path);
261 H_TO_IDNA (reply_to);
262 H_TO_IDNA (mail_followup_to);