e2919ff9d44b7e65c9e8b9184a08e51dde25db1b
[apps/madmutt.git] / mutt_idna.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2003 Thomas Roessler <roessler@does-not-exist.org>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <lib-lib/mem.h>
15 #include <lib-lib/ascii.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
18
19 #include "mutt.h"
20 #include "charset.h"
21 #include "mutt_idna.h"
22
23 /* The low-level interface we use. */
24
25 #ifndef HAVE_LIBIDN
26
27 int mutt_idna_to_local (const char *in, char **out, int flags)
28 {
29   *out = m_strdup(in);
30   return 1;
31 }
32
33 int mutt_local_to_idna (const char *in, char **out)
34 {
35   *out = m_strdup(in);
36   return 0;
37 }
38
39 #else
40
41 int mutt_idna_to_local (const char *in, char **out, int flags)
42 {
43   *out = NULL;
44
45   if (!option (OPTUSEIDN))
46     goto notrans;
47
48   if (!in)
49     goto notrans;
50
51   /* Is this the right function?  Interesting effects with some bad identifiers! */
52   if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
53     goto notrans;
54   if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
55     goto notrans;
56
57   /* 
58    * make sure that we can convert back and come out with the same
59    * domain name. 
60    */
61
62   if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
63     int irrev = 0;
64     char *t2 = NULL;
65     char *tmp = m_strdup(*out);
66
67     if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
68       irrev = 1;
69     if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
70       irrev = 1;
71     if (!irrev && ascii_strcasecmp (t2, in)) {
72       irrev = 1;
73     }
74
75     p_delete(&t2);
76     p_delete(&tmp);
77
78     if (irrev)
79       goto notrans;
80   }
81
82   return 0;
83
84 notrans:
85   p_delete(out);
86   *out = m_strdup(in);
87   return 1;
88 }
89
90 int mutt_local_to_idna (const char *in, char **out)
91 {
92   int rv = 0;
93   char *tmp = m_strdup(in);
94
95   *out = NULL;
96
97   if (!in) {
98     *out = NULL;
99     return -1;
100   }
101
102   if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
103     rv = -1;
104   if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
105     rv = -2;
106
107   p_delete(&tmp);
108   if (rv < 0) {
109     p_delete(out);
110     *out = m_strdup(in);
111   }
112   return rv;
113 }
114
115 #endif
116
117
118 /* higher level functions */
119
120 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
121 {
122   char *p;
123
124   *user = NULL;
125   *domain = NULL;
126
127   p = strchr (mbx, '@');
128   if (!p || !p[1])
129     return -1;
130   *user = p_dupstr(mbx, p - mbx);
131   *domain = m_strdup(p + 1);
132   return 0;
133 }
134
135 int mutt_addrlist_to_idna (address_t * a, const char **err)
136 {
137   char *user = NULL, *domain = NULL;
138   char *tmp = NULL;
139   int e = 0;
140
141   if (err)
142     *err = NULL;
143
144   for (; a; a = a->next) {
145     if (!a->mailbox)
146       continue;
147     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
148       continue;
149
150     if (mutt_local_to_idna (domain, &tmp) < 0) {
151       e = 1;
152       if (err)
153         *err = m_strdup(domain);
154     }
155     else {
156       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
157       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
158     }
159
160     p_delete(&domain);
161     p_delete(&user);
162     p_delete(&tmp);
163
164     if (e)
165       return -1;
166   }
167
168   return 0;
169 }
170
171 int mutt_addrlist_to_local (address_t * a)
172 {
173   char *user, *domain;
174   char *tmp = NULL;
175
176   for (; a; a = a->next) {
177     if (!a->mailbox)
178       continue;
179     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
180       continue;
181
182     if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
183       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
184       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
185     }
186
187     p_delete(&domain);
188     p_delete(&user);
189     p_delete(&tmp);
190   }
191
192   return 0;
193 }
194
195 /* convert just for displaying purposes */
196 const char *mutt_addr_for_display (address_t * a)
197 {
198   static char *buff = NULL;
199   char *tmp = NULL;
200
201   /* user and domain will be either allocated or reseted to the NULL in
202    * the mbox_to_udomain(), but for safety... */
203   char *domain = NULL;
204   char *user = NULL;
205
206   p_delete(&buff);
207
208   if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
209     return a->mailbox;
210   if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
211     p_delete(&user);
212     p_delete(&domain);
213     p_delete(&tmp);
214     return a->mailbox;
215   }
216
217   p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
218   sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
219   p_delete(&tmp);
220   p_delete(&user);
221   p_delete(&domain);
222   return buff;
223 }
224
225 /* Convert an ENVELOPE structure */
226
227 void mutt_env_to_local (ENVELOPE * e)
228 {
229   mutt_addrlist_to_local (e->return_path);
230   mutt_addrlist_to_local (e->from);
231   mutt_addrlist_to_local (e->to);
232   mutt_addrlist_to_local (e->cc);
233   mutt_addrlist_to_local (e->bcc);
234   mutt_addrlist_to_local (e->reply_to);
235   mutt_addrlist_to_local (e->mail_followup_to);
236 }
237
238 /* Note that `a' in the `env->a' expression is macro argument, not
239  * "real" name of an `env' compound member.  Real name will be substituted
240  * by preprocessor at the macro-expansion time.
241  */
242 #define H_TO_IDNA(a)    \
243   if (mutt_addrlist_to_idna (env->a, err) && !e) \
244   { \
245      if (tag) *tag = #a; e = 1; err = NULL; \
246   }
247
248 int mutt_env_to_idna (ENVELOPE * env, const char **tag, const char **err)
249 {
250   int e = 0;
251
252   H_TO_IDNA (return_path);
253   H_TO_IDNA (from);
254   H_TO_IDNA (to);
255   H_TO_IDNA (cc);
256   H_TO_IDNA (bcc);
257   H_TO_IDNA (reply_to);
258   H_TO_IDNA (mail_followup_to);
259   return e;
260 }
261
262 #undef H_TO_IDNA