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/debug.h>
42 #include <lib-lib/rx.h>
44 #include <lib-sys/unix.h>
46 #include <lib-ui/curses.h>
47 #include <lib-ui/enter.h>
48 #include <lib-ui/menu.h>
51 #include "mutt_idna.h"
54 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
56 static struct mapping_t AliasHelp[] = {
57 {N_("Exit"), OP_EXIT},
58 {N_("Del"), OP_DELETE},
59 {N_("Undel"), OP_UNDELETE},
60 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
61 {N_("Help"), OP_HELP},
65 const address_t *alias_lookup(const alias_t *list, const char *s)
68 if (!m_strcasecmp(s, list->name))
76 /* This routine looks to see if the user has an alias defined for the given
78 const address_t *alias_reverse_lookup(const alias_t *list, const address_t *a)
80 if (!a || !a->mailbox)
83 for (; list; list = list->next) {
86 /* cycle through all addresses if this is a group alias */
87 for (ap = list->addr; ap; ap = ap->next) {
88 if (!ap->group && ap->mailbox
89 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
97 static int string_is_address(const char *str, const char *u, const char *d)
99 char buf[LONG_STRING];
100 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
101 return !ascii_strcasecmp(str, buf);
104 /* returns TRUE if the given address belongs to the user. */
105 int mutt_addr_is_user(address_t *addr)
107 /* NULL address is assumed to be the user. */
114 if (!ascii_strcasecmp(addr->mailbox, Username)
115 || string_is_address(addr->mailbox, Username, Hostname)
116 || string_is_address(addr->mailbox, Username, mutt_fqdn(0))
117 || string_is_address(addr->mailbox, Username, mutt_fqdn(1))
118 || (From && !ascii_strcasecmp(From->mailbox, addr->mailbox)))
123 return rx_list_match(Alternates, addr->mailbox)
124 && !rx_list_match(UnAlternates, addr->mailbox);
127 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
129 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
131 if (mutt_addr_is_user(env->from)) {
132 if (env->to && !mutt_is_mail_list(env->to)) {
133 RETURN("To", env->to);
135 RETURN("Cc", env->cc);
138 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
139 RETURN("Reply-To", env->reply_to);
141 RETURN("From", env->from);
148 /* Only characters which are non-special to both the RFC 822 and the mutt
149 configuration parser are permitted. */
150 static int alias_sanitize(const char *s, char *d)
155 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
172 * if someone has an address like
173 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
174 * and the user creates an alias for this, Mutt could wind up executing
175 * the backtics because it writes aliases like
176 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
177 * To avoid this problem, use a backslash (\) to quote any backtics. We also
178 * need to quote backslashes as well, since you could defeat the above by
180 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
181 * since that would get aliased as
182 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
183 * which still gets evaluated because the double backslash is not a quote.
185 * Additionally, we need to quote ' and " characters - otherwise, mutt will
186 * interpret them on the wrong parsing step.
188 * $ wants to be quoted since it may indicate the start of an environment
191 static void write_safe_address(FILE *fp, const char *s)
194 if (strchr("\\`'\"$", *s)) {
202 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
204 char buf[LONG_STRING], prompt[SHORT_STRING];
205 address_t *adr = iadr;
210 adr = mutt_get_address(cur, NULL);
213 if (adr && adr->mailbox) {
214 const char *p = m_strchrnul(adr->mailbox, '@');
215 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
220 /* Don't suggest a bad alias name in the event of a strange local part. */
221 alias_sanitize(buf, buf);
223 /* add a new alias */
224 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
227 /* check to see if the user already has an alias defined */
228 if (alias_lookup(Aliases, buf)) {
229 mutt_error _("You already have an alias defined with that name!");
233 alias_sanitize(buf, buf);
235 new->name = m_strdup(buf);
237 mutt_addrlist_to_local(adr);
239 m_strcpy(buf, sizeof(buf), adr->mailbox);
244 mutt_addrlist_to_idna(adr, NULL);
247 const char *err = NULL;
249 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
250 alias_list_wipe(&new);
254 new->addr = rfc822_parse_adrlist(new->addr, buf);
258 if (mutt_addrlist_to_idna(new->addr, &err)) {
259 mutt_error(_("Error: '%s' is a bad IDN."), err);
263 } while (!new->addr);
265 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
266 m_strcpy(buf, sizeof(buf), adr->personal);
271 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
272 alias_list_wipe(&new);
275 new->addr->personal = m_strdup(buf);
278 rfc822_write_address(buf, sizeof(buf), new->addr, 1);
279 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
280 if (mutt_yesorno(prompt, M_YES) != M_YES) {
281 alias_list_wipe(&new);
285 alias_list_push(&Aliases, new);
287 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
288 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
292 mutt_expand_path(buf, sizeof (buf));
293 rc = safe_fopen (buf, "a");
296 if (alias_sanitize(new->name, NULL)) {
297 mutt_quote_filename(buf, sizeof(buf), new->name);
298 fprintf(rc, "alias %s ", buf);
300 fprintf(rc, "alias %s ", new->name);
304 rfc822_write_address(buf, sizeof(buf), new->addr, 0);
305 write_safe_address(rc, buf);
307 mutt_message _("Alias added.");
313 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
315 address_t *pop, *head = NULL;
316 address_t **last = &head;
318 while ((pop = address_list_pop(&a))) {
319 if (!pop->group && !pop->personal
320 && pop->mailbox && !strchr(pop->mailbox, '@'))
322 const address_t *t = alias_lookup(Aliases, pop->mailbox);
327 for (u = *expn; u; u = u->next) {
328 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
329 address_list_wipe(&pop);
334 /* save the fact we saw it */
335 u = string_item_new();
336 u->data = m_strdup(pop->mailbox);
339 address_list_wipe(&pop);
342 last = address_list_last(last);
343 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
346 struct passwd *pw = getpwnam(pop->mailbox);
349 char namebuf[STRING];
350 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
351 m_strreplace(&pop->personal, namebuf);
356 last = address_list_append(last, pop);
359 if (option(OPTUSEDOMAIN)) {
360 /* now qualify all local addresses */
361 const char *fqdn = mutt_fqdn(1);
363 rfc822_qualify(head, fqdn);
369 address_t *mutt_expand_aliases(address_t *a)
372 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
374 t = mutt_expand_aliases_r(a, &expn);
375 string_list_wipe(&expn);
376 return mutt_remove_duplicates(t);
379 void mutt_expand_aliases_env(ENVELOPE *env)
381 env->from = mutt_expand_aliases(env->from);
382 env->to = mutt_expand_aliases(env->to);
383 env->cc = mutt_expand_aliases(env->cc);
384 env->bcc = mutt_expand_aliases(env->bcc);
385 env->reply_to = mutt_expand_aliases(env->reply_to);
386 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
389 /************* READ MARK *********************/
391 /* alias_complete() -- alias completion routine
393 * given a partial alias, this routine attempts to fill in the alias
394 * from the alias list as much as possible. if given empty search string
395 * or found nothing, present all aliases
397 int mutt_alias_complete (char *s, size_t buflen)
399 alias_t *a = Aliases;
400 alias_t *a_list = NULL, *a_cur = NULL;
401 char bestname[HUGE_STRING];
404 #define min(a,b) ((a<b)?a:b)
406 if (s[0] != 0) { /* avoid empty string as strstr argument */
407 p_clear(bestname, countof(bestname));
410 if (a->name && strstr (a->name, s) == a->name) {
411 if (!bestname[0]) /* init */
412 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
415 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
422 if (bestname[0] != 0) {
423 if (m_strcmp(bestname, s) != 0) {
424 /* we are adding something to the completion */
425 m_strcpy(s, m_strlen(bestname) + 1, bestname);
429 /* build alias list and show it */
433 if (a->name && (strstr (a->name, s) == a->name)) {
434 if (!a_list) /* init */
435 a_cur = a_list = alias_new();
437 a_cur->next = alias_new();
449 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
450 if (bestname[0] != 0)
451 m_strcpy(s, buflen, bestname);
453 /* free the alias list */
456 a_list = a_list->next;
460 /* remove any aliases marked for deletion */
462 for (a_cur = Aliases; a_cur;) {
465 a_list->next = a_cur->next;
467 Aliases = a_cur->next;
470 alias_list_wipe(&a_cur);
486 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
487 const format_t *src, const char *fmt,
488 const char *ifstring __attribute__ ((unused)),
489 const char *elsestring __attribute__ ((unused)),
490 unsigned long data, format_flag flags __attribute__ ((unused)))
492 char tmp[SHORT_STRING], adr[SHORT_STRING];
493 alias_t *alias = (alias_t *) data;
497 m_strcpy(dest, destlen, alias->del ? "D" : " ");
500 mutt_format_s(dest, destlen, fmt, alias->name);
504 rfc822_write_address(adr, sizeof(adr), alias->addr, 1);
505 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
506 snprintf(dest, destlen, tmp, adr);
509 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
510 snprintf(dest, destlen, tmp, alias->num + 1);
513 m_strcpy(dest, destlen, alias->tagged ? "*" : " ");
520 static void alias_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
522 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *)alias_format_str,
523 (unsigned long)((alias_t **)m->data)[num],
524 M_FORMAT_ARROWCURSOR);
527 static int alias_tag (MUTTMENU * menu, int n, int m)
529 alias_t *cur = ((alias_t **) menu->data)[n];
530 int ot = cur->tagged;
532 cur->tagged = (m >= 0 ? m : !cur->tagged);
534 return cur->tagged - ot;
537 static int alias_SortAlias (const void *a, const void *b)
539 alias_t *pa = *(alias_t **) a;
540 alias_t *pb = *(alias_t **) b;
541 int r = m_strcasecmp(pa->name, pb->name);
546 static int alias_SortAddress (const void *a, const void *b)
548 address_t *pa = (*(alias_t **) a)->addr;
549 address_t *pb = (*(alias_t **) b)->addr;
558 else if (pa->personal) {
560 r = m_strcasecmp(pa->personal, pb->personal);
564 else if (pb->personal)
567 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
571 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
575 alias_t **AliasTable = NULL;
579 char helpstr[SHORT_STRING];
584 mutt_error _("You have no aliases!");
589 /* tell whoever called me to redraw the screen when I return */
590 set_option (OPTNEEDREDRAW);
592 menu = mutt_new_menu ();
593 menu->make_entry = alias_entry;
594 menu->tag = alias_tag;
595 menu->menu = MENU_ALIAS;
596 menu->title = _("Aliases");
597 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
598 MENU_ALIAS, AliasHelp);
604 /* count the number of aliases */
605 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
611 p_realloc(&AliasTable, menu->max);
612 menu->data = AliasTable;
614 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
615 AliasTable[i] = aliasp;
619 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
620 qsort (AliasTable, i, sizeof (alias_t *),
621 (SortAlias & SORT_MASK) ==
622 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
625 for (i = 0; i < menu->max; i++)
626 AliasTable[i]->num = i;
630 menu->redraw |= REDRAW_FULL;
631 aliases = aliases->next;
635 switch ((op = mutt_menuLoop (menu))) {
638 if (menu->tagprefix) {
639 for (i = 0; i < menu->max; i++)
640 if (AliasTable[i]->tagged)
641 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
642 menu->redraw |= REDRAW_INDEX;
645 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
646 menu->redraw |= REDRAW_CURRENT;
647 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
649 menu->redraw |= REDRAW_INDEX;
653 case OP_GENERIC_SELECT_ENTRY:
661 for (i = 0; i < menu->max; i++) {
662 if (AliasTable[i]->tagged) {
663 mutt_addrlist_to_local (AliasTable[i]->addr);
664 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
670 mutt_addrlist_to_local (AliasTable[t]->addr);
671 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
674 mutt_menuDestroy (&menu);
675 p_delete(&AliasTable);