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.
21 #include "lib/debug.h"
26 #include "mutt_curses.h"
27 #include "mutt_idna.h"
28 #include "mutt_menu.h"
32 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
34 static struct mapping_t AliasHelp[] = {
35 {N_("Exit"), OP_EXIT},
36 {N_("Del"), OP_DELETE},
37 {N_("Undel"), OP_UNDELETE},
38 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
39 {N_("Help"), OP_HELP},
43 ADDRESS *mutt_lookup_alias (const char *s)
47 for (; t; t = t->next)
48 if (!str_casecmp (s, t->name))
50 return (NULL); /* no such alias */
53 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
55 ADDRESS *head = NULL, *last = NULL, *t, *w;
61 if (!a->group && !a->personal && a->mailbox
62 && strchr (a->mailbox, '@') == NULL) {
63 t = mutt_lookup_alias (a->mailbox);
67 for (u = *expn; u; u = u->next) {
68 if (str_cmp (a->mailbox, u->data) == 0) { /* alias already found */
69 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
76 u = mem_malloc (sizeof (LIST));
77 u->data = str_dup (a->mailbox);
80 w = rfc822_cpy_adr (t);
81 w = mutt_expand_aliases_r (w, expn);
86 while (last && last->next)
92 rfc822_free_address (&t);
96 struct passwd *pw = getpwnam (a->mailbox);
101 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
102 str_replace (&a->personal, namebuf);
117 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
118 /* now qualify all local addresses */
119 rfc822_qualify (head, fqdn);
125 ADDRESS *mutt_expand_aliases (ADDRESS * a)
128 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
130 t = mutt_expand_aliases_r (a, &expn);
131 mutt_free_list (&expn);
132 return (mutt_remove_duplicates (t));
135 void mutt_expand_aliases_env (ENVELOPE * env)
137 env->from = mutt_expand_aliases (env->from);
138 env->to = mutt_expand_aliases (env->to);
139 env->cc = mutt_expand_aliases (env->cc);
140 env->bcc = mutt_expand_aliases (env->bcc);
141 env->reply_to = mutt_expand_aliases (env->reply_to);
142 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
147 * if someone has an address like
148 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
149 * and the user creates an alias for this, Mutt could wind up executing
150 * the backtics because it writes aliases like
151 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
152 * To avoid this problem, use a backslash (\) to quote any backtics. We also
153 * need to quote backslashes as well, since you could defeat the above by
155 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
156 * since that would get aliased as
157 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
158 * which still gets evaluated because the double backslash is not a quote.
160 * Additionally, we need to quote ' and " characters - otherwise, mutt will
161 * interpret them on the wrong parsing step.
163 * $ wants to be quoted since it may indicate the start of an environment
167 static void write_safe_address (FILE * fp, char *s)
170 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
177 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
182 if (mutt_addr_is_user (env->from)) {
183 if (env->to && !mutt_is_mail_list (env->to)) {
192 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
207 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
210 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
212 char fixed[LONG_STRING];
217 adr = mutt_get_address (cur, NULL);
223 if (adr && adr->mailbox) {
224 strfcpy (buf, adr->mailbox, sizeof (buf));
225 if ((pc = strchr (buf, '@')))
231 /* Don't suggest a bad alias name in the event of a strange local part. */
232 mutt_check_alias_name (buf, buf);
235 /* add a new alias */
236 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
239 /* check to see if the user already has an alias defined */
240 if (mutt_lookup_alias (buf)) {
241 mutt_error _("You already have an alias defined with that name!");
246 if (mutt_check_alias_name (buf, fixed)) {
248 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
250 strfcpy (buf, fixed, sizeof (buf));
257 new = mem_calloc (1, sizeof (ALIAS));
259 new->name = str_dup (buf);
261 mutt_addrlist_to_local (adr);
264 strfcpy (buf, adr->mailbox, sizeof (buf));
268 mutt_addrlist_to_idna (adr, NULL);
271 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
272 mutt_free_alias (&new);
276 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
278 if (mutt_addrlist_to_idna (new->addr, &err)) {
279 mutt_error (_("Error: '%s' is a bad IDN."), err);
284 while (new->addr == NULL);
286 if (adr && adr->personal && !mutt_is_mail_list (adr))
287 strfcpy (buf, adr->personal, sizeof (buf));
291 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
292 mutt_free_alias (&new);
295 new->addr->personal = str_dup (buf);
298 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
299 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
300 if (mutt_yesorno (prompt, M_YES) != M_YES) {
301 mutt_free_alias (&new);
313 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
314 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
316 mutt_expand_path (buf, sizeof (buf));
317 if ((rc = safe_fopen (buf, "a"))) {
318 if (mutt_check_alias_name (new->name, NULL))
319 mutt_quote_filename (buf, sizeof (buf), new->name);
321 strfcpy (buf, new->name, sizeof (buf));
322 fprintf (rc, "alias %s ", buf);
324 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
325 write_safe_address (rc, buf);
328 mutt_message _("Alias added.");
335 * Sanity-check an alias name: Only characters which are non-special to both
336 * the RFC 822 and the mutt configuration parser are permitted.
339 static int check_alias_name_char (char c)
341 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
342 isalnum ((unsigned char) c));
345 int mutt_check_alias_name (const char *s, char *d)
350 if (!check_alias_name_char (*s)) {
367 * This routine looks to see if the user has an alias defined for the given
370 ADDRESS *alias_reverse_lookup (ADDRESS * a)
375 if (!a || !a->mailbox)
378 for (; t; t = t->next) {
379 /* cycle through all addresses if this is a group alias */
380 for (ap = t->addr; ap; ap = ap->next) {
381 if (!ap->group && ap->mailbox &&
382 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
389 /* alias_complete() -- alias completion routine
391 * given a partial alias, this routine attempts to fill in the alias
392 * from the alias list as much as possible. if given empty search string
393 * or found nothing, present all aliases
395 int mutt_alias_complete (char *s, size_t buflen)
398 ALIAS *a_list = NULL, *a_cur = NULL;
399 char bestname[HUGE_STRING];
402 #define min(a,b) ((a<b)?a:b)
404 if (s[0] != 0) { /* avoid empty string as strstr argument */
405 memset (bestname, 0, sizeof (bestname));
408 if (a->name && strstr (a->name, s) == a->name) {
409 if (!bestname[0]) /* init */
410 strfcpy (bestname, a->name,
411 min (str_len (a->name) + 1, sizeof (bestname)));
413 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
420 if (bestname[0] != 0) {
421 if (str_cmp (bestname, s) != 0) {
422 /* we are adding something to the completion */
423 strfcpy (s, bestname, str_len (bestname) + 1);
427 /* build alias list and show it */
431 if (a->name && (strstr (a->name, s) == a->name)) {
432 if (!a_list) /* init */
433 a_cur = a_list = (ALIAS *) mem_malloc (sizeof (ALIAS));
435 a_cur->next = (ALIAS *) mem_malloc (sizeof (ALIAS));
438 memcpy (a_cur, a, sizeof (ALIAS));
447 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
448 if (bestname[0] != 0)
449 strfcpy (s, bestname, buflen);
451 /* free the alias list */
454 a_list = a_list->next;
458 /* remove any aliases marked for deletion */
460 for (a_cur = Aliases; a_cur;) {
463 a_list->next = a_cur->next;
465 Aliases = a_cur->next;
468 mutt_free_alias (&a_cur);
484 static int string_is_address (const char *str, const char *u, const char *d)
486 char buf[LONG_STRING];
488 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
489 if (ascii_strcasecmp (str, buf) == 0)
495 /* returns TRUE if the given address belongs to the user. */
496 int mutt_addr_is_user (ADDRESS * addr)
498 /* NULL address is assumed to be the user. */
500 debug_print(5, ("yes, NULL address\n"));
503 if (!addr->mailbox) {
504 debug_print(5, ("no, no mailbox\n"));
508 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
509 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
512 if (string_is_address (addr->mailbox, Username, Hostname)) {
513 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
516 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
517 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
520 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
521 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
525 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
526 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
530 if (rx_list_match (Alternates, addr->mailbox)) {
531 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
532 if (rx_list_match (UnAlternates, addr->mailbox))
533 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
538 debug_print(5, ("no, all failed.\n"));
542 static const char *alias_format_str (char *dest, size_t destlen, char op,
543 const char *src, const char *fmt,
544 const char *ifstring,
545 const char *elsestring,
546 unsigned long data, format_flag flags)
548 char tmp[SHORT_STRING], adr[SHORT_STRING];
549 ALIAS *alias = (ALIAS *) data;
553 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
554 snprintf (dest, destlen, tmp, alias->del ? "D" : " ");
557 mutt_format_s (dest, destlen, fmt, alias->name);
561 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
562 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
563 snprintf (dest, destlen, tmp, adr);
566 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
567 snprintf (dest, destlen, tmp, alias->num + 1);
570 dest[0] = alias->tagged ? '*' : ' ';
578 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
580 mutt_FormatString (s, slen, NONULL (AliasFmt), alias_format_str,
581 (unsigned long) ((ALIAS **) m->data)[num],
582 M_FORMAT_ARROWCURSOR);
585 static int alias_tag (MUTTMENU * menu, int n, int m)
587 ALIAS *cur = ((ALIAS **) menu->data)[n];
588 int ot = cur->tagged;
590 cur->tagged = (m >= 0 ? m : !cur->tagged);
592 return cur->tagged - ot;
595 static int alias_SortAlias (const void *a, const void *b)
597 ALIAS *pa = *(ALIAS **) a;
598 ALIAS *pb = *(ALIAS **) b;
599 int r = str_casecmp (pa->name, pb->name);
604 static int alias_SortAddress (const void *a, const void *b)
606 ADDRESS *pa = (*(ALIAS **) a)->addr;
607 ADDRESS *pb = (*(ALIAS **) b)->addr;
616 else if (pa->personal) {
618 r = str_casecmp (pa->personal, pb->personal);
622 else if (pb->personal)
625 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
629 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
633 ALIAS **AliasTable = NULL;
637 char helpstr[SHORT_STRING];
642 mutt_error _("You have no aliases!");
647 /* tell whoever called me to redraw the screen when I return */
648 set_option (OPTNEEDREDRAW);
650 menu = mutt_new_menu ();
651 menu->make_entry = alias_entry;
652 menu->tag = alias_tag;
653 menu->menu = MENU_ALIAS;
654 menu->title = _("Aliases");
656 mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
662 /* count the number of aliases */
663 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
664 aliasp->self->del = 0;
665 aliasp->self->tagged = 0;
669 mem_realloc (&AliasTable, menu->max * sizeof (ALIAS *));
670 menu->data = AliasTable;
672 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
673 AliasTable[i] = aliasp->self;
677 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
678 qsort (AliasTable, i, sizeof (ALIAS *),
679 (SortAlias & SORT_MASK) ==
680 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
683 for (i = 0; i < menu->max; i++)
684 AliasTable[i]->num = i;
688 menu->redraw |= REDRAW_FULL;
689 aliases = aliases->next;
693 switch ((op = mutt_menuLoop (menu))) {
696 if (menu->tagprefix) {
697 for (i = 0; i < menu->max; i++)
698 if (AliasTable[i]->tagged)
699 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
700 menu->redraw |= REDRAW_INDEX;
703 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
704 menu->redraw |= REDRAW_CURRENT;
705 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
707 menu->redraw |= REDRAW_INDEX;
711 case OP_GENERIC_SELECT_ENTRY:
719 for (i = 0; i < menu->max; i++) {
720 if (AliasTable[i]->tagged) {
721 mutt_addrlist_to_local (AliasTable[i]->addr);
722 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
728 mutt_addrlist_to_local (AliasTable[t]->addr);
729 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
732 mutt_menuDestroy (&menu);
733 mem_free (&AliasTable);