]> diplodocus.org Git - nmh/blob - uip/ali.c
Note in dist, mh-profile, and repl man pages that the @ link
[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 static struct swit switches[] = {
17 #define ALIASW 0
18 { "alias aliasfile", 0 },
19 #define NALIASW 1
20 { "noalias", -7 },
21 #define LISTSW 2
22 { "list", 0 },
23 #define NLISTSW 3
24 { "nolist", 0 },
25 #define NORMSW 4
26 { "normalize", 0 },
27 #define NNORMSW 5
28 { "nonormalize", 0 },
29 #define USERSW 6
30 { "user", 0 },
31 #define NUSERSW 7
32 { "nouser", 0 },
33 #define VERSIONSW 8
34 { "version", 0 },
35 #define HELPSW 9
36 { "help", 0 },
37 { NULL, 0 }
38 };
39
40 static int pos = 1;
41
42 extern struct aka *akahead;
43
44 /*
45 * prototypes
46 */
47 static void print_aka (char *, int, int);
48 static void print_usr (char *, int, int);
49
50
51 int
52 main (int argc, char **argv)
53 {
54 int i, vecp = 0, inverted = 0, list = 0;
55 int noalias = 0, normalize = AD_NHST;
56 char *cp, **ap, **argp, buf[BUFSIZ];
57 /* Really only need to allocate for argc-1, but must allocate at least 1,
58 so go ahead and allocate for argc char pointers. */
59 char **vec = mh_xmalloc (argc * sizeof (char *)), **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 (0);
88 case VERSIONSW:
89 print_version (invo_name);
90 done (0);
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
125 if (vecp < argc) {
126 vec[vecp++] = cp;
127 } else {
128 /* Should never happen, but try to protect against code changes
129 that could allow it. */
130 adios (NULL, "too many arguments");
131 }
132 }
133
134 if (!noalias) {
135 /* allow Aliasfile: profile entry */
136 if ((cp = context_find ("Aliasfile"))) {
137 char *dp = NULL;
138
139 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
140 if ((i = alias (*ap)) != AK_OK)
141 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
142 if (dp)
143 free(dp);
144 }
145 alias (AliasFile);
146 }
147
148 /*
149 * If -user is specified
150 */
151 if (inverted) {
152 if (vecp == 0)
153 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
154 invo_name);
155
156 for (i = 0; i < vecp; i++)
157 print_usr (vec[i], list, normalize);
158 } else {
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
173 free (vec);
174 done (0);
175 return 1;
176 }
177
178 static void
179 print_aka (char *p, int list, int margin)
180 {
181 char c;
182
183 if (p == NULL) {
184 printf ("<empty>\n");
185 return;
186 }
187
188 while ((c = *p++)) {
189 switch (c) {
190 case ',':
191 if (*p) {
192 if (list)
193 printf ("\n%*s", margin, "");
194 else {
195 if (pos >= 68) {
196 printf (",\n ");
197 pos = 2;
198 } else {
199 printf (", ");
200 pos += 2;
201 }
202 }
203 }
204
205 case 0:
206 break;
207
208 default:
209 pos++;
210 putchar (c);
211 }
212 }
213
214 putchar ('\n');
215 pos = 1;
216 }
217
218 static void
219 print_usr (char *s, int list, int norm)
220 {
221 register char *cp, *pp, *vp;
222 register struct aka *ak;
223 register struct mailname *mp, *np;
224
225 if ((pp = getname (s)) == NULL)
226 adios (NULL, "no address in \"%s\"", s);
227 if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
228 adios (NULL, "bad address \"%s\"", s);
229 while (getname (""))
230 continue;
231
232 vp = NULL;
233 for (ak = akahead; ak; ak = ak->ak_next) {
234 pp = akresult (ak);
235 while ((cp = getname (pp))) {
236 if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
237 continue;
238 if (!mh_strcasecmp (mp->m_host, np->m_host)
239 && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
240 vp = vp ? add (ak->ak_name, add (",", vp))
241 : getcpy (ak->ak_name);
242 mnfree (np);
243 while (getname (""))
244 continue;
245 break;
246 }
247 mnfree (np);
248 }
249 }
250 mnfree (mp);
251
252 #if 0
253 printf ("%s: ", s);
254 print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
255 #else
256 print_aka (vp ? vp : s, list, 0);
257 #endif
258
259 if (vp)
260 free (vp);
261 }