~ubuntu-branches/ubuntu/lucid/mutt/lucid-updates

1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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;
1.1.5 by Christoph Berg
Import upstream version 1.5.15+20070412
58
59
  /* we don't want charset-hook effects, so we set flags to 0 */
60
  if (mutt_convert_string (out, "utf-8", Charset, 0) == -1)
1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
61
    goto notrans;
62
63
  /* 
64
   * make sure that we can convert back and come out with the same
65
   * domain name. 
66
   */
67
  
68
  if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0)
69
  {
70
    int irrev = 0;
71
    char *t2 = NULL;
72
    char *tmp = safe_strdup (*out);
1.1.5 by Christoph Berg
Import upstream version 1.5.15+20070412
73
74
    /* we don't want charset-hook effects, so we set flags to 0 */
75
    if (mutt_convert_string (&tmp, Charset, "utf-8", 0) == -1)
1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
76
      irrev = 1;
77
    if (!irrev && idna_to_ascii_8z (tmp, &t2, 1) != IDNA_SUCCESS)
78
      irrev = 1;
79
    if (!irrev && ascii_strcasecmp (t2, in))
80
    {
81
      dprint (1, (debugfile, "mutt_idna_to_local: Not reversible. in = '%s', t2 = '%s'.\n",
82
		  in, t2));
83
      irrev = 1;
84
    }
85
    
86
    FREE (&t2);
87
    FREE (&tmp);
88
89
    if (irrev)
90
      goto notrans;
91
  }
92
93
  return 0;
94
  
95
 notrans:
1.1.3 by Martin Pitt
Import upstream version 1.5.12
96
  FREE (out);		/* __FREE_CHECKED__ */
1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
97
  *out = safe_strdup (in);
98
  return 1;
