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/macros.h>
23 #include "lib/debug.h"
27 #include "mutt_curses.h"
28 #include "mutt_idna.h"
29 #include "mutt_menu.h"
33 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
35 static struct mapping_t AliasHelp[] = {
36 {N_("Exit"), OP_EXIT},
37 {N_("Del"), OP_DELETE},
38 {N_("Undel"), OP_UNDELETE},
39 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
40 {N_("Help"), OP_HELP},
44 ADDRESS *mutt_lookup_alias (const char *s)
48 for (; t; t = t->next)
49 if (!str_casecmp (s, t->name))
51 return (NULL); /* no such alias */
54 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
56 ADDRESS *head = NULL, *last = NULL, *t, *w;
62 if (!a->group && !a->personal && a->mailbox
63 && strchr (a->mailbox, '@') == NULL) {
64 t = mutt_lookup_alias (a->mailbox);
68 for (u = *expn; u; u = u->next) {
69 if (str_cmp (a->mailbox, u->data) == 0) { /* alias already found */
70 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
78 u->data = m_strdup(a->mailbox);
81 w = rfc822_cpy_adr (t);
82 w = mutt_expand_aliases_r (w, expn);
87 while (last && last->next)
93 rfc822_free_address (&t);
97 struct passwd *pw = getpwnam (a->mailbox);
100 char namebuf[STRING];
102 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
103 str_replace (&a->personal, namebuf);
118 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
119 /* now qualify all local addresses */
120 rfc822_qualify (head, fqdn);
126 ADDRESS *mutt_expand_aliases (ADDRESS * a)
129 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
131 t = mutt_expand_aliases_r (a, &expn);
132 mutt_free_list (&expn);
133 return (mutt_remove_duplicates (t));
136 void mutt_expand_aliases_env (ENVELOPE * env)
138 env->from = mutt_expand_aliases (env->from);
139 env->to = mutt_expand_aliases (env->to);
140 env->cc = mutt_expand_aliases (env->cc);
141 env->bcc = mutt_expand_aliases (env->bcc);
142 env->reply_to = mutt_expand_aliases (env->reply_to);
143 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
148 * if someone has an address like
149 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
150 * and the user creates an alias for this, Mutt could wind up executing
151 * the backtics because it writes aliases like
152 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
153 * To avoid this problem, use a backslash (\) to quote any backtics. We also
154 * need to quote backslashes as well, since you could defeat the above by
156 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
157 * since that would get aliased as
158 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
159 * which still gets evaluated because the double backslash is not a quote.
161 * Additionally, we need to quote ' and " characters - otherwise, mutt will
162 * interpret them on the wrong parsing step.
164 * $ wants to be quoted since it may indicate the start of an environment
168 static void write_safe_address (FILE * fp, char *s)
171 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
178 ADDRESS *mutt_get_address (ENVELOPE * env, const char **pfxp)
181 const char *pfx = NULL;
183 if (mutt_addr_is_user (env->from)) {
184 if (env->to && !mutt_is_mail_list (env->to)) {
193 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
208 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
211 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
213 char fixed[LONG_STRING];
218 adr = mutt_get_address (cur, NULL);
224 if (adr && adr->mailbox) {
225 strfcpy (buf, adr->mailbox, sizeof (buf));
226 if ((pc = strchr (buf, '@')))
232 /* Don't suggest a bad alias name in the event of a strange local part. */
233 mutt_check_alias_name (buf, buf);
236 /* add a new alias */
237 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
240 /* check to see if the user already has an alias defined */
241 if (mutt_lookup_alias (buf)) {
242 mutt_error _("You already have an alias defined with that name!");
247 if (mutt_check_alias_name (buf, fixed)) {
249 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
251 strfcpy (buf, fixed, sizeof (buf));
258 new = p_new(ALIAS, 1);
260 new->name = m_strdup(buf);
262 mutt_addrlist_to_local (adr);
265 strfcpy (buf, adr->mailbox, sizeof (buf));
269 mutt_addrlist_to_idna (adr, NULL);
272 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
273 mutt_free_alias (&new);
277 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
279 if (mutt_addrlist_to_idna (new->addr, &err)) {
280 mutt_error (_("Error: '%s' is a bad IDN."), err);
285 while (new->addr == NULL);
287 if (adr && adr->personal && !mutt_is_mail_list (adr))
288 strfcpy (buf, adr->personal, sizeof (buf));
292 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
293 mutt_free_alias (&new);
296 new->addr->personal = m_strdup(buf);
299 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
300 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
301 if (mutt_yesorno (prompt, M_YES) != M_YES) {
302 mutt_free_alias (&new);
314 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
315 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
317 mutt_expand_path (buf, sizeof (buf));
318 if ((rc = safe_fopen (buf, "a"))) {
319 if (mutt_check_alias_name (new->name, NULL))
320 mutt_quote_filename (buf, sizeof (buf), new->name);
322 strfcpy (buf, new->name, sizeof (buf));
323 fprintf (rc, "alias %s ", buf);
325 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
326 write_safe_address (rc, buf);
329 mutt_message _("Alias added.");
336 * Sanity-check an alias name: Only characters which are non-special to both
337 * the RFC 822 and the mutt configuration parser are permitted.
340 static int check_alias_name_char (char c)
342 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
343 isalnum ((unsigned char) c));
346 int mutt_check_alias_name (const char *s, char *d)
351 if (!check_alias_name_char (*s)) {
368 * This routine looks to see if the user has an alias defined for the given
371 ADDRESS *alias_reverse_lookup (ADDRESS * a)
376 if (!a || !a->mailbox)
379 for (; t; t = t->next) {
380 /* cycle through all addresses if this is a group alias */
381 for (ap = t->addr; ap; ap = ap->next) {
382 if (!ap->group && ap->mailbox &&
383 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
390 /* alias_complete() -- alias completion routine
392 * given a partial alias, this routine attempts to fill in the alias
393 * from the alias list as much as possible. if given empty search string
394 * or found nothing, present all aliases
396 int mutt_alias_complete (char *s, size_t buflen)
399 ALIAS *a_list = NULL, *a_cur = NULL;
400 char bestname[HUGE_STRING];
403 #define min(a,b) ((a<b)?a:b)
405 if (s[0] != 0) { /* avoid empty string as strstr argument */
406 p_clear(bestname, sizeof(bestname));
409 if (a->name && strstr (a->name, s) == a->name) {
410 if (!bestname[0]) /* init */
411 strfcpy (bestname, a->name,
412 min (m_strlen(a->name) + 1, sizeof (bestname)));
414 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
421 if (bestname[0] != 0) {
422 if (str_cmp (bestname, s) != 0) {
423 /* we are adding something to the completion */
424 strfcpy (s, bestname, m_strlen(bestname) + 1);
428 /* build alias list and show it */
432 if (a->name && (strstr (a->name, s) == a->name)) {
433 if (!a_list) /* init */
434 a_cur = a_list = p_new(ALIAS, 1);
436 a_cur->next = p_new(ALIAS, 1);
439 memcpy (a_cur, a, sizeof (ALIAS));
448 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
449 if (bestname[0] != 0)
450 strfcpy (s, bestname, buflen);
452 /* free the alias list */
455 a_list = a_list->next;
459 /* remove any aliases marked for deletion */
461 for (a_cur = Aliases; a_cur;) {
464 a_list->next = a_cur->next;
466 Aliases = a_cur->next;
469 mutt_free_alias (&a_cur);
485 static int string_is_address (const char *str, const char *u, const char *d)
487 char buf[LONG_STRING];
489 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
490 if (ascii_strcasecmp (str, buf) == 0)
496 /* returns TRUE if the given address belongs to the user. */
497 int mutt_addr_is_user (ADDRESS * addr)
499 /* NULL address is assumed to be the user. */
501 debug_print(5, ("yes, NULL address\n"));
504 if (!addr->mailbox) {
505 debug_print(5, ("no, no mailbox\n"));
509 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
510 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
513 if (string_is_address (addr->mailbox, Username, Hostname)) {
514 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
517 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
518 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
521 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
522 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
526 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
527 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
531 if (rx_list_match (Alternates, addr->mailbox)) {
532 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
533 if (rx_list_match (UnAlternates, addr->mailbox))
534 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
539 debug_print(5, ("no, all failed.\n"));
543 static const char *alias_format_str (char *dest, size_t destlen, char op,
544 const char *src, const char *fmt,
545 const char *ifstring,
546 const char *elsestring,
547 unsigned long data, format_flag flags)
549 char tmp[SHORT_STRING], adr[SHORT_STRING];
550 ALIAS *alias = (ALIAS *) data;
554 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
555 snprintf (dest, destlen, tmp, alias->del ? "D" : " ");
558 mutt_format_s (dest, destlen, fmt, alias->name);
562 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
563 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
564 snprintf (dest, destlen, tmp, adr);
567 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
568 snprintf (dest, destlen, tmp, alias->num + 1);
571 dest[0] = alias->tagged ? '*' : ' ';
579 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
581 mutt_FormatString (s, slen, NONULL (AliasFmt), alias_format_str,
582 (unsigned long) ((ALIAS **) m->data)[num],
583 M_FORMAT_ARROWCURSOR);
586 static int alias_tag (MUTTMENU * menu, int n, int m)
588 ALIAS *cur = ((ALIAS **) menu->data)[n];
589 int ot = cur->tagged;
591 cur->tagged = (m >= 0 ? m : !cur->tagged);
593 return cur->tagged - ot;
596 static int alias_SortAlias (const void *a, const void *b)
598 ALIAS *pa = *(ALIAS **) a;
599 ALIAS *pb = *(ALIAS **) b;
600 int r = str_casecmp (pa->name, pb->name);
605 static int alias_SortAddress (const void *a, const void *b)
607 ADDRESS *pa = (*(ALIAS **) a)->addr;
608 ADDRESS *pb = (*(ALIAS **) b)->addr;
617 else if (pa->personal) {
619 r = str_casecmp (pa->personal, pb->personal);
623 else if (pb->personal)
626 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
630 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
634 ALIAS **AliasTable = NULL;
638 char helpstr[SHORT_STRING];
643 mutt_error _("You have no aliases!");
648 /* tell whoever called me to redraw the screen when I return */
649 set_option (OPTNEEDREDRAW);
651 menu = mutt_new_menu ();
652 menu->make_entry = alias_entry;
653 menu->tag = alias_tag;
654 menu->menu = MENU_ALIAS;
655 menu->title = _("Aliases");
657 mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
663 /* count the number of aliases */
664 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
665 aliasp->self->del = 0;
666 aliasp->self->tagged = 0;
670 p_realloc(&AliasTable, menu->max);
671 menu->data = AliasTable;
673 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
674 AliasTable[i] = aliasp->self;
678 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
679 qsort (AliasTable, i, sizeof (ALIAS *),
680 (SortAlias & SORT_MASK) ==
681 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
684 for (i = 0; i < menu->max; i++)
685 AliasTable[i]->num = i;
689 menu->redraw |= REDRAW_FULL;
690 aliases = aliases->next;
694 switch ((op = mutt_menuLoop (menu))) {
697 if (menu->tagprefix) {
698 for (i = 0; i < menu->max; i++)
699 if (AliasTable[i]->tagged)
700 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
701 menu->redraw |= REDRAW_INDEX;
704 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
705 menu->redraw |= REDRAW_CURRENT;
706 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
708 menu->redraw |= REDRAW_INDEX;
712 case OP_GENERIC_SELECT_ENTRY:
720 for (i = 0; i < menu->max; i++) {
721 if (AliasTable[i]->tagged) {
722 mutt_addrlist_to_local (AliasTable[i]->addr);
723 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
729 mutt_addrlist_to_local (AliasTable[t]->addr);
730 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
733 mutt_menuDestroy (&menu);
734 p_delete(&AliasTable);