2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
15 #include "mutt_curses.h"
16 #include "mutt_idna.h"
22 #include "lib/debug.h"
27 ADDRESS *mutt_lookup_alias (const char *s)
31 for (; t; t = t->next)
32 if (!safe_strcasecmp (s, t->name))
34 return (NULL); /* no such alias */
37 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
39 ADDRESS *head = NULL, *last = NULL, *t, *w;
45 if (!a->group && !a->personal && a->mailbox
46 && strchr (a->mailbox, '@') == NULL) {
47 t = mutt_lookup_alias (a->mailbox);
51 for (u = *expn; u; u = u->next) {
52 if (safe_strcmp (a->mailbox, u->data) == 0) { /* alias already found */
53 debug_print(1, ("loop in alias found for '%s'\n", a->mailbox));
60 u = safe_malloc (sizeof (LIST));
61 u->data = safe_strdup (a->mailbox);
64 w = rfc822_cpy_adr (t);
65 w = mutt_expand_aliases_r (w, expn);
70 while (last && last->next)
76 rfc822_free_address (&t);
80 struct passwd *pw = getpwnam (a->mailbox);
85 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
86 str_replace (&a->personal, namebuf);
101 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
102 /* now qualify all local addresses */
103 rfc822_qualify (head, fqdn);
109 ADDRESS *mutt_expand_aliases (ADDRESS * a)
112 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
114 t = mutt_expand_aliases_r (a, &expn);
115 mutt_free_list (&expn);
116 return (mutt_remove_duplicates (t));
119 void mutt_expand_aliases_env (ENVELOPE * env)
121 env->from = mutt_expand_aliases (env->from);
122 env->to = mutt_expand_aliases (env->to);
123 env->cc = mutt_expand_aliases (env->cc);
124 env->bcc = mutt_expand_aliases (env->bcc);
125 env->reply_to = mutt_expand_aliases (env->reply_to);
126 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
131 * if someone has an address like
132 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
133 * and the user creates an alias for this, Mutt could wind up executing
134 * the backtics because it writes aliases like
135 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
136 * To avoid this problem, use a backslash (\) to quote any backtics. We also
137 * need to quote backslashes as well, since you could defeat the above by
139 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
140 * since that would get aliased as
141 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
142 * which still gets evaluated because the double backslash is not a quote.
144 * Additionally, we need to quote ' and " characters - otherwise, mutt will
145 * interpret them on the wrong parsing step.
147 * $ wants to be quoted since it may indicate the start of an environment
151 static void write_safe_address (FILE * fp, char *s)
154 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
161 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
166 if (mutt_addr_is_user (env->from)) {
167 if (env->to && !mutt_is_mail_list (env->to)) {
176 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
191 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
194 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
196 char fixed[LONG_STRING];
201 adr = mutt_get_address (cur, NULL);
207 if (adr && adr->mailbox) {
208 strfcpy (buf, adr->mailbox, sizeof (buf));
209 if ((pc = strchr (buf, '@')))
215 /* Don't suggest a bad alias name in the event of a strange local part. */
216 mutt_check_alias_name (buf, buf);
219 /* add a new alias */
220 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
223 /* check to see if the user already has an alias defined */
224 if (mutt_lookup_alias (buf)) {
225 mutt_error _("You already have an alias defined with that name!");
230 if (mutt_check_alias_name (buf, fixed)) {
232 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
234 strfcpy (buf, fixed, sizeof (buf));
241 new = safe_calloc (1, sizeof (ALIAS));
243 new->name = safe_strdup (buf);
245 mutt_addrlist_to_local (adr);
248 strfcpy (buf, adr->mailbox, sizeof (buf));
252 mutt_addrlist_to_idna (adr, NULL);
255 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
256 mutt_free_alias (&new);
260 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
262 if (mutt_addrlist_to_idna (new->addr, &err)) {
263 mutt_error (_("Error: '%s' is a bad IDN."), err);
268 while (new->addr == NULL);
270 if (adr && adr->personal && !mutt_is_mail_list (adr))
271 strfcpy (buf, adr->personal, sizeof (buf));
275 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
276 mutt_free_alias (&new);
279 new->addr->personal = safe_strdup (buf);
282 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
283 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
284 if (mutt_yesorno (prompt, M_YES) != M_YES) {
285 mutt_free_alias (&new);
297 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
298 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
300 mutt_expand_path (buf, sizeof (buf));
301 if ((rc = safe_fopen (buf, "a"))) {
302 if (mutt_check_alias_name (new->name, NULL))
303 mutt_quote_filename (buf, sizeof (buf), new->name);
305 strfcpy (buf, new->name, sizeof (buf));
306 fprintf (rc, "alias %s ", buf);
308 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
309 write_safe_address (rc, buf);
312 mutt_message _("Alias added.");
319 * Sanity-check an alias name: Only characters which are non-special to both
320 * the RFC 822 and the mutt configuration parser are permitted.
323 static int check_alias_name_char (char c)
325 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
326 isalnum ((unsigned char) c));
329 int mutt_check_alias_name (const char *s, char *d)
334 if (!check_alias_name_char (*s)) {
351 * This routine looks to see if the user has an alias defined for the given
354 ADDRESS *alias_reverse_lookup (ADDRESS * a)
359 if (!a || !a->mailbox)
362 for (; t; t = t->next) {
363 /* cycle through all addresses if this is a group alias */
364 for (ap = t->addr; ap; ap = ap->next) {
365 if (!ap->group && ap->mailbox &&
366 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
373 /* alias_complete() -- alias completion routine
375 * given a partial alias, this routine attempts to fill in the alias
376 * from the alias list as much as possible. if given empty search string
377 * or found nothing, present all aliases
379 int mutt_alias_complete (char *s, size_t buflen)
382 ALIAS *a_list = NULL, *a_cur = NULL;
383 char bestname[HUGE_STRING];
386 #define min(a,b) ((a<b)?a:b)
388 if (s[0] != 0) { /* avoid empty string as strstr argument */
389 memset (bestname, 0, sizeof (bestname));
392 if (a->name && strstr (a->name, s) == a->name) {
393 if (!bestname[0]) /* init */
394 strfcpy (bestname, a->name,
395 min (safe_strlen (a->name) + 1, sizeof (bestname)));
397 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
404 if (bestname[0] != 0) {
405 if (safe_strcmp (bestname, s) != 0) {
406 /* we are adding something to the completion */
407 strfcpy (s, bestname, safe_strlen (bestname) + 1);
411 /* build alias list and show it */
415 if (a->name && (strstr (a->name, s) == a->name)) {
416 if (!a_list) /* init */
417 a_cur = a_list = (ALIAS *) safe_malloc (sizeof (ALIAS));
419 a_cur->next = (ALIAS *) safe_malloc (sizeof (ALIAS));
422 memcpy (a_cur, a, sizeof (ALIAS));
431 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
432 if (bestname[0] != 0)
433 strfcpy (s, bestname, buflen);
435 /* free the alias list */
438 a_list = a_list->next;
442 /* remove any aliases marked for deletion */
444 for (a_cur = Aliases; a_cur;) {
447 a_list->next = a_cur->next;
449 Aliases = a_cur->next;
452 mutt_free_alias (&a_cur);
468 static int string_is_address (const char *str, const char *u, const char *d)
470 char buf[LONG_STRING];
472 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
473 if (ascii_strcasecmp (str, buf) == 0)
479 /* returns TRUE if the given address belongs to the user. */
480 int mutt_addr_is_user (ADDRESS * addr)
482 /* NULL address is assumed to be the user. */
484 debug_print(5, ("yes, NULL address\n"));
487 if (!addr->mailbox) {
488 debug_print(5, ("no, no mailbox\n"));
492 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
493 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
496 if (string_is_address (addr->mailbox, Username, Hostname)) {
497 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
500 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
501 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
504 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
505 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
509 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
510 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
514 if (rx_list_match (Alternates, addr->mailbox)) {
515 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
516 if (rx_list_match (UnAlternates, addr->mailbox))
517 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
522 debug_print(5, ("no, all failed.\n"));