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"
39 @import "lib-lua/base.cpkg"
41 static rx_t *Alternates = NULL, *UnAlternates = NULL;
42 rx_t *MailLists = NULL, *UnMailLists = NULL;
43 rx_t *SubscribedLists = NULL, *UnSubscribedLists = NULL;
48 ** Specifies the format of the data displayed for the ``alias'' menu. The
49 ** following \fTprintf(3)\fP-style sequences are available:
52 ** .dt %a .dd alias name
53 ** .dt %f .dd flags - currently, a "d" for an alias marked for deletion
54 ** .dt %n .dd index number
55 ** .dt %r .dd address which alias expands to
56 ** .dt %t .dd character which indicates if the alias is tagged for inclusion
59 string_t alias_format = m_strdup("%4n %2f %t %-10a %r");
62 ** The default file in which to save aliases created by the
63 ** ``$create-alias'' function.
65 ** \fBNote:\fP Madmutt will not automatically source this file; you must
66 ** explicitly use the ``$source'' command for it to be executed.
68 path_t alias_file = m_strdup("~/.madmutt/aliases");
72 ** Specifies the filename of your signature, which is appended to all
73 ** outgoing messages. If the filename ends with a pipe (``\fT|\fP''), it is
74 ** assumed that filename is a shell command and input should be read from
77 path_t signature = m_strdup("~/.signature");
81 ** This specifies the file into which your outgoing messages should be
82 ** appended. (This is meant as the primary method for saving a copy of
83 ** your messages, but another way to do this is using the ``$my_hdr''
84 ** command to create a \fTBcc:\fP header field with your email address in it.)
86 ** The value of \fI$$record\fP is overridden by the ``$$force_name'' and
87 ** ``$$save_name'' variables, and the ``$fcc-hook'' command.
93 ** This variable contains a default from address. It
94 ** can be overridden using my_hdr (including from send-hooks) and
95 ** ``$$reverse_name''. This variable is ignored if ``$$use_from''
99 ** \fTsend-hook Madmutt-devel@lists.berlios.de 'my_hdr From: Foo Bar <foo@bar.fb>'\fP
100 ** when replying to the Madmutt developer's mailing list and Madmutt takes this email address.
102 ** Defaults to the contents of the environment variable \fT$$$EMAIL\fP.
104 address_t from = rfc822_parse_adrlist(NULL, NONULL(getenv("EMAIL")));
106 void alternates(rx_t rx) {
107 rx_list_remove(&UnAlternates, rx);
108 rx_list_add(&Alternates, rx);
111 void unalternates(rx_t rx) {
112 rx_list_remove(&Alternates, rx);
113 rx_list_add(&UnAlternates, rx);
117 void lists(rx_t rx) {
118 rx_list_remove(&UnMailLists, rx);
119 rx_list_add(&MailLists, rx);
122 void unlists(rx_t rx) {
123 rx_list_remove(&MailLists, rx);
124 rx_list_remove(&SubscribedLists, rx);
125 rx_list_add(&UnMailLists, rx);
129 void subscribe(rx_t rx) {
130 rx_list_remove(&UnMailLists, rx);
131 rx_list_remove(&UnSubscribedLists, rx);
132 rx_list_add(&MailLists, rx);
133 rx_list_add(&SubscribedLists, rx_dup(rx));
136 void unsubscribe(rx_t rx) {
137 rx_list_remove(&SubscribedLists, rx);
138 rx_list_add(&UnSubscribedLists, rx);
145 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
147 static struct mapping_t AliasHelp[] = {
148 {N_("Exit"), OP_EXIT},
149 {N_("Del"), OP_DELETE},
150 {N_("Undel"), OP_UNDELETE},
151 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
152 {N_("Help"), OP_HELP},
156 static void mutt_alias_menu(char *, size_t, alias_t *);
158 const address_t *alias_lookup(const char *s)
162 for (list = Aliases; list; list = list->next) {
163 if (!m_strcasecmp(s, list->name))
170 /* This routine looks to see if the user has an alias defined for the given
172 const address_t *alias_reverse_lookup(const address_t *a)
176 if (!a || !a->mailbox)
179 for (list = Aliases; list; list = list->next) {
182 /* cycle through all addresses if this is a group alias */
183 for (ap = list->addr; ap; ap = ap->next) {
184 if (!ap->group && ap->mailbox
185 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
193 static int string_is_address(const char *str, const char *u, const char *d)
195 char buf[LONG_STRING];
196 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
197 return !ascii_strcasecmp(str, buf);
200 /* returns TRUE if the given address belongs to the user. */
201 int mutt_addr_is_user(address_t *addr)
203 /* NULL address is assumed to be the user. */
210 if (!ascii_strcasecmp(addr->mailbox, mod_core.username)
211 || string_is_address(addr->mailbox, mod_core.username, mod_core.shorthost)
212 || string_is_address(addr->mailbox, mod_core.username, mutt_fqdn(0))
213 || string_is_address(addr->mailbox, mod_core.username, mutt_fqdn(1))
214 || (MAlias.from && !ascii_strcasecmp(MAlias.from->mailbox, addr->mailbox)))
219 return rx_list_match(Alternates, addr->mailbox)
220 && !rx_list_match(UnAlternates, addr->mailbox);
223 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
225 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
227 if (mutt_addr_is_user(env->from)) {
228 if (env->to && !mutt_is_mail_list(env->to)) {
229 RETURN("To", env->to);
231 RETURN("Cc", env->cc);
234 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
235 RETURN("Reply-To", env->reply_to);
237 RETURN("From", env->from);
244 /* Only characters which are non-special to both the RFC 822 and the mutt
245 configuration parser are permitted. */
246 static int alias_sanitize(const char *s, char *d)
251 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
269 * if someone has an address like
270 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
271 * and the user creates an alias for this, Mutt could wind up executing
272 * the backtics because it writes aliases like
273 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
274 * To avoid this problem, use a backslash (\) to quote any backtics. We also
275 * need to quote backslashes as well, since you could defeat the above by
277 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
278 * since that would get aliased as
279 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
280 * which still gets evaluated because the double backslash is not a quote.
282 * Additionally, we need to quote ' and " characters - otherwise, mutt will
283 * interpret them on the wrong parsing step.
285 * $ wants to be quoted since it may indicate the start of an environment
288 static void write_safe_address(FILE *fp, const char *s)
291 if (strchr("\\`'\"$", *s)) {
299 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
301 char buf[LONG_STRING], prompt[STRING];
302 address_t *adr = iadr;
307 adr = mutt_get_address(cur, NULL);
310 if (adr && adr->mailbox) {
311 const char *p = m_strchrnul(adr->mailbox, '@');
312 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
317 /* Don't suggest a bad alias name in the event of a strange local part. */
318 alias_sanitize(buf, buf);
320 /* add a new alias */
321 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
324 /* check to see if the user already has an alias defined */
325 if (alias_lookup(buf)) {
326 mutt_error _("You already have an alias defined with that name!");
330 alias_sanitize(buf, buf);
332 new->name = m_strdup(buf);
334 mutt_addrlist_to_local(adr);
336 m_strcpy(buf, sizeof(buf), adr->mailbox);
341 mutt_addrlist_to_idna(adr, NULL);
346 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
347 alias_list_wipe(&new);
351 new->addr = rfc822_parse_adrlist(new->addr, buf);
355 if (mutt_addrlist_to_idna(new->addr, &err)) {
356 mutt_error(_("Error: '%s' is a bad IDN."), err);
361 } while (!new->addr);
363 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
364 m_strcpy(buf, sizeof(buf), adr->personal);
369 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
370 alias_list_wipe(&new);
373 new->addr->personal = m_strdup(buf);
376 rfc822_addrcat(buf, sizeof(buf), new->addr, 1);
377 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
378 if (mutt_yesorno(prompt, M_YES) != M_YES) {
379 alias_list_wipe(&new);
383 alias_list_push(&Aliases, new);
385 m_strcpy(buf, sizeof(buf), NONULL(MAlias.alias_file));
386 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
390 mutt_expand_path(buf, sizeof(buf));
391 rc = safe_fopen (buf, "a");
394 if (alias_sanitize(new->name, NULL)) {
395 mutt_quote_filename(buf, sizeof(buf), new->name);
396 fprintf(rc, "alias %s ", buf);
398 fprintf(rc, "alias %s ", new->name);
402 rfc822_addrcat(buf, sizeof(buf), new->addr, 0);
403 write_safe_address(rc, buf);
405 mutt_message _("Alias added.");
411 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
413 address_t *pop, *head = NULL;
414 address_t **last = &head;
416 while ((pop = address_list_pop(&a))) {
417 if (!pop->group && !pop->personal
418 && pop->mailbox && !strchr(pop->mailbox, '@'))
420 const address_t *t = alias_lookup(pop->mailbox);
425 for (u = *expn; u; u = u->next) {
426 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
427 address_list_wipe(&pop);
432 /* save the fact we saw it */
433 u = string_item_new();
434 u->data = m_strdup(pop->mailbox);
437 address_list_wipe(&pop);
440 last = address_list_last(last);
441 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
444 struct passwd *pw = getpwnam(pop->mailbox);
447 char namebuf[STRING];
448 mutt_gecos_name(namebuf, sizeof(namebuf), pw, mod_core.gecos_mask);
449 m_strreplace(&pop->personal, namebuf);
454 last = address_list_append(last, pop);
457 if (mod_core.use_domain) {
458 /* now qualify all local addresses */
459 rfc822_qualify(head, mutt_fqdn(1));
465 address_t *mutt_expand_aliases(address_t *a)
468 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
470 t = mutt_expand_aliases_r(a, &expn);
471 string_list_wipe(&expn);
472 address_list_uniq(t);
476 void mutt_expand_aliases_env(ENVELOPE *env)
478 env->from = mutt_expand_aliases(env->from);
479 env->to = mutt_expand_aliases(env->to);
480 env->cc = mutt_expand_aliases(env->cc);
481 env->bcc = mutt_expand_aliases(env->bcc);
482 env->reply_to = mutt_expand_aliases(env->reply_to);
483 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
486 /************* READ MARK *********************/
488 /* alias_complete() -- alias completion routine
490 * given a partial alias, this routine attempts to fill in the alias
491 * from the alias list as much as possible. if given empty search string
492 * or found nothing, present all aliases
494 int mutt_alias_complete (char *s, size_t buflen)
496 alias_t *a = Aliases;
497 alias_t *a_list = NULL, *a_cur = NULL;
498 char bestname[HUGE_STRING];
501 #define min(a,b) ((a<b)?a:b)
503 if (s[0] != 0) { /* avoid empty string as strstr argument */
504 p_clear(bestname, countof(bestname));
507 if (a->name && strstr (a->name, s) == a->name) {
508 if (!bestname[0]) /* init */
509 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
512 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
519 if (bestname[0] != 0) {
520 if (m_strcmp(bestname, s) != 0) {
521 /* we are adding something to the completion */
522 m_strcpy(s, m_strlen(bestname) + 1, bestname);
526 /* build alias list and show it */
530 if (a->name && (strstr (a->name, s) == a->name)) {
531 if (!a_list) /* init */
532 a_cur = a_list = alias_new();
534 a_cur->next = alias_new();
546 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
547 if (bestname[0] != 0)
548 m_strcpy(s, buflen, bestname);
550 /* free the alias list */
553 a_list = a_list->next;
557 /* remove any aliases marked for deletion */
559 for (a_cur = Aliases; a_cur;) {
562 a_list->next = a_cur->next;
564 Aliases = a_cur->next;
567 alias_list_wipe(&a_cur);
584 alias_format_str(char *dest, ssize_t destlen, char op, const char *src,
585 const char *fmt, const char *ifstr __attribute__ ((unused)),
586 const char *elstr __attribute__ ((unused)),
587 anytype data, format_flag flags __attribute__ ((unused)))
589 char tmp[STRING], adr[STRING];
590 alias_t *alias = data.ptr;
594 m_strputc(dest, destlen, alias->del ? 'D' : ' ');
597 mutt_format_s(dest, destlen, fmt, alias->name);
601 rfc822_addrcat(adr, sizeof(adr), alias->addr, 1);
602 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
603 snprintf(dest, destlen, tmp, adr);
606 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
607 snprintf(dest, destlen, tmp, alias->num + 1);
610 m_strputc(dest, destlen, alias->tagged ? '*' : ' ');
617 static void alias_entry(char *s, ssize_t slen, MUTTMENU *m, int num)
619 m_strformat(s, slen, COLS - SW, MAlias.alias_format, alias_format_str,
620 ((alias_t **)m->data)[num], 0);
623 static int alias_tag (MUTTMENU * menu, int n, int m)
625 alias_t *cur = ((alias_t **) menu->data)[n];
626 int ot = cur->tagged;
628 cur->tagged = (m >= 0 ? m : !cur->tagged);
630 return cur->tagged - ot;
633 static int alias_SortAlias (const void *a, const void *b)
635 alias_t *pa = *(alias_t **) a;
636 alias_t *pb = *(alias_t **) b;
637 int r = m_strcasecmp(pa->name, pb->name);
642 static int alias_SortAddress (const void *a, const void *b)
644 address_t *pa = (*(alias_t **) a)->addr;
645 address_t *pb = (*(alias_t **) b)->addr;
654 else if (pa->personal) {
656 r = m_strcasecmp(pa->personal, pb->personal);
660 else if (pb->personal)
663 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
667 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
671 alias_t **AliasTable = NULL;
675 char helpstr[STRING];
680 mutt_error _("You have no aliases!");
685 /* tell whoever called me to redraw the screen when I return */
686 set_option (OPTNEEDREDRAW);
688 menu = mutt_new_menu ();
689 menu->make_entry = alias_entry;
690 menu->tag = alias_tag;
691 menu->menu = MENU_ALIAS;
692 menu->title = _("Aliases");
693 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
694 MENU_ALIAS, AliasHelp);
700 /* count the number of aliases */
701 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
707 p_realloc(&AliasTable, menu->max);
708 menu->data = AliasTable;
710 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
711 AliasTable[i] = aliasp;
715 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
716 qsort (AliasTable, i, sizeof (alias_t *),
717 (SortAlias & SORT_MASK) ==
718 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
721 for (i = 0; i < menu->max; i++)
722 AliasTable[i]->num = i;
726 menu->redraw |= REDRAW_FULL;
727 aliases = aliases->next;
731 switch ((op = mutt_menuLoop (menu))) {
734 if (menu->tagprefix) {
735 for (i = 0; i < menu->max; i++)
736 if (AliasTable[i]->tagged)
737 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
738 menu->redraw |= REDRAW_INDEX;
741 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
742 menu->redraw |= REDRAW_CURRENT;
743 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
745 menu->redraw |= REDRAW_INDEX;
749 case OP_GENERIC_SELECT_ENTRY:
757 for (i = 0; i < menu->max; i++) {
758 if (AliasTable[i]->tagged) {
759 mutt_addrlist_to_local (AliasTable[i]->addr);
760 rfc822_addrcat(buf, buflen, AliasTable[i]->addr, 0);
766 mutt_addrlist_to_local (AliasTable[t]->addr);
767 rfc822_addrcat(buf, buflen, AliasTable[t]->addr, 0);
770 mutt_menuDestroy (&menu);
771 p_delete(&AliasTable);