]> diplodocus.org Git - nmh/blob - uip/ali.c
Formatting cleanup.
[nmh] / uip / ali.c
1
2 /*
3 * ali.c -- list nmh mail aliases
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/aliasbr.h>
13 #include <h/mts.h>
14 #include <h/utils.h>
15
16 /*
17 * maximum number of names
18 */
19 #define NVEC 50
20
21 static struct swit switches[] = {
22 #define ALIASW 0
23 { "alias aliasfile", 0 },
24 #define NALIASW 1
25 { "noalias", -7 },
26 #define LISTSW 2
27 { "list", 0 },
28 #define NLISTSW 3
29 { "nolist", 0 },
30 #define NORMSW 4
31 { "normalize", 0 },
32 #define NNORMSW 5
33 { "nonormalize", 0 },
34 #define USERSW 6
35 { "user", 0 },
36 #define NUSERSW 7
37 { "nouser", 0 },
38 #define VERSIONSW 8
39 { "version", 0 },
40 #define HELPSW 9
41 { "help", 0 },
42 { NULL, 0 }
43 };
44
45 static int pos = 1;
46
47 extern struct aka *akahead;
48
49 /*
50 * prototypes
51 */
52 static void print_aka (char *, int, int);
53 static void print_usr (char *, int, int);
54
55
56 int
57 main (int argc, char **argv)
58 {
59 int i, vecp = 0, inverted = 0, list = 0;
60 int noalias = 0, normalize = AD_NHST;
61 char *cp, **ap, **argp, buf[BUFSIZ];
62 char *vec[NVEC], **arguments;
63 struct aka *ak;
64
65 #ifdef LOCALE
66 setlocale(LC_ALL, "");
67 #endif
68 invo_name = r1bindex (argv[0], '/');
69
70 /* read user profile/context */
71 context_read();
72
73 mts_init (invo_name);
74 arguments = getarguments (invo_name, argc, argv, 1);
75 argp = arguments;
76
77 while ((cp = *argp++)) {
78 if (*cp == '-') {
79 switch (smatch (++cp, switches)) {
80 case AMBIGSW:
81 ambigsw (cp, switches);
82 done (1);
83 case UNKWNSW:
84 adios (NULL, "-%s unknown", cp);
85
86 case HELPSW:
87 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
88 invo_name);
89 print_help (buf, switches, 1);
90 done (1);
91 case VERSIONSW:
92 print_version (invo_name);
93 done (1);
94
95 case ALIASW:
96 if (!(cp = *argp++) || *cp == '-')
97 adios (NULL, "missing argument to %s", argp[-2]);
98 if ((i = alias (cp)) != AK_OK)
99 adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
100 continue;
101 case NALIASW:
102 noalias++;
103 continue;
104
105 case LISTSW:
106 list++;
107 continue;
108 case NLISTSW:
109 list = 0;
110 continue;
111
112 case NORMSW:
113 normalize = AD_HOST;
114 continue;
115 case NNORMSW:
116 normalize = AD_NHST;
117 continue;
118
119 case USERSW:
120 inverted++;
121 continue;
122 case NUSERSW:
123 inverted = 0;
124 continue;
125 }
126 }
127 vec[vecp++] = cp;
128 }
129
130 if (!noalias) {
131 /* allow Aliasfile: profile entry */
132 if ((cp = context_find ("Aliasfile"))) {
133 char *dp = NULL;
134
135 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
136 if ((i = alias (*ap)) != AK_OK)
137 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
138 if (dp)
139 free(dp);
140 }
141 alias (AliasFile);
142 }
143
144 /*
145 * If -user is specified
146 */
147 if (inverted) {
148 if (vecp == 0)
149 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
150 invo_name);
151
152 for (i = 0; i < vecp; i++)
153 print_usr (vec[i], list, normalize);
154
155 done (0);
156 }
157
158 if (vecp) {
159 /* print specified aliases */
160 for (i = 0; i < vecp; i++)
161 print_aka (akvalue (vec[i]), list, 0);
162 } else {
163 /* print them all */
164 for (ak = akahead; ak; ak = ak->ak_next) {
165 printf ("%s: ", ak->ak_name);
166 pos += strlen (ak->ak_name) + 1;
167 print_aka (akresult (ak), list, pos);
168 }
169 }
170
171 done (0);
172 return 1;
173 }
174
175 static 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 static 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 (!mh_strcasecmp (mp->m_host, np->m_host)
236 && !mh_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 }