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