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/lib-ui.h>
33 #include <lib-ui/menu.h>
36 #include "mutt_idna.h"
38 @import "lib-lua/base.cpkg"
40 static rx_t *Alternates = NULL, *UnAlternates = NULL;
41 rx_t *MailLists = NULL, *UnMailLists = NULL;
42 rx_t *SubscribedLists = NULL, *UnSubscribedLists = NULL;
47 ** Specifies the format of the data displayed for the ``alias'' menu. The
48 ** following \fTprintf(3)\fP-style sequences are available:
51 ** .dt %a .dd alias name
52 ** .dt %f .dd flags - currently, a "d" for an alias marked for deletion
53 ** .dt %n .dd index number
54 ** .dt %r .dd address which alias expands to
55 ** .dt %t .dd character which indicates if the alias is tagged for inclusion
58 string_t alias_format = m_strdup("%4n %2f %t %-10a %r");
61 ** The default file in which to save aliases created by the
62 ** ``$create-alias'' function.
64 ** \fBNote:\fP Madmutt will not automatically source this file; you must
65 ** explicitly use the ``$source'' command for it to be executed.
67 path_t alias_file = m_strdup("~/.madmutt/aliases");
71 ** Specifies the filename of your signature, which is appended to all
72 ** outgoing messages. If the filename ends with a pipe (``\fT|\fP''), it is
73 ** assumed that filename is a shell command and input should be read from
76 path_t signature = m_strdup("~/.signature");
80 ** This specifies the file into which your outgoing messages should be
81 ** appended. (This is meant as the primary method for saving a copy of
82 ** your messages, but another way to do this is using the ``$my_hdr''
83 ** command to create a \fTBcc:\fP header field with your email address in it.)
85 ** The value of \fI$$record\fP is overridden by the ``$$force_name'' and
86 ** ``$$save_name'' variables, and the ``$fcc-hook'' command.
92 ** This variable contains a default from address. It
93 ** can be overridden using my_hdr (including from send-hooks) and
94 ** ``$$reverse_name''. This variable is ignored if ``$$use_from''
98 ** \fTsend-hook Madmutt-devel@lists.berlios.de 'my_hdr From: Foo Bar <foo@bar.fb>'\fP
99 ** when replying to the Madmutt developer's mailing list and Madmutt takes this email address.
101 ** Defaults to the contents of the environment variable \fT$$$EMAIL\fP.
103 address_t from = rfc822_parse_adrlist(NULL, NONULL(getenv("EMAIL")));
105 void alternates(rx_t rx) {
106 rx_list_remove(&UnAlternates, rx);
107 rx_list_add(&Alternates, rx);
110 void unalternates(rx_t rx) {
111 rx_list_remove(&Alternates, rx);
112 rx_list_add(&UnAlternates, rx);
116 void lists(rx_t rx) {
117 rx_list_remove(&UnMailLists, rx);
118 rx_list_add(&MailLists, rx);
121 void unlists(rx_t rx) {
122 rx_list_remove(&MailLists, rx);
123 rx_list_remove(&SubscribedLists, rx);
124 rx_list_add(&UnMailLists, rx);
128 void subscribe(rx_t rx) {
129 rx_list_remove(&UnMailLists, rx);
130 rx_list_remove(&UnSubscribedLists, rx);
131 rx_list_add(&MailLists, rx);
132 rx_list_add(&SubscribedLists, rx_dup(rx));
135 void unsubscribe(rx_t rx) {
136 rx_list_remove(&SubscribedLists, rx);
137 rx_list_add(&UnSubscribedLists, rx);
144 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
146 static void mutt_alias_menu(char *, size_t, alias_t *);
148 const address_t *alias_lookup(const char *s)
152 for (list = Aliases; list; list = list->next) {
153 if (!m_strcasecmp(s, list->name))
160 /* This routine looks to see if the user has an alias defined for the given
162 const address_t *alias_reverse_lookup(const address_t *a)
166 if (!a || !a->mailbox)
169 for (list = Aliases; list; list = list->next) {
172 /* cycle through all addresses if this is a group alias */
173 for (ap = list->addr; ap; ap = ap->next) {
174 if (!ap->group && ap->mailbox
175 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
183 static int string_is_address(const char *str, const char *u, const char *d)
185 char buf[LONG_STRING];
186 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
187 return !ascii_strcasecmp(str, buf);
190 /* returns TRUE if the given address belongs to the user. */
191 int mutt_addr_is_user(address_t *addr)
193 /* NULL address is assumed to be the user. */
200 if (!ascii_strcasecmp(addr->mailbox, mod_core.username)
201 || string_is_address(addr->mailbox, mod_core.username, mod_core.shorthost)
202 || string_is_address(addr->mailbox, mod_core.username, mutt_fqdn(0))
203 || string_is_address(addr->mailbox, mod_core.username, mutt_fqdn(1))
204 || (MAlias.from && !ascii_strcasecmp(MAlias.from->mailbox, addr->mailbox)))
209 return rx_list_match(Alternates, addr->mailbox)
210 && !rx_list_match(UnAlternates, addr->mailbox);
213 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
215 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
217 if (mutt_addr_is_user(env->from)) {
218 if (env->to && !mutt_is_mail_list(env->to)) {
219 RETURN("To", env->to);
221 RETURN("Cc", env->cc);
224 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
225 RETURN("Reply-To", env->reply_to);
227 RETURN("From", env->from);
234 /* Only characters which are non-special to both the RFC 822 and the mutt
235 configuration parser are permitted. */
236 static int alias_sanitize(const char *s, char *d)
241 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
259 * if someone has an address like
260 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
261 * and the user creates an alias for this, Mutt could wind up executing
262 * the backtics because it writes aliases like
263 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
264 * To avoid this problem, use a backslash (\) to quote any backtics. We also
265 * need to quote backslashes as well, since you could defeat the above by
267 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
268 * since that would get aliased as
269 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
270 * which still gets evaluated because the double backslash is not a quote.
272 * Additionally, we need to quote ' and " characters - otherwise, mutt will
273 * interpret them on the wrong parsing step.
275 * $ wants to be quoted since it may indicate the start of an environment
278 static void write_safe_address(FILE *fp, const char *s)
281 if (strchr("\\`'\"$", *s)) {
289 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
291 char buf[LONG_STRING], prompt[STRING];
292 address_t *adr = iadr;
297 adr = mutt_get_address(cur, NULL);
300 if (adr && adr->mailbox) {
301 const char *p = m_strchrnul(adr->mailbox, '@');
302 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
307 /* Don't suggest a bad alias name in the event of a strange local part. */
308 alias_sanitize(buf, buf);
310 /* add a new alias */
311 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
314 /* check to see if the user already has an alias defined */
315 if (alias_lookup(buf)) {
316 mutt_error _("You already have an alias defined with that name!");
320 alias_sanitize(buf, buf);
322 new->name = m_strdup(buf);
324 mutt_addrlist_to_local(adr);
326 m_strcpy(buf, sizeof(buf), adr->mailbox);
331 mutt_addrlist_to_idna(adr, NULL);
336 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
337 alias_list_wipe(&new);
341 new->addr = rfc822_parse_adrlist(new->addr, buf);
345 if (mutt_addrlist_to_idna(new->addr, &err)) {
346 mutt_error(_("Error: '%s' is a bad IDN."), err);
351 } while (!new->addr);
353 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
354 m_strcpy(buf, sizeof(buf), adr->personal);
359 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
360 alias_list_wipe(&new);
363 new->addr->personal = m_strdup(buf);
366 rfc822_addrcat(buf, sizeof(buf), new->addr, 1);
367 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
368 if (mutt_yesorno(prompt, M_YES) != M_YES) {
369 alias_list_wipe(&new);
373 alias_list_push(&Aliases, new);
375 m_strcpy(buf, sizeof(buf), NONULL(MAlias.alias_file));
376 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
380 mutt_expand_path(buf, sizeof(buf));
381 rc = safe_fopen (buf, "a");
384 if (alias_sanitize(new->name, NULL)) {
385 mutt_quote_filename(buf, sizeof(buf), new->name);
386 fprintf(rc, "alias %s ", buf);
388 fprintf(rc, "alias %s ", new->name);
392 rfc822_addrcat(buf, sizeof(buf), new->addr, 0);
393 write_safe_address(rc, buf);
395 mutt_message _("Alias added.");
401 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
403 address_t *pop, *head = NULL;
404 address_t **last = &head;
406 while ((pop = address_list_pop(&a))) {
407 if (!pop->group && !pop->personal
408 && pop->mailbox && !strchr(pop->mailbox, '@'))
410 const address_t *t = alias_lookup(pop->mailbox);
415 for (u = *expn; u; u = u->next) {
416 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
417 address_list_wipe(&pop);
422 /* save the fact we saw it */
423 u = string_item_new();
424 u->data = m_strdup(pop->mailbox);
427 address_list_wipe(&pop);
430 last = address_list_last(last);
431 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
434 struct passwd *pw = getpwnam(pop->mailbox);
437 char namebuf[STRING];
438 mutt_gecos_name(namebuf, sizeof(namebuf), pw, mod_core.gecos_mask);
439 m_strreplace(&pop->personal, namebuf);
444 last = address_list_append(last, pop);
447 if (mod_core.use_domain) {
448 /* now qualify all local addresses */
449 rfc822_qualify(head, mutt_fqdn(1));
455 address_t *mutt_expand_aliases(address_t *a)
458 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
460 t = mutt_expand_aliases_r(a, &expn);
461 string_list_wipe(&expn);
462 address_list_uniq(t);
466 void mutt_expand_aliases_env(ENVELOPE *env)
468 env->from = mutt_expand_aliases(env->from);
469 env->to = mutt_expand_aliases(env->to);
470 env->cc = mutt_expand_aliases(env->cc);
471 env->bcc = mutt_expand_aliases(env->bcc);
472 env->reply_to = mutt_expand_aliases(env->reply_to);
473 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
476 /************* READ MARK *********************/
478 /* alias_complete() -- alias completion routine
480 * given a partial alias, this routine attempts to fill in the alias
481 * from the alias list as much as possible. if given empty search string
482 * or found nothing, present all aliases
484 int mutt_alias_complete (char *s, size_t buflen)
486 alias_t *a = Aliases;
487 alias_t *a_list = NULL, *a_cur = NULL;
488 char bestname[HUGE_STRING];
491 #define min(a,b) ((a<b)?a:b)
493 if (s[0] != 0) { /* avoid empty string as strstr argument */
494 p_clear(bestname, countof(bestname));
497 if (a->name && strstr (a->name, s) == a->name) {
498 if (!bestname[0]) /* init */
499 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
502 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
509 if (bestname[0] != 0) {
510 if (m_strcmp(bestname, s) != 0) {
511 /* we are adding something to the completion */
512 m_strcpy(s, m_strlen(bestname) + 1, bestname);
516 /* build alias list and show it */
520 if (a->name && (strstr (a->name, s) == a->name)) {
521 if (!a_list) /* init */
522 a_cur = a_list = alias_new();
524 a_cur->next = alias_new();
536 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
537 if (bestname[0] != 0)
538 m_strcpy(s, buflen, bestname);
540 /* free the alias list */
543 a_list = a_list->next;
547 /* remove any aliases marked for deletion */
549 for (a_cur = Aliases; a_cur;) {
552 a_list->next = a_cur->next;
554 Aliases = a_cur->next;
557 alias_list_wipe(&a_cur);
574 alias_format_str(char *dest, ssize_t destlen, char op, const char *src,
575 const char *fmt, const char *ifstr __attribute__ ((unused)),
576 const char *elstr __attribute__ ((unused)),
577 anytype data, format_flag flags __attribute__ ((unused)))
579 char tmp[STRING], adr[STRING];
580 alias_t *alias = data.ptr;
584 m_strputc(dest, destlen, alias->del ? 'D' : ' ');
587 mutt_format_s(dest, destlen, fmt, alias->name);
591 rfc822_addrcat(adr, sizeof(adr), alias->addr, 1);
592 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
593 snprintf(dest, destlen, tmp, adr);
596 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
597 snprintf(dest, destlen, tmp, alias->num + 1);
600 m_strputc(dest, destlen, alias->tagged ? '*' : ' ');
607 static void alias_entry(char *s, ssize_t slen, MUTTMENU *m, int num)
609 m_strformat(s, slen, getmaxx(main_w), MAlias.alias_format, alias_format_str,
610 ((alias_t **)m->data)[num], 0);
613 static int alias_tag (MUTTMENU * menu, int n, int m)
615 alias_t *cur = ((alias_t **) menu->data)[n];
616 int ot = cur->tagged;
618 cur->tagged = (m >= 0 ? m : !cur->tagged);
620 return cur->tagged - ot;
623 static int alias_SortAlias (const void *a, const void *b)
625 alias_t *pa = *(alias_t **) a;
626 alias_t *pb = *(alias_t **) b;
627 int r = m_strcasecmp(pa->name, pb->name);
632 static int alias_SortAddress (const void *a, const void *b)
634 address_t *pa = (*(alias_t **) a)->addr;
635 address_t *pb = (*(alias_t **) b)->addr;
644 else if (pa->personal) {
646 r = m_strcasecmp(pa->personal, pb->personal);
650 else if (pb->personal)
653 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
657 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
661 alias_t **AliasTable = NULL;
669 mutt_error _("You have no aliases!");
674 /* tell whoever called me to redraw the screen when I return */
675 set_option (OPTNEEDREDRAW);
677 menu = mutt_new_menu ();
678 menu->make_entry = alias_entry;
679 menu->tag = alias_tag;
680 menu->menu = MENU_ALIAS;
681 menu->title = _("Aliases");
687 /* count the number of aliases */
688 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
694 p_realloc(&AliasTable, menu->max);
695 menu->data = AliasTable;
697 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
698 AliasTable[i] = aliasp;
702 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
703 qsort (AliasTable, i, sizeof (alias_t *),
704 (SortAlias & SORT_MASK) ==
705 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
708 for (i = 0; i < menu->max; i++)
709 AliasTable[i]->num = i;
713 menu->redraw |= REDRAW_FULL;
714 aliases = aliases->next;
718 switch ((op = mutt_menuLoop (menu))) {
721 if (menu->tagprefix) {
722 for (i = 0; i < menu->max; i++)
723 if (AliasTable[i]->tagged)
724 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
725 menu->redraw |= REDRAW_INDEX;
728 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
729 menu->redraw |= REDRAW_CURRENT;
730 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
732 menu->redraw |= REDRAW_INDEX;
736 case OP_GENERIC_SELECT_ENTRY:
744 for (i = 0; i < menu->max; i++) {
745 if (AliasTable[i]->tagged) {
746 mutt_addrlist_to_local (AliasTable[i]->addr);
747 rfc822_addrcat(buf, buflen, AliasTable[i]->addr, 0);
753 mutt_addrlist_to_local (AliasTable[t]->addr);
754 rfc822_addrcat(buf, buflen, AliasTable[t]->addr, 0);
757 mutt_menuDestroy (&menu);
758 p_delete(&AliasTable);