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>
23 #include <lib-lib/debug.h>
24 #include <lib-lib/rx.h>
26 #include <lib-sys/unix.h>
28 #include <lib-ui/curses.h>
29 #include <lib-ui/enter.h>
30 #include <lib-ui/menu.h>
33 #include "mutt_idna.h"
36 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
38 static struct mapping_t AliasHelp[] = {
39 {N_("Exit"), OP_EXIT},
40 {N_("Del"), OP_DELETE},
41 {N_("Undel"), OP_UNDELETE},
42 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
43 {N_("Help"), OP_HELP},
47 address_t *mutt_lookup_alias (const char *s)
51 for (; t; t = t->next)
52 if (!m_strcasecmp(s, t->name))
54 return (NULL); /* no such alias */
57 static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn)
59 address_t *head = NULL, *last = NULL, *t, *w;
65 if (!a->group && !a->personal && a->mailbox
66 && strchr (a->mailbox, '@') == NULL) {
67 t = mutt_lookup_alias (a->mailbox);
71 for (u = *expn; u; u = u->next) {
72 if (m_strcmp(a->mailbox, u->data) == 0) { /* alias already found */
73 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
81 u->data = m_strdup(a->mailbox);
84 w = address_list_dup (t);
85 w = mutt_expand_aliases_r (w, expn);
90 while (last && last->next)
100 struct passwd *pw = getpwnam (a->mailbox);
103 char namebuf[STRING];
105 mutt_gecos_name(namebuf, sizeof (namebuf), pw, GecosMask.rx);
106 m_strreplace(&a->personal, namebuf);
121 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
122 /* now qualify all local addresses */
123 rfc822_qualify (head, fqdn);
129 address_t *mutt_expand_aliases (address_t * a)
132 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
134 t = mutt_expand_aliases_r (a, &expn);
135 mutt_free_list (&expn);
136 return (mutt_remove_duplicates (t));
139 void mutt_expand_aliases_env (ENVELOPE * env)
141 env->from = mutt_expand_aliases (env->from);
142 env->to = mutt_expand_aliases (env->to);
143 env->cc = mutt_expand_aliases (env->cc);
144 env->bcc = mutt_expand_aliases (env->bcc);
145 env->reply_to = mutt_expand_aliases (env->reply_to);
146 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
151 * if someone has an address like
152 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
153 * and the user creates an alias for this, Mutt could wind up executing
154 * the backtics because it writes aliases like
155 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
156 * To avoid this problem, use a backslash (\) to quote any backtics. We also
157 * need to quote backslashes as well, since you could defeat the above by
159 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
160 * since that would get aliased as
161 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
162 * which still gets evaluated because the double backslash is not a quote.
164 * Additionally, we need to quote ' and " characters - otherwise, mutt will
165 * interpret them on the wrong parsing step.
167 * $ wants to be quoted since it may indicate the start of an environment
171 static void write_safe_address (FILE * fp, char *s)
174 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
181 address_t *mutt_get_address (ENVELOPE * env, const char **pfxp)
184 const char *pfx = NULL;
186 if (mutt_addr_is_user (env->from)) {
187 if (env->to && !mutt_is_mail_list (env->to)) {
196 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
211 void mutt_create_alias (ENVELOPE * cur, address_t * iadr)
214 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
216 char fixed[LONG_STRING];
218 address_t *adr = NULL;
221 adr = mutt_get_address (cur, NULL);
227 if (adr && adr->mailbox) {
228 m_strcpy(buf, sizeof(buf), adr->mailbox);
229 if ((pc = strchr (buf, '@')))
235 /* Don't suggest a bad alias name in the event of a strange local part. */
236 mutt_check_alias_name (buf, buf);
239 /* add a new alias */
240 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
243 /* check to see if the user already has an alias defined */
244 if (mutt_lookup_alias (buf)) {
245 mutt_error _("You already have an alias defined with that name!");
250 if (mutt_check_alias_name (buf, fixed)) {
252 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
254 m_strcpy(buf, sizeof(buf), fixed);
261 new = p_new(ALIAS, 1);
263 new->name = m_strdup(buf);
265 mutt_addrlist_to_local (adr);
268 m_strcpy(buf, sizeof(buf), adr->mailbox);
272 mutt_addrlist_to_idna (adr, NULL);
275 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
276 mutt_free_alias (&new);
280 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
282 if (mutt_addrlist_to_idna (new->addr, &err)) {
283 mutt_error (_("Error: '%s' is a bad IDN."), err);
288 while (new->addr == NULL);
290 if (adr && adr->personal && !mutt_is_mail_list(adr))
291 m_strcpy(buf, sizeof(buf), adr->personal);
295 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
296 mutt_free_alias (&new);
299 new->addr->personal = m_strdup(buf);
302 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
303 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
304 if (mutt_yesorno (prompt, M_YES) != M_YES) {
305 mutt_free_alias (&new);
317 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
318 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
320 mutt_expand_path (buf, sizeof (buf));
321 if ((rc = safe_fopen (buf, "a"))) {
322 if (mutt_check_alias_name (new->name, NULL))
323 mutt_quote_filename(buf, sizeof(buf), new->name);
325 m_strcpy(buf, sizeof(buf), new->name);
326 fprintf (rc, "alias %s ", buf);
328 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
329 write_safe_address (rc, buf);
332 mutt_message _("Alias added.");
339 * Sanity-check an alias name: Only characters which are non-special to both
340 * the RFC 822 and the mutt configuration parser are permitted.
343 static int check_alias_name_char (char c)
345 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
346 isalnum ((unsigned char) c));
349 int mutt_check_alias_name (const char *s, char *d)
354 if (!check_alias_name_char (*s)) {
371 * This routine looks to see if the user has an alias defined for the given
374 address_t *alias_reverse_lookup (address_t * a)
379 if (!a || !a->mailbox)
382 for (; t; t = t->next) {
383 /* cycle through all addresses if this is a group alias */
384 for (ap = t->addr; ap; ap = ap->next) {
385 if (!ap->group && ap->mailbox &&
386 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
393 /* alias_complete() -- alias completion routine
395 * given a partial alias, this routine attempts to fill in the alias
396 * from the alias list as much as possible. if given empty search string
397 * or found nothing, present all aliases
399 int mutt_alias_complete (char *s, size_t buflen)
402 ALIAS *a_list = NULL, *a_cur = NULL;
403 char bestname[HUGE_STRING];
406 #define min(a,b) ((a<b)?a:b)
408 if (s[0] != 0) { /* avoid empty string as strstr argument */
409 p_clear(bestname, countof(bestname));
412 if (a->name && strstr (a->name, s) == a->name) {
413 if (!bestname[0]) /* init */
414 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
417 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
424 if (bestname[0] != 0) {
425 if (m_strcmp(bestname, s) != 0) {
426 /* we are adding something to the completion */
427 m_strcpy(s, m_strlen(bestname) + 1, bestname);
431 /* build alias list and show it */
435 if (a->name && (strstr (a->name, s) == a->name)) {
436 if (!a_list) /* init */
437 a_cur = a_list = p_new(ALIAS, 1);
439 a_cur->next = p_new(ALIAS, 1);
442 memcpy (a_cur, a, sizeof (ALIAS));
451 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
452 if (bestname[0] != 0)
453 m_strcpy(s, buflen, bestname);
455 /* free the alias list */
458 a_list = a_list->next;
462 /* remove any aliases marked for deletion */
464 for (a_cur = Aliases; a_cur;) {
467 a_list->next = a_cur->next;
469 Aliases = a_cur->next;
472 mutt_free_alias (&a_cur);
488 static int string_is_address (const char *str, const char *u, const char *d)
490 char buf[LONG_STRING];
492 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
493 if (ascii_strcasecmp (str, buf) == 0)
499 /* returns TRUE if the given address belongs to the user. */
500 int mutt_addr_is_user (address_t * addr)
502 /* NULL address is assumed to be the user. */
504 debug_print(5, ("yes, NULL address\n"));
507 if (!addr->mailbox) {
508 debug_print(5, ("no, no mailbox\n"));
512 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
513 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
516 if (string_is_address (addr->mailbox, Username, Hostname)) {
517 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
520 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
521 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
524 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
525 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
529 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
530 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
534 if (rx_list_match (Alternates, addr->mailbox)) {
535 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
536 if (rx_list_match (UnAlternates, addr->mailbox))
537 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
542 debug_print(5, ("no, all failed.\n"));
546 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
547 const format_t *src, const char *fmt,
548 const char *ifstring __attribute__ ((unused)),
549 const char *elsestring __attribute__ ((unused)),
550 unsigned long data, format_flag flags __attribute__ ((unused)))
552 char tmp[SHORT_STRING], adr[SHORT_STRING];
553 ALIAS *alias = (ALIAS *) data;
558 m_strcpy (dest, sizeof(dest), "D");
560 m_strcpy (dest, sizeof(dest), " ");
563 mutt_format_s (dest, destlen, fmt, alias->name);
567 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
568 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
569 snprintf (dest, destlen, tmp, adr);
572 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
573 snprintf (dest, destlen, tmp, alias->num + 1);
576 dest[0] = alias->tagged ? '*' : ' ';
584 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
586 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *) alias_format_str,
587 (unsigned long) ((ALIAS **) m->data)[num],
588 M_FORMAT_ARROWCURSOR);
591 static int alias_tag (MUTTMENU * menu, int n, int m)
593 ALIAS *cur = ((ALIAS **) menu->data)[n];
594 int ot = cur->tagged;
596 cur->tagged = (m >= 0 ? m : !cur->tagged);
598 return cur->tagged - ot;
601 static int alias_SortAlias (const void *a, const void *b)
603 ALIAS *pa = *(ALIAS **) a;
604 ALIAS *pb = *(ALIAS **) b;
605 int r = m_strcasecmp(pa->name, pb->name);
610 static int alias_SortAddress (const void *a, const void *b)
612 address_t *pa = (*(ALIAS **) a)->addr;
613 address_t *pb = (*(ALIAS **) b)->addr;
622 else if (pa->personal) {
624 r = m_strcasecmp(pa->personal, pb->personal);
628 else if (pb->personal)
631 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
635 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
639 ALIAS **AliasTable = NULL;
643 char helpstr[SHORT_STRING];
648 mutt_error _("You have no aliases!");
653 /* tell whoever called me to redraw the screen when I return */
654 set_option (OPTNEEDREDRAW);
656 menu = mutt_new_menu ();
657 menu->make_entry = alias_entry;
658 menu->tag = alias_tag;
659 menu->menu = MENU_ALIAS;
660 menu->title = _("Aliases");
662 mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
668 /* count the number of aliases */
669 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
670 aliasp->self->del = 0;
671 aliasp->self->tagged = 0;
675 p_realloc(&AliasTable, menu->max);
676 menu->data = AliasTable;
678 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
679 AliasTable[i] = aliasp->self;
683 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
684 qsort (AliasTable, i, sizeof (ALIAS *),
685 (SortAlias & SORT_MASK) ==
686 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
689 for (i = 0; i < menu->max; i++)
690 AliasTable[i]->num = i;
694 menu->redraw |= REDRAW_FULL;
695 aliases = aliases->next;
699 switch ((op = mutt_menuLoop (menu))) {
702 if (menu->tagprefix) {
703 for (i = 0; i < menu->max; i++)
704 if (AliasTable[i]->tagged)
705 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
706 menu->redraw |= REDRAW_INDEX;
709 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
710 menu->redraw |= REDRAW_CURRENT;
711 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
713 menu->redraw |= REDRAW_INDEX;
717 case OP_GENERIC_SELECT_ENTRY:
725 for (i = 0; i < menu->max; i++) {
726 if (AliasTable[i]->tagged) {
727 mutt_addrlist_to_local (AliasTable[i]->addr);
728 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
734 mutt_addrlist_to_local (AliasTable[t]->addr);
735 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
738 mutt_menuDestroy (&menu);
739 p_delete(&AliasTable);