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