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.
28 #include <lib-lib/lib-lib.h>
30 #include <lib-sys/unix.h>
32 #include <lib-ui/curses.h>
33 #include <lib-ui/enter.h>
34 #include <lib-ui/menu.h>
37 #include "mutt_idna.h"
45 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
47 static struct mapping_t AliasHelp[] = {
48 {N_("Exit"), OP_EXIT},
49 {N_("Del"), OP_DELETE},
50 {N_("Undel"), OP_UNDELETE},
51 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
52 {N_("Help"), OP_HELP},
56 const address_t *alias_lookup(const char *s)
60 for (list = Aliases; list; list = list->next) {
61 if (!m_strcasecmp(s, list->name))
68 /* This routine looks to see if the user has an alias defined for the given
70 const address_t *alias_reverse_lookup(const address_t *a)
74 if (!a || !a->mailbox)
77 for (list = Aliases; list; list = list->next) {
80 /* cycle through all addresses if this is a group alias */
81 for (ap = list->addr; ap; ap = ap->next) {
82 if (!ap->group && ap->mailbox
83 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
91 static int string_is_address(const char *str, const char *u, const char *d)
93 char buf[LONG_STRING];
94 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
95 return !ascii_strcasecmp(str, buf);
98 /* returns TRUE if the given address belongs to the user. */
99 int mutt_addr_is_user(address_t *addr)
101 /* NULL address is assumed to be the user. */
108 if (!ascii_strcasecmp(addr->mailbox, Username)
109 || string_is_address(addr->mailbox, Username, Hostname)
110 || string_is_address(addr->mailbox, Username, mutt_fqdn(0))
111 || string_is_address(addr->mailbox, Username, mutt_fqdn(1))
112 || (From && !ascii_strcasecmp(From->mailbox, addr->mailbox)))
117 return rx_list_match(Alternates, addr->mailbox)
118 && !rx_list_match(UnAlternates, addr->mailbox);
121 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
123 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
125 if (mutt_addr_is_user(env->from)) {
126 if (env->to && !mutt_is_mail_list(env->to)) {
127 RETURN("To", env->to);
129 RETURN("Cc", env->cc);
132 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
133 RETURN("Reply-To", env->reply_to);
135 RETURN("From", env->from);
142 /* Only characters which are non-special to both the RFC 822 and the mutt
143 configuration parser are permitted. */
144 static int alias_sanitize(const char *s, char *d)
149 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
166 * if someone has an address like
167 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
168 * and the user creates an alias for this, Mutt could wind up executing
169 * the backtics because it writes aliases like
170 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
171 * To avoid this problem, use a backslash (\) to quote any backtics. We also
172 * need to quote backslashes as well, since you could defeat the above by
174 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
175 * since that would get aliased as
176 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
177 * which still gets evaluated because the double backslash is not a quote.
179 * Additionally, we need to quote ' and " characters - otherwise, mutt will
180 * interpret them on the wrong parsing step.
182 * $ wants to be quoted since it may indicate the start of an environment
185 static void write_safe_address(FILE *fp, const char *s)
188 if (strchr("\\`'\"$", *s)) {
196 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
198 char buf[LONG_STRING], prompt[SHORT_STRING];
199 address_t *adr = iadr;
204 adr = mutt_get_address(cur, NULL);
207 if (adr && adr->mailbox) {
208 const char *p = m_strchrnul(adr->mailbox, '@');
209 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
214 /* Don't suggest a bad alias name in the event of a strange local part. */
215 alias_sanitize(buf, buf);
217 /* add a new alias */
218 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
221 /* check to see if the user already has an alias defined */
222 if (alias_lookup(buf)) {
223 mutt_error _("You already have an alias defined with that name!");
227 alias_sanitize(buf, buf);
229 new->name = m_strdup(buf);
231 mutt_addrlist_to_local(adr);
233 m_strcpy(buf, sizeof(buf), adr->mailbox);
238 mutt_addrlist_to_idna(adr, NULL);
243 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
244 alias_list_wipe(&new);
248 new->addr = rfc822_parse_adrlist(new->addr, buf);
252 if (mutt_addrlist_to_idna(new->addr, &err)) {
253 mutt_error(_("Error: '%s' is a bad IDN."), err);
258 } while (!new->addr);
260 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
261 m_strcpy(buf, sizeof(buf), adr->personal);
266 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
267 alias_list_wipe(&new);
270 new->addr->personal = m_strdup(buf);
273 rfc822_write_address(buf, sizeof(buf), new->addr, 1);
274 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
275 if (mutt_yesorno(prompt, M_YES) != M_YES) {
276 alias_list_wipe(&new);
280 alias_list_push(&Aliases, new);
282 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
283 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
287 mutt_expand_path(buf, sizeof (buf));
288 rc = safe_fopen (buf, "a");
291 if (alias_sanitize(new->name, NULL)) {
292 mutt_quote_filename(buf, sizeof(buf), new->name);
293 fprintf(rc, "alias %s ", buf);
295 fprintf(rc, "alias %s ", new->name);
299 rfc822_write_address(buf, sizeof(buf), new->addr, 0);
300 write_safe_address(rc, buf);
302 mutt_message _("Alias added.");
308 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
310 address_t *pop, *head = NULL;
311 address_t **last = &head;
313 while ((pop = address_list_pop(&a))) {
314 if (!pop->group && !pop->personal
315 && pop->mailbox && !strchr(pop->mailbox, '@'))
317 const address_t *t = alias_lookup(pop->mailbox);
322 for (u = *expn; u; u = u->next) {
323 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
324 address_list_wipe(&pop);
329 /* save the fact we saw it */
330 u = string_item_new();
331 u->data = m_strdup(pop->mailbox);
334 address_list_wipe(&pop);
337 last = address_list_last(last);
338 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
341 struct passwd *pw = getpwnam(pop->mailbox);
344 char namebuf[STRING];
345 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
346 m_strreplace(&pop->personal, namebuf);
351 last = address_list_append(last, pop);
354 if (option(OPTUSEDOMAIN)) {
355 /* now qualify all local addresses */
356 const char *fqdn = mutt_fqdn(1);
358 rfc822_qualify(head, fqdn);
364 address_t *mutt_expand_aliases(address_t *a)
367 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
369 t = mutt_expand_aliases_r(a, &expn);
370 string_list_wipe(&expn);
371 address_list_uniq(t);
375 void mutt_expand_aliases_env(ENVELOPE *env)
377 env->from = mutt_expand_aliases(env->from);
378 env->to = mutt_expand_aliases(env->to);
379 env->cc = mutt_expand_aliases(env->cc);
380 env->bcc = mutt_expand_aliases(env->bcc);
381 env->reply_to = mutt_expand_aliases(env->reply_to);
382 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
385 /************* READ MARK *********************/
387 /* alias_complete() -- alias completion routine
389 * given a partial alias, this routine attempts to fill in the alias
390 * from the alias list as much as possible. if given empty search string
391 * or found nothing, present all aliases
393 int mutt_alias_complete (char *s, size_t buflen)
395 alias_t *a = Aliases;
396 alias_t *a_list = NULL, *a_cur = NULL;
397 char bestname[HUGE_STRING];
400 #define min(a,b) ((a<b)?a:b)
402 if (s[0] != 0) { /* avoid empty string as strstr argument */
403 p_clear(bestname, countof(bestname));
406 if (a->name && strstr (a->name, s) == a->name) {
407 if (!bestname[0]) /* init */
408 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
411 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
418 if (bestname[0] != 0) {
419 if (m_strcmp(bestname, s) != 0) {
420 /* we are adding something to the completion */
421 m_strcpy(s, m_strlen(bestname) + 1, bestname);
425 /* build alias list and show it */
429 if (a->name && (strstr (a->name, s) == a->name)) {
430 if (!a_list) /* init */
431 a_cur = a_list = alias_new();
433 a_cur->next = alias_new();
445 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
446 if (bestname[0] != 0)
447 m_strcpy(s, buflen, bestname);
449 /* free the alias list */
452 a_list = a_list->next;
456 /* remove any aliases marked for deletion */
458 for (a_cur = Aliases; a_cur;) {
461 a_list->next = a_cur->next;
463 Aliases = a_cur->next;
466 alias_list_wipe(&a_cur);
482 static const format_t *alias_format_str (char *dest, size_t destlen, char op,
483 const format_t *src, const char *fmt,
484 const char *ifstring __attribute__ ((unused)),
485 const char *elsestring __attribute__ ((unused)),
486 unsigned long data, format_flag flags __attribute__ ((unused)))
488 char tmp[SHORT_STRING], adr[SHORT_STRING];
489 alias_t *alias = (alias_t *) data;
493 m_strcpy(dest, destlen, alias->del ? "D" : " ");
496 mutt_format_s(dest, destlen, fmt, alias->name);
500 rfc822_write_address(adr, sizeof(adr), alias->addr, 1);
501 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
502 snprintf(dest, destlen, tmp, adr);
505 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
506 snprintf(dest, destlen, tmp, alias->num + 1);
509 m_strcpy(dest, destlen, alias->tagged ? "*" : " ");
516 static void alias_entry (char *s, ssize_t slen, MUTTMENU * m, int num)
518 mutt_FormatString (s, slen, NONULL (AliasFmt), (format_t *)alias_format_str,
519 (unsigned long)((alias_t **)m->data)[num],
520 M_FORMAT_ARROWCURSOR);
523 static int alias_tag (MUTTMENU * menu, int n, int m)
525 alias_t *cur = ((alias_t **) menu->data)[n];
526 int ot = cur->tagged;
528 cur->tagged = (m >= 0 ? m : !cur->tagged);
530 return cur->tagged - ot;
533 static int alias_SortAlias (const void *a, const void *b)
535 alias_t *pa = *(alias_t **) a;
536 alias_t *pb = *(alias_t **) b;
537 int r = m_strcasecmp(pa->name, pb->name);
542 static int alias_SortAddress (const void *a, const void *b)
544 address_t *pa = (*(alias_t **) a)->addr;
545 address_t *pb = (*(alias_t **) b)->addr;
554 else if (pa->personal) {
556 r = m_strcasecmp(pa->personal, pb->personal);
560 else if (pb->personal)
563 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
567 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
571 alias_t **AliasTable = NULL;
575 char helpstr[SHORT_STRING];
580 mutt_error _("You have no aliases!");
585 /* tell whoever called me to redraw the screen when I return */
586 set_option (OPTNEEDREDRAW);
588 menu = mutt_new_menu ();
589 menu->make_entry = alias_entry;
590 menu->tag = alias_tag;
591 menu->menu = MENU_ALIAS;
592 menu->title = _("Aliases");
593 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
594 MENU_ALIAS, AliasHelp);
600 /* count the number of aliases */
601 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
607 p_realloc(&AliasTable, menu->max);
608 menu->data = AliasTable;
610 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
611 AliasTable[i] = aliasp;
615 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
616 qsort (AliasTable, i, sizeof (alias_t *),
617 (SortAlias & SORT_MASK) ==
618 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
621 for (i = 0; i < menu->max; i++)
622 AliasTable[i]->num = i;
626 menu->redraw |= REDRAW_FULL;
627 aliases = aliases->next;
631 switch ((op = mutt_menuLoop (menu))) {
634 if (menu->tagprefix) {
635 for (i = 0; i < menu->max; i++)
636 if (AliasTable[i]->tagged)
637 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
638 menu->redraw |= REDRAW_INDEX;
641 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
642 menu->redraw |= REDRAW_CURRENT;
643 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
645 menu->redraw |= REDRAW_INDEX;
649 case OP_GENERIC_SELECT_ENTRY:
657 for (i = 0; i < menu->max; i++) {
658 if (AliasTable[i]->tagged) {
659 mutt_addrlist_to_local (AliasTable[i]->addr);
660 rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0);
666 mutt_addrlist_to_local (AliasTable[t]->addr);
667 rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0);
670 mutt_menuDestroy (&menu);
671 p_delete(&AliasTable);