Rocco Rutte:
[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 "mutt.h"
15 #include "charset.h"
16 #include "mutt_idna.h"
17
18 #include "lib/mem.h"
19 #include "lib/intl.h"
20 #include "lib/str.h"
21 #include "lib/debug.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 = safe_strdup (in);
30   return 1;
31 }
32
33 int mutt_local_to_idna (const char *in, char **out)
34 {
35   *out = safe_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 = safe_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       debug_print (1, ("not reversible. in = '%s', t2 = '%s'.\n", in, t2));
73       irrev = 1;
74     }
75
76     FREE (&t2);
77     FREE (&tmp);
78
79     if (irrev)
80       goto notrans;
81   }
82
83   return 0;
84
85 notrans:
86   FREE (out);
87   *out = safe_strdup (in);
88   return 1;
89 }
90
91 int mutt_local_to_idna (const char *in, char **out)
92 {
93   int rv = 0;
94   char *tmp = safe_strdup (in);
95
96   *out = NULL;
97
98   if (!in) {
99     *out = NULL;
100     return -1;
101   }
102
103   if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
104     rv = -1;
105   if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
106     rv = -2;
107
108   FREE (&tmp);
109   if (rv < 0) {
110     FREE (out);
111     *out = safe_strdup (in);
112   }
113   return rv;
114 }
115
116 #endif
117
118
119 /* higher level functions */
120
121 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
122 {
123   char *p;
124
125   *user = NULL;
126   *domain = NULL;
127
128   p = strchr (mbx, '@');
129   if (!p)
130     return -1;
131   *user = safe_calloc ((p - mbx + 1), sizeof (mbx[0]));
132   strfcpy (*user, mbx, (p - mbx + 1));
133   *domain = safe_strdup (p + 1);
134   return 0;
135 }
136
137 int mutt_addrlist_to_idna (ADDRESS * a, char **err)
138 {
139   char *user = NULL, *domain = NULL;
140   char *tmp = NULL;
141   int e = 0;
142
143   if (err)
144     *err = NULL;
145
146   for (; a; a = a->next) {
147     if (!a->mailbox)
148       continue;
149     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
150       continue;
151
152     if (mutt_local_to_idna (domain, &tmp) < 0) {
153       e = 1;
154       if (err)
155         *err = safe_strdup (domain);
156     }
157     else {
158       safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
159       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
160     }
161
162     FREE (&domain);
163     FREE (&user);
164     FREE (&tmp);
165
166     if (e)
167       return -1;
168   }
169
170   return 0;
171 }
172
173 int mutt_addrlist_to_local (ADDRESS * a)
174 {
175   char *user, *domain;
176   char *tmp = NULL;
177
178   for (; a; a = a->next) {
179     if (!a->mailbox)
180       continue;
181     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
182       continue;
183
184     if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
185       safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
186       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
187     }
188
189     FREE (&domain);
190     FREE (&user);
191     FREE (&tmp);
192   }
193
194   return 0;
195 }
196
197 /* convert just for displaying purposes */
198 const char *mutt_addr_for_display (ADDRESS * a)
199 {
200   static char *buff = NULL;
201   char *tmp = NULL;
202
203   /* user and domain will be either allocated or reseted to the NULL in
204    * the mbox_to_udomain(), but for safety... */
205   char *domain = NULL;
206   char *user = NULL;
207
208   FREE (&buff);
209
210   if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
211     return a->mailbox;
212   if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
213     FREE (&user);
214     FREE (&domain);
215     FREE (&tmp);
216     return a->mailbox;
217   }
218
219   safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
220   sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
221   FREE (&tmp);
222   FREE (&user);
223   FREE (&domain);
224   return buff;
225 }
226
227 /* Convert an ENVELOPE structure */
228
229 void mutt_env_to_local (ENVELOPE * e)
230 {
231   mutt_addrlist_to_local (e->return_path);
232   mutt_addrlist_to_local (e->from);
233   mutt_addrlist_to_local (e->to);
234   mutt_addrlist_to_local (e->cc);
235   mutt_addrlist_to_local (e->bcc);
236   mutt_addrlist_to_local (e->reply_to);
237   mutt_addrlist_to_local (e->mail_followup_to);
238 }
239
240 /* Note that `a' in the `env->a' expression is macro argument, not
241  * "real" name of an `env' compound member.  Real name will be substituted
242  * by preprocessor at the macro-expansion time.
243  */
244 #define H_TO_IDNA(a)    \
245   if (mutt_addrlist_to_idna (env->a, err) && !e) \
246   { \
247      if (tag) *tag = #a; e = 1; err = NULL; \
248   }
249
250 int mutt_env_to_idna (ENVELOPE * env, char **tag, char **err)
251 {
252   int e = 0;
253
254   H_TO_IDNA (return_path);
255   H_TO_IDNA (from);
256   H_TO_IDNA (to);
257   H_TO_IDNA (cc);
258   H_TO_IDNA (bcc);
259   H_TO_IDNA (reply_to);
260   H_TO_IDNA (mail_followup_to);
261   return e;
262 }
263
264 #undef H_TO_IDNA