Blame view

mail/alias.c 5.53 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2001, 2002, 2005, 2006, 2007 Free Software Foundation, Inc.
3

4
   GNU Mailutils 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 3, or (at your option)
7 8
   any later version.

9
   GNU Mailutils is distributed in the hope that it will be useful,
10 11 12 13 14
   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
15
   along with GNU Mailutils; if not, write to the Free Software
16 17
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */
18 19 20

#include "mail.h"

21
typedef struct _alias *alias_t;
Sergey Poznyakoff authored
22 23 24

struct _alias
{
25
  mu_list_t list;
Sergey Poznyakoff authored
26 27
};

28
static mu_assoc_t aliases;
Sergey Poznyakoff authored
29

30 31
static void
alias_free (void *data)
Sergey Poznyakoff authored
32
{
33 34
  alias_t al = data;
  util_slist_destroy (&al->list);
Sergey Poznyakoff authored
35 36
}

37 38
static void
alias_print_group (const char *name, alias_t al)
Sergey Poznyakoff authored
39
{
40 41 42
  fprintf (ofile, "%s    ", name);
  util_slist_print (al->list, 0);
  fprintf (ofile, "\n");
Sergey Poznyakoff authored
43 44
}

45 46
static alias_t
alias_lookup (const char *name)
Sergey Poznyakoff authored
47
{
48
  return mu_assoc_ref (aliases, name);
Sergey Poznyakoff authored
49 50
}

51
static void
52
alias_print (char *name)
Sergey Poznyakoff authored
53 54 55
{
  if (!name)
    {
56
      mu_iterator_t itr;
Sergey Poznyakoff authored
57 58 59

      if (!aliases)
	return;
60

61 62 63
      mu_assoc_get_iterator (aliases, &itr);
      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
	   mu_iterator_next (itr))
Sergey Poznyakoff authored
64
	{
65 66 67 68 69
	  const char *name;
	  alias_t al;
	  if (mu_iterator_current_kv (itr, (const void **)&name, (void**)&al))
	    continue;
	  alias_print_group (name, al);
Sergey Poznyakoff authored
70 71 72 73
	}
    }
  else
    {
74
      alias_t al;
Sergey Poznyakoff authored
75

76 77
      al = alias_lookup (name);
      if (!al)
Sergey Poznyakoff authored
78
	{
79
	  util_error (_("\"%s\": not a group"), name);
Sergey Poznyakoff authored
80 81
	  return;
	}
82
      alias_print_group (name, al);
Sergey Poznyakoff authored
83 84 85
    }
}

86 87
static int
alias_create (const char *name, alias_t *al)
Sergey Poznyakoff authored
88
{
89
  int rc;
Sergey Poznyakoff authored
90

91
  if (!aliases)
Sergey Poznyakoff authored
92
    {
93
      mu_assoc_create (&aliases, sizeof (struct _alias), 0);
94
      mu_assoc_set_free (aliases, alias_free);
Sergey Poznyakoff authored
95
    }
96 97 98 99 100 101 102
  
  rc = mu_assoc_ref_install (aliases, name, (void**) al);
  if (rc == MU_ERR_EXISTS)
    return 0;
  if (rc == 0)
    return mu_list_create (&(*al)->list);
  return 1;
Sergey Poznyakoff authored
103 104 105
}

void
106
alias_destroy (const char *name)
Sergey Poznyakoff authored
107
{
108
  mu_assoc_remove (aliases, name);
Sergey Poznyakoff authored
109 110
}

111

