sort out some prototypes, put them where they belong.
[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 #ifdef HAVE_LIBIDN
15 #include <idna.h>
16 #endif
17
18 #include <lib-lib/lib-lib.h>
19
20 #include "mutt.h"
21 #include "charset.h"
22 #include "mutt_idna.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 = m_strdup(in);
31   return 1;
32 }
33
34 int mutt_local_to_idna (const char *in, char **out)
35 {
36   *out = m_strdup(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 = m_strdup(*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       irrev = 1;
74     }
75
76     p_delete(&t2);
77     p_delete(&tmp);
78
79     if (irrev)
80       goto notrans;
81   }
82
83   return 0;
84
85 notrans:
86   p_delete(out);
87   *out = m_strdup(in);
88   return 1;
89 }
90
91 int mutt_local_to_idna (const char *in, char **out)
92 {
93   int rv = 0;
94   char *tmp = m_strdup(in);
95
96   *out = NULL;
97
98   if (!in) {
99     *out = NULL;
100     return -1;
101   }
102
103   if (mutt_convert_string (&tmp, Charset, "utf-8", M_ICONV_HOOK_FROM) == -1)
104     rv = -1;
105   if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
106     rv = -2;
107
108   p_delete(&tmp);
109   if (rv < 0) {
110     p_delete(out);
111     *out = m_strdup(in);
112   }
113   return rv;
114 }
115
116 #endif
117
118
119 /* higher level functions */
120
121 static int mbox_to_udomain (const char *mbx, char **user, char **domain)
122 {
123   char *p;
124
125   *user = NULL;
126   *domain = NULL;
127
128   p = strchr (mbx, '@');
129   if (!p || !p[1])
130     return -1;
131   *user = p_dupstr(mbx, p - mbx);
132   *domain = m_strdup(p + 1);
133   return 0;
134 }
135
136 int mutt_addrlist_to_idna (address_t * a, char **err)
137 {
138   char *user = NULL, *domain = NULL;
139   char *tmp = NULL;
140   int e = 0;
141
142   if (err)
143     *err = NULL;
144
145   for (; a; a = a->next) {
146     if (!a->mailbox)
147       continue;
148     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
149       continue;
150
151     if (mutt_local_to_idna (domain, &tmp) < 0) {
152       e = 1;
153       if (err)
154         *err = m_strdup(domain);
155     }
156     else {
157       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
158       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
159     }
160
161     p_delete(&domain);
162     p_delete(&user);
163     p_delete(&tmp);
164
165     if (e)
166       return -1;
167   }
168
169   return 0;
170 }
171
172 int mutt_addrlist_to_local (address_t * a)
173 {
174   char *user, *domain;
175   char *tmp = NULL;
176
177   for (; a; a = a->next) {
178     if (!a->mailbox)
179       continue;
180     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
181       continue;
182
183     if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
184       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
185       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
186     }
187
188     p_delete(&domain);
189     p_delete(&user);
190     p_delete(&tmp);
191   }
192
193   return 0;
194 }
195
196 /* convert just for displaying purposes */
197 const char *mutt_addr_for_display (address_t * a)
198 {
199   static char *buff = NULL;
200   char *tmp = NULL;
201
202   /* user and domain will be either allocated or reseted to the NULL in
203    * the mbox_to_udomain(), but for safety... */
204   char *domain = NULL;
205   char *user = NULL;
206
207   p_delete(&buff);
208
209   if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
210     return a->mailbox;
211   if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
212     p_delete(&user);
213     p_delete(&domain);
214     p_delete(&tmp);
215     return a->mailbox;
216   }
217
218   p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
219   sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
220   p_delete(&tmp);
221   p_delete(&user);
222   p_delete(&domain);
223   return buff;
224 }
225
226 /* Convert an ENVELOPE structure */
227
228 void mutt_env_to_local (ENVELOPE * e)
229 {
230   mutt_addrlist_to_local (e->return_path);
231   mutt_addrlist_to_local (e->from);
232   mutt_addrlist_to_local (e->to);
233   mutt_addrlist_to_local (e->cc);
234   mutt_addrlist_to_local (e->bcc);
235   mutt_addrlist_to_local (e->reply_to);
236   mutt_addrlist_to_local (e->mail_followup_to);
237 }
238
239 /* Note that `a' in the `env->a' expression is macro argument, not
240  * "real" name of an `env' compound member.  Real name will be substituted
241  * by preprocessor at the macro-expansion time.
242  */
243 #define H_TO_IDNA(a)    \
244   if (mutt_addrlist_to_idna (env->a, err) && !e) \
245   { \
246      if (tag) *tag = #a; e = 1; err = NULL; \
247   }
248
249 int mutt_env_to_idna (ENVELOPE * env, const char **tag, char **err)
250 {
251   int e = 0;
252
253   H_TO_IDNA (return_path);
254   H_TO_IDNA (from);
255   H_TO_IDNA (to);
256   H_TO_IDNA (cc);
257   H_TO_IDNA (bcc);
258   H_TO_IDNA (reply_to);
259   H_TO_IDNA (mail_followup_to);
260   return e;
261 }
262
263 #undef H_TO_IDNA