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