112
static void
113
recursive_alias_expand (const char *name, mu_list_t exlist, mu_list_t origlist)
114
{ 
115
  alias_t al;
116
  mu_iterator_t itr;
117 118
  
  if ((al = alias_lookup (name)) == NULL)
119
    {
120 121
      if (mu_list_locate (exlist, (void*)name, NULL) == MU_ERR_NOENT)
	mu_list_append (exlist, (void*)name);
122 123 124
      return;
    }
  
125
  mu_list_get_iterator (al->list, &itr);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  for (mu_iterator_first (itr);
       !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      char *word;
      
      mu_iterator_current (itr, (void **)&word);
      if (mu_list_locate (origlist, word, NULL) == MU_ERR_NOENT)
	{
	  mu_list_prepend (origlist, word);
	  recursive_alias_expand (word, exlist, origlist);
	  mu_list_remove (origlist, word);
	}
    }
  mu_iterator_destroy (&itr);
}

static int
string_comp (const void *item, const void *value)
{
  return strcmp (item, value);
}

Sergey Poznyakoff authored
149
char *
150
alias_expand (const char *name)
Sergey Poznyakoff authored
151
{
152
  alias_t al;
153
  mu_list_t list;
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
  if (util_getenv (NULL, "recursivealiases", Mail_env_boolean, 0) == 0)
    {
      char *s;
      mu_list_t origlist;
      
      int status = mu_list_create (&list);
      if (status)
	{
	  mu_error (_("Cannot create list: %s"), mu_strerror (status));
	  return NULL;
	}
      status = mu_list_create (&origlist);
      if (status)
	{
	  mu_list_destroy (&origlist);
	  mu_error (_("Cannot create list: %s"), mu_strerror (status));
	  return NULL;
	}
      mu_list_set_comparator (list, string_comp);
      mu_list_set_comparator (origlist, string_comp);
      recursive_alias_expand (name, list, origlist);
      s = util_slist_to_string (list, ",");
      mu_list_destroy (&origlist);
      mu_list_destroy (&list);
      return s;
    }
  
182
  if ((al = alias_lookup (name)) == NULL)
183
    return NULL;
184
  return util_slist_to_string (al->list, ",");
185 186
}

187

188 189
struct alias_iterator
{
190
  mu_iterator_t itr;
191 192 193 194 195 196
  const char *prefix;
  int prefixlen;
  int pos;
};

const char *
197
alias_iterate_next (alias_iterator_t atr)
198
{
199 200 201 202 203 204 205 206 207 208 209 210
  while (!mu_iterator_is_done (atr->itr))
    {
      const char *name;
      alias_t al;

      if (mu_iterator_current_kv (atr->itr, (const void **)&name, (void**)&al))
	continue;
      mu_iterator_next (atr->itr);
      if (strlen (name) >= atr->prefixlen
	  && strncmp (name, atr->prefix, atr->prefixlen) == 0)
	return name;
    }
211 212 213 214 215 216
  return NULL;
}

const char *
alias_iterate_first (const char *prefix, alias_iterator_t *pc)
{
217 218 219
  mu_iterator_t itr;
  alias_iterator_t atr;
  
220 221 222 223 224
  if (!aliases)
    {
      *pc = NULL;
      return NULL;
    }
225 226 227 228 229 230 231 232 233 234 235 236

  if (mu_assoc_get_iterator (aliases, &itr))
    return NULL;
  mu_iterator_first (itr);
  atr = xmalloc (sizeof *atr);
  atr->prefix = prefix;
  atr->prefixlen = strlen (prefix);
  atr->pos = 0;
  atr->itr = itr;
  *pc = atr;

  return alias_iterate_next (atr);
237 238 239 240 241
}

void
alias_iterate_end (alias_iterator_t *pc)
{
242
  mu_iterator_destroy (&(*pc)->itr);
243 244
  free (*pc);
  *pc = NULL;
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



/*
 * a[lias] [alias [address...]]
 * g[roup] [alias [address...]]
 */

int
mail_alias (int argc, char **argv)
{
  if (argc == 1)
    alias_print (NULL);
  else if (argc == 2)
    alias_print (argv[1]);
  else
    {
      alias_t al;

      if (alias_create (argv[1], &al))
	return 1;

      argc--;
      argv++;
      while (--argc)
	util_slist_add (&al->list, *++argv);
    }
  return 0;
}