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.
14 #include <lib-lib/mem.h>
19 #include "mutt_idna.h"
24 #include "lib/debug.h"
26 /* The low-level interface we use. */
30 int mutt_idna_to_local (const char *in, char **out, int flags)
36 int mutt_local_to_idna (const char *in, char **out)
44 int mutt_idna_to_local (const char *in, char **out, int flags)
48 if (!option (OPTUSEIDN))
54 /* Is this the right function? Interesting effects with some bad identifiers! */
55 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
57 if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
61 * make sure that we can convert back and come out with the same
65 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
68 char *tmp = str_dup (*out);
70 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
72 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
74 if (!irrev && ascii_strcasecmp (t2, in)) {
75 debug_print (1, ("not reversible. in = '%s', t2 = '%s'.\n", in, t2));
94 int mutt_local_to_idna (const char *in, char **out)
97 char *tmp = str_dup (in);
106 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
108 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
122 /* higher level functions */
124 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
131 p = strchr (mbx, '@');
134 *user = mem_calloc ((p - mbx + 1), sizeof (mbx[0]));
135 strfcpy (*user, mbx, (p - mbx + 1));
136 *domain = str_dup (p + 1);
140 int mutt_addrlist_to_idna (ADDRESS * a, char **err)
142 char *user = NULL, *domain = NULL;
149 for (; a; a = a->next) {
152 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
155 if (mutt_local_to_idna (domain, &tmp) < 0) {
158 *err = str_dup (domain);
161 mem_realloc (&a->mailbox, str_len (user) + str_len (tmp) + 2);
162 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
176 int mutt_addrlist_to_local (ADDRESS * a)
181 for (; a; a = a->next) {
184 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
187 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
188 mem_realloc (&a->mailbox, str_len (user) + str_len (tmp) + 2);
189 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
200 /* convert just for displaying purposes */
201 const char *mutt_addr_for_display (ADDRESS * a)
203 static char *buff = NULL;
206 /* user and domain will be either allocated or reseted to the NULL in
207 * the mbox_to_udomain(), but for safety... */
213 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
215 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
222 mem_realloc (&buff, str_len (tmp) + str_len (user) + 2);
223 sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
230 /* Convert an ENVELOPE structure */
232 void mutt_env_to_local (ENVELOPE * e)
234 mutt_addrlist_to_local (e->return_path);
235 mutt_addrlist_to_local (e->from);
236 mutt_addrlist_to_local (e->to);
237 mutt_addrlist_to_local (e->cc);
238 mutt_addrlist_to_local (e->bcc);
239 mutt_addrlist_to_local (e->reply_to);
240 mutt_addrlist_to_local (e->mail_followup_to);
243 /* Note that `a' in the `env->a' expression is macro argument, not
244 * "real" name of an `env' compound member. Real name will be substituted
245 * by preprocessor at the macro-expansion time.
247 #define H_TO_IDNA(a) \
248 if (mutt_addrlist_to_idna (env->a, err) && !e) \
250 if (tag) *tag = #a; e = 1; err = NULL; \
253 int mutt_env_to_idna (ENVELOPE * env, const char **tag, const char **err)
257 H_TO_IDNA (return_path);
262 H_TO_IDNA (reply_to);
263 H_TO_IDNA (mail_followup_to);