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 void alias_wipe(alias_t *a) {
49 address_delete(&a->addr);
50 alias_delete(&a->next);
53 const address_t *alias_lookup(const alias_t *list, const char *s)
56 if (!m_strcasecmp(s, list->name))
64 static int string_is_address(const char *str, const char *u, const char *d)
66 char buf[LONG_STRING];
67 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
68 return !ascii_strcasecmp(str, buf);
71 /* returns TRUE if the given address belongs to the user. */
72 int mutt_addr_is_user(address_t *addr)
74 /* NULL address is assumed to be the user. */
81 if (!ascii_strcasecmp(addr->mailbox, Username)
82 || string_is_address(addr->mailbox, Username, Hostname)
83 || string_is_address(addr->mailbox, Username, mutt_fqdn(0))
84 || string_is_address(addr->mailbox, Username, mutt_fqdn(1))
85 || (From && !ascii_strcasecmp(From->mailbox, addr->mailbox)))
90 return rx_list_match(Alternates, addr->mailbox)
91 && !rx_list_match(UnAlternates, addr->mailbox);
94 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
96 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
98 if (mutt_addr_is_user(env->from)) {
99 if (env->to && !mutt_is_mail_list(env->to)) {
100 RETURN("To", env->to);
102 RETURN("Cc", env->cc);
105 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
106 RETURN("Reply-To", env->reply_to);
108 RETURN("From", env->from);
115 /* Only characters which are non-special to both the RFC 822 and the mutt
116 configuration parser are permitted. */
117 static int alias_sanitize(const char *s, char *d)
122 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
139 * if someone has an address like
140 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
141 * and the user creates an alias for this, Mutt could wind up executing
142 * the backtics because it writes aliases like
143 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
144 * To avoid this problem, use a backslash (\) to quote any backtics. We also
145 * need to quote backslashes as well, since you could defeat the above by
147 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
148 * since that would get aliased as
149 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
150 * which still gets evaluated because the double backslash is not a quote.
152 * Additionally, we need to quote ' and " characters - otherwise, mutt will
153 * interpret them on the wrong parsing step.
155 * $ wants to be quoted since it may indicate the start of an environment
158 static void write_safe_address(FILE *fp, const char *s)
161 if (strchr("\\`'\"$", *s)) {
169 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
171 char buf[LONG_STRING], prompt[SHORT_STRING];
172 address_t *adr = iadr;
177 adr = mutt_get_address(cur, NULL);
180 if (adr && adr->mailbox) {
181 const char *p = m_strchrnul(adr->mailbox, '@');
182 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
187 /* Don't suggest a bad alias name in the event of a strange local part. */
188 alias_sanitize(buf, buf);
190 /* add a new alias */
191 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
194 /* check to see if the user already has an alias defined */
195 if (alias_lookup(Aliases, buf)) {
196 mutt_error _("You already have an alias defined with that name!");
200 alias_sanitize(buf, buf);
202 new->name = m_strdup(buf);
204 mutt_addrlist_to_local(adr);
206 m_strcpy(buf, sizeof(buf), adr->mailbox);
211 mutt_addrlist_to_idna(adr, NULL);
214 const char *err = NULL;
216 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
221 new->addr = rfc822_parse_adrlist(new->addr, buf);
225 if (mutt_addrlist_to_idna(new->addr, &err)) {
226 mutt_error(_("Error: '%s' is a bad IDN."), err);
230 } while (!new->addr);
232 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
233 m_strcpy(buf, sizeof(buf), adr->personal);
238 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
242 new->addr->personal = m_strdup(buf);
245 rfc822_write_address(buf, sizeof(buf), new->addr, 1);
246 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
247 if (mutt_yesorno(prompt, M_YES) != M_YES) {
252 alias_list_push(&Aliases, new);
254 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
255 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
259 mutt_expand_path(buf, sizeof (buf));
260 rc = safe_fopen (buf, "a");
263 if (alias_sanitize(new->name, NULL)) {
264 mutt_quote_filename(buf, sizeof(buf), new->name);
265 fprintf(rc, "alias %s ", buf);
267 fprintf(rc, "alias %s ", new->name);
271 rfc822_write_address(buf, sizeof(buf), new->addr, 0);
272 write_safe_address(rc, buf);
274 mutt_message _("Alias added.");
280 static address_t *mutt_expand_aliases_r(address_t *a, LIST **expn)
282 address_t *pop, *head = NULL;
283 address_t **last = &head;
285 while ((pop = address_list_pop(&a))) {
286 if (!pop->group && !pop->personal
287 && pop->mailbox && !strchr(pop->mailbox, '@'))
289 const address_t *t = alias_lookup(Aliases, pop->mailbox);
294 for (u = *expn; u; u = u->next) {
295 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
296 address_delete(&pop);
301 /* save the fact we saw it */
303 u->data = m_strdup(pop->mailbox);
306 address_delete(&pop);
309 last = address_list_last(last);
310 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
313 struct passwd *pw = getpwnam(pop->mailbox);
316 char namebuf[STRING];
317 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
318 m_strreplace(&pop->personal, namebuf);
323 last = address_list_append(last, pop);
326 if (option(OPTUSEDOMAIN)) {
327 /* now qualify all local addresses */
328 const char *fqdn = mutt_fqdn(1);
330 rfc822_qualify(head, fqdn);
336 address_t *mutt_expand_aliases(address_t *a)
339 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
341 t = mutt_expand_aliases_r(a, &expn);
342 mutt_free_list(&expn);
343 return mutt_remove_duplicates(t);
346 void mutt_expand_aliases_env(ENVELOPE *env)
348 env->from = mutt_expand_aliases(env->from);
349 env->to = mutt_expand_aliases(env->to);
350 env->cc = mutt_expand_aliases(env->cc);
351 env->bcc = mutt_expand_aliases(env->bcc);
352 env->reply_to = mutt_expand_aliases(env->reply_to);
353 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
356 /************* READ MARK *********************/
360 * This routine looks to see if the user has an alias defined for the given
363 address_t *alias_reverse_lookup (address_t * a)
365 alias_t *t = Aliases;
368 if (!a || !a->mailbox)
371 for (; t; t = t->next) {
372 /* cycle through all addresses if this is a group alias */
373 for (ap = t->addr; ap; ap = ap->next) {
374 if (!ap->group && ap->mailbox &&
375 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
382 /* alias_complete() -- alias completion routine
384 * given a partial alias, this routine attempts to fill in the alias
385 * from the alias list as much as possible. if given empty search string
386 * or found nothing, present all aliases
388 int mutt_alias_complete (char *s, size_t buflen)
390 alias_t *a = Aliases;
391 alias_t *a_list = NULL, *a_cur = NULL;
392 char bestname[HUGE_STRING];
395 #define min(a,b) ((a<b)?a:b)
397 if (s[0] != 0) { /* avoid empty string as strstr argument */
398 p_clear(bestname, countof(bestname));
401 if (a->name && strstr (a->name, s) == a->name) {
402 if (!bestname[0]) /* init */
403 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
406 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
413 if (bestname[0] != 0) {
414 if (m_strcmp(bestname, s) != 0) {
415 /* we are adding something to the completion */
416 m_strcpy(s, m_strlen(bestname) + 1, bestname);
420 /* build alias list and show it */
424 if (a->name && (strstr (a->name, s) == a->name)) {
425 if (!a_list) /* init */
426 a_cur = a_list = alias_new();
428 a_cur->next = alias_new();
440 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
441 if (bestname[0] != 0)
442 m_strcpy(s, buflen, bestname);
444 /* free the alias list */
447 a_list = a_list->next;
451 /* remove any aliases marked for deletion */
453 for (a_cur = Aliases; a_cur;) {
456 a_list->next = a_cur->next;
458 Aliases = a_cur->next;
461 alias_delete(&a_cur);
477 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
478 const format_t *src, const char *fmt,
479 const char *ifstring __attribute__ ((unused)),
480 const char *elsestring __attribute__ ((unused)),
481 unsigned long data, format_flag flags __attribute__ ((unused)))
483 char tmp[SHORT_STRING], adr[SHORT_STRING];
484 alias_t *alias = (alias_t *) data;
488 m_strcpy(dest, destlen, alias->del ? "D" : " ");
491 mutt_format_s(dest, destlen, fmt, alias->name);
495 rfc822_write_address(adr, sizeof(adr), alias->addr, 1);
496 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
497 snprintf(dest, destlen, tmp, adr);
500 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
501 snprintf(dest, destlen, tmp, alias->num + 1);
504 m_strcpy(dest, destlen, alias->tagged ? "*" : " ");
511 static void alias_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
513 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *)alias_format_str,
514 (unsigned long)((alias_t **)m->data)[num],
515 M_FORMAT_ARROWCURSOR);
518 static int alias_tag (MUTTMENU * menu, int n, int m)
520 alias_t *cur = ((alias_t **) menu->data)[n];
521 int ot = cur->tagged;
523 cur->tagged = (m >= 0 ? m : !cur->tagged);
525 return cur->tagged - ot;
528 static int alias_SortAlias (const void *a, const void *b)
530 alias_t *pa = *(alias_t **) a;
531 alias_t *pb = *(alias_t **) b;
532 int r = m_strcasecmp(pa->name, pb->name);
537 static int alias_SortAddress (const void *a, const void *b)
539 address_t *pa = (*(alias_t **) a)->addr;
540 address_t *pb = (*(alias_t **) b)->addr;
549 else if (pa->personal) {
551 r = m_strcasecmp(pa->personal, pb->personal);
555 else if (pb->personal)
558 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
562 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
566 alias_t **AliasTable = NULL;
570 char helpstr[SHORT_STRING];
575 mutt_error _("You have no aliases!");
580 /* tell whoever called me to redraw the screen when I return */
581 set_option (OPTNEEDREDRAW);
583 menu = mutt_new_menu ();
584 menu->make_entry = alias_entry;
585 menu->tag = alias_tag;
586 menu->menu = MENU_ALIAS;
587 menu->title = _("Aliases");
588 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
589 MENU_ALIAS, AliasHelp);
595 /* count the number of aliases */
596 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
602 p_realloc(&AliasTable, menu->max);
603 menu->data = AliasTable;
605 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
606 AliasTable[i] = aliasp;
610 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
611 qsort (AliasTable, i, sizeof (alias_t *),
612 (SortAlias & SORT_MASK) ==
613 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
616 for (i = 0; i < menu->max; i++)
617 AliasTable[i]->num = i;
621 menu->redraw |= REDRAW_FULL;
622 aliases = aliases->next;
626 switch ((op = mutt_menuLoop (menu))) {
629 if (menu->tagprefix) {
630 for (i = 0; i < menu->max; i++)
631 if (AliasTable[i]->tagged)
632 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
633 menu->redraw |= REDRAW_INDEX;
636 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
637 menu->redraw |= REDRAW_CURRENT;
638 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
640 menu->redraw |= REDRAW_INDEX;
644 case OP_GENERIC_SELECT_ENTRY:
652 for (i = 0; i < menu->max; i++) {
653 if (AliasTable[i]->tagged) {
654 mutt_addrlist_to_local (AliasTable[i]->addr);
655 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
661 mutt_addrlist_to_local (AliasTable[t]->addr);
662 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
665 mutt_menuDestroy (&menu);
666 p_delete(&AliasTable);