Removed configure flag --disable-locale and have it always enabled.
[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", 2 },
28 #define NORMSW     4
29         { "normalize", 0 },
30 #define NNORMSW    5
31         { "nonormalize", 2 },
32 #define USERSW     6
33         { "user", 0 },
34 #define NUSERSW    7
35         { "nouser", 2 },
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         setlocale(LC_ALL, "");
64         invo_name = mhbasename(argv[0]);
65
66         /* read user profile/context */
67         context_read();
68
69         arguments = getarguments(invo_name, argc, argv, 1);
70         argp = arguments;
71
72         while ((cp = *argp++)) {
73                 if (*cp == '-') {
74                         switch (smatch(++cp, switches)) {
75                         case AMBIGSW:
76                                 ambigsw(cp, switches);
77                                 done(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                                 done(1);
86                         case VERSIONSW:
87                                 print_version(invo_name);
88                                 done(1);
89
90                         case ALIASW:
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                                 continue;
96                         case NALIASW:
97                                 noalias++;
98                                 continue;
99
100                         case LISTSW:
101                                 list++;
102                                 continue;
103                         case NLISTSW:
104                                 list = 0;
105                                 continue;
106
107                         case NORMSW:
108                                 normalize = AD_HOST;
109                                 continue;
110                         case NNORMSW:
111                                 normalize = AD_NHST;
112                                 continue;
113
114                         case USERSW:
115                                 inverted++;
116                                 continue;
117                         case NUSERSW:
118                                 inverted = 0;
119                                 continue;
120                         }
121                 }
122                 vec[vecp++] = cp;
123         }
124
125         if (!noalias) {
126                 /* allow Aliasfile: profile entry */
127                 if ((cp = context_find("Aliasfile"))) {
128                         char *dp = NULL;
129
130                         for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
131                                 if ((i = alias(etcpath(*ap))) != AK_OK)
132                                         adios(NULL, "aliasing error in %s - %s", *ap, akerror(i));
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                 done(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         done(0);
166         return 1;
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 }