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);
254 const char *err = 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);
270 } while (!new->addr);
272 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
273 m_strcpy(buf, sizeof(buf), adr->personal);
278 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
279 alias_list_wipe(&new);
282 new->addr->personal = m_strdup(buf);
285 rfc822_write_address(buf, sizeof(buf), new->addr, 1);
286 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
287 if (mutt_yesorno(prompt, M_YES) != M_YES) {
288 alias_list_wipe(&new);
292 alias_list_push(&Aliases, new);
294 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
295 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
299 mutt_expand_path(buf, sizeof (buf));
300 rc = safe_fopen (buf, "a");
303 if (alias_sanitize(new->name, NULL)) {
304 mutt_quote_filename(buf, sizeof(buf), new->name);
305 fprintf(rc, "alias %s ", buf);
307 fprintf(rc, "alias %s ", new->name);
311 rfc822_write_address(buf, sizeof(buf), new->addr, 0);
312 write_safe_address(rc, buf);
314 mutt_message _("Alias added.");
320 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
322 address_t *pop, *head = NULL;
323 address_t **last = &head;
325 while ((pop = address_list_pop(&a))) {
326 if (!pop->group && !pop->personal
327 && pop->mailbox && !strchr(pop->mailbox, '@'))
329 const address_t *t = alias_lookup(pop->mailbox);
334 for (u = *expn; u; u = u->next) {
335 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
336 address_list_wipe(&pop);
341 /* save the fact we saw it */
342 u = string_item_new();
343 u->data = m_strdup(pop->mailbox);
346 address_list_wipe(&pop);
349 last = address_list_last(last);
350 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
353 struct passwd *pw = getpwnam(pop->mailbox);
356 char namebuf[STRING];
357 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
358 m_strreplace(&pop->personal, namebuf);
363 last = address_list_append(last, pop);
366 if (option(OPTUSEDOMAIN)) {
367 /* now qualify all local addresses */
368 const char *fqdn = mutt_fqdn(1);
370 rfc822_qualify(head, fqdn);
376 address_t *mutt_expand_aliases(address_t *a)
379 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
381 t = mutt_expand_aliases_r(a, &expn);
382 string_list_wipe(&expn);
383 return mutt_remove_duplicates(t);
386 void mutt_expand_aliases_env(ENVELOPE *env)
388 env->from = mutt_expand_aliases(env->from);
389 env->to = mutt_expand_aliases(env->to);
390 env->cc = mutt_expand_aliases(env->cc);
391 env->bcc = mutt_expand_aliases(env->bcc);
392 env->reply_to = mutt_expand_aliases(env->reply_to);
393 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
396 /************* READ MARK *********************/
398 /* alias_complete() -- alias completion routine
400 * given a partial alias, this routine attempts to fill in the alias
401 * from the alias list as much as possible. if given empty search string
402 * or found nothing, present all aliases
404 int mutt_alias_complete (char *s, size_t buflen)
406 alias_t *a = Aliases;
407 alias_t *a_list = NULL, *a_cur = NULL;
408 char bestname[HUGE_STRING];
411 #define min(a,b) ((a<b)?a:b)
413 if (s[0] != 0) { /* avoid empty string as strstr argument */
414 p_clear(bestname, countof(bestname));
417 if (a->name && strstr (a->name, s) == a->name) {
418 if (!bestname[0]) /* init */
419 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
422 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
429 if (bestname[0] != 0) {
430 if (m_strcmp(bestname, s) != 0) {
431 /* we are adding something to the completion */
432 m_strcpy(s, m_strlen(bestname) + 1, bestname);
436 /* build alias list and show it */
440 if (a->name && (strstr (a->name, s) == a->name)) {
441 if (!a_list) /* init */
442 a_cur = a_list = alias_new();
444 a_cur->next = alias_new();
456 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
457 if (bestname[0] != 0)
458 m_strcpy(s, buflen, bestname);
460 /* free the alias list */
463 a_list = a_list->next;
467 /* remove any aliases marked for deletion */
469 for (a_cur = Aliases; a_cur;) {
472 a_list->next = a_cur->next;
474 Aliases = a_cur->next;
477 alias_list_wipe(&a_cur);
493 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
494 const format_t *src, const char *fmt,
495 const char *ifstring __attribute__ ((unused)),
496 const char *elsestring __attribute__ ((unused)),
497 unsigned long data, format_flag flags __attribute__ ((unused)))
499 char tmp[SHORT_STRING], adr[SHORT_STRING];
500 alias_t *alias = (alias_t *) data;
504 m_strcpy(dest, destlen, alias->del ? "D" : " ");
507 mutt_format_s(dest, destlen, fmt, alias->name);
511 rfc822_write_address(adr, sizeof(adr), alias->addr, 1);
512 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
513 snprintf(dest, destlen, tmp, adr);
516 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
517 snprintf(dest, destlen, tmp, alias->num + 1);
520 m_strcpy(dest, destlen, alias->tagged ? "*" : " ");
527 static void alias_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
529 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *)alias_format_str,
530 (unsigned long)((alias_t **)m->data)[num],
531 M_FORMAT_ARROWCURSOR);
534 static int alias_tag (MUTTMENU * menu, int n, int m)
536 alias_t *cur = ((alias_t **) menu->data)[n];
537 int ot = cur->tagged;
539 cur->tagged = (m >= 0 ? m : !cur->tagged);
541 return cur->tagged - ot;
544 static int alias_SortAlias (const void *a, const void *b)
546 alias_t *pa = *(alias_t **) a;
547 alias_t *pb = *(alias_t **) b;
548 int r = m_strcasecmp(pa->name, pb->name);
553 static int alias_SortAddress (const void *a, const void *b)
555 address_t *pa = (*(alias_t **) a)->addr;
556 address_t *pb = (*(alias_t **) b)->addr;
565 else if (pa->personal) {
567 r = m_strcasecmp(pa->personal, pb->personal);
571 else if (pb->personal)
574 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
578 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
582 alias_t **AliasTable = NULL;
586 char helpstr[SHORT_STRING];
591 mutt_error _("You have no aliases!");
596 /* tell whoever called me to redraw the screen when I return */
597 set_option (OPTNEEDREDRAW);
599 menu = mutt_new_menu ();
600 menu->make_entry = alias_entry;
601 menu->tag = alias_tag;
602 menu->menu = MENU_ALIAS;
603 menu->title = _("Aliases");
604 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
605 MENU_ALIAS, AliasHelp);
611 /* count the number of aliases */
612 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
618 p_realloc(&AliasTable, menu->max);
619 menu->data = AliasTable;
621 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
622 AliasTable[i] = aliasp;
626 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
627 qsort (AliasTable, i, sizeof (alias_t *),
628 (SortAlias & SORT_MASK) ==
629 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
632 for (i = 0; i < menu->max; i++)
633 AliasTable[i]->num = i;
637 menu->redraw |= REDRAW_FULL;
638 aliases = aliases->next;
642 switch ((op = mutt_menuLoop (menu))) {
645 if (menu->tagprefix) {
646 for (i = 0; i < menu->max; i++)
647 if (AliasTable[i]->tagged)
648 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
649 menu->redraw |= REDRAW_INDEX;
652 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
653 menu->redraw |= REDRAW_CURRENT;
654 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
656 menu->redraw |= REDRAW_INDEX;
660 case OP_GENERIC_SELECT_ENTRY:
668 for (i = 0; i < menu->max; i++) {
669 if (AliasTable[i]->tagged) {
670 mutt_addrlist_to_local (AliasTable[i]->addr);
671 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
677 mutt_addrlist_to_local (AliasTable[t]->addr);
678 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
681 mutt_menuDestroy (&menu);
682 p_delete(&AliasTable);