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