1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* Copyright (C) 2003,2005,2008-2009 Thomas Roessler <roessler@does-not-exist.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "mutt.h"
#include "charset.h"
#include "mutt_idna.h"
#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
static int check_idn (char *domain)
{
if (! domain)
return 0;
if (ascii_strncasecmp (domain, "xn--", 4) == 0)
return 1;
while ((domain = strchr (domain, '.')) != NULL)
{
if (ascii_strncasecmp (++domain, "xn--", 4) == 0)
return 1;
}
return 0;
}
#endif /* defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2) */
static int mbox_to_udomain (const char *mbx, char **user, char **domain)
{
static char *buff = NULL;
char *p;
mutt_str_replace (&buff, mbx);
p = strchr (buff, '@');
if (!p || !p[1])
return -1;
*p = '\0';
*user = buff;
*domain = p + 1;
return 0;
}
static int addr_is_local (ADDRESS *a)
{
return (a->intl_checked && !a->is_intl);
}
static int addr_is_intl (ADDRESS *a)
{
return (a->intl_checked && a->is_intl);
}
static void set_local_mailbox (ADDRESS *a, char *local_mailbox)
{
FREE (&a->mailbox);
a->mailbox = local_mailbox;
a->intl_checked = 1;
a->is_intl = 0;
}
static void set_intl_mailbox (ADDRESS *a, char *intl_mailbox)
{
FREE (&a->mailbox);
a->mailbox = intl_mailbox;
a->intl_checked = 1;
a->is_intl = 1;
}
static char *intl_to_local (char *orig_user, char *orig_domain, int flags)
{
char *local_user = NULL, *local_domain = NULL, *mailbox = NULL;
char *reversed_user = NULL, *reversed_domain = NULL;
char *tmp = NULL;
#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
int is_idn_encoded = 0;
#endif /* defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2) */
local_user = safe_strdup (orig_user);
local_domain = safe_strdup (orig_domain);
#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
is_idn_encoded = check_idn (local_domain);
if (is_idn_encoded && option (OPTIDNDECODE))
{
if (idna_to_unicode_8z8z (local_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
goto cleanup;
mutt_str_replace (&local_domain, tmp);
FREE (&tmp);
}
#endif /* defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2) */
/* we don't want charset-hook effects, so we set flags to 0 */
if (mutt_convert_string (&local_user, "utf-8", Charset, 0) == -1)
goto cleanup;
if (mutt_convert_string (&local_domain, "utf-8", Charset, 0) == -1)
goto cleanup;
/*
* make sure that we can convert back and come out with the same
* user and domain name.
*/
if ((flags & MI_MAY_BE_IRREVERSIBLE) == 0)
{
reversed_user = safe_strdup (local_user);
if (mutt_convert_string (&reversed_user, Charset, "utf-8", 0) == -1)
{
dprint (1, (debugfile,
"intl_to_local: Not reversible. Charset conv to utf-8 failed for user = '%s'.\n",
reversed_user));
goto cleanup;
}
if (ascii_strcasecmp (orig_user, reversed_user))
{
dprint (1, (debugfile, "intl_to_local: Not reversible. orig = '%s', reversed = '%s'.\n",
orig_user, reversed_user));
goto cleanup;
}
reversed_domain = safe_strdup (local_domain);
if (mutt_convert_string (&reversed_domain, Charset, "utf-8", 0) == -1)
{
dprint (1, (debugfile,
"intl_to_local: Not reversible. Charset conv to utf-8 failed for domain = '%s'.\n",
reversed_domain));
goto cleanup;
}
#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
/* If the original domain was UTF-8, idna encoding here could
* produce a non-matching domain! Thus we only want to do the
* idna_to_ascii_8z() if the original domain was IDNA encoded.
*/
if (is_idn_encoded && option (OPTIDNDECODE))
{
if (idna_to_ascii_8z (reversed_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
{
dprint (1, (debugfile,
"intl_to_local: Not reversible. idna_to_ascii_8z failed for domain = '%s'.\n",
reversed_domain));
goto cleanup;
}
mutt_str_replace (&reversed_domain, tmp);
}
#endif /* defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2) */
if (ascii_strcasecmp (orig_domain, reversed_domain))
{
dprint (1, (debugfile, "intl_to_local: Not reversible. orig = '%s', reversed = '%s'.\n",
orig_domain, reversed_domain));
goto cleanup;
}
}
mailbox = safe_malloc (mutt_strlen (local_user) + mutt_strlen (local_domain) + 2);
sprintf (mailbox, "%s@%s", NONULL(local_user), NONULL(local_domain)); /* __SPRINTF_CHECKED__ */
cleanup:
FREE (&local_user);
FREE (&local_domain);
FREE (&tmp);
FREE (&reversed_domain);
FREE (&reversed_user);
return mailbox;
}
static char *local_to_intl (char *user, char *domain)
{
char *intl_user = NULL, *intl_domain = NULL;
char *mailbox = NULL;
char *tmp = NULL;
intl_user = safe_strdup (user);
intl_domain = safe_strdup (domain);
/* we don't want charset-hook effects, so we set flags to 0 */
if (mutt_convert_string (&intl_user, Charset, "utf-8", 0) == -1)
goto cleanup;
if (mutt_convert_string (&intl_domain, Charset, "utf-8", 0) == -1)
goto cleanup;
#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
if (option (OPTIDNENCODE) &&
intl_domain && *intl_domain != '[') /* don't encode domain-literals */
{
if (idna_to_ascii_8z (intl_domain, &tmp, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
goto cleanup;
mutt_str_replace (&intl_domain, tmp);
}
#endif /* defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2) */
mailbox = safe_malloc (mutt_strlen (intl_user) + mutt_strlen (intl_domain) + 2);
sprintf (mailbox, "%s@%s", NONULL(intl_user), NONULL(intl_domain)); /* __SPRINTF_CHECKED__ */
cleanup:
FREE (&intl_user);
FREE (&intl_domain);
FREE (&tmp);
return mailbox;
}
/* higher level functions */
int mutt_addrlist_to_intl (ADDRESS *a, char **err)
{
char *user = NULL, *domain = NULL;
char *intl_mailbox = NULL;
int rv = 0;
if (err)
*err = NULL;
for (; a; a = a->next)
{
if (!a->mailbox || addr_is_intl (a))
continue;
if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
continue;
intl_mailbox = local_to_intl (user, domain);
if (! intl_mailbox)
{
rv = -1;
if (err && !*err)
*err = safe_strdup (a->mailbox);
continue;
}
set_intl_mailbox (a, intl_mailbox);
}
return rv;
}
int mutt_addrlist_to_local (ADDRESS *a)
{
char *user = NULL, *domain = NULL;
char *local_mailbox = NULL;
for (; a; a = a->next)
{
if (!a->mailbox || addr_is_local (a))
continue;
if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
continue;
local_mailbox = intl_to_local (user, domain, 0);
if (local_mailbox)
set_local_mailbox (a, local_mailbox);
}
return 0;
}
/* convert just for displaying purposes */
const char *mutt_addr_for_display (ADDRESS *a)
{
char *user = NULL, *domain = NULL;
static char *buff = NULL;
char *local_mailbox = NULL;
FREE (&buff);
if (!a->mailbox || addr_is_local (a))
return a->mailbox;
if (mbox_to_udomain (a->mailbox, &user, &domain) == -1)
return a->mailbox;
local_mailbox = intl_to_local (user, domain, MI_MAY_BE_IRREVERSIBLE);
if (! local_mailbox)
return a->mailbox;
mutt_str_replace (&buff, local_mailbox);
FREE (&local_mailbox);
return buff;
}
/* Convert an ENVELOPE structure */
void mutt_env_to_local (ENVELOPE *e)
{
mutt_addrlist_to_local (e->return_path);
mutt_addrlist_to_local (e->from);
mutt_addrlist_to_local (e->to);
mutt_addrlist_to_local (e->cc);
mutt_addrlist_to_local (e->bcc);
mutt_addrlist_to_local (e->reply_to);
mutt_addrlist_to_local (e->mail_followup_to);
}
/* Note that `a' in the `env->a' expression is macro argument, not
* "real" name of an `env' compound member. Real name will be substituted
* by preprocessor at the macro-expansion time.
* Note that #a escapes and double quotes the argument.
*/
#define H_TO_INTL(a) \
if (mutt_addrlist_to_intl (env->a, err) && !e) \
{ \
if (tag) *tag = #a; e = 1; err = NULL; \
}
int mutt_env_to_intl (ENVELOPE *env, char **tag, char **err)
{
int e = 0;
H_TO_INTL(return_path);
H_TO_INTL(from);
H_TO_INTL(to);
H_TO_INTL(cc);
H_TO_INTL(bcc);
H_TO_INTL(reply_to);
H_TO_INTL(mail_followup_to);
return e;
}
#undef H_TO_INTL
|