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