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.
10 #include <lib-lib/lib-lib.h>
18 #include "mutt_idna.h"
20 /* The low-level interface we use. */
24 int mutt_idna_to_local (const char *in, char **out, int flags __attribute__ ((unused)))
30 int mutt_local_to_idna (const char *in, char **out)
38 int mutt_idna_to_local (const char *in, char **out, int flags)
42 if (!option (OPTUSEIDN))
48 /* Is this the right function? Interesting effects with some bad identifiers! */
49 if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
51 if (mutt_convert_string (out, "utf-8", MCharset.charset, M_ICONV_HOOK_TO) == -1)
55 * make sure that we can convert back and come out with the same
59 if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
62 char *tmp = m_strdup(*out);
64 if (mutt_convert_string (&tmp, MCharset.charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
66 if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
68 if (!irrev && ascii_strcasecmp (t2, in)) {
87 int mutt_local_to_idna (const char *in, char **out)
90 char *tmp = m_strdup(in);
99 if (mutt_convert_string (&tmp, MCharset.charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
101 if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
115 /* higher level functions */
117 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
124 p = strchr (mbx, '@');
127 *user = p_dupstr(mbx, p - mbx);
128 *domain = m_strdup(p + 1);
132 int mutt_addrlist_to_idna (address_t * a, char **err)
134 char *user = NULL, *domain = NULL;
141 for (; a; a = a->next) {
144 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
147 if (mutt_local_to_idna (domain, &tmp) < 0) {
150 *err = m_strdup(domain);
153 p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
154 sprintf(a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));
168 int mutt_addrlist_to_local (address_t * a)
173 for (; a; a = a->next) {
176 if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
179 if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
180 p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
181 sprintf(a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));
192 /* convert just for displaying purposes */
193 const char *mutt_addr_for_display (address_t * a)
195 static char *buff = NULL;
198 /* user and domain will be either allocated or reseted to the NULL in
199 * the mbox_to_udomain(), but for safety... */
205 if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
207 if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
214 p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
215 sprintf(buff, "%s@%s", NONULL (user), NONULL (tmp));
222 /* Convert an ENVELOPE structure */
224 void mutt_env_to_local (ENVELOPE * e)
226 mutt_addrlist_to_local (e->return_path);
227 mutt_addrlist_to_local (e->from);
228 mutt_addrlist_to_local (e->to);
229 mutt_addrlist_to_local (e->cc);
230 mutt_addrlist_to_local (e->bcc);
231 mutt_addrlist_to_local (e->reply_to);
232 mutt_addrlist_to_local (e->mail_followup_to);
235 /* Note that `a' in the `env->a' expression is macro argument, not
236 * "real" name of an `env' compound member. Real name will be substituted
237 * by preprocessor at the macro-expansion time.
239 #define H_TO_IDNA(a) \
240 if (mutt_addrlist_to_idna (env->a, err) && !e) \
242 if (tag) *tag = #a; e = 1; err = NULL; \
245 int mutt_env_to_idna (ENVELOPE * env, const char **tag, char **err)
249 H_TO_IDNA (return_path);
254 H_TO_IDNA (reply_to);
255 H_TO_IDNA (mail_followup_to);