alias.c 10.3 KB
Newer Older
Thomas Roessler's avatar
Thomas Roessler committed
1
/*
2
 * Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
Thomas Roessler's avatar
Thomas Roessler committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */ 

#include "mutt.h"
#include "mutt_regex.h"
Thomas Roessler's avatar
Thomas Roessler committed
21
#include "mutt_curses.h"
Thomas Roessler's avatar
Thomas Roessler committed
22 23 24 25

#include <pwd.h>
#include <string.h>

26
ADDRESS *mutt_lookup_alias (const char *s)
Thomas Roessler's avatar
Thomas Roessler committed
27 28 29 30
{
  ALIAS *t = Aliases;

  for (; t; t = t->next)
31
    if (!mutt_strcasecmp (s, t->name))
Thomas Roessler's avatar
Thomas Roessler committed
32 33 34 35 36 37 38 39 40
      return (t->addr);
  return (NULL);   /* no such alias */
}

static ADDRESS *mutt_expand_aliases_r (ADDRESS *a, LIST **expn)
{
  ADDRESS *head = NULL, *last = NULL, *t, *w;
  LIST *u;
  char i;
Thomas Roessler's avatar
Thomas Roessler committed
41 42
  const char *fqdn;
  
Thomas Roessler's avatar
Thomas Roessler committed
43 44 45 46
  while (a)
  {
    if (!a->group && !a->personal && a->mailbox && strchr (a->mailbox, '@') == NULL)
    {
47
      t = mutt_lookup_alias (a->mailbox);
Thomas Roessler's avatar
Thomas Roessler committed
48 49 50 51 52 53

      if (t)
      {	
        i = 0;
        for (u = *expn; u; u = u->next)
	{
54
	  if (mutt_strcmp (a->mailbox, u->data) == 0) /* alias already found */
Thomas Roessler's avatar
Thomas Roessler committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	  {
	    dprint (1, (debugfile, "mutt_expand_aliases_r(): loop in alias found for '%s'\n", a->mailbox));
	    i = 1;
	    break;
	  }
	}

        if (!i)
	{
          u = safe_malloc (sizeof (LIST));
          u->data = safe_strdup (a->mailbox);
          u->next = *expn;
          *expn = u;
	  w = rfc822_cpy_adr (t);
	  w = mutt_expand_aliases_r (w, expn);
	  if (head)
	    last->next = w;
	  else
	    head = last = w;
	  while (last && last->next)
	    last = last->next;
        }
	t = a;
	a = a->next;
	t->next = NULL;
	rfc822_free_address (&t);
	continue;
      }
      else
      {
	struct passwd *pw = getpwnam (a->mailbox);

	if (pw)
	{
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
           regmatch_t pat_match[1];

           /* Use regular expression to parse Gecos field.  This result of the
            * parsing will be used as the personal ID string when the alias is
            * expaned.
            */
	  if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
	  {
	    /* Malloc enough for the matching pattern + terminating NULL */
	    a->personal = safe_malloc ((pat_match[0].rm_eo - 
					pat_match[0].rm_so) + 1);
	    
	    strfcpy (a->personal, pw->pw_gecos + pat_match[0].rm_so, 
		     pat_match[0].rm_eo - pat_match[0].rm_so + 1);
	  }

Thomas Roessler's avatar
Thomas Roessler committed
105
#ifdef EXACT_ADDRESS
Thomas Roessler's avatar
Thomas Roessler committed
106
	  FREE (&a->val);
Thomas Roessler's avatar
Thomas Roessler committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#endif
	}
      }
    }

    if (head)
    {
      last->next = a;
      last = last->next;
    }
    else
      head = last = a;
    a = a->next;
    last->next = NULL;
  }

Thomas Roessler's avatar
Thomas Roessler committed
123
  if (option (OPTUSEDOMAIN) && (fqdn = mutt_fqdn(1)))
Thomas Roessler's avatar
Thomas Roessler committed
124 125
  {
    /* now qualify all local addresses */
Thomas Roessler's avatar
Thomas Roessler committed
126
    rfc822_qualify (head, fqdn);
Thomas Roessler's avatar
Thomas Roessler committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  }

  return (head);
}

