2 ** whom.c -- list recipients of message
4 ** This code is Copyright (c) 2012, by the authors of mmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
16 static struct swit switches[] = {
40 char *version=VERSION;
44 #define HRESENT (1<<1)
50 struct mailname head = {0};
51 struct mailname *mp = &head;
55 static int resent = 0; /* consider normal or resent headers */
56 static int toccsw = 1; /* list sighted recipients */
57 static int dccsw = 1; /* list hidden dcc recipients */
58 static int bccsw = 1; /* list hidden bcc recipients */
59 static int alisw = 1; /* expand aliases on rcpt addrs */
61 static char *dccsep = "\t==DCC==";
62 static char *bccsep = "\t==BCC==";
68 static int process(char *);
69 static void proc_hdr(char *, char *);
70 static int print(void);
71 static int printdcc(void);
72 static int printbcc(void);
73 static void printone(struct mailname *);
77 main(int argc, char **argv)
81 char buf[BUFSIZ], **argp;
85 struct msgs_array msgs = {0};
86 struct msgs_array files = {0};
89 setlocale(LC_ALL, "");
90 invo_name = mhbasename(argv[0]);
92 /* read user profile/context */
95 arguments = getarguments(invo_name, argc, argv, 1);
98 while ((cp = *argp++)) {
100 switch (smatch(++cp, switches)) {
102 ambigsw(cp, switches);
106 adios(EX_USAGE, NULL, "-%s unknown", cp);
109 snprintf(buf, sizeof(buf),
110 "%s [switches] file ...",
112 print_help(buf, switches, 1);
113 exit(argc == 2 ? EX_OK : EX_USAGE);
115 print_version(invo_name);
116 exit(argc == 2 ? EX_OK : EX_USAGE);
147 if (*cp == '+' || *cp == '@') {
149 adios(EX_USAGE, NULL, "only one folder at a time!");
151 folder = mh_xstrdup(expandfol(cp));
154 app_msgarg(&msgs, cp);
158 adios(EX_USAGE, NULL, "usage: %s [switches] file ...", invo_name);
160 if (!toccsw && !dccsw && !bccsw) {
161 adios(EX_USAGE, NULL, "use at least one of: -tocc -dcc -bcc");
163 if (parse_msgs(&msgs, folder, &files) < 0) {
166 for (filep = 0; filep < files.size; filep++) {
167 process(files.msgs[filep]);
170 cmd = add("ali -list", NULL);
171 if ((n=print()) && alisw) {
172 if (!(in = popen(cmd, "r"))) {
173 adios(EX_IOERR, "popen", "unable to");
175 while (fgets(buf, sizeof buf, in)) {
183 cmd = add("ali -list", NULL);
184 if ((n=printdcc()) && alisw) {
185 if (!(in = popen(cmd, "r"))) {
186 adios(EX_IOERR, "popen", "unable to");
188 while (fgets(buf, sizeof buf, in)) {
196 cmd = add("ali -list", NULL);
197 if ((n=printbcc()) && alisw) {
198 if (!(in = popen(cmd, "r"))) {
199 adios(EX_IOERR, "popen", "unable to");
201 while (fgets(buf, sizeof buf, in)) {
209 return naddrs ? 0 : 1;
217 struct field f = {{0}};
222 if ((in = fopen(file, "r")) == NULL) {
223 adios(EX_IOERR, file, "unable to open");
226 for (compnum=1, state=FLD2;; compnum++) {
227 switch (state = m_getfld2(state, &f, in)) {
232 proc_hdr(f.name, f.value);
240 advise(NULL, "message format error in component #%d", compnum);
244 adios(EX_DATAERR, NULL, "message format error in component #%d",
248 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
258 ** Check if the header contains addresses we're interested in.
259 ** If so, extract the addresses and add them to the global list.
262 proc_hdr(char *name, char *val)
267 while (*val==' ' || *val=='\t' || *val=='\n') {
270 if (strncasecmp(name, "resent-", 7)==0) {
273 if (mh_strcasecmp(name, "to")==0) {
275 } else if (mh_strcasecmp(name, "cc")==0) {
277 } else if (mh_strcasecmp(name, "dcc")==0) {
279 } else if (mh_strcasecmp(name, "bcc")==0) {
281 } else if (mh_strcasecmp(name, "resent-to")==0) {
282 type = (HRESENT | HTO);
283 } else if (mh_strcasecmp(name, "resent-cc")==0) {
284 type = (HRESENT | HCC);
285 } else if (mh_strcasecmp(name, "resent-dcc")==0) {
286 type = (HRESENT | HDCC);
287 } else if (mh_strcasecmp(name, "resent-bcc")==0) {
288 type = (HRESENT | HBCC);
290 /* ignore non-recpient headers */
294 /* ignore recipient headers we are not interested in */
295 if ((type&HTO || type&HCC) && !toccsw) {
298 if ((type&HDCC) && !dccsw) {
301 if ((type&HBCC) && !bccsw) {
305 while ((cp = getname(val))) {
306 if (!(mp->m_next = getm(cp, NULL, 0, AD_NAME, NULL))) {
307 adios(EX_DATAERR, NULL, "illegal address: %s", cp);
310 if (mp->m_type == BADHOST) {
311 admonish(NULL, "bad address `%s'", mp->m_text);
320 ** Walk through the list of addresses and print the right ones.
327 for (mp=head.m_next; mp; mp=mp->m_next) {
328 /* skip unless both are resent or neither one is */
329 if (resent != (mp->m_type&HRESENT)) {
332 if (mp->m_type & (HTO|HCC)) {
341 ** Walk through the list of addresses and print the right ones.
348 for (mp=head.m_next; mp; mp=mp->m_next) {
349 /* skip unless both are resent or neither one is */
350 if (resent != (mp->m_type&HRESENT)) {
353 if (mp->m_type & HDCC) {
354 if (!naddrs && (toccsw || bccsw)) {
365 ** Walk through the list of addresses and print the right ones.
372 for (mp=head.m_next; mp; mp=mp->m_next) {
373 /* skip unless both are resent or neither one is */
374 if (resent != (mp->m_type&HRESENT)) {
377 if (mp->m_type & HBCC) {
378 if (!naddrs && (toccsw || dccsw)) {
390 ** Print one single address in appropriate form.
393 printone(struct mailname *mp)
398 snprintf(buf, sizeof buf, " %s@%s", mp->m_mbox, mp->m_host);
400 snprintf(buf, sizeof buf, " %s", mp->m_mbox);