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.
17 #include <lib-lib/mem.h>
18 #include <lib-lib/ascii.h>
19 #include <lib-lib/str.h>
20 #include <lib-lib/file.h>
21 #include <lib-lib/macros.h>
22 #include <lib-lib/mapping.h>
25 #include "lib/debug.h"
29 #include "mutt_curses.h"
30 #include "mutt_idna.h"
31 #include "mutt_menu.h"
34 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
36 static struct mapping_t AliasHelp[] = {
37 {N_("Exit"), OP_EXIT},
38 {N_("Del"), OP_DELETE},
39 {N_("Undel"), OP_UNDELETE},
40 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
41 {N_("Help"), OP_HELP},
45 address_t *mutt_lookup_alias (const char *s)
49 for (; t; t = t->next)
50 if (!m_strcasecmp(s, t->name))
52 return (NULL); /* no such alias */
55 static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn)
57 address_t *head = NULL, *last = NULL, *t, *w;
63 if (!a->group && !a->personal && a->mailbox
64 && strchr (a->mailbox, '@') == NULL) {
65 t = mutt_lookup_alias (a->mailbox);
69 for (u = *expn; u; u = u->next) {
70 if (m_strcmp(a->mailbox, u->data) == 0) { /* alias already found */
71 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
79 u->data = m_strdup(a->mailbox);
82 w = address_list_dup (t);
83 w = mutt_expand_aliases_r (w, expn);
88 while (last && last->next)
98 struct passwd *pw = getpwnam (a->mailbox);
101 char namebuf[STRING];
103 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
104 m_strreplace (&a->personal, namebuf);
119 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
120 /* now qualify all local addresses */
121 rfc822_qualify (head, fqdn);
127 address_t *mutt_expand_aliases (address_t * a)
130 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
132 t = mutt_expand_aliases_r (a, &expn);
133 mutt_free_list (&expn);
134 return (mutt_remove_duplicates (t));
137 void mutt_expand_aliases_env (ENVELOPE * env)
139 env->from = mutt_expand_aliases (env->from);
140 env->to = mutt_expand_aliases (env->to);
141 env->cc = mutt_expand_aliases (env->cc);
142 env->bcc = mutt_expand_aliases (env->bcc);
143 env->reply_to = mutt_expand_aliases (env->reply_to);
144 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
149 * if someone has an address like
150 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
151 * and the user creates an alias for this, Mutt could wind up executing
152 * the backtics because it writes aliases like
153 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
154 * To avoid this problem, use a backslash (\) to quote any backtics. We also
155 * need to quote backslashes as well, since you could defeat the above by
157 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
158 * since that would get aliased as
159 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
160 * which still gets evaluated because the double backslash is not a quote.
162 * Additionally, we need to quote ' and " characters - otherwise, mutt will
163 * interpret them on the wrong parsing step.
165 * $ wants to be quoted since it may indicate the start of an environment
169 static void write_safe_address (FILE * fp, char *s)
172 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
179 address_t *mutt_get_address (ENVELOPE * env, const char **pfxp)
182 const char *pfx = NULL;
184 if (mutt_addr_is_user (env->from)) {
185 if (env->to && !mutt_is_mail_list (env->to)) {
194 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
209 void mutt_create_alias (ENVELOPE * cur, address_t * iadr)
212 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
214 char fixed[LONG_STRING];
216 address_t *adr = NULL;
219 adr = mutt_get_address (cur, NULL);
225 if (adr && adr->mailbox) {
226 m_strcpy(buf, sizeof(buf), adr->mailbox);
227 if ((pc = strchr (buf, '@')))
233 /* Don't suggest a bad alias name in the event of a strange local part. */
234 mutt_check_alias_name (buf, buf);
237 /* add a new alias */
238 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
241 /* check to see if the user already has an alias defined */
242 if (mutt_lookup_alias (buf)) {
243 mutt_error _("You already have an alias defined with that name!");
248 if (mutt_check_alias_name (buf, fixed)) {
250 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
252 m_strcpy(buf, sizeof(buf), fixed);
259 new = p_new(ALIAS, 1);
261 new->name = m_strdup(buf);
263 mutt_addrlist_to_local (adr);
266 m_strcpy(buf, sizeof(buf), adr->mailbox);
270 mutt_addrlist_to_idna (adr, NULL);
273 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
274 mutt_free_alias (&new);
278 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
280 if (mutt_addrlist_to_idna (new->addr, &err)) {
281 mutt_error (_("Error: '%s' is a bad IDN."), err);
286 while (new->addr == NULL);
288 if (adr && adr->personal && !mutt_is_mail_list(adr))
289 m_strcpy(buf, sizeof(buf), adr->personal);
293 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
294 mutt_free_alias (&new);
297 new->addr->personal = m_strdup(buf);
300 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
301 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
302 if (mutt_yesorno (prompt, M_YES) != M_YES) {
303 mutt_free_alias (&new);
315 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
316 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
318 mutt_expand_path (buf, sizeof (buf));
319 if ((rc = safe_fopen (buf, "a"))) {
320 if (mutt_check_alias_name (new->name, NULL))
321 mutt_quote_filename(buf, sizeof(buf), new->name);
323 m_strcpy(buf, sizeof(buf), new->name);
324 fprintf (rc, "alias %s ", buf);
326 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
327 write_safe_address (rc, buf);
330 mutt_message _("Alias added.");
337 * Sanity-check an alias name: Only characters which are non-special to both
338 * the RFC 822 and the mutt configuration parser are permitted.
341 static int check_alias_name_char (char c)
343 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
344 isalnum ((unsigned char) c));
347 int mutt_check_alias_name (const char *s, char *d)
352 if (!check_alias_name_char (*s)) {
369 * This routine looks to see if the user has an alias defined for the given
372 address_t *alias_reverse_lookup (address_t * a)
377 if (!a || !a->mailbox)
380 for (; t; t = t->next) {
381 /* cycle through all addresses if this is a group alias */
382 for (ap = t->addr; ap; ap = ap->next) {
383 if (!ap->group && ap->mailbox &&
384 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
391 /* alias_complete() -- alias completion routine
393 * given a partial alias, this routine attempts to fill in the alias
394 * from the alias list as much as possible. if given empty search string
395 * or found nothing, present all aliases
397 int mutt_alias_complete (char *s, size_t buflen)
400 ALIAS *a_list = NULL, *a_cur = NULL;
401 char bestname[HUGE_STRING];
404 #define min(a,b) ((a<b)?a:b)
406 if (s[0] != 0) { /* avoid empty string as strstr argument */
407 p_clear(bestname, sizeof(bestname));
410 if (a->name && strstr (a->name, s) == a->name) {
411 if (!bestname[0]) /* init */
412 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
415 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
422 if (bestname[0] != 0) {
423 if (m_strcmp(bestname, s) != 0) {
424 /* we are adding something to the completion */
425 m_strcpy(s, m_strlen(bestname) + 1, bestname);
429 /* build alias list and show it */
433 if (a->name && (strstr (a->name, s) == a->name)) {
434 if (!a_list) /* init */
435 a_cur = a_list = p_new(ALIAS, 1);
437 a_cur->next = p_new(ALIAS, 1);
440 memcpy (a_cur, a, sizeof (ALIAS));
449 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
450 if (bestname[0] != 0)
451 m_strcpy(s, buflen, bestname);
453 /* free the alias list */
456 a_list = a_list->next;
460 /* remove any aliases marked for deletion */
462 for (a_cur = Aliases; a_cur;) {
465 a_list->next = a_cur->next;
467 Aliases = a_cur->next;
470 mutt_free_alias (&a_cur);
486 static int string_is_address (const char *str, const char *u, const char *d)
488 char buf[LONG_STRING];
490 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
491 if (ascii_strcasecmp (str, buf) == 0)
497 /* returns TRUE if the given address belongs to the user. */
498 int mutt_addr_is_user (address_t * addr)
500 /* NULL address is assumed to be the user. */
502 debug_print(5, ("yes, NULL address\n"));
505 if (!addr->mailbox) {
506 debug_print(5, ("no, no mailbox\n"));
510 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
511 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
514 if (string_is_address (addr->mailbox, Username, Hostname)) {
515 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
518 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
519 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
522 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
523 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
527 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
528 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
532 if (rx_list_match (Alternates, addr->mailbox)) {
533 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
534 if (rx_list_match (UnAlternates, addr->mailbox))
535 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
540 debug_print(5, ("no, all failed.\n"));
544 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
545 const format_t *src, const char *fmt,
546 const char *ifstring __attribute__ ((unused)),
547 const char *elsestring __attribute__ ((unused)),
548 unsigned long data, format_flag flags __attribute__ ((unused)))
550 char tmp[SHORT_STRING], adr[SHORT_STRING];
551 ALIAS *alias = (ALIAS *) data;
556 m_strcpy (dest, sizeof(dest), "D");
558 m_strcpy (dest, sizeof(dest), " ");
561 mutt_format_s (dest, destlen, fmt, alias->name);
565 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
566 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
567 snprintf (dest, destlen, tmp, adr);
570 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
571 snprintf (dest, destlen, tmp, alias->num + 1);
574 dest[0] = alias->tagged ? '*' : ' ';
582 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
584 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *) alias_format_str,
585 (unsigned long) ((ALIAS **) m->data)[num],
586 M_FORMAT_ARROWCURSOR);
589 static int alias_tag (MUTTMENU * menu, int n, int m)
591 ALIAS *cur = ((ALIAS **) menu->data)[n];
592 int ot = cur->tagged;
594 cur->tagged = (m >= 0 ? m : !cur->tagged);
596 return cur->tagged - ot;
599 static int alias_SortAlias (const void *a, const void *b)
601 ALIAS *pa = *(ALIAS **) a;
602 ALIAS *pb = *(ALIAS **) b;
603 int r = m_strcasecmp(pa->name, pb->name);
608 static int alias_SortAddress (const void *a, const void *b)
610 address_t *pa = (*(ALIAS **) a)->addr;
611 address_t *pb = (*(ALIAS **) b)->addr;
620 else if (pa->personal) {
622 r = m_strcasecmp(pa->personal, pb->personal);
626 else if (pb->personal)
629 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
633 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
637 ALIAS **AliasTable = NULL;
641 char helpstr[SHORT_STRING];
646 mutt_error _("You have no aliases!");
651 /* tell whoever called me to redraw the screen when I return */
652 set_option (OPTNEEDREDRAW);
654 menu = mutt_new_menu ();
655 menu->make_entry = alias_entry;
656 menu->tag = alias_tag;
657 menu->menu = MENU_ALIAS;
658 menu->title = _("Aliases");
660 mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
666 /* count the number of aliases */
667 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
668 aliasp->self->del = 0;
669 aliasp->self->tagged = 0;
673 p_realloc(&AliasTable, menu->max);
674 menu->data = AliasTable;
676 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
677 AliasTable[i] = aliasp->self;
681 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
682 qsort (AliasTable, i, sizeof (ALIAS *),
683 (SortAlias & SORT_MASK) ==
684 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
687 for (i = 0; i < menu->max; i++)
688 AliasTable[i]->num = i;
692 menu->redraw |= REDRAW_FULL;
693 aliases = aliases->next;
697 switch ((op = mutt_menuLoop (menu))) {
700 if (menu->tagprefix) {
701 for (i = 0; i < menu->max; i++)
702 if (AliasTable[i]->tagged)
703 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
704 menu->redraw |= REDRAW_INDEX;
707 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
708 menu->redraw |= REDRAW_CURRENT;
709 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
711 menu->redraw |= REDRAW_INDEX;
715 case OP_GENERIC_SELECT_ENTRY:
723 for (i = 0; i < menu->max; i++) {
724 if (AliasTable[i]->tagged) {
725 mutt_addrlist_to_local (AliasTable[i]->addr);
726 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
732 mutt_addrlist_to_local (AliasTable[t]->addr);
733 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
736 mutt_menuDestroy (&menu);
737 p_delete(&AliasTable);