* patch #3968: Move the add() function from its own file (add.c) and
[mmh] / uip / ali.c
1
2 /*
3  * ali.c -- list nmh mail aliases
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/mts.h>
16 #include <h/utils.h>
17
18 /*
19  * maximum number of names
20  */
21 #define NVEC 50
22
23 static struct swit switches[] = {
24 #define ALIASW                0
25     { "alias aliasfile", 0 },
26 #define NALIASW               1
27     { "noalias", -7 },
28 #define LISTSW                2
29     { "list", 0 },
30 #define NLISTSW               3
31     { "nolist", 0 },
32 #define NORMSW                4
33     { "normalize", 0 },
34 #define NNORMSW               5
35     { "nonormalize", 0 },
36 #define USERSW                6
37     { "user", 0 },
38 #define NUSERSW               7
39     { "nouser", 0 },
40 #define VERSIONSW             8
41     { "version", 0 },
42 #define HELPSW                9
43     { "help", 0 },
44     { NULL, 0 }
45 };
46
47 static int pos = 1;
48
49 extern struct aka *akahead;
50
51 /*
52  * prototypes
53  */
54 void print_aka (char *, int, int);
55 void print_usr (char *, int, int);
56
57
58 int
59 main (int argc, char **argv)
60 {
61     int i, vecp = 0, inverted = 0, list = 0;
62     int noalias = 0, normalize = AD_NHST;
63     char *cp, **ap, **argp, buf[BUFSIZ];
64     char *vec[NVEC], **arguments;
65     struct aka *ak;
66
67 #ifdef LOCALE
68     setlocale(LC_ALL, "");
69 #endif
70     invo_name = r1bindex (argv[0], '/');
71
72     /* read user profile/context */
73     context_read();
74
75     mts_init (invo_name);
76     arguments = getarguments (invo_name, argc, argv, 1);
77     argp = arguments;
78
79     while ((cp = *argp++)) {
80         if (*cp == '-') {
81             switch (smatch (++cp, switches)) {
82                 case AMBIGSW: 
83                     ambigsw (cp, switches);
84                     done (1);
85                 case UNKWNSW: 
86                     adios (NULL, "-%s unknown", cp);
87
88                 case HELPSW: 
89                     snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
90                         invo_name);
91                     print_help (buf, switches, 1);
92                     done (1);
93                 case VERSIONSW:
94                     print_version (invo_name);
95                     done (1);
96
97                 case ALIASW: 
98                     if (!(cp = *argp++) || *cp == '-')
99                         adios (NULL, "missing argument to %s", argp[-2]);
100                     if ((i = alias (cp)) != AK_OK)
101                         adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
102                     continue;
103                 case NALIASW: 
104                     noalias++;
105                     continue;
106
107                 case LISTSW: 
108                     list++;
109                     continue;
110                 case NLISTSW: 
111                     list = 0;
112                     continue;
113
114                 case NORMSW: 
115                     normalize = AD_HOST;
116                     continue;
117                 case NNORMSW: 
118                     normalize = AD_NHST;
119                     continue;
120
121                 case USERSW: 
122                     inverted++;
123                     continue;
124                 case NUSERSW: 
125                     inverted = 0;
126                     continue;
127             }
128         }
129         vec[vecp++] = cp;
130     }
131
132     if (!noalias) {
133         /* allow Aliasfile: profile entry */
134         if ((cp = context_find ("Aliasfile"))) {
135             char *dp = NULL;
136
137             for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
138                 if ((i = alias (*ap)) != AK_OK)
139                     adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
140             if (dp)
141                 free(dp);
142         }
143         alias (AliasFile);
144     }
145
146     /*
147      * If -user is specified
148      */
149     if (inverted) {
150         if (vecp == 0)
151             adios (NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
152                    invo_name);
153
154         for (i = 0; i < vecp; i++)
155             print_usr (vec[i], list, normalize);
156
157         done (0);
158     }
159
160     if (vecp) {
161         /* print specified aliases */
162         for (i = 0; i < vecp; i++)
163             print_aka (akvalue (vec[i]), list, 0);
164     } else {
165         /* print them all */
166         for (ak = akahead; ak; ak = ak->ak_next) {
167             printf ("%s: ", ak->ak_name);
168             pos += strlen (ak->ak_name) + 1;
169             print_aka (akresult (ak), list, pos);
170         }
171     }
172
173     return done (0);
174 }
175
176 void
177 print_aka (char *p, int list, int margin)
178 {
179     char c;
180
181     if (p == NULL) {
182         printf ("<empty>\n");
183         return;
184     }
185
186     while ((c = *p++)) {
187         switch (c) {
188             case ',': 
189                 if (*p) {
190                     if (list)
191                         printf ("\n%*s", margin, "");
192                     else {
193                         if (pos >= 68) {
194                             printf (",\n ");
195                             pos = 2;
196                         } else {
197                             printf (", ");
198                             pos += 2;
199                         }
200                     }
201                 }
202
203             case 0: 
204                 break;
205
206             default: 
207                 pos++;
208                 putchar (c);
209         }
210     }
211
212     putchar ('\n');
213     pos = 1;
214 }
215
216 void
217 print_usr (char *s, int list, int norm)
218 {
219     register char *cp, *pp, *vp;
220     register struct aka *ak;
221     register struct mailname *mp, *np;
222
223     if ((pp = getname (s)) == NULL)
224         adios (NULL, "no address in \"%s\"", s);
225     if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
226         adios (NULL, "bad address \"%s\"", s);
227     while (getname (""))
228         continue;
229
230     vp = NULL;
231     for (ak = akahead; ak; ak = ak->ak_next) {
232         pp = akresult (ak);
233         while ((cp = getname (pp))) {
234             if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
235                 continue;
236             if (!strcasecmp (mp->m_host, np->m_host)
237                     && !strcasecmp (mp->m_mbox, np->m_mbox)) {
238                 vp = vp ? add (ak->ak_name, add (",", vp))
239                     : getcpy (ak->ak_name);
240                 mnfree (np);
241                 while (getname (""))
242                     continue;
243                 break;
244             }
245             mnfree (np);
246         }
247     }
248     mnfree (mp);
249
250 #if 0
251     printf ("%s: ", s);
252     print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
253 #else
254     print_aka (vp ? vp : s, list, 0);
255 #endif
256
257     if (vp)
258         free (vp);
259 }