2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
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.
15 #include "mutt_curses.h"
16 #include "mutt_idna.h"
26 ADDRESS *mutt_lookup_alias (const char *s)
30 for (; t; t = t->next)
31 if (!mutt_strcasecmp (s, t->name))
33 return (NULL); /* no such alias */
36 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
38 ADDRESS *head = NULL, *last = NULL, *t, *w;
44 if (!a->group && !a->personal && a->mailbox
45 && strchr (a->mailbox, '@') == NULL) {
46 t = mutt_lookup_alias (a->mailbox);
50 for (u = *expn; u; u = u->next) {
51 if (mutt_strcmp (a->mailbox, u->data) == 0) { /* alias already found */
54 "mutt_expand_aliases_r(): loop in alias found for '%s'\n",
62 u = safe_malloc (sizeof (LIST));
63 u->data = safe_strdup (a->mailbox);
66 w = rfc822_cpy_adr (t);
67 w = mutt_expand_aliases_r (w, expn);
72 while (last && last->next)
78 rfc822_free_address (&t);
82 struct passwd *pw = getpwnam (a->mailbox);
87 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
88 mutt_str_replace (&a->personal, namebuf);
107 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
108 /* now qualify all local addresses */
109 rfc822_qualify (head, fqdn);
115 ADDRESS *mutt_expand_aliases (ADDRESS * a)
118 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
120 t = mutt_expand_aliases_r (a, &expn);
121 mutt_free_list (&expn);
122 return (mutt_remove_duplicates (t));
125 void mutt_expand_aliases_env (ENVELOPE * env)
127 env->from = mutt_expand_aliases (env->from);
128 env->to = mutt_expand_aliases (env->to);
129 env->cc = mutt_expand_aliases (env->cc);
130 env->bcc = mutt_expand_aliases (env->bcc);
131 env->reply_to = mutt_expand_aliases (env->reply_to);
132 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
137 * if someone has an address like
138 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
139 * and the user creates an alias for this, Mutt could wind up executing
140 * the backtics because it writes aliases like
141 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
142 * To avoid this problem, use a backslash (\) to quote any backtics. We also
143 * need to quote backslashes as well, since you could defeat the above by
145 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
146 * since that would get aliased as
147 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
148 * which still gets evaluated because the double backslash is not a quote.
150 * Additionally, we need to quote ' and " characters - otherwise, mutt will
151 * interpret them on the wrong parsing step.
153 * $ wants to be quoted since it may indicate the start of an environment
157 static void write_safe_address (FILE * fp, char *s)
160 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
167 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
172 if (mutt_addr_is_user (env->from)) {
173 if (env->to && !mutt_is_mail_list (env->to)) {
182 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
197 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
200 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
202 char fixed[LONG_STRING];
207 adr = mutt_get_address (cur, NULL);
213 if (adr && adr->mailbox) {
214 strfcpy (buf, adr->mailbox, sizeof (buf));
215 if ((pc = strchr (buf, '@')))
221 /* Don't suggest a bad alias name in the event of a strange local part. */
222 mutt_check_alias_name (buf, buf);
225 /* add a new alias */
226 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
229 /* check to see if the user already has an alias defined */
230 if (mutt_lookup_alias (buf)) {
231 mutt_error _("You already have an alias defined with that name!");
236 if (mutt_check_alias_name (buf, fixed)) {
238 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
240 strfcpy (buf, fixed, sizeof (buf));
247 new = safe_calloc (1, sizeof (ALIAS));
249 new->name = safe_strdup (buf);
251 mutt_addrlist_to_local (adr);
254 strfcpy (buf, adr->mailbox, sizeof (buf));
258 mutt_addrlist_to_idna (adr, NULL);
261 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
262 mutt_free_alias (&new);
266 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
268 if (mutt_addrlist_to_idna (new->addr, &err)) {
269 mutt_error (_("Error: '%s' is a bad IDN."), err);
274 while (new->addr == NULL);
276 if (adr && adr->personal && !mutt_is_mail_list (adr))
277 strfcpy (buf, adr->personal, sizeof (buf));
281 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
282 mutt_free_alias (&new);
285 new->addr->personal = safe_strdup (buf);
288 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
289 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
290 if (mutt_yesorno (prompt, M_YES) != M_YES) {
291 mutt_free_alias (&new);
303 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
304 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
306 mutt_expand_path (buf, sizeof (buf));
307 if ((rc = safe_fopen (buf, "a"))) {
308 if (mutt_check_alias_name (new->name, NULL))
309 mutt_quote_filename (buf, sizeof (buf), new->name);
311 strfcpy (buf, new->name, sizeof (buf));
312 fprintf (rc, "alias %s ", buf);
314 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
315 write_safe_address (rc, buf);
318 mutt_message _("Alias added.");
325 * Sanity-check an alias name: Only characters which are non-special to both
326 * the RFC 822 and the mutt configuration parser are permitted.
329 static int check_alias_name_char (char c)
331 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
332 isalnum ((unsigned char) c));
335 int mutt_check_alias_name (const char *s, char *d)
340 if (!check_alias_name_char (*s)) {
357 * This routine looks to see if the user has an alias defined for the given
360 ADDRESS *alias_reverse_lookup (ADDRESS * a)
365 if (!a || !a->mailbox)
368 for (; t; t = t->next) {
369 /* cycle through all addresses if this is a group alias */
370 for (ap = t->addr; ap; ap = ap->next) {
371 if (!ap->group && ap->mailbox &&
372 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
379 /* alias_complete() -- alias completion routine
381 * given a partial alias, this routine attempts to fill in the alias
382 * from the alias list as much as possible. if given empty search string
383 * or found nothing, present all aliases
385 int mutt_alias_complete (char *s, size_t buflen)
388 ALIAS *a_list = NULL, *a_cur = NULL;
389 char bestname[HUGE_STRING];
392 #define min(a,b) ((a<b)?a:b)
394 if (s[0] != 0) { /* avoid empty string as strstr argument */
395 memset (bestname, 0, sizeof (bestname));
398 if (a->name && strstr (a->name, s) == a->name) {
399 if (!bestname[0]) /* init */
400 strfcpy (bestname, a->name,
401 min (mutt_strlen (a->name) + 1, sizeof (bestname)));
403 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
410 if (bestname[0] != 0) {
411 if (mutt_strcmp (bestname, s) != 0) {
412 /* we are adding something to the completion */
413 strfcpy (s, bestname, mutt_strlen (bestname) + 1);
417 /* build alias list and show it */
421 if (a->name && (strstr (a->name, s) == a->name)) {
422 if (!a_list) /* init */
423 a_cur = a_list = (ALIAS *) safe_malloc (sizeof (ALIAS));
425 a_cur->next = (ALIAS *) safe_malloc (sizeof (ALIAS));
428 memcpy (a_cur, a, sizeof (ALIAS));
437 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
438 if (bestname[0] != 0)
439 strfcpy (s, bestname, buflen);
441 /* free the alias list */
444 a_list = a_list->next;
448 /* remove any aliases marked for deletion */
450 for (a_cur = Aliases; a_cur;) {
453 a_list->next = a_cur->next;
455 Aliases = a_cur->next;
458 mutt_free_alias (&a_cur);
474 static int string_is_address (const char *str, const char *u, const char *d)
476 char buf[LONG_STRING];
478 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
479 if (ascii_strcasecmp (str, buf) == 0)
485 /* returns TRUE if the given address belongs to the user. */
486 int mutt_addr_is_user (ADDRESS * addr)
488 /* NULL address is assumed to be the user. */
490 dprint (5, (debugfile, "mail_addr_is_user: yes, NULL address\n"));
493 if (!addr->mailbox) {
494 dprint (5, (debugfile, "mail_addr_is_user: no, no mailbox\n"));
498 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
500 (debugfile, "mail_addr_is_user: yes, %s = %s\n", addr->mailbox,
504 if (string_is_address (addr->mailbox, Username, Hostname)) {
506 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
507 addr->mailbox, Username, Hostname));
510 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
512 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
513 addr->mailbox, Username, mutt_fqdn (0)));
516 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
518 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
519 addr->mailbox, Username, mutt_fqdn (1)));
523 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
525 (debugfile, "mail_addr_is_user: yes, %s = %s\n", addr->mailbox,
530 if (rx_list_match (Alternates, addr->mailbox)) {
532 (debugfile, "mail_addr_is_user: yes, %s matched by alternates.\n",
534 if (rx_list_match (UnAlternates, addr->mailbox))
537 "mail_addr_is_user: but, %s matched by unalternates.\n",
543 dprint (5, (debugfile, "mail_addr_is_user: no, all failed.\n"));