99
}
100
101
int mutt_local_to_idna (const char *in, char **out)
102
{
103
  int rv = 0;
104
  char *tmp = safe_strdup (in);
105
  *out = NULL;
106
107
  if (!in)
108
  {
109
    *out = NULL;
110
    return -1;
111
  }
112
  
1.1.5 by Christoph Berg
Import upstream version 1.5.15+20070412
113
  /* we don't want charset-hook effects, so we set flags to 0 */
114
  if (mutt_convert_string (&tmp, Charset, "utf-8", 0) == -1)
1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
115
    rv = -1;
116
  if (!rv && idna_to_ascii_8z (tmp, out, 1) != IDNA_SUCCESS)
117
    rv = -2;
118
  
119
  FREE (&tmp);
120
  if (rv < 0)
121
  {
1.1.3 by Martin Pitt
Import upstream version 1.5.12
122
    FREE (out);		/* __FREE_CHECKED__ */
1.1.2 by Martin Pitt
Import upstream version 1.5.11+cvs20060403
123
    *out = safe_strdup (in);
124
  }
125
  return rv;
126
}
127
128
#endif
129
130
131
/* higher level functions */
132
133
static int mbox_to_udomain (const char *mbx, char **user, char **domain)
134
{
135
  char *p;
136
  *user = NULL;
137
  *domain = NULL;
138
  
139
  p = strchr (mbx, '@');
140
  if (!p || !p[1])
141
    return -1;
142
  *user = safe_calloc((p - mbx + 1), sizeof(mbx[0]));
143
  strfcpy (*user, mbx, (p - mbx + 1));
144
  *domain = safe_strdup(p + 1);
145
  return 0;
146
}
147
148
int mutt_addrlist_to_idna (ADDRESS *a, char **err)
149
{
150
  char *user = NULL, *domain = NULL;
151
  char *tmp = NULL;
152
  int e = 0;
153
  
154
  if (err)
155
    *err = NULL;
156
157
  for (; a; a = a->next)
158
  {
159
    if (!a->mailbox)
160
      continue;
161
    if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
162
      continue;
163
    
164
    if (mutt_local_to_idna (domain, &tmp) < 0)
165
    {
166
      e = 1;
167
      if (err)
168
	*err = safe_strdup (domain);
169
    }
170
    else
171
    {
172
      safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
173
      sprintf (a->mailbox, "%s@%s", NONULL(user), NONULL(tmp)); /* __SPRINTF_CHECKED__ */
174
    }
175
    
176
    FREE (&domain);
177
    FREE (&user);
178
    FREE (&tmp);
179
    
180
    if (e)
181
      return -1;
182
  }
183
  
184
  return 0;
185
}
186
187
int mutt_addrlist_to_local (ADDRESS *a)
188
{
189
  char *user, *domain;
190
  char *tmp = NULL;
191
  
192
  for (; a; a = a->next)
193
  {
194
    if (!a->mailbox)
195
      continue;
196
    if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
197
      continue;
198
    
199
    if (mutt_idna_to_local (domain, &tmp, 0) == 0)
200
    {
201
      safe_realloc (&a->mailbox, mutt_strlen (user) + mutt_strlen (tmp) + 2);
202
      sprintf (a->mailbox, "%s@%s", NONULL (user), NONULL (tmp)); /* __SPRINTF_CHECKED__ */
203
    }
204
    
205
    FREE (&domain);
206
    FREE (&user);
207
    FREE (&tmp);
208
  }
209
  
210
  return 0;
211
}
212
213
/* convert just for displaying purposes */
214
const char *mutt_addr_for_display (ADDRESS *a)
215
{
216
  static char *buff = NULL;
217
  char *tmp = NULL;
218
  /* user and domain will be either allocated or reseted to the NULL in
219
   * the mbox_to_udomain(), but for safety... */
220
  char *domain = NULL;
221
  char *user = NULL;
222
  
223
  FREE (&buff);
224
  
225
  if (mbox_to_udomain (a->mailbox, &user, &domain) != 0)
226
    return a->mailbox;
227
  if (mutt_idna_to_local (domain, &tmp, MI_MAY_BE_IRREVERSIBLE) != 0)
228
  {
229
    FREE (&user);
230
    FREE (&domain);
231
    FREE (&tmp);
232
    return a->mailbox;
233
  }
234
  
235
  safe_realloc (&buff, mutt_strlen (tmp) + mutt_strlen (user) + 2);
236
  sprintf (buff, "%s@%s", NONULL(user), NONULL(tmp)); /* __SPRINTF_CHECKED__ */
237
  FREE (&tmp);
238
  FREE (&user);
239
  FREE (&domain);
240
  return buff;
241
}
242
243
/* Convert an ENVELOPE structure */
244
245
void mutt_env_to_local (ENVELOPE *e)
246
{
247
  mutt_addrlist_to_local (e->return_path);
248
  mutt_addrlist_to_local (e->from);
249
  mutt_addrlist_to_local (e->to);
250
  mutt_addrlist_to_local (e->cc);
251
  mutt_addrlist_to_local (e->bcc);
252
  mutt_addrlist_to_local (e->reply_to);
253
  mutt_addrlist_to_local (e->mail_followup_to);
254
}
255
256
/* Note that `a' in the `env->a' expression is macro argument, not
257
 * "real" name of an `env' compound member.  Real name will be substituted
258
 * by preprocessor at the macro-expansion time.
259
 */
260
#define H_TO_IDNA(a)	\
261
  if (mutt_addrlist_to_idna (env->a, err) && !e) \
262
  { \
263
     if (tag) *tag = #a; e = 1; err = NULL; \
264
  }
265
266
int mutt_env_to_idna (ENVELOPE *env, char **tag, char **err)
267
{
268
  int e = 0;
269
  H_TO_IDNA(return_path);
270
  H_TO_IDNA(from);
271
  H_TO_IDNA(to);
272
  H_TO_IDNA(cc);
273
  H_TO_IDNA(bcc);
274
  H_TO_IDNA(reply_to);
275
  H_TO_IDNA(mail_followup_to);
276
  return e;
277
}
278
279
#undef H_TO_IDNA