always build the dotlock program standalone, many other appplications use
[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 #include "lib/debug.h"
24
25 /* The low-level interface we use. */
26
27 #ifndef HAVE_LIBIDN
28
29 int mutt_idna_to_local (const char *in, char **out, int flags)
30 {
31   *out = m_strdup(in);
32   return 1;
33 }
34
35 int mutt_local_to_idna (const char *in, char **out)
36 {
37   *out = m_strdup(in);
38   return 0;
39 }
40
41 #else
42
43 int mutt_idna_to_local (const char *in, char **out, int flags)
44 {
45   *out = NULL;
46
47   if (!option (OPTUSEIDN))
48     goto notrans;
49
50   if (!in)
51     goto notrans;
52
53   /* Is this the right function?  Interesting effects with some bad identifiers! */
54   if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
55     goto notrans;
56   if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
57     goto notrans;
58
59   /* 
60    * make sure that we can convert back and come out with the same
61    * domain name. 
62    */
63
64   if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
65     int irrev = 0;
66     char *t2 = NULL;
67     char *tmp = m_strdup(*out);
68
69     if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
70       irrev = 1;
71     if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
72       irrev = 1;
73     if (!irrev && ascii_strcasecmp (t2, in)) {
74       debug_print (1, ("not reversible. in = '%s', t2 = '%s'.\n", in, t2));
75       irrev = 1;
76     }
77
78     p_delete(&t2);
79     p_delete(&tmp);
80
81     if (irrev)
82       goto notrans;
83   }
84
85   return 0;
86
87 notrans:
88   p_delete(out);
89   *out = m_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 = m_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   p_delete(&tmp);
111   if (rv < 0) {
112     p_delete(out);
113     *out = m_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 || !p[1])
132     return -1;
133   *user = p_dupstr(mbx, p - mbx);
134   *domain = m_strdup(p + 1);
135   return 0;
136 }
137
138 int mutt_addrlist_to_idna (address_t * 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 = m_strdup(domain);
157     }
158     else {
159       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
160       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
161     }
162
163     p_delete(&domain);
164     p_delete(&user);
165     p_delete(&tmp);
166
167     if (e)
168       return -1;
169   }
170
171   return 0;
172 }
173
174 int mutt_addrlist_to_local (address_t * 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       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
187       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
188     }
189
190     p_delete(&domain);
191     p_delete(&user);
192     p_delete(&tmp);
193   }
194
195   return 0;
196 }
197
198 /* convert just for displaying purposes */
199 const char *mutt_addr_for_display (address_t * 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   p_delete(&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     p_delete(&user);
215     p_delete(&domain);
216     p_delete(&tmp);
217     return a->mailbox;
218   }
219
220   p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
221   sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
222   p_delete(&tmp);
223   p_delete(&user);
224   p_delete(&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, const char **tag, const 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