Nico Golde:
[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 /* The low-level interface we use. */
19
20 #ifndef HAVE_LIBIDN
21
22 int mutt_idna_to_local (const char *in, char **out, int flags)
23 {
24   *out = safe_strdup (in);
25   return 1;
26 }
27
28 int mutt_local_to_idna (const char *in, char **out)
29 {
30   *out = safe_strdup (in);
31   return 0;
32 }
33
34 #else
35
36 int mutt_idna_to_local (const char *in, char **out, int flags)
37 {
38   *out = NULL;
39
40   if (!option (OPTUSEIDN))
41     goto notrans;
42
43   if (!in)
44     goto notrans;
45
46   /* Is this the right function?  Interesting effects with some bad identifiers! */
47   if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
48     goto notrans;
49   if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
50     goto notrans;
51
52   /* 
53    * make sure that we can convert back and come out with the same
54    * domain name. 
55    */
56
57   if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0) {
58     int irrev = 0;
59     char *t2 = NULL;
60     char *tmp = safe_strdup (*out);
61
62     if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
63       irrev = 1;
64     if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
65       irrev = 1;
66     if (!irrev && ascii_strcasecmp (t2, in)) {
67       dprint (1,
68               (debugfile,
69                "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
70                in, t2));
71       irrev = 1;
72     }
73
74     FREE (&t2);
75     FREE (&tmp);
76
77     if (irrev)
78       goto notrans;
79   }
80
81   return 0;
82
83 notrans:
84   FREE (out);
85   *out = safe_strdup (in);
86   return 1;
87 }
88
89 int mutt_local_to_idna (const char *in, char **out)
90 {
91   int rv = 0;
92   char *tmp = safe_strdup (in);
93
94   *out = NULL;
95
96   if (!in) {
97     *out = NULL;
98     return -1;
99   }
100
101   if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
102     rv = -1;
103   if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
104     rv = -2;
105
106   FREE (&tmp);
107   if (rv < 0) {
108     FREE (out);
109     *out = safe_strdup (in);
110   }
111   return rv;
112 }
113
114 #endif
115
116
117 /* higher level functions */
118
119 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
120 {
121   char *p;
122
123   *user = NULL;
124   *domain = NULL;
125
126   p = strchr (mbx, '@');
127   if (!p)
128     return -1;
129   *user = safe_calloc ((p - mbx + 1), sizeof (mbx[0]));
130   strfcpy (*user, mbx, (p - mbx + 1));
131   *domain = safe_strdup (p + 1);
132   return 0;
133 }
134
135 int mutt_addrlist_to_idna (ADDRESS * a, 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 = safe_strdup (domain);
154     }
155     else {
156       safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
157       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
158     }
159
160     FREE (&domain);
161     FREE (&user);
162     FREE (&tmp);
163
164     if (e)
165       return -1;
166   }
167
168   return 0;
169 }
170
171 int mutt_addrlist_to_local (ADDRESS * 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       safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
184       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
185     }
186
187     FREE (&domain);
188     FREE (&user);
189     FREE (&tmp);
190   }
191
192   return 0;
193 }
194
195 /* convert just for displaying purposes */
196 const char *mutt_addr_for_display (ADDRESS * 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   FREE (&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     FREE (&user);
212     FREE (&domain);
213     FREE (&tmp);
214     return a->mailbox;
215   }
216
217   safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
218   sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
219   FREE (&tmp);
220   FREE (&user);
221   FREE (&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, char **tag, 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