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