Reformated comments and long lines
[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 = r1bindex (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                 alias (AliasFile);
141         }
142
143         /*
144         ** If -user is specified
145         */
146         if (inverted) {
147                 if (vecp == 0)
148                         adios (NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
149                                    invo_name);
150
151                 for (i = 0; i < vecp; i++)
152                         print_usr (vec[i], list, normalize);
153
154                 done (0);
155         }
156
157         if (vecp) {
158                 /* print specified aliases */
159                 for (i = 0; i < vecp; i++)
160                         print_aka (akvalue (vec[i]), list, 0);
161         } else {
162                 /* print them all */
163                 for (ak = akahead; ak; ak = ak->ak_next) {
164                         printf ("%s: ", ak->ak_name);
165                         pos += strlen (ak->ak_name) + 1;
166                         print_aka (akresult (ak), list, pos);
167                 }
168         }
169
170         done (0);
171         return 1;
172 }
173
174 static void
175 print_aka (char *p, int list, int margin)
176 {
177         char c;
178
179         if (p == NULL) {
180                 printf ("<empty>\n");
181                 return;
182         }
183
184         while ((c = *p++)) {
185                 switch (c) {
186                         case ',':
187                                 if (*p) {
188                                         if (list)
189                                                 printf ("\n%*s", margin, "");
190                                         else {
191                                                 if (pos >= 68) {
192                                                         printf (",\n ");
193                                                         pos = 2;
194                                                 } else {
195                                                         printf (", ");
196                                                         pos += 2;
197                                                 }
198                                         }
199                                 }
200
201                         case 0:
202                                 break;
203
204                         default:
205                                 pos++;
206                                 putchar (c);
207                 }
208         }
209
210         putchar ('\n');
211         pos = 1;
212 }
213
214 static void
215 print_usr (char *s, int list, int norm)
216 {
217         register char *cp, *pp, *vp;
218         register struct aka *ak;
219         register struct mailname *mp, *np;
220
221         if ((pp = getname (s)) == NULL)
222                 adios (NULL, "no address in \"%s\"", s);
223         if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
224                 adios (NULL, "bad address \"%s\"", s);
225         while (getname (""))
226                 continue;
227
228         vp = NULL;
229         for (ak = akahead; ak; ak = ak->ak_next) {
230                 pp = akresult (ak);
231                 while ((cp = getname (pp))) {
232                         if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
233                                 continue;
234                         if (!mh_strcasecmp (mp->m_host, np->m_host)
235                                         && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
236                                 vp = vp ? add (ak->ak_name, add (",", vp))
237                                         : getcpy (ak->ak_name);
238                                 mnfree (np);
239                                 while (getname (""))
240                                         continue;
241                                 break;
242                         }
243                         mnfree (np);
244                 }
245         }
246         mnfree (mp);
247
248 #if 0
249         printf ("%s: ", s);
250         print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
251 #else
252         print_aka (vp ? vp : s, list, 0);
253 #endif
254
255         if (vp)
256                 free (vp);
257 }