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