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