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.
18 #include <lib-lib/lib-lib.h>
22 #include "mutt_idna.h"
24 /* The low-level interface we use. */
28 int mutt_idna_to_local (const char *in, char **out, int flags)
34 int mutt_local_to_idna (const char *in, char **out)
42 int mutt_idna_to_local (const char *in, char **out, int flags)
46 if (!option (OPTUSEIDN))
52 /* Is this the right function? Interesting effects with some bad identifiers! */
53 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
55 if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
59 * make sure that we can convert back and come out with the same
63 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
66 char *tmp = m_strdup(*out);
68 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
70 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
72 if (!irrev && ascii_strcasecmp (t2, in)) {
91 int mutt_local_to_idna (const char *in, char **out)
94 char *tmp = m_strdup(in);
103 if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
105 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
119 /* higher level functions */
121 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
128 p = strchr (mbx, '@');
131 *user = p_dupstr(mbx, p - mbx);
132 *domain = m_strdup(p + 1);
136 int mutt_addrlist_to_idna (address_t * a, char **err)
138 char *user = NULL, *domain = NULL;
145 for (; a; a = a->next) {
148 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
151 if (mutt_local_to_idna (domain, &tmp) < 0) {
154 *err = m_strdup(domain);
157 p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
158 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
172 int mutt_addrlist_to_local (address_t * a)
177 for (; a; a = a->next) {
180 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
183 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
184 p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
185 sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
196 /* convert just for displaying purposes */
197 const char *mutt_addr_for_display (address_t * a)
199 static char *buff = NULL;
202 /* user and domain will be either allocated or reseted to the NULL in
203 * the mbox_to_udomain(), but for safety... */
209 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
211 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
218 p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
219 sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
226 /* Convert an ENVELOPE structure */
228 void mutt_env_to_local (ENVELOPE * e)
230 mutt_addrlist_to_local (e->return_path);
231 mutt_addrlist_to_local (e->from);
232 mutt_addrlist_to_local (e->to);
233 mutt_addrlist_to_local (e->cc);
234 mutt_addrlist_to_local (e->bcc);
235 mutt_addrlist_to_local (e->reply_to);
236 mutt_addrlist_to_local (e->mail_followup_to);
239 /* Note that `a' in the `env->a' expression is macro argument, not
240 * "real" name of an `env' compound member. Real name will be substituted
241 * by preprocessor at the macro-expansion time.
243 #define H_TO_IDNA(a) \
244 if (mutt_addrlist_to_idna (env->a, err) && !e) \
246 if (tag) *tag = #a; e = 1; err = NULL; \
249 int mutt_env_to_idna (ENVELOPE * env, const char **tag, char **err)
253 H_TO_IDNA (return_path);
258 H_TO_IDNA (reply_to);
259 H_TO_IDNA (mail_followup_to);