and less size_t's
[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 #include <lib-lib/debug.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       debug_print (1, ("not reversible. in = '%s', t2 = '%s'.\n", in, t2));
74       irrev = 1;
75     }
76
77     p_delete(&t2);
78     p_delete(&tmp);
79
80     if (irrev)
81       goto notrans;
82   }
83
84   return 0;
85
86 notrans:
87   p_delete(out);
88   *out = m_strdup(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 = m_strdup(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   p_delete(&tmp);
110   if (rv < 0) {
111     p_delete(out);
112     *out = m_strdup(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 || !p[1])
131     return -1;
132   *user = p_dupstr(mbx, p - mbx);
133   *domain = m_strdup(p + 1);
134   return 0;
135 }
136
137 int mutt_addrlist_to_idna (address_t * a, const char **err)
138 {
139   char *user = NULL, *domain = NULL;
140   char *tmp = NULL;
141   int e = 0;
142
143   if (err)
144     *err = NULL;
145
146   for (; a; a = a->next) {
147     if (!a->mailbox)
148       continue;
149     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
150       continue;
151
152     if (mutt_local_to_idna (domain, &tmp) < 0) {
153       e = 1;
154       if (err)
155         *err = m_strdup(domain);
156     }
157     else {
158       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
159       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
160     }
161
162     p_delete(&domain);
163     p_delete(&user);
164     p_delete(&tmp);
165
166     if (e)
167       return -1;
168   }
169
170   return 0;
171 }
172
173 int mutt_addrlist_to_local (address_t * a)
174 {
175   char *user, *domain;
176   char *tmp = NULL;
177
178   for (; a; a = a->next) {
179     if (!a->mailbox)
180       continue;
181     if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
182       continue;
183
184     if (mutt_idna_to_local (domain, &tmp, 0) == 0) {
185       p_realloc(&a->mailbox, m_strlen(user) + m_strlen(tmp) + 2);
186       sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp));       /* __SPRINTF_CHECKED__ */
187     }
188
189     p_delete(&domain);
190     p_delete(&user);
191     p_delete(&tmp);
192   }
193
194   return 0;
195 }
196
197 /* convert just for displaying purposes */
198 const char *mutt_addr_for_display (address_t * a)
199 {
200   static char *buff = NULL;
201   char *tmp = NULL;
202
203   /* user and domain will be either allocated or reseted to the NULL in
204    * the mbox_to_udomain(), but for safety... */
205   char *domain = NULL;
206   char *user = NULL;
207
208   p_delete(&buff);
209
210   if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
211     return a->mailbox;
212   if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0) {
213     p_delete(&user);
214     p_delete(&domain);
215     p_delete(&tmp);
216     return a->mailbox;
217   }
218
219   p_realloc(&buff, m_strlen(tmp) + m_strlen(user) + 2);
220   sprintf (buff, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
221   p_delete(&tmp);
222   p_delete(&user);
223   p_delete(&domain);
224   return buff;
225 }
226
227 /* Convert an ENVELOPE structure */
228
229 void mutt_env_to_local (ENVELOPE * e)
230 {
231   mutt_addrlist_to_local (e->return_path);
232   mutt_addrlist_to_local (e->from);
233   mutt_addrlist_to_local (e->to);
234   mutt_addrlist_to_local (e->cc);
235   mutt_addrlist_to_local (e->bcc);
236   mutt_addrlist_to_local (e->reply_to);
237   mutt_addrlist_to_local (e->mail_followup_to);
238 }
239
240 /* Note that `a' in the `env->a' expression is macro argument, not
241  * "real" name of an `env' compound member.  Real name will be substituted
242  * by preprocessor at the macro-expansion time.
243  */
244 #define H_TO_IDNA(a)    \
245   if (mutt_addrlist_to_idna (env->a, err) && !e) \
246   { \
247      if (tag) *tag = #a; e = 1; err = NULL; \
248   }
249
250 int mutt_env_to_idna (ENVELOPE * env, const char **tag, const char **err)
251 {
252   int e = 0;
253
254   H_TO_IDNA (return_path);
255   H_TO_IDNA (from);
256   H_TO_IDNA (to);
257   H_TO_IDNA (cc);
258   H_TO_IDNA (bcc);
259   H_TO_IDNA (reply_to);
260   H_TO_IDNA (mail_followup_to);
261   return e;
262 }
263
264 #undef H_TO_IDNA