ADDRESS *mutt_expand_aliases (ADDRESS *a)
{
  ADDRESS *t;
  LIST *expn = NULL; /* previously expanded aliases to avoid loops */

  t = mutt_expand_aliases_r (a, &expn);
  mutt_free_list (&expn);
  return (t);
}

142 143
/* 
 * if someone has an address like
Thomas Roessler's avatar
Thomas Roessler committed
144 145 146 147 148 149 150 151 152 153 154
 *	From: Michael `/bin/rm -f ~` Elkins <me@cs.hmc.edu>
 * and the user creates an alias for this, Mutt could wind up executing
 * the backtics because it writes aliases like
 *	alias me Michael `/bin/rm -f ~` Elkins <me@cs.hmc.edu>
 * To avoid this problem, use a backslash (\) to quote any backtics.  We also
 * need to quote backslashes as well, since you could defeat the above by
 * doing
 *	From: Michael \`/bin/rm -f ~\` Elkins <me@cs.hmc.edu>
 * since that would get aliased as
 *	alias me Michael \\`/bin/rm -f ~\\` Elkins <me@cs.hmc.edu>
 * which still gets evaluated because the double backslash is not a quote.
155 156 157 158 159 160
 * 
 * Additionally, we need to quote ' and " characters - otherwise, mutt will
 * interpret them on the wrong parsing step.
 * 
 * $ wants to be quoted since it may indicate the start of an environment
 * variable.
Thomas Roessler's avatar
Thomas Roessler committed
161
 */
162

