2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 #include "mutt_regex.h"
25 #include "mutt_curses.h"
26 #include "mutt_idna.h"
31 ADDRESS *mutt_lookup_alias (const char *s)
35 for (; t; t = t->next)
36 if (!mutt_strcasecmp (s, t->name))
38 return (NULL); /* no such alias */
41 static ADDRESS *mutt_expand_aliases_r (ADDRESS * a, LIST ** expn)
43 ADDRESS *head = NULL, *last = NULL, *t, *w;
49 if (!a->group && !a->personal && a->mailbox
50 && strchr (a->mailbox, '@') == NULL) {
51 t = mutt_lookup_alias (a->mailbox);
55 for (u = *expn; u; u = u->next) {
56 if (mutt_strcmp (a->mailbox, u->data) == 0) { /* alias already found */
59 "mutt_expand_aliases_r(): loop in alias found for '%s'\n",
67 u = safe_malloc (sizeof (LIST));
68 u->data = safe_strdup (a->mailbox);
71 w = rfc822_cpy_adr (t);
72 w = mutt_expand_aliases_r (w, expn);
77 while (last && last->next)
83 rfc822_free_address (&t);
87 struct passwd *pw = getpwnam (a->mailbox);
92 mutt_gecos_name (namebuf, sizeof (namebuf), pw);
93 mutt_str_replace (&a->personal, namebuf);
112 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
113 /* now qualify all local addresses */
114 rfc822_qualify (head, fqdn);
120 ADDRESS *mutt_expand_aliases (ADDRESS * a)
123 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
125 t = mutt_expand_aliases_r (a, &expn);
126 mutt_free_list (&expn);
127 return (mutt_remove_duplicates (t));
130 void mutt_expand_aliases_env (ENVELOPE * env)
132 env->from = mutt_expand_aliases (env->from);
133 env->to = mutt_expand_aliases (env->to);
134 env->cc = mutt_expand_aliases (env->cc);
135 env->bcc = mutt_expand_aliases (env->bcc);
136 env->reply_to = mutt_expand_aliases (env->reply_to);
137 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
142 * if someone has an address like
143 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
144 * and the user creates an alias for this, Mutt could wind up executing
145 * the backtics because it writes aliases like
146 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
147 * To avoid this problem, use a backslash (\) to quote any backtics. We also
148 * need to quote backslashes as well, since you could defeat the above by
150 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
151 * since that would get aliased as
152 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
153 * which still gets evaluated because the double backslash is not a quote.
155 * Additionally, we need to quote ' and " characters - otherwise, mutt will
156 * interpret them on the wrong parsing step.
158 * $ wants to be quoted since it may indicate the start of an environment
162 static void write_safe_address (FILE * fp, char *s)
165 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
172 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
177 if (mutt_addr_is_user (env->from)) {
178 if (env->to && !mutt_is_mail_list (env->to)) {
187 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
202 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
205 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
207 char fixed[LONG_STRING];
212 adr = mutt_get_address (cur, NULL);
218 if (adr && adr->mailbox) {
219 strfcpy (buf, adr->mailbox, sizeof (buf));
220 if ((pc = strchr (buf, '@')))
226 /* Don't suggest a bad alias name in the event of a strange local part. */
227 mutt_check_alias_name (buf, buf);
230 /* add a new alias */
231 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
234 /* check to see if the user already has an alias defined */
235 if (mutt_lookup_alias (buf)) {
236 mutt_error _("You already have an alias defined with that name!");
241 if (mutt_check_alias_name (buf, fixed)) {
243 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
245 strfcpy (buf, fixed, sizeof (buf));
252 new = safe_calloc (1, sizeof (ALIAS));
254 new->name = safe_strdup (buf);
256 mutt_addrlist_to_local (adr);
259 strfcpy (buf, adr->mailbox, sizeof (buf));
263 mutt_addrlist_to_idna (adr, NULL);
266 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
267 mutt_free_alias (&new);
271 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
273 if (mutt_addrlist_to_idna (new->addr, &err)) {
274 mutt_error (_("Error: '%s' is a bad IDN."), err);
279 while (new->addr == NULL);
281 if (adr && adr->personal && !mutt_is_mail_list (adr))
282 strfcpy (buf, adr->personal, sizeof (buf));
286 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
287 mutt_free_alias (&new);
290 new->addr->personal = safe_strdup (buf);
293 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
294 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
295 if (mutt_yesorno (prompt, M_YES) != M_YES) {
296 mutt_free_alias (&new);
308 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
309 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
311 mutt_expand_path (buf, sizeof (buf));
312 if ((rc = safe_fopen (buf, "a"))) {
313 if (mutt_check_alias_name (new->name, NULL))
314 mutt_quote_filename (buf, sizeof (buf), new->name);
316 strfcpy (buf, new->name, sizeof (buf));
317 fprintf (rc, "alias %s ", buf);
319 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
320 write_safe_address (rc, buf);
323 mutt_message _("Alias added.");
330 * Sanity-check an alias name: Only characters which are non-special to both
331 * the RFC 822 and the mutt configuration parser are permitted.
334 static int check_alias_name_char (char c)
336 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
337 isalnum ((unsigned char) c));
340 int mutt_check_alias_name (const char *s, char *d)
345 if (!check_alias_name_char (*s)) {
362 * This routine looks to see if the user has an alias defined for the given
365 ADDRESS *alias_reverse_lookup (ADDRESS * a)
370 if (!a || !a->mailbox)
373 for (; t; t = t->next) {
374 /* cycle through all addresses if this is a group alias */
375 for (ap = t->addr; ap; ap = ap->next) {
376 if (!ap->group && ap->mailbox &&
377 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
384 /* alias_complete() -- alias completion routine
386 * given a partial alias, this routine attempts to fill in the alias
387 * from the alias list as much as possible. if given empty search string
388 * or found nothing, present all aliases
390 int mutt_alias_complete (char *s, size_t buflen)
393 ALIAS *a_list = NULL, *a_cur = NULL;
394 char bestname[HUGE_STRING];
397 #define min(a,b) ((a<b)?a:b)
399 if (s[0] != 0) { /* avoid empty string as strstr argument */
400 memset (bestname, 0, sizeof (bestname));
403 if (a->name && strstr (a->name, s) == a->name) {
404 if (!bestname[0]) /* init */
405 strfcpy (bestname, a->name,
406 min (mutt_strlen (a->name) + 1, sizeof (bestname)));
408 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
415 if (bestname[0] != 0) {
416 if (mutt_strcmp (bestname, s) != 0) {
417 /* we are adding something to the completion */
418 strfcpy (s, bestname, mutt_strlen (bestname) + 1);
422 /* build alias list and show it */
426 if (a->name && (strstr (a->name, s) == a->name)) {
427 if (!a_list) /* init */
428 a_cur = a_list = (ALIAS *) safe_malloc (sizeof (ALIAS));
430 a_cur->next = (ALIAS *) safe_malloc (sizeof (ALIAS));
433 memcpy (a_cur, a, sizeof (ALIAS));
442 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
443 if (bestname[0] != 0)
444 strfcpy (s, bestname, buflen);
446 /* free the alias list */
449 a_list = a_list->next;
453 /* remove any aliases marked for deletion */
455 for (a_cur = Aliases; a_cur;) {
458 a_list->next = a_cur->next;
460 Aliases = a_cur->next;
463 mutt_free_alias (&a_cur);
479 static int string_is_address (const char *str, const char *u, const char *d)
481 char buf[LONG_STRING];
483 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
484 if (ascii_strcasecmp (str, buf) == 0)
490 /* returns TRUE if the given address belongs to the user. */
491 int mutt_addr_is_user (ADDRESS * addr)
493 /* NULL address is assumed to be the user. */
495 dprint (5, (debugfile, "mail_addr_is_user: yes, NULL address\n"));
498 if (!addr->mailbox) {
499 dprint (5, (debugfile, "mail_addr_is_user: no, no mailbox\n"));
503 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
505 (debugfile, "mail_addr_is_user: yes, %s = %s\n", addr->mailbox,
509 if (string_is_address (addr->mailbox, Username, Hostname)) {
511 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
512 addr->mailbox, Username, Hostname));
515 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
517 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
518 addr->mailbox, Username, mutt_fqdn (0)));
521 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
523 (debugfile, "mail_addr_is_user: yes, %s = %s @ %s \n",
524 addr->mailbox, Username, mutt_fqdn (1)));
528 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
530 (debugfile, "mail_addr_is_user: yes, %s = %s\n", addr->mailbox,
535 if (mutt_match_rx_list (addr->mailbox, Alternates)) {
537 (debugfile, "mail_addr_is_user: yes, %s matched by alternates.\n",
539 if (mutt_match_rx_list (addr->mailbox, UnAlternates))
542 "mail_addr_is_user: but, %s matched by unalternates.\n",
548 dprint (5, (debugfile, "mail_addr_is_user: no, all failed.\n"));