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/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 void mutt_alias_menu(char *, size_t, alias_t *);
149 const address_t *alias_lookup(const char *s)
153 for (list = Aliases; list; list = list->next) {
154 if (!m_strcasecmp(s, list->name))
161 /* This routine looks to see if the user has an alias defined for the given
163 const address_t *alias_reverse_lookup(const address_t *a)
167 if (!a || !a->mailbox)
170 for (list = Aliases; list; list = list->next) {
173 /* cycle through all addresses if this is a group alias */
174 for (ap = list->addr; ap; ap = ap->next) {
175 if (!ap->group && ap->mailbox
176 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
184 static int string_is_address(const char *str, const char *u, const char *d)
186 char buf[LONG_STRING];
187 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
188 return !ascii_strcasecmp(str, buf);
191 /* returns TRUE if the given address belongs to the user. */
192 int mutt_addr_is_user(address_t *addr)
194 /* NULL address is assumed to be the user. */
201 if (!ascii_strcasecmp(addr->mailbox, mod_core.username)
202 || string_is_address(addr->mailbox, mod_core.username, mod_core.shorthost)
203 || string_is_address(addr->mailbox, mod_core.username, mutt_fqdn(0))
204 || string_is_address(addr->mailbox, mod_core.username, mutt_fqdn(1))
205 || (MAlias.from && !ascii_strcasecmp(MAlias.from->mailbox, addr->mailbox)))
210 return rx_list_match(Alternates, addr->mailbox)
211 && !rx_list_match(UnAlternates, addr->mailbox);
214 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
216 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
218 if (mutt_addr_is_user(env->from)) {
219 if (env->to && !mutt_is_mail_list(env->to)) {
220 RETURN("To", env->to);
222 RETURN("Cc", env->cc);
225 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
226 RETURN("Reply-To", env->reply_to);
228 RETURN("From", env->from);
235 /* Only characters which are non-special to both the RFC 822 and the mutt
236 configuration parser are permitted. */
237 static int alias_sanitize(const char *s, char *d)
242 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
260 * if someone has an address like
261 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
262 * and the user creates an alias for this, Mutt could wind up executing
263 * the backtics because it writes aliases like
264 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
265 * To avoid this problem, use a backslash (\) to quote any backtics. We also
266 * need to quote backslashes as well, since you could defeat the above by
268 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
269 * since that would get aliased as
270 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
271 * which still gets evaluated because the double backslash is not a quote.
273 * Additionally, we need to quote ' and " characters - otherwise, mutt will
274 * interpret them on the wrong parsing step.
276 * $ wants to be quoted since it may indicate the start of an environment
279 static void write_safe_address(FILE *fp, const char *s)
282 if (strchr("\\`'\"$", *s)) {
290 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
292 char buf[LONG_STRING], prompt[STRING];
293 address_t *adr = iadr;
298 adr = mutt_get_address(cur, NULL);
301 if (adr && adr->mailbox) {
302 const char *p = m_strchrnul(adr->mailbox, '@');
303 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
308 /* Don't suggest a bad alias name in the event of a strange local part. */
309 alias_sanitize(buf, buf);
311 /* add a new alias */
312 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
315 /* check to see if the user already has an alias defined */
316 if (alias_lookup(buf)) {
317 mutt_error _("You already have an alias defined with that name!");
321 alias_sanitize(buf, buf);
323 new->name = m_strdup(buf);
325 mutt_addrlist_to_local(adr);
327 m_strcpy(buf, sizeof(buf), adr->mailbox);
332 mutt_addrlist_to_idna(adr, NULL);
337 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
338 alias_list_wipe(&new);
342 new->addr = rfc822_parse_adrlist(new->addr, buf);
346 if (mutt_addrlist_to_idna(new->addr, &err)) {
347 mutt_error(_("Error: '%s' is a bad IDN."), err);
352 } while (!new->addr);
354 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
355 m_strcpy(buf, sizeof(buf), adr->personal);
360 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
361 alias_list_wipe(&new);
364 new->addr->personal = m_strdup(buf);
367 rfc822_addrcat(buf, sizeof(buf), new->addr, 1);
368 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
369 if (mutt_yesorno(prompt, M_YES) != M_YES) {
370 alias_list_wipe(&new);
374 alias_list_push(&Aliases, new);
376 m_strcpy(buf, sizeof(buf), NONULL(MAlias.alias_file));
377 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
381 mutt_expand_path(buf, sizeof(buf));
382 rc = safe_fopen (buf, "a");
385 if (alias_sanitize(new->name, NULL)) {
386 mutt_quote_filename(buf, sizeof(buf), new->name);
387 fprintf(rc, "alias %s ", buf);
389 fprintf(rc, "alias %s ", new->name);
393 rfc822_addrcat(buf, sizeof(buf), new->addr, 0);
394 write_safe_address(rc, buf);
396 mutt_message _("Alias added.");
402 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
404 address_t *pop, *head = NULL;
405 address_t **last = &head;
407 while ((pop = address_list_pop(&a))) {
408 if (!pop->group && !pop->personal
409 && pop->mailbox && !strchr(pop->mailbox, '@'))
411 const address_t *t = alias_lookup(pop->mailbox);
416 for (u = *expn; u; u = u->next) {
417 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
418 address_list_wipe(&pop);
423 /* save the fact we saw it */
424 u = string_item_new();
425 u->data = m_strdup(pop->mailbox);
428 address_list_wipe(&pop);
431 last = address_list_last(last);
432 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
435 struct passwd *pw = getpwnam(pop->mailbox);
438 char namebuf[STRING];
439 mutt_gecos_name(namebuf, sizeof(namebuf), pw, mod_core.gecos_mask);
440 m_strreplace(&pop->personal, namebuf);
445 last = address_list_append(last, pop);
448 if (mod_core.use_domain) {
449 /* now qualify all local addresses */
450 rfc822_qualify(head, mutt_fqdn(1));
456 address_t *mutt_expand_aliases(address_t *a)
459 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
461 t = mutt_expand_aliases_r(a, &expn);
462 string_list_wipe(&expn);
463 address_list_uniq(t);
467 void mutt_expand_aliases_env(ENVELOPE *env)
469 env->from = mutt_expand_aliases(env->from);
470 env->to = mutt_expand_aliases(env->to);
471 env->cc = mutt_expand_aliases(env->cc);
472 env->bcc = mutt_expand_aliases(env->bcc);
473 env->reply_to = mutt_expand_aliases(env->reply_to);
474 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
477 /************* READ MARK *********************/
479 /* alias_complete() -- alias completion routine
481 * given a partial alias, this routine attempts to fill in the alias
482 * from the alias list as much as possible. if given empty search string
483 * or found nothing, present all aliases
485 int mutt_alias_complete (char *s, size_t buflen)
487 alias_t *a = Aliases;
488 alias_t *a_list = NULL, *a_cur = NULL;
489 char bestname[HUGE_STRING];
492 #define min(a,b) ((a<b)?a:b)
494 if (s[0] != 0) { /* avoid empty string as strstr argument */
495 p_clear(bestname, countof(bestname));
498 if (a->name && strstr (a->name, s) == a->name) {
499 if (!bestname[0]) /* init */
500 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
503 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
510 if (bestname[0] != 0) {
511 if (m_strcmp(bestname, s) != 0) {
512 /* we are adding something to the completion */
513 m_strcpy(s, m_strlen(bestname) + 1, bestname);
517 /* build alias list and show it */
521 if (a->name && (strstr (a->name, s) == a->name)) {
522 if (!a_list) /* init */
523 a_cur = a_list = alias_new();
525 a_cur->next = alias_new();
537 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
538 if (bestname[0] != 0)
539 m_strcpy(s, buflen, bestname);
541 /* free the alias list */
544 a_list = a_list->next;
548 /* remove any aliases marked for deletion */
550 for (a_cur = Aliases; a_cur;) {
553 a_list->next = a_cur->next;
555 Aliases = a_cur->next;
558 alias_list_wipe(&a_cur);
575 alias_format_str(char *dest, ssize_t destlen, char op, const char *src,
576 const char *fmt, const char *ifstr __attribute__ ((unused)),
577 const char *elstr __attribute__ ((unused)),
578 anytype data, format_flag flags __attribute__ ((unused)))
580 char tmp[STRING], adr[STRING];
581 alias_t *alias = data.ptr;
585 m_strputc(dest, destlen, alias->del ? 'D' : ' ');
588 mutt_format_s(dest, destlen, fmt, alias->name);
592 rfc822_addrcat(adr, sizeof(adr), alias->addr, 1);
593 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
594 snprintf(dest, destlen, tmp, adr);
597 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
598 snprintf(dest, destlen, tmp, alias->num + 1);
601 m_strputc(dest, destlen, alias->tagged ? '*' : ' ');
608 static void alias_entry(char *s, ssize_t slen, MUTTMENU *m, int num)
610 m_strformat(s, slen, getmaxx(main_w), MAlias.alias_format, alias_format_str,
611 ((alias_t **)m->data)[num], 0);
614 static int alias_tag (MUTTMENU * menu, int n, int m)
616 alias_t *cur = ((alias_t **) menu->data)[n];
617 int ot = cur->tagged;
619 cur->tagged = (m >= 0 ? m : !cur->tagged);
621 return cur->tagged - ot;
624 static int alias_SortAlias (const void *a, const void *b)
626 alias_t *pa = *(alias_t **) a;
627 alias_t *pb = *(alias_t **) b;
628 int r = m_strcasecmp(pa->name, pb->name);
633 static int alias_SortAddress (const void *a, const void *b)
635 address_t *pa = (*(alias_t **) a)->addr;
636 address_t *pb = (*(alias_t **) b)->addr;
645 else if (pa->personal) {
647 r = m_strcasecmp(pa->personal, pb->personal);
651 else if (pb->personal)
654 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
658 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
662 alias_t **AliasTable = NULL;
670 mutt_error _("You have no aliases!");
675 /* tell whoever called me to redraw the screen when I return */
676 set_option (OPTNEEDREDRAW);
678 menu = mutt_new_menu ();
679 menu->make_entry = alias_entry;
680 menu->tag = alias_tag;
681 menu->menu = MENU_ALIAS;
682 menu->title = _("Aliases");
688 /* count the number of aliases */
689 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
695 p_realloc(&AliasTable, menu->max);
696 menu->data = AliasTable;
698 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
699 AliasTable[i] = aliasp;
703 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
704 qsort (AliasTable, i, sizeof (alias_t *),
705 (SortAlias & SORT_MASK) ==
706 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
709 for (i = 0; i < menu->max; i++)
710 AliasTable[i]->num = i;
714 menu->redraw |= REDRAW_FULL;
715 aliases = aliases->next;
719 switch ((op = mutt_menuLoop (menu))) {
722 if (menu->tagprefix) {
723 for (i = 0; i < menu->max; i++)
724 if (AliasTable[i]->tagged)
725 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
726 menu->redraw |= REDRAW_INDEX;
729 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
730 menu->redraw |= REDRAW_CURRENT;
731 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
733 menu->redraw |= REDRAW_INDEX;
737 case OP_GENERIC_SELECT_ENTRY:
745 for (i = 0; i < menu->max; i++) {
746 if (AliasTable[i]->tagged) {
747 mutt_addrlist_to_local (AliasTable[i]->addr);
748 rfc822_addrcat(buf, buflen, AliasTable[i]->addr, 0);
754 mutt_addrlist_to_local (AliasTable[t]->addr);
755 rfc822_addrcat(buf, buflen, AliasTable[t]->addr, 0);
758 mutt_menuDestroy (&menu);
759 p_delete(&AliasTable);