Thomas Roessler's avatar
Thomas Roessler committed
163 164 165 166
static void write_safe_address (FILE *fp, char *s)
{
  while (*s)
  {
167 168
    if (*s == '\\' || *s == '`' || *s == '\'' || *s == '"'
	|| *s == '$')
Thomas Roessler's avatar
Thomas Roessler committed
169 170 171 172 173 174
      fputc ('\\', fp);
    fputc (*s, fp);
    s++;
  }
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
ADDRESS *mutt_get_address (ENVELOPE *env, char **pfxp)
{
  ADDRESS *adr;
  char *pfx = NULL;

  if (mutt_addr_is_user (env->from))
  {
    if (env->to && !mutt_is_mail_list (env->to))
    {
      pfx = "To";
      adr = env->to;
    }
    else
    {
      pfx = "Cc";
      adr = env->cc;
    }
  }
  else if (env->reply_to && !mutt_is_mail_list (env->reply_to))
  {
    pfx = "Reply-To";
    adr = env->reply_to;
  }
  else
  {
    adr = env->from;
    pfx = "From";
  }

  if (pfxp) *pfxp = pfx;

  return adr;
}

Thomas Roessler's avatar
Thomas Roessler committed
209 210 211 212 213 214 215 216 217
void mutt_create_alias (ENVELOPE *cur, ADDRESS *iadr)
{
  ALIAS *new, *t;
  char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
  FILE *rc;
  ADDRESS *adr = NULL;

  if (cur)
  {
218
    adr = mutt_get_address (cur, NULL);
Thomas Roessler's avatar
Thomas Roessler committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
  }
  else if (iadr)
  {
    adr = iadr;
  }

  if (adr && adr->mailbox)
  {
    strfcpy (buf, adr->mailbox, sizeof (buf));
    if ((pc = strchr (buf, '@')))
      *pc = 0;
  }
  else
    buf[0] = '\0';
  
234 235
  /* add a new alias */
  if (mutt_get_field (_("Alias as: "), buf, sizeof (buf), 0) != 0 || !buf[0])
Thomas Roessler's avatar
Thomas Roessler committed
236 237 238
    return;

  /* check to see if the user already has an alias defined */
239
  if (mutt_lookup_alias (buf))
Thomas Roessler's avatar
Thomas Roessler committed
240
  {
241
    mutt_error _("You already have an alias defined with that name!");
Thomas Roessler's avatar
Thomas Roessler committed
242 243 244 245 246 247 248 249 250 251 252
    return;
  }

  new = safe_calloc (1, sizeof (ALIAS));
  new->name = safe_strdup (buf);

  if (adr)
    strfcpy (buf, adr->mailbox, sizeof (buf));
  else
    buf[0] = 0;

Thomas Roessler's avatar
Thomas Roessler committed
253
  do
Thomas Roessler's avatar
Thomas Roessler committed
254
  {
255
    if (mutt_get_field (_("Address: "), buf, sizeof (buf), 0) != 0 || !buf[0])
Thomas Roessler's avatar
Thomas Roessler committed
256 257 258 259 260 261 262
    {
      mutt_free_alias (&new);
      return;
    }
    
    if((new->addr = rfc822_parse_adrlist (new->addr, buf)) == NULL)
      BEEP ();
Thomas Roessler's avatar
Thomas Roessler committed
263
  }
Thomas Roessler's avatar
Thomas Roessler committed
264 265
  while(new->addr == NULL);
  
Thomas Roessler's avatar
Thomas Roessler committed
266 267 268 269 270
  if (adr && adr->personal && !mutt_is_mail_list (adr))
    strfcpy (buf, adr->personal, sizeof (buf));
  else
    buf[0] = 0;

271
  if (mutt_get_field (_("Personal name: "), buf, sizeof (buf), 0) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
272 273 274 275 276 277 278 279
  {
    mutt_free_alias (&new);
    return;
  }
  new->addr->personal = safe_strdup (buf);

  buf[0] = 0;
  rfc822_write_address (buf, sizeof (buf), new->addr);
280
  snprintf (prompt, sizeof (prompt), _("[%s = %s] Accept?"), new->name, buf);
Thomas Roessler's avatar
Thomas Roessler committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  if (mutt_yesorno (prompt, 1) != 1)
  {
    mutt_free_alias (&new);
    return;
  }

  if ((t = Aliases))
  {
    while (t->next)
      t = t->next;
    t->next = new;
  }
  else
    Aliases = new;

  strfcpy (buf, NONULL (AliasFile), sizeof (buf));
297
  if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
Thomas Roessler's avatar
Thomas Roessler committed
298 299 300 301 302 303 304 305 306 307
    return;
  mutt_expand_path (buf, sizeof (buf));
  if ((rc = fopen (buf, "a")))
  {
    buf[0] = 0;
    rfc822_write_address (buf, sizeof (buf), new->addr);
    fprintf (rc, "alias %s ", new->name);
    write_safe_address (rc, buf);
    fputc ('\n', rc);
    fclose (rc);
308
    mutt_message _("Alias added.");
Thomas Roessler's avatar
Thomas Roessler committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  }
  else
    mutt_perror (buf);
}

/*
 * This routine looks to see if the user has an alias defined for the given
 * address.
 */
ADDRESS *alias_reverse_lookup (ADDRESS *a)
{
  ALIAS *t = Aliases;
  ADDRESS *ap;

  if (!a || !a->mailbox)
    return NULL;

  for (; t; t = t->next)
  {
    /* cycle through all addresses if this is a group alias */
    for (ap = t->addr; ap; ap = ap->next)
    {
      if (!ap->group && ap->mailbox &&
332
	  mutt_strcasecmp (ap->mailbox, a->mailbox) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
333 334 335 336 337 338 339 340 341
	return ap;
    }
  }
  return 0;
}

/* alias_complete() -- alias completion routine
 *
 * given a partial alias, this routine attempts to fill in the alias
342 343
 * from the alias list as much as possible. if given empty search string
 * or found nothing, present all aliases
Thomas Roessler's avatar
Thomas Roessler committed
344 345 346 347 348
 */
int mutt_alias_complete (char *s, size_t buflen)
{
  ALIAS *a = Aliases;
  ALIAS *a_list = NULL, *a_cur = NULL;
349
  char bestname[HUGE_STRING];
Thomas Roessler's avatar
Thomas Roessler committed
350 351 352 353
  int i;

#define min(a,b)        ((a<b)?a:b)

354
  if (s[0] != 0) /* avoid empty string as strstr argument */
Thomas Roessler's avatar
Thomas Roessler committed
355
  {
356 357 358
    memset (bestname, 0, sizeof (bestname));

    while (a)
Thomas Roessler's avatar
Thomas Roessler committed
359
    {
360
      if (a->name && strstr (a->name, s) == a->name)
Thomas Roessler's avatar
Thomas Roessler committed
361
      {
362 363 364 365 366 367 368 369 370
	if (!bestname[0]) /* init */
	  strfcpy (bestname, a->name,
		   min (mutt_strlen (a->name) + 1, sizeof (bestname)));
	else
	{
	  for (i = 0 ; a->name[i] && a->name[i] == bestname[i] ; i++)
	    ;
	  bestname[i] = 0;
	}
Thomas Roessler's avatar
Thomas Roessler committed
371
      }
372
      a = a->next;
Thomas Roessler's avatar
Thomas Roessler committed
373 374
    }

375
    if (bestname[0] != 0)
Thomas Roessler's avatar
Thomas Roessler committed
376
    {
377 378 379 380 381 382 383
      if (mutt_strcmp (bestname, s) != 0)
      {
	/* we are adding something to the completion */
	strfcpy (s, bestname, mutt_strlen (bestname) + 1);
	return 1;
      }

Thomas Roessler's avatar
Thomas Roessler committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
      /* build alias list and show it */
      a = Aliases;
      while (a)
      {
	if (a->name && (strstr (a->name, s) == a->name))
	{
	  if (!a_list)  /* init */
	    a_cur = a_list = (ALIAS *) safe_malloc (sizeof (ALIAS));
	  else
	  {
	    a_cur->next = (ALIAS *) safe_malloc (sizeof (ALIAS));
	    a_cur = a_cur->next;
	  }
	  memcpy (a_cur, a, sizeof (ALIAS));
	  a_cur->next = NULL;
	}
	a = a->next;
      }
402 403
    }
  }
Thomas Roessler's avatar
Thomas Roessler committed
404

405 406 407 408
  bestname[0] = 0;
  mutt_alias_menu (bestname, sizeof(bestname), a_list ? a_list : Aliases);
  if (bestname[0] != 0)
    strfcpy (s, bestname, buflen);
Thomas Roessler's avatar
Thomas Roessler committed
409

410 411 412 413 414 415
  /* free the alias list */
  while (a_list)
  {
    a_cur = a_list;
    a_list = a_list->next;
    safe_free ((void **) &a_cur);
Thomas Roessler's avatar
Thomas Roessler committed
416 417
  }

418
  return 0;
Thomas Roessler's avatar
Thomas Roessler committed
419 420
}

Thomas Roessler's avatar
Thomas Roessler committed
421
static int string_is_address(const char *str, const char *u, const char *d)
Thomas Roessler's avatar
Thomas Roessler committed
422 423
{
  char buf[LONG_STRING];
Thomas Roessler's avatar
Thomas Roessler committed
424 425
  
  snprintf(buf, sizeof(buf), "%s@%s", NONULL(u), NONULL(d));
426
  if (mutt_strcasecmp(str, buf) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
427 428 429 430
    return 1;
  
  return 0;
}
Thomas Roessler's avatar
Thomas Roessler committed
431

Thomas Roessler's avatar
Thomas Roessler committed
432 433 434
/* returns TRUE if the given address belongs to the user. */
int mutt_addr_is_user (ADDRESS *addr)
{
Thomas Roessler's avatar
Thomas Roessler committed
435 436 437 438 439
  /* NULL address is assumed to be the user. */
  if (!addr)
    return 1;
  if (!addr->mailbox)
    return 0;
Thomas Roessler's avatar
Thomas Roessler committed
440

441
  if (mutt_strcasecmp (addr->mailbox, Username) == 0)
Thomas Roessler's avatar
Thomas Roessler committed
442
    return 1;
443
  if (string_is_address(addr->mailbox, Username, Hostname))
Thomas Roessler's avatar
Thomas Roessler committed
444
    return 1;
445
  if (string_is_address(addr->mailbox, Username, mutt_fqdn(0)))
Thomas Roessler's avatar
Thomas Roessler committed
446
    return 1;
447 448 449 450
  if (string_is_address(addr->mailbox, Username, mutt_fqdn(1)))
    return 1;

  if (From && !mutt_strcasecmp (From->mailbox, addr->mailbox))
Thomas Roessler's avatar
Thomas Roessler committed
451 452 453 454 455
    return 1;

  if (Alternates.pattern &&
      regexec (Alternates.rx, addr->mailbox, 0, NULL, 0) == 0)
    return 1;
Thomas Roessler's avatar
Thomas Roessler committed
456
  
Thomas Roessler's avatar
Thomas Roessler committed
457 458
  return 0;
}