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"
24 #include "mutt_curses.h"
25 #include "mutt_idna.h"
26 #include "mutt_menu.h"
30 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
32 static struct mapping_t AliasHelp[] = {
33 {N_("Exit"), OP_EXIT},
34 {N_("Del"), OP_DELETE},
35 {N_("Undel"), OP_UNDELETE},
36 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
37 {N_("Help"), OP_HELP},
41 ADDRESS *mutt_lookup_alias (const char *s)
45 for (; t; t = t->next)
46 if (!str_casecmp (s, t->name))
48 return (NULL); /* no such alias */
51 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
53 ADDRESS *head = NULL, *last = NULL, *t, *w;
59 if (!a->group && !a->personal && a->mailbox
60 && strchr (a->mailbox, '@') == NULL) {
61 t = mutt_lookup_alias (a->mailbox);
65 for (u = *expn; u; u = u->next) {
66 if (str_cmp (a->mailbox, u->data) == 0) { /* alias already found */
67 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
74 u = mem_malloc (sizeof (LIST));
75 u->data = str_dup (a->mailbox);
78 w = rfc822_cpy_adr (t);
79 w = mutt_expand_aliases_r (w, expn);
84 while (last && last->next)
90 rfc822_free_address (&t);
94 struct passwd *pw = getpwnam (a->mailbox);
99 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
100 str_replace (&a->personal, namebuf);
115 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
116 /* now qualify all local addresses */
117 rfc822_qualify (head, fqdn);
123 ADDRESS *mutt_expand_aliases (ADDRESS * a)
126 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
128 t = mutt_expand_aliases_r (a, &expn);
129 mutt_free_list (&expn);
130 return (mutt_remove_duplicates (t));
133 void mutt_expand_aliases_env (ENVELOPE * env)
135 env->from = mutt_expand_aliases (env->from);
136 env->to = mutt_expand_aliases (env->to);
137 env->cc = mutt_expand_aliases (env->cc);
138 env->bcc = mutt_expand_aliases (env->bcc);
139 env->reply_to = mutt_expand_aliases (env->reply_to);
140 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
145 * if someone has an address like
146 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
147 * and the user creates an alias for this, Mutt could wind up executing
148 * the backtics because it writes aliases like
149 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
150 * To avoid this problem, use a backslash (\) to quote any backtics. We also
151 * need to quote backslashes as well, since you could defeat the above by
153 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
154 * since that would get aliased as
155 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
156 * which still gets evaluated because the double backslash is not a quote.
158 * Additionally, we need to quote ' and " characters - otherwise, mutt will
159 * interpret them on the wrong parsing step.
161 * $ wants to be quoted since it may indicate the start of an environment
165 static void write_safe_address (FILE * fp, char *s)
168 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
175 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
180 if (mutt_addr_is_user (env->from)) {
181 if (env->to && !mutt_is_mail_list (env->to)) {
190 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
205 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
208 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
210 char fixed[LONG_STRING];
215 adr = mutt_get_address (cur, NULL);
221 if (adr && adr->mailbox) {
222 strfcpy (buf, adr->mailbox, sizeof (buf));
223 if ((pc = strchr (buf, '@')))
229 /* Don't suggest a bad alias name in the event of a strange local part. */
230 mutt_check_alias_name (buf, buf);
233 /* add a new alias */
234 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
237 /* check to see if the user already has an alias defined */
238 if (mutt_lookup_alias (buf)) {
239 mutt_error _("You already have an alias defined with that name!");
244 if (mutt_check_alias_name (buf, fixed)) {
246 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
248 strfcpy (buf, fixed, sizeof (buf));
255 new = mem_calloc (1, sizeof (ALIAS));
257 new->name = str_dup (buf);
259 mutt_addrlist_to_local (adr);
262 strfcpy (buf, adr->mailbox, sizeof (buf));
266 mutt_addrlist_to_idna (adr, NULL);
269 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
270 mutt_free_alias (&new);
274 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
276 if (mutt_addrlist_to_idna (new->addr, &err)) {
277 mutt_error (_("Error: '%s' is a bad IDN."), err);
282 while (new->addr == NULL);
284 if (adr && adr->personal && !mutt_is_mail_list (adr))
285 strfcpy (buf, adr->personal, sizeof (buf));
289 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
290 mutt_free_alias (&new);
293 new->addr->personal = str_dup (buf);
296 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
297 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
298 if (mutt_yesorno (prompt, M_YES) != M_YES) {
299 mutt_free_alias (&new);
311 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
312 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
314 mutt_expand_path (buf, sizeof (buf));
315 if ((rc = safe_fopen (buf, "a"))) {
316 if (mutt_check_alias_name (new->name, NULL))
317 mutt_quote_filename (buf, sizeof (buf), new->name);
319 strfcpy (buf, new->name, sizeof (buf));
320 fprintf (rc, "alias %s ", buf);
322 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
323 write_safe_address (rc, buf);
326 mutt_message _("Alias added.");
333 * Sanity-check an alias name: Only characters which are non-special to both
334 * the RFC 822 and the mutt configuration parser are permitted.
337 static int check_alias_name_char (char c)
339 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
340 isalnum ((unsigned char) c));
343 int mutt_check_alias_name (const char *s, char *d)
348 if (!check_alias_name_char (*s)) {
365 * This routine looks to see if the user has an alias defined for the given
368 ADDRESS *alias_reverse_lookup (ADDRESS * a)
373 if (!a || !a->mailbox)
376 for (; t; t = t->next) {
377 /* cycle through all addresses if this is a group alias */
378 for (ap = t->addr; ap; ap = ap->next) {
379 if (!ap->group && ap->mailbox &&
380 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
387 /* alias_complete() -- alias completion routine
389 * given a partial alias, this routine attempts to fill in the alias
390 * from the alias list as much as possible. if given empty search string
391 * or found nothing, present all aliases
393 int mutt_alias_complete (char *s, size_t buflen)
396 ALIAS *a_list = NULL, *a_cur = NULL;
397 char bestname[HUGE_STRING];
400 #define min(a,b) ((a<b)?a:b)
402 if (s[0] != 0) { /* avoid empty string as strstr argument */
403 memset (bestname, 0, sizeof (bestname));
406 if (a->name && strstr (a->name, s) == a->name) {
407 if (!bestname[0]) /* init */
408 strfcpy (bestname, a->name,
409 min (str_len (a->name) + 1, sizeof (bestname)));
411 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
418 if (bestname[0] != 0) {
419 if (str_cmp (bestname, s) != 0) {
420 /* we are adding something to the completion */
421 strfcpy (s, bestname, str_len (bestname) + 1);
425 /* build alias list and show it */
429 if (a->name && (strstr (a->name, s) == a->name)) {
430 if (!a_list) /* init */
431 a_cur = a_list = (ALIAS *) mem_malloc (sizeof (ALIAS));
433 a_cur->next = (ALIAS *) mem_malloc (sizeof (ALIAS));
436 memcpy (a_cur, a, sizeof (ALIAS));
445 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
446 if (bestname[0] != 0)
447 strfcpy (s, bestname, buflen);
449 /* free the alias list */
452 a_list = a_list->next;
456 /* remove any aliases marked for deletion */
458 for (a_cur = Aliases; a_cur;) {
461 a_list->next = a_cur->next;
463 Aliases = a_cur->next;
466 mutt_free_alias (&a_cur);
482 static int string_is_address (const char *str, const char *u, const char *d)
484 char buf[LONG_STRING];
486 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
487 if (ascii_strcasecmp (str, buf) == 0)
493 /* returns TRUE if the given address belongs to the user. */
494 int mutt_addr_is_user (ADDRESS * addr)
496 /* NULL address is assumed to be the user. */
498 debug_print(5, ("yes, NULL address\n"));
501 if (!addr->mailbox) {
502 debug_print(5, ("no, no mailbox\n"));
506 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
507 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
510 if (string_is_address (addr->mailbox, Username, Hostname)) {
511 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
514 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
515 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
518 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
519 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
523 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
524 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
528 if (rx_list_match (Alternates, addr->mailbox)) {
529 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
530 if (rx_list_match (UnAlternates, addr->mailbox))
531 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
536 debug_print(5, ("no, all failed.\n"));
540 static const char *alias_format_str (char *dest, size_t destlen, char op,
541 const char *src, const char *fmt,
542 const char *ifstring,
543 const char *elsestring,
544 unsigned long data, format_flag flags)
546 char tmp[SHORT_STRING], adr[SHORT_STRING];
547 ALIAS *alias = (ALIAS *) data;
551 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
552 snprintf (dest, destlen, tmp, alias->del ? "D" : " ");
555 mutt_format_s (dest, destlen, fmt, alias->name);
559 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
560 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
561 snprintf (dest, destlen, tmp, adr);
564 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
565 snprintf (dest, destlen, tmp, alias->num + 1);
568 dest[0] = alias->tagged ? '*' : ' ';
576 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
578 mutt_FormatString (s, slen, NONULL (AliasFmt), alias_format_str,
579 (unsigned long) ((ALIAS **) m->data)[num],
580 M_FORMAT_ARROWCURSOR);
583 static int alias_tag (MUTTMENU * menu, int n, int m)
585 ALIAS *cur = ((ALIAS **) menu->data)[n];
586 int ot = cur->tagged;
588 cur->tagged = (m >= 0 ? m : !cur->tagged);
590 return cur->tagged - ot;
593 static int alias_SortAlias (const void *a, const void *b)
595 ALIAS *pa = *(ALIAS **) a;
596 ALIAS *pb = *(ALIAS **) b;
597 int r = str_casecmp (pa->name, pb->name);
602 static int alias_SortAddress (const void *a, const void *b)
604 ADDRESS *pa = (*(ALIAS **) a)->addr;
605 ADDRESS *pb = (*(ALIAS **) b)->addr;
614 else if (pa->personal) {
616 r = str_casecmp (pa->personal, pb->personal);
620 else if (pb->personal)
623 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
627 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
631 ALIAS **AliasTable = NULL;
635 char helpstr[SHORT_STRING];
640 mutt_error _("You have no aliases!");
645 /* tell whoever called me to redraw the screen when I return */
646 set_option (OPTNEEDREDRAW);
648 menu = mutt_new_menu ();
649 menu->make_entry = alias_entry;
650 menu->tag = alias_tag;
651 menu->menu = MENU_ALIAS;
652 menu->title = _("Aliases");
654 mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
660 /* count the number of aliases */
661 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
662 aliasp->self->del = 0;
663 aliasp->self->tagged = 0;
667 mem_realloc (&AliasTable, menu->max * sizeof (ALIAS *));
668 menu->data = AliasTable;
670 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
671 AliasTable[i] = aliasp->self;
675 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
676 qsort (AliasTable, i, sizeof (ALIAS *),
677 (SortAlias & SORT_MASK) ==
678 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
681 for (i = 0; i < menu->max; i++)
682 AliasTable[i]->num = i;
686 menu->redraw |= REDRAW_FULL;
687 aliases = aliases->next;
691 switch ((op = mutt_menuLoop (menu))) {
694 if (menu->tagprefix) {
695 for (i = 0; i < menu->max; i++)
696 if (AliasTable[i]->tagged)
697 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
698 menu->redraw |= REDRAW_INDEX;
701 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
702 menu->redraw |= REDRAW_CURRENT;
703 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
705 menu->redraw |= REDRAW_INDEX;
709 case OP_GENERIC_SELECT_ENTRY:
717 for (i = 0; i < menu->max; i++) {
718 if (AliasTable[i]->tagged) {
719 mutt_addrlist_to_local (AliasTable[i]->addr);
720 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
726 mutt_addrlist_to_local (AliasTable[t]->addr);
727 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
730 mutt_menuDestroy (&menu);
731 mem_free (&AliasTable);