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 const address_t *alias_lookup(const alias_t *list, const char *s)
50 if (!m_strcasecmp(s, list->name))
58 static int string_is_address(const char *str, const char *u, const char *d)
60 char buf[LONG_STRING];
61 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
62 return !ascii_strcasecmp(str, buf);
65 /* returns TRUE if the given address belongs to the user. */
66 int mutt_addr_is_user(address_t *addr)
68 /* NULL address is assumed to be the user. */
75 if (!ascii_strcasecmp(addr->mailbox, Username)
76 || string_is_address(addr->mailbox, Username, Hostname)
77 || string_is_address(addr->mailbox, Username, mutt_fqdn(0))
78 || string_is_address(addr->mailbox, Username, mutt_fqdn(1))
79 || (From && !ascii_strcasecmp(From->mailbox, addr->mailbox)))
84 return rx_list_match(Alternates, addr->mailbox)
85 && !rx_list_match(UnAlternates, addr->mailbox);
88 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
90 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
92 if (mutt_addr_is_user(env->from)) {
93 if (env->to && !mutt_is_mail_list(env->to)) {
94 RETURN("To", env->to);
96 RETURN("Cc", env->cc);
99 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
100 RETURN("Reply-To", env->reply_to);
102 RETURN("From", env->from);
109 /* Only characters which are non-special to both the RFC 822 and the mutt
110 configuration parser are permitted. */
111 static int alias_sanitize(const char *s, char *d)
116 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
133 * if someone has an address like
134 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
135 * and the user creates an alias for this, Mutt could wind up executing
136 * the backtics because it writes aliases like
137 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
138 * To avoid this problem, use a backslash (\) to quote any backtics. We also
139 * need to quote backslashes as well, since you could defeat the above by
141 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
142 * since that would get aliased as
143 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
144 * which still gets evaluated because the double backslash is not a quote.
146 * Additionally, we need to quote ' and " characters - otherwise, mutt will
147 * interpret them on the wrong parsing step.
149 * $ wants to be quoted since it may indicate the start of an environment
152 static void write_safe_address(FILE *fp, const char *s)
155 if (strchr("\\`'\"$", *s)) {
163 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
165 char buf[LONG_STRING], prompt[SHORT_STRING];
166 address_t *adr = iadr;
171 adr = mutt_get_address(cur, NULL);
174 if (adr && adr->mailbox) {
175 const char *p = m_strchrnul(adr->mailbox, '@');
176 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
181 /* Don't suggest a bad alias name in the event of a strange local part. */
182 alias_sanitize(buf, buf);
184 /* add a new alias */
185 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
188 /* check to see if the user already has an alias defined */
189 if (alias_lookup(Aliases, buf)) {
190 mutt_error _("You already have an alias defined with that name!");
194 alias_sanitize(buf, buf);
196 new->name = m_strdup(buf);
198 mutt_addrlist_to_local(adr);
200 m_strcpy(buf, sizeof(buf), adr->mailbox);
205 mutt_addrlist_to_idna(adr, NULL);
208 const char *err = NULL;
210 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
211 alias_list_wipe(&new);
215 new->addr = rfc822_parse_adrlist(new->addr, buf);
219 if (mutt_addrlist_to_idna(new->addr, &err)) {
220 mutt_error(_("Error: '%s' is a bad IDN."), err);
224 } while (!new->addr);
226 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
227 m_strcpy(buf, sizeof(buf), adr->personal);
232 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
233 alias_list_wipe(&new);
236 new->addr->personal = m_strdup(buf);
239 rfc822_write_address(buf, sizeof(buf), new->addr, 1);
240 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
241 if (mutt_yesorno(prompt, M_YES) != M_YES) {
242 alias_list_wipe(&new);
246 alias_list_push(&Aliases, new);
248 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
249 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
253 mutt_expand_path(buf, sizeof (buf));
254 rc = safe_fopen (buf, "a");
257 if (alias_sanitize(new->name, NULL)) {
258 mutt_quote_filename(buf, sizeof(buf), new->name);
259 fprintf(rc, "alias %s ", buf);
261 fprintf(rc, "alias %s ", new->name);
265 rfc822_write_address(buf, sizeof(buf), new->addr, 0);
266 write_safe_address(rc, buf);
268 mutt_message _("Alias added.");
274 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
276 address_t *pop, *head = NULL;
277 address_t **last = &head;
279 while ((pop = address_list_pop(&a))) {
280 if (!pop->group && !pop->personal
281 && pop->mailbox && !strchr(pop->mailbox, '@'))
283 const address_t *t = alias_lookup(Aliases, pop->mailbox);
288 for (u = *expn; u; u = u->next) {
289 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
290 address_list_wipe(&pop);
295 /* save the fact we saw it */
296 u = string_item_new();
297 u->data = m_strdup(pop->mailbox);
300 address_list_wipe(&pop);
303 last = address_list_last(last);
304 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
307 struct passwd *pw = getpwnam(pop->mailbox);
310 char namebuf[STRING];
311 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
312 m_strreplace(&pop->personal, namebuf);
317 last = address_list_append(last, pop);
320 if (option(OPTUSEDOMAIN)) {
321 /* now qualify all local addresses */
322 const char *fqdn = mutt_fqdn(1);
324 rfc822_qualify(head, fqdn);
330 address_t *mutt_expand_aliases(address_t *a)
333 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
335 t = mutt_expand_aliases_r(a, &expn);
336 string_list_wipe(&expn);
337 return mutt_remove_duplicates(t);
340 void mutt_expand_aliases_env(ENVELOPE *env)
342 env->from = mutt_expand_aliases(env->from);
343 env->to = mutt_expand_aliases(env->to);
344 env->cc = mutt_expand_aliases(env->cc);
345 env->bcc = mutt_expand_aliases(env->bcc);
346 env->reply_to = mutt_expand_aliases(env->reply_to);
347 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
350 /************* READ MARK *********************/
354 * This routine looks to see if the user has an alias defined for the given
357 address_t *alias_reverse_lookup (address_t * a)
359 alias_t *t = Aliases;
362 if (!a || !a->mailbox)
365 for (; t; t = t->next) {
366 /* cycle through all addresses if this is a group alias */
367 for (ap = t->addr; ap; ap = ap->next) {
368 if (!ap->group && ap->mailbox &&
369 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
376 /* alias_complete() -- alias completion routine
378 * given a partial alias, this routine attempts to fill in the alias
379 * from the alias list as much as possible. if given empty search string
380 * or found nothing, present all aliases
382 int mutt_alias_complete (char *s, size_t buflen)
384 alias_t *a = Aliases;
385 alias_t *a_list = NULL, *a_cur = NULL;
386 char bestname[HUGE_STRING];
389 #define min(a,b) ((a<b)?a:b)
391 if (s[0] != 0) { /* avoid empty string as strstr argument */
392 p_clear(bestname, countof(bestname));
395 if (a->name && strstr (a->name, s) == a->name) {
396 if (!bestname[0]) /* init */
397 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
400 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
407 if (bestname[0] != 0) {
408 if (m_strcmp(bestname, s) != 0) {
409 /* we are adding something to the completion */
410 m_strcpy(s, m_strlen(bestname) + 1, bestname);
414 /* build alias list and show it */
418 if (a->name && (strstr (a->name, s) == a->name)) {
419 if (!a_list) /* init */
420 a_cur = a_list = alias_new();
422 a_cur->next = alias_new();
434 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
435 if (bestname[0] != 0)
436 m_strcpy(s, buflen, bestname);
438 /* free the alias list */
441 a_list = a_list->next;
445 /* remove any aliases marked for deletion */
447 for (a_cur = Aliases; a_cur;) {
450 a_list->next = a_cur->next;
452 Aliases = a_cur->next;
455 alias_list_wipe(&a_cur);
471 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
472 const format_t *src, const char *fmt,
473 const char *ifstring __attribute__ ((unused)),
474 const char *elsestring __attribute__ ((unused)),
475 unsigned long data, format_flag flags __attribute__ ((unused)))
477 char tmp[SHORT_STRING], adr[SHORT_STRING];
478 alias_t *alias = (alias_t *) data;
482 m_strcpy(dest, destlen, alias->del ? "D" : " ");
485 mutt_format_s(dest, destlen, fmt, alias->name);
489 rfc822_write_address(adr, sizeof(adr), alias->addr, 1);
490 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
491 snprintf(dest, destlen, tmp, adr);
494 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
495 snprintf(dest, destlen, tmp, alias->num + 1);
498 m_strcpy(dest, destlen, alias->tagged ? "*" : " ");
505 static void alias_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
507 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *)alias_format_str,
508 (unsigned long)((alias_t **)m->data)[num],
509 M_FORMAT_ARROWCURSOR);
512 static int alias_tag (MUTTMENU * menu, int n, int m)
514 alias_t *cur = ((alias_t **) menu->data)[n];
515 int ot = cur->tagged;
517 cur->tagged = (m >= 0 ? m : !cur->tagged);
519 return cur->tagged - ot;
522 static int alias_SortAlias (const void *a, const void *b)
524 alias_t *pa = *(alias_t **) a;
525 alias_t *pb = *(alias_t **) b;
526 int r = m_strcasecmp(pa->name, pb->name);
531 static int alias_SortAddress (const void *a, const void *b)
533 address_t *pa = (*(alias_t **) a)->addr;
534 address_t *pb = (*(alias_t **) b)->addr;
543 else if (pa->personal) {
545 r = m_strcasecmp(pa->personal, pb->personal);
549 else if (pb->personal)
552 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
556 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
560 alias_t **AliasTable = NULL;
564 char helpstr[SHORT_STRING];
569 mutt_error _("You have no aliases!");
574 /* tell whoever called me to redraw the screen when I return */
575 set_option (OPTNEEDREDRAW);
577 menu = mutt_new_menu ();
578 menu->make_entry = alias_entry;
579 menu->tag = alias_tag;
580 menu->menu = MENU_ALIAS;
581 menu->title = _("Aliases");
582 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
583 MENU_ALIAS, AliasHelp);
589 /* count the number of aliases */
590 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
596 p_realloc(&AliasTable, menu->max);
597 menu->data = AliasTable;
599 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
600 AliasTable[i] = aliasp;
604 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
605 qsort (AliasTable, i, sizeof (alias_t *),
606 (SortAlias & SORT_MASK) ==
607 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
610 for (i = 0; i < menu->max; i++)
611 AliasTable[i]->num = i;
615 menu->redraw |= REDRAW_FULL;
616 aliases = aliases->next;
620 switch ((op = mutt_menuLoop (menu))) {
623 if (menu->tagprefix) {
624 for (i = 0; i < menu->max; i++)
625 if (AliasTable[i]->tagged)
626 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
627 menu->redraw |= REDRAW_INDEX;
630 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
631 menu->redraw |= REDRAW_CURRENT;
632 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
634 menu->redraw |= REDRAW_INDEX;
638 case OP_GENERIC_SELECT_ENTRY:
646 for (i = 0; i < menu->max; i++) {
647 if (AliasTable[i]->tagged) {
648 mutt_addrlist_to_local (AliasTable[i]->addr);
649 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
655 mutt_addrlist_to_local (AliasTable[t]->addr);
656 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
659 mutt_menuDestroy (&menu);
660 p_delete(&AliasTable);