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