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>
24 #include <lib-ui/curses.h>
25 #include <lib-ui/enter.h>
28 #include "lib/debug.h"
31 #include "mutt_idna.h"
32 #include "mutt_menu.h"
35 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
37 static struct mapping_t AliasHelp[] = {
38 {N_("Exit"), OP_EXIT},
39 {N_("Del"), OP_DELETE},
40 {N_("Undel"), OP_UNDELETE},
41 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
42 {N_("Help"), OP_HELP},
46 address_t *mutt_lookup_alias (const char *s)
50 for (; t; t = t->next)
51 if (!m_strcasecmp(s, t->name))
53 return (NULL); /* no such alias */
56 static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn)
58 address_t *head = NULL, *last = NULL, *t, *w;
64 if (!a->group && !a->personal && a->mailbox
65 && strchr (a->mailbox, '@') == NULL) {
66 t = mutt_lookup_alias (a->mailbox);
70 for (u = *expn; u; u = u->next) {
71 if (m_strcmp(a->mailbox, u->data) == 0) { /* alias already found */
72 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
80 u->data = m_strdup(a->mailbox);
83 w = address_list_dup (t);
84 w = mutt_expand_aliases_r (w, expn);
89 while (last && last->next)
99 struct passwd *pw = getpwnam (a->mailbox);
102 char namebuf[STRING];
104 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
105 m_strreplace (&a->personal, namebuf);
120 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
121 /* now qualify all local addresses */
122 rfc822_qualify (head, fqdn);
128 address_t *mutt_expand_aliases (address_t * a)
131 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
133 t = mutt_expand_aliases_r (a, &expn);
134 mutt_free_list (&expn);
135 return (mutt_remove_duplicates (t));
138 void mutt_expand_aliases_env (ENVELOPE * env)
140 env->from = mutt_expand_aliases (env->from);
141 env->to = mutt_expand_aliases (env->to);
142 env->cc = mutt_expand_aliases (env->cc);
143 env->bcc = mutt_expand_aliases (env->bcc);
144 env->reply_to = mutt_expand_aliases (env->reply_to);
145 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
150 * if someone has an address like
151 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
152 * and the user creates an alias for this, Mutt could wind up executing
153 * the backtics because it writes aliases like
154 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
155 * To avoid this problem, use a backslash (\) to quote any backtics. We also
156 * need to quote backslashes as well, since you could defeat the above by
158 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
159 * since that would get aliased as
160 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
161 * which still gets evaluated because the double backslash is not a quote.
163 * Additionally, we need to quote ' and " characters - otherwise, mutt will
164 * interpret them on the wrong parsing step.
166 * $ wants to be quoted since it may indicate the start of an environment
170 static void write_safe_address (FILE * fp, char *s)
173 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
180 address_t *mutt_get_address (ENVELOPE * env, const char **pfxp)
183 const char *pfx = NULL;
185 if (mutt_addr_is_user (env->from)) {
186 if (env->to && !mutt_is_mail_list (env->to)) {
195 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
210 void mutt_create_alias (ENVELOPE * cur, address_t * iadr)
213 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
215 char fixed[LONG_STRING];
217 address_t *adr = NULL;
220 adr = mutt_get_address (cur, NULL);
226 if (adr && adr->mailbox) {
227 m_strcpy(buf, sizeof(buf), adr->mailbox);
228 if ((pc = strchr (buf, '@')))
234 /* Don't suggest a bad alias name in the event of a strange local part. */
235 mutt_check_alias_name (buf, buf);
238 /* add a new alias */
239 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
242 /* check to see if the user already has an alias defined */
243 if (mutt_lookup_alias (buf)) {
244 mutt_error _("You already have an alias defined with that name!");
249 if (mutt_check_alias_name (buf, fixed)) {
251 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
253 m_strcpy(buf, sizeof(buf), fixed);
260 new = p_new(ALIAS, 1);
262 new->name = m_strdup(buf);
264 mutt_addrlist_to_local (adr);
267 m_strcpy(buf, sizeof(buf), adr->mailbox);
271 mutt_addrlist_to_idna (adr, NULL);
274 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
275 mutt_free_alias (&new);
279 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
281 if (mutt_addrlist_to_idna (new->addr, &err)) {
282 mutt_error (_("Error: '%s' is a bad IDN."), err);
287 while (new->addr == NULL);
289 if (adr && adr->personal && !mutt_is_mail_list(adr))
290 m_strcpy(buf, sizeof(buf), adr->personal);
294 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
295 mutt_free_alias (&new);
298 new->addr->personal = m_strdup(buf);
301 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
302 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
303 if (mutt_yesorno (prompt, M_YES) != M_YES) {
304 mutt_free_alias (&new);
316 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
317 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
319 mutt_expand_path (buf, sizeof (buf));
320 if ((rc = safe_fopen (buf, "a"))) {
321 if (mutt_check_alias_name (new->name, NULL))
322 mutt_quote_filename(buf, sizeof(buf), new->name);
324 m_strcpy(buf, sizeof(buf), new->name);
325 fprintf (rc, "alias %s ", buf);
327 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
328 write_safe_address (rc, buf);
331 mutt_message _("Alias added.");
338 * Sanity-check an alias name: Only characters which are non-special to both
339 * the RFC 822 and the mutt configuration parser are permitted.
342 static int check_alias_name_char (char c)
344 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
345 isalnum ((unsigned char) c));
348 int mutt_check_alias_name (const char *s, char *d)
353 if (!check_alias_name_char (*s)) {
370 * This routine looks to see if the user has an alias defined for the given
373 address_t *alias_reverse_lookup (address_t * a)
378 if (!a || !a->mailbox)
381 for (; t; t = t->next) {
382 /* cycle through all addresses if this is a group alias */
383 for (ap = t->addr; ap; ap = ap->next) {
384 if (!ap->group && ap->mailbox &&
385 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
392 /* alias_complete() -- alias completion routine
394 * given a partial alias, this routine attempts to fill in the alias
395 * from the alias list as much as possible. if given empty search string
396 * or found nothing, present all aliases
398 int mutt_alias_complete (char *s, size_t buflen)
401 ALIAS *a_list = NULL, *a_cur = NULL;
402 char bestname[HUGE_STRING];
405 #define min(a,b) ((a<b)?a:b)
407 if (s[0] != 0) { /* avoid empty string as strstr argument */
408 p_clear(bestname, sizeof(bestname));
411 if (a->name && strstr (a->name, s) == a->name) {
412 if (!bestname[0]) /* init */
413 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
416 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
423 if (bestname[0] != 0) {
424 if (m_strcmp(bestname, s) != 0) {
425 /* we are adding something to the completion */
426 m_strcpy(s, m_strlen(bestname) + 1, bestname);
430 /* build alias list and show it */
434 if (a->name && (strstr (a->name, s) == a->name)) {
435 if (!a_list) /* init */
436 a_cur = a_list = p_new(ALIAS, 1);
438 a_cur->next = p_new(ALIAS, 1);
441 memcpy (a_cur, a, sizeof (ALIAS));
450 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
451 if (bestname[0] != 0)
452 m_strcpy(s, buflen, bestname);
454 /* free the alias list */
457 a_list = a_list->next;
461 /* remove any aliases marked for deletion */
463 for (a_cur = Aliases; a_cur;) {
466 a_list->next = a_cur->next;
468 Aliases = a_cur->next;
471 mutt_free_alias (&a_cur);
487 static int string_is_address (const char *str, const char *u, const char *d)
489 char buf[LONG_STRING];
491 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
492 if (ascii_strcasecmp (str, buf) == 0)
498 /* returns TRUE if the given address belongs to the user. */
499 int mutt_addr_is_user (address_t * addr)
501 /* NULL address is assumed to be the user. */
503 debug_print(5, ("yes, NULL address\n"));
506 if (!addr->mailbox) {
507 debug_print(5, ("no, no mailbox\n"));
511 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
512 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
515 if (string_is_address (addr->mailbox, Username, Hostname)) {
516 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
519 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
520 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
523 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
524 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
528 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
529 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
533 if (rx_list_match (Alternates, addr->mailbox)) {
534 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
535 if (rx_list_match (UnAlternates, addr->mailbox))
536 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
541 debug_print(5, ("no, all failed.\n"));
545 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
546 const format_t *src, const char *fmt,
547 const char *ifstring __attribute__ ((unused)),
548 const char *elsestring __attribute__ ((unused)),
549 unsigned long data, format_flag flags __attribute__ ((unused)))
551 char tmp[SHORT_STRING], adr[SHORT_STRING];
552 ALIAS *alias = (ALIAS *) data;
557 m_strcpy (dest, sizeof(dest), "D");
559 m_strcpy (dest, sizeof(dest), " ");
562 mutt_format_s (dest, destlen, fmt, alias->name);
566 rfc822_write_address (adr, sizeof (adr), alias->addr, 1);
567 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
568 snprintf (dest, destlen, tmp, adr);
571 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
572 snprintf (dest, destlen, tmp, alias->num + 1);
575 dest[0] = alias->tagged ? '*' : ' ';
583 static void alias_entry (char *s, size_t slen, MUTTMENU * m, int num)
585 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *) alias_format_str,
586 (unsigned long) ((ALIAS **) m->data)[num],
587 M_FORMAT_ARROWCURSOR);
590 static int alias_tag (MUTTMENU * menu, int n, int m)
592 ALIAS *cur = ((ALIAS **) menu->data)[n];
593 int ot = cur->tagged;
595 cur->tagged = (m >= 0 ? m : !cur->tagged);
597 return cur->tagged - ot;
600 static int alias_SortAlias (const void *a, const void *b)
602 ALIAS *pa = *(ALIAS **) a;
603 ALIAS *pb = *(ALIAS **) b;
604 int r = m_strcasecmp(pa->name, pb->name);
609 static int alias_SortAddress (const void *a, const void *b)
611 address_t *pa = (*(ALIAS **) a)->addr;
612 address_t *pb = (*(ALIAS **) b)->addr;
621 else if (pa->personal) {
623 r = m_strcasecmp(pa->personal, pb->personal);
627 else if (pb->personal)
630 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
634 void mutt_alias_menu (char *buf, size_t buflen, ALIAS * aliases)
638 ALIAS **AliasTable = NULL;
642 char helpstr[SHORT_STRING];
647 mutt_error _("You have no aliases!");
652 /* tell whoever called me to redraw the screen when I return */
653 set_option (OPTNEEDREDRAW);
655 menu = mutt_new_menu ();
656 menu->make_entry = alias_entry;
657 menu->tag = alias_tag;
658 menu->menu = MENU_ALIAS;
659 menu->title = _("Aliases");
661 mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp);
667 /* count the number of aliases */
668 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
669 aliasp->self->del = 0;
670 aliasp->self->tagged = 0;
674 p_realloc(&AliasTable, menu->max);
675 menu->data = AliasTable;
677 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
678 AliasTable[i] = aliasp->self;
682 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
683 qsort (AliasTable, i, sizeof (ALIAS *),
684 (SortAlias & SORT_MASK) ==
685 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
688 for (i = 0; i < menu->max; i++)
689 AliasTable[i]->num = i;
693 menu->redraw |= REDRAW_FULL;
694 aliases = aliases->next;
698 switch ((op = mutt_menuLoop (menu))) {
701 if (menu->tagprefix) {
702 for (i = 0; i < menu->max; i++)
703 if (AliasTable[i]->tagged)
704 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
705 menu->redraw |= REDRAW_INDEX;
708 AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0;
709 menu->redraw |= REDRAW_CURRENT;
710 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
712 menu->redraw |= REDRAW_INDEX;
716 case OP_GENERIC_SELECT_ENTRY:
724 for (i = 0; i < menu->max; i++) {
725 if (AliasTable[i]->tagged) {
726 mutt_addrlist_to_local (AliasTable[i]->addr);
727 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
733 mutt_addrlist_to_local (AliasTable[t]->addr);
734 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
737 mutt_menuDestroy (&menu);
738 p_delete(&AliasTable);