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"
42 .ctype = unsigned : 1;
53 .push = luaM_path_post($$);
62 .check = luaM_checkquadopt($L, $$);
63 .ctype = unsigned : 2;
66 static @package MAlias {
67 path_t alias_format = m_strdup("%4n %2f %t %-10a %r");
74 #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x
76 static struct mapping_t AliasHelp[] = {
77 {N_("Exit"), OP_EXIT},
78 {N_("Del"), OP_DELETE},
79 {N_("Undel"), OP_UNDELETE},
80 {N_("Select"), OP_GENERIC_SELECT_ENTRY},
81 {N_("Help"), OP_HELP},
85 static void mutt_alias_menu(char *, size_t, alias_t *);
87 const address_t *alias_lookup(const char *s)
91 for (list = Aliases; list; list = list->next) {
92 if (!m_strcasecmp(s, list->name))
99 /* This routine looks to see if the user has an alias defined for the given
101 const address_t *alias_reverse_lookup(const address_t *a)
105 if (!a || !a->mailbox)
108 for (list = Aliases; list; list = list->next) {
111 /* cycle through all addresses if this is a group alias */
112 for (ap = list->addr; ap; ap = ap->next) {
113 if (!ap->group && ap->mailbox
114 && !ascii_strcasecmp(ap->mailbox, a->mailbox))
122 static int string_is_address(const char *str, const char *u, const char *d)
124 char buf[LONG_STRING];
125 snprintf(buf, sizeof (buf), "%s@%s", NONULL(u), NONULL(d));
126 return !ascii_strcasecmp(str, buf);
129 /* returns TRUE if the given address belongs to the user. */
130 int mutt_addr_is_user(address_t *addr)
132 /* NULL address is assumed to be the user. */
139 if (!ascii_strcasecmp(addr->mailbox, MCore.username)
140 || string_is_address(addr->mailbox, MCore.username, Hostname)
141 || string_is_address(addr->mailbox, MCore.username, mutt_fqdn(0))
142 || string_is_address(addr->mailbox, MCore.username, mutt_fqdn(1))
143 || (From && !ascii_strcasecmp(From->mailbox, addr->mailbox)))
148 return rx_list_match(Alternates, addr->mailbox)
149 && !rx_list_match(UnAlternates, addr->mailbox);
152 address_t *mutt_get_address(ENVELOPE *env, const char **pfxp)
154 #define RETURN(s, adr) do { if (pfxp) *pfxp = s; return adr; } while (0)
156 if (mutt_addr_is_user(env->from)) {
157 if (env->to && !mutt_is_mail_list(env->to)) {
158 RETURN("To", env->to);
160 RETURN("Cc", env->cc);
163 if (env->reply_to && !mutt_is_mail_list(env->reply_to)) {
164 RETURN("Reply-To", env->reply_to);
166 RETURN("From", env->from);
173 /* Only characters which are non-special to both the RFC 822 and the mutt
174 configuration parser are permitted. */
175 static int alias_sanitize(const char *s, char *d)
180 if (isalnum((unsigned char)(*s)) || strchr("-_+=.", *s)) {
198 * if someone has an address like
199 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
200 * and the user creates an alias for this, Mutt could wind up executing
201 * the backtics because it writes aliases like
202 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
203 * To avoid this problem, use a backslash (\) to quote any backtics. We also
204 * need to quote backslashes as well, since you could defeat the above by
206 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
207 * since that would get aliased as
208 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
209 * which still gets evaluated because the double backslash is not a quote.
211 * Additionally, we need to quote ' and " characters - otherwise, mutt will
212 * interpret them on the wrong parsing step.
214 * $ wants to be quoted since it may indicate the start of an environment
217 static void write_safe_address(FILE *fp, const char *s)
220 if (strchr("\\`'\"$", *s)) {
228 void mutt_create_alias(ENVELOPE *cur, address_t *iadr)
230 char buf[LONG_STRING], prompt[STRING];
231 address_t *adr = iadr;
236 adr = mutt_get_address(cur, NULL);
239 if (adr && adr->mailbox) {
240 const char *p = m_strchrnul(adr->mailbox, '@');
241 m_strncpy(buf, sizeof(buf), adr->mailbox, p - adr->mailbox);
246 /* Don't suggest a bad alias name in the event of a strange local part. */
247 alias_sanitize(buf, buf);
249 /* add a new alias */
250 if (mutt_get_field(_("Alias as: "), buf, sizeof(buf), 0) || !buf[0])
253 /* check to see if the user already has an alias defined */
254 if (alias_lookup(buf)) {
255 mutt_error _("You already have an alias defined with that name!");
259 alias_sanitize(buf, buf);
261 new->name = m_strdup(buf);
263 mutt_addrlist_to_local(adr);
265 m_strcpy(buf, sizeof(buf), adr->mailbox);
270 mutt_addrlist_to_idna(adr, NULL);
275 if (mutt_get_field(_("Address: "), buf, sizeof(buf), 0) || !buf[0]) {
276 alias_list_wipe(&new);
280 new->addr = rfc822_parse_adrlist(new->addr, buf);
284 if (mutt_addrlist_to_idna(new->addr, &err)) {
285 mutt_error(_("Error: '%s' is a bad IDN."), err);
290 } while (!new->addr);
292 if (adr && adr->personal && !mutt_is_mail_list(adr)) {
293 m_strcpy(buf, sizeof(buf), adr->personal);
298 if (mutt_get_field(_("Personal name: "), buf, sizeof(buf), 0)) {
299 alias_list_wipe(&new);
302 new->addr->personal = m_strdup(buf);
305 rfc822_addrcat(buf, sizeof(buf), new->addr, 1);
306 snprintf(prompt, sizeof(prompt), _("[%s = %s] Accept?"), new->name, buf);
307 if (mutt_yesorno(prompt, M_YES) != M_YES) {
308 alias_list_wipe(&new);
312 alias_list_push(&Aliases, new);
314 m_strcpy(buf, sizeof(buf), NONULL(AliasFile));
315 if (mutt_get_field(_("Save to file: "), buf, sizeof(buf), M_FILE)) {
319 mutt_expand_path(buf, sizeof(buf));
320 rc = safe_fopen (buf, "a");
323 if (alias_sanitize(new->name, NULL)) {
324 mutt_quote_filename(buf, sizeof(buf), new->name);
325 fprintf(rc, "alias %s ", buf);
327 fprintf(rc, "alias %s ", new->name);
331 rfc822_addrcat(buf, sizeof(buf), new->addr, 0);
332 write_safe_address(rc, buf);
334 mutt_message _("Alias added.");
340 static address_t *mutt_expand_aliases_r(address_t *a, string_list_t **expn)
342 address_t *pop, *head = NULL;
343 address_t **last = &head;
345 while ((pop = address_list_pop(&a))) {
346 if (!pop->group && !pop->personal
347 && pop->mailbox && !strchr(pop->mailbox, '@'))
349 const address_t *t = alias_lookup(pop->mailbox);
354 for (u = *expn; u; u = u->next) {
355 if (!m_strcmp(pop->mailbox, u->data)) { /* alias already found */
356 address_list_wipe(&pop);
361 /* save the fact we saw it */
362 u = string_item_new();
363 u->data = m_strdup(pop->mailbox);
366 address_list_wipe(&pop);
369 last = address_list_last(last);
370 *last = mutt_expand_aliases_r(address_list_dup(t), expn);
373 struct passwd *pw = getpwnam(pop->mailbox);
376 char namebuf[STRING];
377 mutt_gecos_name(namebuf, sizeof(namebuf), pw, GecosMask.rx);
378 m_strreplace(&pop->personal, namebuf);
383 last = address_list_append(last, pop);
386 if (option(OPTUSEDOMAIN)) {
387 /* now qualify all local addresses */
388 rfc822_qualify(head, mutt_fqdn(1));
394 address_t *mutt_expand_aliases(address_t *a)
397 string_list_t *expn = NULL; /* previously expanded aliases to avoid loops */
399 t = mutt_expand_aliases_r(a, &expn);
400 string_list_wipe(&expn);
401 address_list_uniq(t);
405 void mutt_expand_aliases_env(ENVELOPE *env)
407 env->from = mutt_expand_aliases(env->from);
408 env->to = mutt_expand_aliases(env->to);
409 env->cc = mutt_expand_aliases(env->cc);
410 env->bcc = mutt_expand_aliases(env->bcc);
411 env->reply_to = mutt_expand_aliases(env->reply_to);
412 env->mail_followup_to = mutt_expand_aliases(env->mail_followup_to);
415 /************* READ MARK *********************/
417 /* alias_complete() -- alias completion routine
419 * given a partial alias, this routine attempts to fill in the alias
420 * from the alias list as much as possible. if given empty search string
421 * or found nothing, present all aliases
423 int mutt_alias_complete (char *s, size_t buflen)
425 alias_t *a = Aliases;
426 alias_t *a_list = NULL, *a_cur = NULL;
427 char bestname[HUGE_STRING];
430 #define min(a,b) ((a<b)?a:b)
432 if (s[0] != 0) { /* avoid empty string as strstr argument */
433 p_clear(bestname, countof(bestname));
436 if (a->name && strstr (a->name, s) == a->name) {
437 if (!bestname[0]) /* init */
438 m_strcpy(bestname, MIN(m_strlen(a->name) + 1, ssizeof(bestname)),
441 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
448 if (bestname[0] != 0) {
449 if (m_strcmp(bestname, s) != 0) {
450 /* we are adding something to the completion */
451 m_strcpy(s, m_strlen(bestname) + 1, bestname);
455 /* build alias list and show it */
459 if (a->name && (strstr (a->name, s) == a->name)) {
460 if (!a_list) /* init */
461 a_cur = a_list = alias_new();
463 a_cur->next = alias_new();
475 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
476 if (bestname[0] != 0)
477 m_strcpy(s, buflen, bestname);
479 /* free the alias list */
482 a_list = a_list->next;
486 /* remove any aliases marked for deletion */
488 for (a_cur = Aliases; a_cur;) {
491 a_list->next = a_cur->next;
493 Aliases = a_cur->next;
496 alias_list_wipe(&a_cur);
513 alias_format_str(char *dest, ssize_t destlen, char op, const char *src,
514 const char *fmt, const char *ifstr __attribute__ ((unused)),
515 const char *elstr __attribute__ ((unused)),
516 anytype data, format_flag flags __attribute__ ((unused)))
518 char tmp[STRING], adr[STRING];
519 alias_t *alias = data.ptr;
523 m_strputc(dest, destlen, alias->del ? 'D' : ' ');
526 mutt_format_s(dest, destlen, fmt, alias->name);
530 rfc822_addrcat(adr, sizeof(adr), alias->addr, 1);
531 snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
532 snprintf(dest, destlen, tmp, adr);
535 snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
536 snprintf(dest, destlen, tmp, alias->num + 1);
539 m_strputc(dest, destlen, alias->tagged ? '*' : ' ');
546 static void alias_entry(char *s, ssize_t slen, MUTTMENU *m, int num)
548 m_strformat(s, slen, COLS - SW, MAlias.alias_format, alias_format_str,
549 ((alias_t **)m->data)[num],
550 option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0);
553 static int alias_tag (MUTTMENU * menu, int n, int m)
555 alias_t *cur = ((alias_t **) menu->data)[n];
556 int ot = cur->tagged;
558 cur->tagged = (m >= 0 ? m : !cur->tagged);
560 return cur->tagged - ot;
563 static int alias_SortAlias (const void *a, const void *b)
565 alias_t *pa = *(alias_t **) a;
566 alias_t *pb = *(alias_t **) b;
567 int r = m_strcasecmp(pa->name, pb->name);
572 static int alias_SortAddress (const void *a, const void *b)
574 address_t *pa = (*(alias_t **) a)->addr;
575 address_t *pb = (*(alias_t **) b)->addr;
584 else if (pa->personal) {
586 r = m_strcasecmp(pa->personal, pb->personal);
590 else if (pb->personal)
593 r = ascii_strcasecmp (pa->mailbox, pb->mailbox);
597 void mutt_alias_menu (char *buf, size_t buflen, alias_t * aliases)
601 alias_t **AliasTable = NULL;
605 char helpstr[STRING];
610 mutt_error _("You have no aliases!");
615 /* tell whoever called me to redraw the screen when I return */
616 set_option (OPTNEEDREDRAW);
618 menu = mutt_new_menu ();
619 menu->make_entry = alias_entry;
620 menu->tag = alias_tag;
621 menu->menu = MENU_ALIAS;
622 menu->title = _("Aliases");
623 menu->help = mutt_compile_help(helpstr, sizeof(helpstr),
624 MENU_ALIAS, AliasHelp);
630 /* count the number of aliases */
631 for (aliasp = aliases; aliasp; aliasp = aliasp->next) {
637 p_realloc(&AliasTable, menu->max);
638 menu->data = AliasTable;
640 for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) {
641 AliasTable[i] = aliasp;
645 if ((SortAlias & SORT_MASK) != SORT_ORDER) {
646 qsort (AliasTable, i, sizeof (alias_t *),
647 (SortAlias & SORT_MASK) ==
648 SORT_ADDRESS ? alias_SortAddress : alias_SortAlias);
651 for (i = 0; i < menu->max; i++)
652 AliasTable[i]->num = i;
656 menu->redraw |= REDRAW_FULL;
657 aliases = aliases->next;
661 switch ((op = mutt_menuLoop (menu))) {
664 if (menu->tagprefix) {
665 for (i = 0; i < menu->max; i++)
666 if (AliasTable[i]->tagged)
667 AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0;
668 menu->redraw |= REDRAW_INDEX;
671 AliasTable[menu->current]->del = (op == OP_DELETE) ? 1 : 0;
672 menu->redraw |= REDRAW_CURRENT;
673 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
675 menu->redraw |= REDRAW_INDEX;
679 case OP_GENERIC_SELECT_ENTRY:
687 for (i = 0; i < menu->max; i++) {
688 if (AliasTable[i]->tagged) {
689 mutt_addrlist_to_local (AliasTable[i]->addr);
690 rfc822_addrcat(buf, buflen, AliasTable[i]->addr, 0);
696 mutt_addrlist_to_local (AliasTable[t]->addr);
697 rfc822_addrcat(buf, buflen, AliasTable[t]->addr, 0);
700 mutt_menuDestroy (&menu);
701 p_delete(&AliasTable);