Removed the global alias file
[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         mts_init(invo_name);
73         arguments = getarguments(invo_name, argc, argv, 1);
74         argp = arguments;
75
76         while ((cp = *argp++)) {
77                 if (*cp == '-') {
78                         switch (smatch(++cp, switches)) {
79                                 case AMBIGSW:
80                                         ambigsw(cp, switches);
81                                         done(1);
82                                 case UNKWNSW:
83                                         adios(NULL, "-%s unknown", cp);
84
85                                 case HELPSW:
86                                         snprintf(buf, sizeof(buf), "%s [switches] aliases ...",
87                                                 invo_name);
88                                         print_help(buf, switches, 1);
89                                         done(1);
90                                 case VERSIONSW:
91                                         print_version(invo_name);
92                                         done(1);
93
94                                 case ALIASW:
95                                         if (!(cp = *argp++) || *cp == '-')
96                                                 adios(NULL, "missing argument to %s", argp[-2]);
97                                         if ((i = alias(cp)) != AK_OK)
98                                                 adios(NULL, "aliasing error in %s - %s", cp, akerror(i));
99                                         continue;
100                                 case NALIASW:
101                                         noalias++;
102                                         continue;
103
104                                 case LISTSW:
105                                         list++;
106                                         continue;
107                                 case NLISTSW:
108                                         list = 0;
109                                         continue;
110
111                                 case NORMSW:
112                                         normalize = AD_HOST;
113                                         continue;
114                                 case NNORMSW:
115                                         normalize = AD_NHST;
116                                         continue;
117
118                                 case USERSW:
119                                         inverted++;
120                                         continue;
121                                 case NUSERSW:
122                                         inverted = 0;
123                                         continue;
124                         }
125                 }
126                 vec[vecp++] = cp;
127         }
128
129         if (!noalias) {
130                 /* allow Aliasfile: profile entry */
131                 if ((cp = context_find("Aliasfile"))) {
132                         char *dp = NULL;
133
134                         for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
135                                 if ((i = alias(*ap)) != AK_OK)
136                                         adios(NULL, "aliasing error in %s - %s", *ap, akerror(i));
137                         if (dp)
138                                 free(dp);
139                 }
140         }
141
142         /*
143         ** If -user is specified
144         */
145         if (inverted) {
146                 if (vecp == 0)
147                         adios(NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
148                                    invo_name);
149
150                 for (i = 0; i < vecp; i++)
151                         print_usr(vec[i], list, normalize);
152
153                 done(0);
154         }
155
156         if (vecp) {
157                 /* print specified aliases */
158                 for (i = 0; i < vecp; i++)
159                         print_aka(akvalue(vec[i]), list, 0);
160         } else {
161                 /* print them all */
162                 for (ak = akahead; ak; ak = ak->ak_next) {
163                         printf("%s: ", ak->ak_name);
164                         pos += strlen(ak->ak_name) + 1;
165                         print_aka(akresult(ak), list, pos);
166                 }
167         }
168
169         done(0);
170         return 1;
171 }
172
173 static void
174 print_aka(char *p, int list, int margin)
175 {
176         char c;
177
178         if (p == NULL) {
179                 printf("<empty>\n");
180                 return;
181         }
182
183         while ((c = *p++)) {
184                 switch (c) {
185                         case ',':
186                                 if (*p) {
187                                         if (list)
188                                                 printf("\n%*s", margin, "");
189                                         else {
190                                                 if (pos >= 68) {
191                                                         printf(",\n ");
192                                                         pos = 2;
193                                                 } else {
194                                                         printf(", ");
195                                                         pos += 2;
196                                                 }
197                                         }
198                                 }
199
200                         case 0:
201                                 break;
202
203                         default:
204                                 pos++;
205                                 putchar(c);
206                 }
207         }
208
209         putchar('\n');
210         pos = 1;
211 }
212
213 static void
214 print_usr(char *s, int list, int norm)
215 {
216         register char *cp, *pp, *vp;
217         register struct aka *ak;
218         register struct mailname *mp, *np;
219
220         if ((pp = getname(s)) == NULL)
221                 adios(NULL, "no address in \"%s\"", s);
222         if ((mp = getm(pp, NULL, 0, norm, NULL)) == NULL)
223                 adios(NULL, "bad address \"%s\"", s);
224         while (getname(""))
225                 continue;
226
227         vp = NULL;
228         for (ak = akahead; ak; ak = ak->ak_next) {
229                 pp = akresult(ak);
230                 while ((cp = getname(pp))) {
231                         if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
232                                 continue;
233                         if (!mh_strcasecmp(mp->m_host, np->m_host)
234                                         && !mh_strcasecmp(mp->m_mbox, np->m_mbox)) {
235                                 vp = vp ? add(ak->ak_name, add(",", vp))
236                                         : getcpy(ak->ak_name);
237                                 mnfree(np);
238                                 while (getname(""))
239                                         continue;
240                                 break;
241                         }
242                         mnfree(np);
243                 }
244         }
245         mnfree(mp);
246
247 #if 0
248         printf("%s: ", s);
249         print_aka(vp ? vp : s, list, pos += strlen(s) + 1);
250 #else
251         print_aka(vp ? vp : s, list, 0);
252 #endif
253
254         if (vp)
255                 free(vp);
256 }