Nico Golde:
[apps/madmutt.git] / mutt_idna.c
1 /*
2  * Copyright (C) 2003 Thomas Roessler <roessler@does-not-exist.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */ 
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "charset.h"
25 #include "mutt_idna.h"
26
27 /* The low-level interface we use. */
28
29 #ifndef HAVE_LIBIDN
30
31 int mutt_idna_to_local (const char *in, char **out, int flags)
32 {
33   *out = safe_strdup (in);
34   return 1;
35 }
36
37 int mutt_local_to_idna (const char *in, char **out)
38 {
39   *out = safe_strdup (in);
40   return 0;
41 }
42                         
43 #else
44
45 int mutt_idna_to_local (const char *in, char **out, int flags)
46 {
47   *out = NULL;
48
49   if (!option (OPTUSEIDN))
50     goto notrans;
51
52   if (!in)
53     goto notrans;
54   
55   /* Is this the right function?  Interesting effects with some bad identifiers! */
56   if (idna_to_unicode_8z8z (in, out, 1) != IDNA_SUCCESS)
57     goto notrans;
58   if (mutt_convert_string (out, "utf-8", Charset, M_ICONV_HOOK_TO) == -1)
59     goto notrans;
60
61   /* 
62    * make sure that we can convert back and come out with the same
63    * domain name. 
64    */
65   
66   if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0)
67   {
68     int irrev = 0;
69     char *t2 = NULL;
70     char *tmp = safe_strdup (*out);
71     if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
72       irrev = 1;
73     if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
74       irrev = 1;
75     if (!irrev && ascii_strcasecmp (t2, in))
76     {
77       dprint (1, (debugfile, "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
78                   in, t2));
79       irrev = 1;
80     }
81     
82     FREE (&t2);
83     FREE (&tmp);
84
85     if (irrev)
86       goto notrans;
87   }
88
89   return 0;
90   
91  notrans:
92   FREE (out);
93   *out = safe_strdup (in);
94   return 1;
95 }
96
97 int mutt_local_to_idna (const char *in, char **out)
98 {
99   int rv = 0;
100   char *tmp = safe_strdup (in);
101   *out = NULL;
102
103   if (!in)
104   {
105     *out = NULL;
106     return -1;
107   }
108   
109   if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
110     rv = -1;
111   if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
112     rv = -2;
113   
114   FREE (&tmp);
115   if (rv < 0)
116   {
117     FREE (out);
118     *out = safe_strdup (in);
119   }
120   return rv;
121 }
122
123 #endif
124
125
126 /* higher level functions */
127
128 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
129 {
130   char *p;
131   *user = NULL;
132   *domain = NULL;
133   
134   p = strchr (mbx, '@');
135   if (!p)
136     return -1;
137   *user = safe_calloc((p - mbx + 1), sizeof(mbx[0]));
138   strfcpy (*user, mbx, (p - mbx + 1));
139   *domain = safe_strdup(p + 1);
140   return 0;
141 }
142
143 int mutt_addrlist_to_idna (ADDRESS *a, char **err)
144 {
145   char *user = NULL, *domain = NULL;
146   char *tmp = NULL;
147   int e = 0;
148   
149   if (err)
150     *err = NULL;
151
152   for (; a; a = a->next)
153   {
154     if (!a->mailbox)
155       continue;
156     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
157       continue;
158     
159     if (mutt_local_to_idna (domain, &tmp) < 0)
160     {
161       e = 1;
162       if (err)
163         *err = safe_strdup (domain);
164     }
165     else
166     {
167       safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
168       sprintf (a->mailbox, "%s@%s", NONULL(user), NONULL(tmp)); /* __SPRINTF_CHECKED__ */
169     }
170     
171     FREE (&domain);
172     FREE (&user);
173     FREE (&tmp);
174     
175     if (e)
176       return -1;
177   }
178   
179   return 0;
180 }
181
182 int mutt_addrlist_to_local (ADDRESS *a)
183 {
184   char *user, *domain;
185   char *tmp = NULL;
186   
187   for (; a; a = a->next)
188   {
189     if (!a->mailbox)
190       continue;
191     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
192       continue;
193     
194     if (mutt_idna_to_local (domain, &tmp, 0) == 0)
195     {
196       safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
197       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
198     }
199     
200     FREE (&domain);
201     FREE (&user);
202     FREE (&tmp);
203   }
204   
205   return 0;
206 }
207
208 /* convert just for displaying purposes */
209 const char *mutt_addr_for_display (ADDRESS *a)
210 {
211   static char *buff = NULL;
212   char *tmp = NULL;
213   /* user and domain will be either allocated or reseted to the NULL in
214    * the mbox_to_udomain(), but for safety... */
215   char *domain = NULL;
216   char *user = NULL;
217   
218   FREE (&buff);
219   
220   if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
221     return a->mailbox;
222   if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0)
223   {
224     FREE (&user);
225     FREE (&domain);
226     FREE (&tmp);
227     return a->mailbox;
228   }
229   
230   safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
231   sprintf (buff, "%s@%s", NONULL(user), NONULL(tmp)); /* __SPRINTF_CHECKED__ */
232   FREE (&tmp);
233   FREE (&user);
234   FREE (&domain);
235   return buff;
236 }
237
238 /* Convert an ENVELOPE structure */
239
240 void mutt_env_to_local (ENVELOPE *e)
241 {
242   mutt_addrlist_to_local (e->return_path);
243   mutt_addrlist_to_local (e->from);
244   mutt_addrlist_to_local (e->to);
245   mutt_addrlist_to_local (e->cc);
246   mutt_addrlist_to_local (e->bcc);
247   mutt_addrlist_to_local (e->reply_to);
248   mutt_addrlist_to_local (e->mail_followup_to);
249 }
250
251 /* Note that `a' in the `env->a' expression is macro argument, not
252  * "real" name of an `env' compound member.  Real name will be substituted
253  * by preprocessor at the macro-expansion time.
254  */
255 #define H_TO_IDNA(a)    \
256   if (mutt_addrlist_to_idna (env->a, err) && !e) \
257   { \
258      if (tag) *tag = #a; e = 1; err = NULL; \
259   }
260
261 int mutt_env_to_idna (ENVELOPE *env, char **tag, char **err)
262 {
263   int e = 0;
264   H_TO_IDNA(return_path);
265   H_TO_IDNA(from);
266   H_TO_IDNA(to);
267   H_TO_IDNA(cc);
268   H_TO_IDNA(bcc);
269   H_TO_IDNA(reply_to);
270   H_TO_IDNA(mail_followup_to);
271   return e;
272 }
273
274 #undef H_TO_IDNA