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>
15 #include <lib-lib/ascii.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
21 #include "mutt_idna.h"
23 #include "lib/debug.h"
25 /* The low-level interface we use. */
29 int mutt_idna_to_local (const char *in, char **out, int flags)
35 int mutt_local_to_idna (const char *in, char **out)
43 int mutt_idna_to_local (const char *in, char **out, int flags)
47 if (!option (OPTUSEIDN))
53 /* Is this the right function? Interesting effects with some bad identifiers! */
54 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
56 if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
60 * make sure that we can convert back and come out with the same
64 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
67 char *tmp = m_strdup(*out);
69 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
71 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
73 if (!irrev && ascii_strcasecmp (t2, in)) {
74 debug_print (1, ("not reversible. in = '%s', t2 = '%s'.\n", in, t2));
93 int mutt_local_to_idna (const char *in, char **out)
96 char *tmp = m_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)
121 /* higher level functions */
123 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
130 p = strchr (mbx, '@');
133 *user = p_dupstr(mbx, p - mbx);
134 *domain = m_strdup(p + 1);
138 int mutt_addrlist_to_idna (address_t * a, const char **err)
140 char *user = NULL, *domain = NULL;
147 for (; a; a = a->next) {
150 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
153 if (mutt_local_to_idna (domain, &tmp) < 0) {
156 *err = m_strdup(domain);
159 p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
160 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
174 int mutt_addrlist_to_local (address_t * a)
179 for (; a; a = a->next) {
182 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
185 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
186 p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
187 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
198 /* convert just for displaying purposes */
199 const char *mutt_addr_for_display (address_t * a)
201 static char *buff = NULL;
204 /* user and domain will be either allocated or reseted to the NULL in
205 * the mbox_to_udomain(), but for safety... */
211 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
213 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
220 p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
221 sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
228 /* Convert an ENVELOPE structure */
230 void mutt_env_to_local (ENVELOPE * e)
232 mutt_addrlist_to_local (e->return_path);
233 mutt_addrlist_to_local (e->from);
234 mutt_addrlist_to_local (e->to);
235 mutt_addrlist_to_local (e->cc);
236 mutt_addrlist_to_local (e->bcc);
237 mutt_addrlist_to_local (e->reply_to);
238 mutt_addrlist_to_local (e->mail_followup_to);
241 /* Note that `a' in the `env->a' expression is macro argument, not
242 * "real" name of an `env' compound member. Real name will be substituted
243 * by preprocessor at the macro-expansion time.
245 #define H_TO_IDNA(a) \
246 if (mutt_addrlist_to_idna (env->a, err) && !e) \
248 if (tag) *tag = #a; e = 1; err = NULL; \
251 int mutt_env_to_idna (ENVELOPE * env, const char **tag, const char **err)
255 H_TO_IDNA (return_path);
260 H_TO_IDNA (reply_to);
261 H_TO_IDNA (mail_followup_to);