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_regex.h"
16 #include "mutt_curses.h"
17 #include "mutt_idna.h"
22 ADDRESS *mutt_lookup_alias (const char *s)
26 for (; t; t = t->next)
27 if (!mutt_strcasecmp (s, t->name))
29 return (NULL); /* no such alias */
32 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
34 ADDRESS *head = NULL, *last = NULL, *t, *w;
40 if (!a->group && !a->personal && a->mailbox
41 && strchr (a->mailbox, '@') == NULL) {
42 t = mutt_lookup_alias (a->mailbox);
46 for (u = *expn; u; u = u->next) {
47 if (mutt_strcmp (a->mailbox, u->data) == 0) { /* alias already found */
50 "mutt_expand_aliases_r(): loop in alias found for '%s'\n",
58 u = safe_malloc (sizeof (LIST));
59 u->data = safe_strdup (a->mailbox);
62 w = rfc822_cpy_adr (t);
63 w = mutt_expand_aliases_r (w, expn);
68 while (last && last->next)
74 rfc822_free_address (&t);
78 struct passwd *pw = getpwnam (a->mailbox);
83 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
84 mutt_str_replace (&a->personal, namebuf);
103 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
104 /* now qualify all local addresses */
105 rfc822_qualify (head, fqdn);
111 ADDRESS *mutt_expand_aliases (ADDRESS * a)
114 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
116 t = mutt_expand_aliases_r (a, &expn);
117 mutt_free_list (&expn);
118 return (mutt_remove_duplicates (t));
121 void mutt_expand_aliases_env (ENVELOPE * env)
123 env->from = mutt_expand_aliases (env->from);
124 env->to = mutt_expand_aliases (env->to);
125 env->cc = mutt_expand_aliases (env->cc);
126 env->bcc = mutt_expand_aliases (env->bcc);
127 env->reply_to = mutt_expand_aliases (env->reply_to);
128 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
133 * if someone has an address like
134 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
135 * and the user creates an alias for this, Mutt could wind up executing
136 * the backtics because it writes aliases like
137 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
138 * To avoid this problem, use a backslash (\) to quote any backtics. We also
139 * need to quote backslashes as well, since you could defeat the above by
141 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
142 * since that would get aliased as
143 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
144 * which still gets evaluated because the double backslash is not a quote.
146 * Additionally, we need to quote ' and " characters - otherwise, mutt will
147 * interpret them on the wrong parsing step.
149 * $ wants to be quoted since it may indicate the start of an environment
153 static void write_safe_address (FILE * fp, char *s)
156 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
163 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
168 if (mutt_addr_is_user (env->from)) {
169 if (env->to && !mutt_is_mail_list (env->to)) {
178 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
193 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
196 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
198 char fixed[LONG_STRING];
203 adr = mutt_get_address (cur, NULL);
209 if (adr && adr->mailbox) {
210 strfcpy (buf, adr->mailbox, sizeof (buf));
211 if ((pc = strchr (buf, '@')))
217 /* Don't suggest a bad alias name in the event of a strange local part. */
218 mutt_check_alias_name (buf, buf);
221 /* add a new alias */
222 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
225 /* check to see if the user already has an alias defined */
226 if (mutt_lookup_alias (buf)) {
227 mutt_error _("You already have an alias defined with that name!");
232 if (mutt_check_alias_name (buf, fixed)) {
234 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
236 strfcpy (buf, fixed, sizeof (buf));
243 new = safe_calloc (1, sizeof (ALIAS));
245 new->name = safe_strdup (buf);
247 mutt_addrlist_to_local (adr);
250 strfcpy (buf, adr->mailbox, sizeof (buf));
254 mutt_addrlist_to_idna (adr, NULL);
257 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
258 mutt_free_alias (&new);
262 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
264 if (mutt_addrlist_to_idna (new->addr, &err)) {
265 mutt_error (_("Error: '%s' is a bad IDN."), err);
270 while (new->addr == NULL);
272 if (adr && adr->personal && !mutt_is_mail_list (adr))
273 strfcpy (buf, adr->personal, sizeof (buf));
277 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
278 mutt_free_alias (&new);
281 new->addr->personal = safe_strdup (buf);
284 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
285 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
286 if (mutt_yesorno (prompt, M_YES) != M_YES) {
287 mutt_free_alias (&new);
299 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
300 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
302 mutt_expand_path (buf, sizeof (buf));
303 if ((rc = safe_fopen (buf, "a"))) {
304 if (mutt_check_alias_name (new->name, NULL))
305 mutt_quote_filename (buf, sizeof (buf), new->name);
307 strfcpy (buf, new->name, sizeof (buf));
308 fprintf (rc, "alias %s ", buf);
310 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
311 write_safe_address (rc, buf);
314 mutt_message _("Alias added.");
321 * Sanity-check an alias name: Only characters which are non-special to both
322 * the RFC 822 and the mutt configuration parser are permitted.
325 static int check_alias_name_char (char c)
327 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
328 isalnum ((unsigned char) c));
331 int mutt_check_alias_name (const char *s, char *d)
336 if (!check_alias_name_char (*s)) {
353 * This routine looks to see if the user has an alias defined for the given
356 ADDRESS *alias_reverse_lookup (ADDRESS * a)
361 if (!a || !a->mailbox)
364 for (; t; t = t->next) {
365 /* cycle through all addresses if this is a group alias */
366 for (ap = t->addr; ap; ap = ap->next) {
367 if (!ap->group && ap->mailbox &&
368 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
375 /* alias_complete() -- alias completion routine
377 * given a partial alias, this routine attempts to fill in the alias
378 * from the alias list as much as possible. if given empty search string
379 * or found nothing, present all aliases
381 int mutt_alias_complete (char *s, size_t buflen)
384 ALIAS *a_list = NULL, *a_cur = NULL;
385 char bestname[HUGE_STRING];
388 #define min(a,b) ((a<b)?a:b)
390 if (s[0] != 0) { /* avoid empty string as strstr argument */
391 memset (bestname, 0, sizeof (bestname));
394 if (a->name && strstr (a->name, s) == a->name) {
395 if (!bestname[0]) /* init */
396 strfcpy (bestname, a->name,
397 min (mutt_strlen (a->name) + 1, sizeof (bestname)));
399 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
406 if (bestname[0] != 0) {
407 if (mutt_strcmp (bestname, s) != 0) {
408 /* we are adding something to the completion */
409 strfcpy (s, bestname, mutt_strlen (bestname) + 1);
413 /* build alias list and show it */
417 if (a->name && (strstr (a->name, s) == a->name)) {
418 if (!a_list) /* init */
419 a_cur = a_list = (ALIAS *) safe_malloc (sizeof (ALIAS));
421 a_cur->next = (ALIAS *) safe_malloc (sizeof (ALIAS));
424 memcpy (a_cur, a, sizeof (ALIAS));
433 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
434 if (bestname[0] != 0)
435 strfcpy (s, bestname, buflen);
437 /* free the alias list */
440 a_list = a_list->next;
444 /* remove any aliases marked for deletion */
446 for (a_cur = Aliases; a_cur;) {
449 a_list->next = a_cur->next;
451 Aliases = a_cur->next;
454 mutt_free_alias (&a_cur);
470 static int string_is_address (const char *str, const char *u, const char *d)
472 char buf[LONG_STRING];
474 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
475 if (ascii_strcasecmp (str, buf) == 0)
481 /* returns TRUE if the given address belongs to the user. */
482 int mutt_addr_is_user (ADDRESS * addr)
484 /* NULL address is assumed to be the user. */
486 dprint (5, (debugfile, "mail_addr_is_user: yes, NULL address\n"));
489 if (!addr->mailbox) {
490 dprint (5, (debugfile, "mail_addr_is_user: no, no mailbox\n"));
494 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
496 (debugfile, "mail_addr_is_user: yes, %s = %s\n", addr->mailbox,
500 if (string_is_address (addr->mailbox, Username, Hostname)) {
502 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
503 addr->mailbox, Username, Hostname));
506 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
508 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
509 addr->mailbox, Username, mutt_fqdn (0)));
512 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
514 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
515 addr->mailbox, Username, mutt_fqdn (1)));
519 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
521 (debugfile, "mail_addr_is_user: yes, %s = %s\n", addr->mailbox,
526 if (mutt_match_rx_list (addr->mailbox, Alternates)) {
528 (debugfile, "mail_addr_is_user: yes, %s matched by alternates.\n",
530 if (mutt_match_rx_list (addr->mailbox, UnAlternates))
533 "mail_addr_is_user: but, %s matched by unalternates.\n",
539 dprint (5, (debugfile, "mail_addr_is_user: no, all failed.\n"));