2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
20 * Copyright notice from original mutt:
21 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
23 * This file is part of mutt-ng, see http://www.muttng.org/.
24 * It's licensed under the GNU General Public License,
25 * please see the file GPL in the top level source directory.
35 #include <lib-lib/mem.h>
36 #include <lib-lib/ascii.h>
37 #include <lib-lib/str.h>
38 #include <lib-lib/file.h>
39 #include <lib-lib/macros.h>
40 #include <lib-lib/mapping.h>
41 #include <lib-lib/rx.h>
43 #include <lib-sys/unix.h>
45 #include <lib-ui/curses.h>
46 #include <lib-ui/enter.h>
47 #include <lib-ui/menu.h>
50 #include "mutt_idna.h"
58 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
60 static struct mapping_t AliasHelp[] = {
61 {N_("Exit"), OP_EXIT},
62 {N_("Del"), OP_DELETE},
63 {N_("Undel"), OP_UNDELETE},
64 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
65 {N_("Help"), OP_HELP},
69 const address_t *alias_lookup(const char *s)
73 for (list = Aliases; list; list = list->next) {
74 if (!m_strcasecmp(s, list->name))
81 /* This routine looks to see if the user has an alias defined for the given
83 const address_t *alias_reverse_lookup(const address_t *a)
87 if (!a || !a->mailbox)
90 for (list = Aliases; list; list = list->next) {
93 /* cycle through all addresses if this is a group alias */
94 for (ap = list->addr; ap; ap = ap->next) {
95 if (!ap->group && ap->mailbox
96 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
104 static int string_is_address(const char *str, const char *u, const char *d)
106 char buf[LONG_STRING];
107 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
108 return !ascii_strcasecmp(str, buf);
111 /* returns TRUE if the given address belongs to the user. */
112 int mutt_addr_is_user(address_t *addr)
114 /* NULL address is assumed to be the user. */
121 if (!ascii_strcasecmp(addr->mailbox, Username)
122 || string_is_address(addr->mailbox, Username, Hostname)
123 || string_is_address(addr->mailbox, Username, mutt_fqdn(0))
124 || string_is_address(addr->mailbox, Username, mutt_fqdn(1))
125 || (From && !ascii_strcasecmp(From->mailbox, addr->mailbox)))
130 return rx_list_match(Alternates, addr->mailbox)
131 && !rx_list_match(UnAlternates, addr->mailbox);
134 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
136 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
138 if (mutt_addr_is_user(env->from)) {
139 if (env->to && !mutt_is_mail_list(env->to)) {
140 RETURN("To", env->to);
142 RETURN("Cc", env->cc);
145 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
146 RETURN("Reply-To", env->reply_to);
148 RETURN("From", env->from);
155 /* Only characters which are non-special to both the RFC 822 and the mutt
156 configuration parser are permitted. */
157 static int alias_sanitize(const char *s, char *d)
162 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
179 * if someone has an address like
180 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
181 * and the user creates an alias for this, Mutt could wind up executing
182 * the backtics because it writes aliases like
183 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
184 * To avoid this problem, use a backslash (\) to quote any backtics. We also
185 * need to quote backslashes as well, since you could defeat the above by
187 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
188 * since that would get aliased as
189 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
190 * which still gets evaluated because the double backslash is not a quote.
192 * Additionally, we need to quote ' and " characters - otherwise, mutt will
193 * interpret them on the wrong parsing step.
195 * $ wants to be quoted since it may indicate the start of an environment
198 static void write_safe_address(FILE *fp, const char *s)
201 if (strchr("\\`'\"$", *s)) {
209 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
211 char buf[LONG_STRING], prompt[SHORT_STRING];
212 address_t *adr = iadr;
217 adr = mutt_get_address(cur, NULL);
220 if (adr && adr->mailbox) {
221 const char *p = m_strchrnul(adr->mailbox, '@');
222 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
227 /* Don't suggest a bad alias name in the event of a strange local part. */
228 alias_sanitize(buf, buf);
230 /* add a new alias */
231 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
234 /* check to see if the user already has an alias defined */
235 if (alias_lookup(buf)) {
236 mutt_error _("You already have an alias defined with that name!");
240 alias_sanitize(buf, buf);
242 new->name = m_strdup(buf);
244 mutt_addrlist_to_local(adr);
246 m_strcpy(buf, sizeof(buf), adr->mailbox);
251 mutt_addrlist_to_idna(adr, NULL);
256 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
257 alias_list_wipe(&new);
261 new->addr = rfc822_parse_adrlist(new->addr, buf);
265 if (mutt_addrlist_to_idna(new->addr, &err)) {
266 mutt_error(_("Error: '%s' is a bad IDN."), err);
271 } while (!new->addr);
273 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
274 m_strcpy(buf, sizeof(buf), adr->personal);
279 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
280 alias_list_wipe(&new);
283 new->addr->personal = m_strdup(buf);
286 rfc822_write_address(buf, sizeof(buf), new->addr, 1);
287 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
288 if (mutt_yesorno(prompt, M_YES) != M_YES) {
289 alias_list_wipe(&new);
293 alias_list_push(&Aliases, new);
295 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
296 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
300 mutt_expand_path(buf, sizeof (buf));
301 rc = safe_fopen (buf, "a");
304 if (alias_sanitize(new->name, NULL)) {
305 mutt_quote_filename(buf, sizeof(buf), new->name);
306 fprintf(rc, "alias %s ", buf);
308 fprintf(rc, "alias %s ", new->name);
312 rfc822_write_address(buf, sizeof(buf), new->addr, 0);
313 write_safe_address(rc, buf);
315 mutt_message _("Alias added.");
321 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
323 address_t *pop, *head = NULL;
324 address_t **last = &head;
326 while ((pop = address_list_pop(&a))) {
327 if (!pop->group && !pop->personal
328 && pop->mailbox && !strchr(pop->mailbox, '@'))
330 const address_t *t = alias_lookup(pop->mailbox);
335 for (u = *expn; u; u = u->next) {
336 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
337 address_list_wipe(&pop);
342 /* save the fact we saw it */
343 u = string_item_new();
344 u->data = m_strdup(pop->mailbox);
347 address_list_wipe(&pop);
350 last = address_list_last(last);
351 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
354 struct passwd *pw = getpwnam(pop->mailbox);
357 char namebuf[STRING];
358 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
359 m_strreplace(&pop->personal, namebuf);
364 last = address_list_append(last, pop);
367 if (option(OPTUSEDOMAIN)) {
368 /* now qualify all local addresses */
369 const char *fqdn = mutt_fqdn(1);
371 rfc822_qualify(head, fqdn);
377 address_t *mutt_expand_aliases(address_t *a)
380 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
382 t = mutt_expand_aliases_r(a, &expn);
383 string_list_wipe(&expn);
384 return mutt_remove_duplicates(t);
387 void mutt_expand_aliases_env(ENVELOPE *env)
389 env->from = mutt_expand_aliases(env->from);
390 env->to = mutt_expand_aliases(env->to);
391 env->cc = mutt_expand_aliases(env->cc);
392 env->bcc = mutt_expand_aliases(env->bcc);
393 env->reply_to = mutt_expand_aliases(env->reply_to);
394 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
397 /************* READ MARK *********************/
399 /* alias_complete() -- alias completion routine
401 * given a partial alias, this routine attempts to fill in the alias
402 * from the alias list as much as possible. if given empty search string
403 * or found nothing, present all aliases
405 int mutt_alias_complete (char *s, size_t buflen)
407 alias_t *a = Aliases;
408 alias_t *a_list = NULL, *a_cur = NULL;
409 char bestname[HUGE_STRING];
412 #define min(a,b) ((a<b)?a:b)
414 if (s[0] != 0) { /* avoid empty string as strstr argument */
415 p_clear(bestname, countof(bestname));
418 if (a->name && strstr (a->name, s) == a->name) {
419 if (!bestname[0]) /* init */
420 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
423 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
430 if (bestname[0] != 0) {
431 if (m_strcmp(bestname, s) != 0) {
432 /* we are adding something to the completion */
433 m_strcpy(s, m_strlen(bestname) + 1, bestname);
437 /* build alias list and show it */
441 if (a->name && (strstr (a->name, s) == a->name)) {
442 if (!a_list) /* init */
443 a_cur = a_list = alias_new();
445 a_cur->next = alias_new();
457 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
458 if (bestname[0] != 0)
459 m_strcpy(s, buflen, bestname);
461 /* free the alias list */
464 a_list = a_list->next;
468 /* remove any aliases marked for deletion */
470 for (a_cur = Aliases; a_cur;) {
473 a_list->next = a_cur->next;
475 Aliases = a_cur->next;
478 alias_list_wipe(&a_cur);
494 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
495 const format_t *src, const char *fmt,
496 const char *ifstring __attribute__ ((unused)),
497 const char *elsestring __attribute__ ((unused)),
498 unsigned long data, format_flag flags __attribute__ ((unused)))
500 char tmp[SHORT_STRING], adr[SHORT_STRING];
501 alias_t *alias = (alias_t *) data;
505 m_strcpy(dest, destlen, alias->del ? "D" : " ");
508 mutt_format_s(dest, destlen, fmt, alias->name);
512 rfc822_write_address(adr, sizeof(adr), alias->addr, 1);
513 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
514 snprintf(dest, destlen, tmp, adr);
517 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
518 snprintf(dest, destlen, tmp, alias->num + 1);
521 m_strcpy(dest, destlen, alias->tagged ? "*" : " ");
528 static void alias_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
530 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *)alias_format_str,
531 (unsigned long)((alias_t **)m->data)[num],
532 M_FORMAT_ARROWCURSOR);
535 static int alias_tag (MUTTMENU * menu, int n, int m)
537 alias_t *cur = ((alias_t **) menu->data)[n];
538 int ot = cur->tagged;
540 cur->tagged = (m >= 0 ? m : !cur->tagged);
542 return cur->tagged - ot;
545 static int alias_SortAlias (const void *a, const void *b)
547 alias_t *pa = *(alias_t **) a;
548 alias_t *pb = *(alias_t **) b;
549 int r = m_strcasecmp(pa->name, pb->name);
554 static int alias_SortAddress (const void *a, const void *b)
556 address_t *pa = (*(alias_t **) a)->addr;
557 address_t *pb = (*(alias_t **) b)->addr;
566 else if (pa->personal) {
568 r = m_strcasecmp(pa->personal, pb->personal);
572 else if (pb->personal)
575 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
579 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
583 alias_t **AliasTable = NULL;
587 char helpstr[SHORT_STRING];
592 mutt_error _("You have no aliases!");
597 /* tell whoever called me to redraw the screen when I return */
598 set_option (OPTNEEDREDRAW);
600 menu = mutt_new_menu ();
601 menu->make_entry = alias_entry;
602 menu->tag = alias_tag;
603 menu->menu = MENU_ALIAS;
604 menu->title = _("Aliases");
605 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
606 MENU_ALIAS, AliasHelp);
612 /* count the number of aliases */
613 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
619 p_realloc(&AliasTable, menu->max);
620 menu->data = AliasTable;
622 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
623 AliasTable[i] = aliasp;
627 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
628 qsort (AliasTable, i, sizeof (alias_t *),
629 (SortAlias & SORT_MASK) ==
630 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
633 for (i = 0; i < menu->max; i++)
634 AliasTable[i]->num = i;
638 menu->redraw |= REDRAW_FULL;
639 aliases = aliases->next;
643 switch ((op = mutt_menuLoop (menu))) {
646 if (menu->tagprefix) {
647 for (i = 0; i < menu->max; i++)
648 if (AliasTable[i]->tagged)
649 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
650 menu->redraw |= REDRAW_INDEX;
653 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
654 menu->redraw |= REDRAW_CURRENT;
655 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
657 menu->redraw |= REDRAW_INDEX;
661 case OP_GENERIC_SELECT_ENTRY:
669 for (i = 0; i < menu->max; i++) {
670 if (AliasTable[i]->tagged) {
671 mutt_addrlist_to_local (AliasTable[i]->addr);
672 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
678 mutt_addrlist_to_local (AliasTable[t]->addr);
679 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
682 mutt_menuDestroy (&menu);
683 p_delete(&AliasTable);