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);
105 if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn (1))) {
106 /* now qualify all local addresses */
107 rfc822_qualify (head, fqdn);
113 ADDRESS *mutt_expand_aliases (ADDRESS * a)
116 LIST *expn = NULL; /* previously expanded aliases to avoid loops */
118 t = mutt_expand_aliases_r (a, &expn);
119 mutt_free_list (&expn);
120 return (mutt_remove_duplicates (t));
123 void mutt_expand_aliases_env (ENVELOPE * env)
125 env->from = mutt_expand_aliases (env->from);
126 env->to = mutt_expand_aliases (env->to);
127 env->cc = mutt_expand_aliases (env->cc);
128 env->bcc = mutt_expand_aliases (env->bcc);
129 env->reply_to = mutt_expand_aliases (env->reply_to);
130 env->mail_followup_to = mutt_expand_aliases (env->mail_followup_to);
135 * if someone has an address like
136 * From: Michael `/bin/rm -f ~` Elkins <me@mutt.org>
137 * and the user creates an alias for this, Mutt could wind up executing
138 * the backtics because it writes aliases like
139 * alias me Michael `/bin/rm -f ~` Elkins <me@mutt.org>
140 * To avoid this problem, use a backslash (\) to quote any backtics. We also
141 * need to quote backslashes as well, since you could defeat the above by
143 * From: Michael \`/bin/rm -f ~\` Elkins <me@mutt.org>
144 * since that would get aliased as
145 * alias me Michael \\`/bin/rm -f ~\\` Elkins <me@mutt.org>
146 * which still gets evaluated because the double backslash is not a quote.
148 * Additionally, we need to quote ' and " characters - otherwise, mutt will
149 * interpret them on the wrong parsing step.
151 * $ wants to be quoted since it may indicate the start of an environment
155 static void write_safe_address (FILE * fp, char *s)
158 if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"' || *s == '$')
165 ADDRESS *mutt_get_address (ENVELOPE * env, char **pfxp)
170 if (mutt_addr_is_user (env->from)) {
171 if (env->to && !mutt_is_mail_list (env->to)) {
180 else if (env->reply_to && !mutt_is_mail_list (env->reply_to)) {
195 void mutt_create_alias (ENVELOPE * cur, ADDRESS * iadr)
198 char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
200 char fixed[LONG_STRING];
205 adr = mutt_get_address (cur, NULL);
211 if (adr && adr->mailbox) {
212 strfcpy (buf, adr->mailbox, sizeof (buf));
213 if ((pc = strchr (buf, '@')))
219 /* Don't suggest a bad alias name in the event of a strange local part. */
220 mutt_check_alias_name (buf, buf);
223 /* add a new alias */
224 if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
227 /* check to see if the user already has an alias defined */
228 if (mutt_lookup_alias (buf)) {
229 mutt_error _("You already have an alias defined with that name!");
234 if (mutt_check_alias_name (buf, fixed)) {
236 (_("Warning: This alias name may not work. Fix it?"), M_YES)) {
238 strfcpy (buf, fixed, sizeof (buf));
245 new = safe_calloc (1, sizeof (ALIAS));
247 new->name = safe_strdup (buf);
249 mutt_addrlist_to_local (adr);
252 strfcpy (buf, adr->mailbox, sizeof (buf));
256 mutt_addrlist_to_idna (adr, NULL);
259 if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0]) {
260 mutt_free_alias (&new);
264 if ((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
266 if (mutt_addrlist_to_idna (new->addr, &err)) {
267 mutt_error (_("Error: '%s' is a bad IDN."), err);
272 while (new->addr == NULL);
274 if (adr && adr->personal && !mutt_is_mail_list (adr))
275 strfcpy (buf, adr->personal, sizeof (buf));
279 if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0) {
280 mutt_free_alias (&new);
283 new->addr->personal = safe_strdup (buf);
286 rfc822_write_address (buf, sizeof (buf), new->addr, 1);
287 snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
288 if (mutt_yesorno (prompt, M_YES) != M_YES) {
289 mutt_free_alias (&new);
301 strfcpy (buf, NONULL (AliasFile), sizeof (buf));
302 if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
304 mutt_expand_path (buf, sizeof (buf));
305 if ((rc = safe_fopen (buf, "a"))) {
306 if (mutt_check_alias_name (new->name, NULL))
307 mutt_quote_filename (buf, sizeof (buf), new->name);
309 strfcpy (buf, new->name, sizeof (buf));
310 fprintf (rc, "alias %s ", buf);
312 rfc822_write_address (buf, sizeof (buf), new->addr, 0);
313 write_safe_address (rc, buf);
316 mutt_message _("Alias added.");
323 * Sanity-check an alias name: Only characters which are non-special to both
324 * the RFC 822 and the mutt configuration parser are permitted.
327 static int check_alias_name_char (char c)
329 return (c == '-' || c == '_' || c == '+' || c == '=' || c == '.' ||
330 isalnum ((unsigned char) c));
333 int mutt_check_alias_name (const char *s, char *d)
338 if (!check_alias_name_char (*s)) {
355 * This routine looks to see if the user has an alias defined for the given
358 ADDRESS *alias_reverse_lookup (ADDRESS * a)
363 if (!a || !a->mailbox)
366 for (; t; t = t->next) {
367 /* cycle through all addresses if this is a group alias */
368 for (ap = t->addr; ap; ap = ap->next) {
369 if (!ap->group && ap->mailbox &&
370 ascii_strcasecmp (ap->mailbox, a->mailbox) == 0)
377 /* alias_complete() -- alias completion routine
379 * given a partial alias, this routine attempts to fill in the alias
380 * from the alias list as much as possible. if given empty search string
381 * or found nothing, present all aliases
383 int mutt_alias_complete (char *s, size_t buflen)
386 ALIAS *a_list = NULL, *a_cur = NULL;
387 char bestname[HUGE_STRING];
390 #define min(a,b) ((a<b)?a:b)
392 if (s[0] != 0) { /* avoid empty string as strstr argument */
393 memset (bestname, 0, sizeof (bestname));
396 if (a->name && strstr (a->name, s) == a->name) {
397 if (!bestname[0]) /* init */
398 strfcpy (bestname, a->name,
399 min (safe_strlen (a->name) + 1, sizeof (bestname)));
401 for (i = 0; a->name[i] && a->name[i] == bestname[i]; i++);
408 if (bestname[0] != 0) {
409 if (safe_strcmp (bestname, s) != 0) {
410 /* we are adding something to the completion */
411 strfcpy (s, bestname, safe_strlen (bestname) + 1);
415 /* build alias list and show it */
419 if (a->name && (strstr (a->name, s) == a->name)) {
420 if (!a_list) /* init */
421 a_cur = a_list = (ALIAS *) safe_malloc (sizeof (ALIAS));
423 a_cur->next = (ALIAS *) safe_malloc (sizeof (ALIAS));
426 memcpy (a_cur, a, sizeof (ALIAS));
435 mutt_alias_menu (bestname, sizeof (bestname), a_list ? a_list : Aliases);
436 if (bestname[0] != 0)
437 strfcpy (s, bestname, buflen);
439 /* free the alias list */
442 a_list = a_list->next;
446 /* remove any aliases marked for deletion */
448 for (a_cur = Aliases; a_cur;) {
451 a_list->next = a_cur->next;
453 Aliases = a_cur->next;
456 mutt_free_alias (&a_cur);
472 static int string_is_address (const char *str, const char *u, const char *d)
474 char buf[LONG_STRING];
476 snprintf (buf, sizeof (buf), "%s@%s", NONULL (u), NONULL (d));
477 if (ascii_strcasecmp (str, buf) == 0)
483 /* returns TRUE if the given address belongs to the user. */
484 int mutt_addr_is_user (ADDRESS * addr)
486 /* NULL address is assumed to be the user. */
488 debug_print(5, ("yes, NULL address\n"));
491 if (!addr->mailbox) {
492 debug_print(5, ("no, no mailbox\n"));
496 if (ascii_strcasecmp (addr->mailbox, Username) == 0) {
497 debug_print(5, ("yes, %s = %s\n", addr->mailbox, Username));
500 if (string_is_address (addr->mailbox, Username, Hostname)) {
501 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, Hostname));
504 if (string_is_address (addr->mailbox, Username, mutt_fqdn (0))) {
505 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (0)));
508 if (string_is_address (addr->mailbox, Username, mutt_fqdn (1))) {
509 debug_print(5, ("yes, %s = %s @ %s \n", addr->mailbox, Username, mutt_fqdn (1)));
513 if (From && !ascii_strcasecmp (From->mailbox, addr->mailbox)) {
514 debug_print(5, ("yes, %s = %s\n", addr->mailbox, From->mailbox));
518 if (rx_list_match (Alternates, addr->mailbox)) {
519 debug_print(5, ("yes, %s matched by alternates.\n", addr->mailbox));
520 if (rx_list_match (UnAlternates, addr->mailbox))
521 debug_print(5, ("but, %s matched by unalternates.\n", addr->mailbox));
526 debug_print(5, ("no, all failed.\n"));