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