File: alias.c

package info (click to toggle)
mutt 0.91.2-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,056 kB
  • ctags: 2,189
  • sloc: ansic: 34,582; sh: 203; makefile: 197; perl: 21; awk: 2
file content (397 lines) | stat: -rw-r--r-- 9,192 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
 * 
 *     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"

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

static ADDRESS *lookup_alias (const char *s)
{
  ALIAS *t = Aliases;

  for (; t; t = t->next)
    if (!strcasecmp (s, t->name))
      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;

  while (a)
  {
    if (!a->group && !a->personal && a->mailbox && strchr (a->mailbox, '@') == NULL)
    {
      t = lookup_alias (a->mailbox);

      if (t)
      {	
        i = 0;
        for (u = *expn; u; u = u->next)
	{
	  if (strcmp (a->mailbox, u->data) == 0) /* alias already found */
	  {
	    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);
	char buffer[256], *p;

	if (pw)
	{
	  strfcpy (buffer, pw->pw_gecos, sizeof (buffer));
	  if ((p = strchr (buffer, ',')))
	    *p = 0;
	  a->personal = safe_strdup (buffer);
#ifdef EXACT_ADDRESS
	  free (a->val);
	  a->val = NULL;
#endif
	}
      }
    }

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

  if (option (OPTUSEDOMAIN) && Fqdn[0] != '@')
  {
    /* now qualify all local addresses */
    rfc822_qualify (head, Fqdn);
  }

  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);
}

/* if someone has an address like
 *	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.
 */
static void write_safe_address (FILE *fp, char *s)
{
  while (*s)
  {
    if (*s == '\\' || *s == '`')
      fputc ('\\', fp);
    fputc (*s, fp);
    s++;
  }
}

void mutt_create_alias (ENVELOPE *cur)
{
  ALIAS *new, *t;
  char buf[LONG_STRING], prompt[SHORT_STRING], *pc;
  FILE *rc;
  ADDRESS *adr = NULL;

  if (cur)
  {
    if (mutt_addr_is_user (cur->from))
    {
      if (cur->to && !mutt_is_mail_list (cur->to))
	adr = cur->to;
      else
	adr = cur->cc;
    }
    else if (cur->reply_to && !mutt_is_mail_list (cur->reply_to))
      adr = cur->reply_to;
    else
      adr = cur->from;

  }

  if (adr && adr->mailbox)
  {
    strfcpy (buf, adr->mailbox, sizeof (buf));
    if ((pc = strchr (buf, '@')))
      *pc = 0;
  }
  else
    buf[0] = '\0';
  
  if (mutt_get_field ("Alias as: ", buf, sizeof (buf), 0) != 0 || !buf[0])
    return;

  /* check to see if the user already has an alias defined */
  if (lookup_alias (buf))
  {
    mutt_error ("You already have an alias defined with that name!");
    return;
  }

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

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

  if (mutt_get_field ("Address: ", buf, sizeof (buf), 0) != 0 || !buf[0])
  {
    mutt_free_alias (&new);
    return;
  }
  new->addr = rfc822_parse_adrlist (new->addr, buf);

  if (adr && adr->personal && !mutt_is_mail_list (adr))
    strfcpy (buf, adr->personal, sizeof (buf));
  else
    buf[0] = 0;

  if (mutt_get_field ("Personal name: ", buf, sizeof (buf), 0) != 0)
  {
    mutt_free_alias (&new);
    return;
  }
  new->addr->personal = safe_strdup (buf);

  buf[0] = 0;
  rfc822_write_address (buf, sizeof (buf), new->addr);
  snprintf (prompt, sizeof (prompt), "[%s = %s] Accept?", new->name, buf);
  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, AliasFile, sizeof (buf));
  if (mutt_get_field ("Save to file: ", buf, sizeof (buf), M_FILE) != 0)
    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);
    mutt_message ("Alias added.");
  }
  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 &&
	  strcasecmp (ap->mailbox, a->mailbox) == 0)
	return ap;
    }
  }
  return 0;
}

/* alias_complete() -- alias completion routine
 *
 * given a partial alias, this routine attempts to fill in the alias
 * from the alias list as much as possible
 */
int mutt_alias_complete (char *s, size_t buflen)
{
  ALIAS *a = Aliases;
  ALIAS *a_list = NULL, *a_cur = NULL;
  char bestname[STRING];
  int i;

  memset (bestname, 0, sizeof (bestname));

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

  while (a)
  {
    if (a->name && strstr (a->name, s) == a->name)
    {
      if (!bestname[0]) /* init */
	strfcpy (bestname, a->name, min (strlen (a->name) + 1, sizeof (bestname)));
      else
      {
	for (i = 0 ; a->name[i] && a->name[i] == bestname[i] ; i++)
	  ;
	bestname[i] = 0;
      }
    }
    a = a->next;
  }

  if ((bestname[0] == 0) || /* if we didn't find anything */
      (s[0] == 0))          /* or we weren't given anything */
  {
    mutt_alias_menu (s, buflen, Aliases);
    return 0;
  }
  else
  {
    if (strcmp (bestname, s) == 0) /* add anything to the completion? */
    {
      /* 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;
      }

      s[0] = 0; /* reset string before passing to alias_menu */
      mutt_alias_menu (s, buflen, a_list);

      /* free the alias list */
      while (a_list)
      {
	a_cur = a_list;
	a_list = a_list->next;
	safe_free ((void **) &a_cur);
      }

      return 0;
    }
    else /* we are adding something to the completion */
      strfcpy (s, bestname, strlen (bestname) + 1);
  }

  return 1;
}

/* returns TRUE if the given address belongs to the user. */
int mutt_addr_is_user (ADDRESS *addr)
{
  char buf[LONG_STRING];

  /* NULL address is assumed to be the user. */
  if (!addr)
    return 1;
  if (!addr->mailbox)
    return 0;
  if (strcasecmp (addr->mailbox, Username) == 0)
    return 1;
  snprintf (buf, sizeof (buf), "%s@%s", Username, Hostname);
  if (strcasecmp (addr->mailbox, buf) == 0)
    return 1;
  snprintf (buf, sizeof (buf), "%s@%s", Username, Fqdn);
  if (strcasecmp (addr->mailbox, buf) == 0)
    return 1;

  if (Alternates.pattern &&
      regexec (Alternates.rx, addr->mailbox, 0, NULL, 0) == 0)
    return 1;
  return 0;
}

/* returns 1 if the list of address contains a known mailing list */
int mutt_is_list_recipient (ADDRESS *a)
{
  for (; a; a = a->next)
    if (mutt_is_mail_list (a))
      return 1;
  return 0;
}