Added support for optional Content_Disposition header in mhbuild directive.s
[mmh] / uip / whom.c
1
2 /*
3  * whom.c -- report to whom a message would be sent
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/signals.h>
14 #include <signal.h>
15
16 static struct swit switches[] = {
17 #define ALIASW              0
18     { "alias aliasfile", 0 },
19 #define CHKSW               1
20     { "check", 0 },
21 #define NOCHKSW             2
22     { "nocheck", 0 },
23 #define DRAFTSW             3
24     { "draft", 0 },
25 #define DFOLDSW             4
26     { "draftfolder +folder", 6 },
27 #define DMSGSW              5
28     { "draftmessage msg", 6 },
29 #define NDFLDSW             6
30     { "nodraftfolder", 0 },
31 #define VERSIONSW           7
32     { "version", 0 },
33 #define HELPSW              8
34     { "help", 0 },
35 #define CLIESW              9
36     { "client host", -6 },
37 #define SERVSW             10
38     { "server host", -6 },
39 #define SNOOPSW            11
40     { "snoop", -5 },
41     { NULL, 0 }
42 };
43
44
45 int
46 main (int argc, char **argv)
47 {
48     pid_t child_id;
49     int i, status, isdf = 0;
50     int distsw = 0, vecp = 0;
51     char *cp, *dfolder = NULL, *dmsg = NULL;
52     char *msg = NULL, **ap, **argp, backup[BUFSIZ];
53     char buf[BUFSIZ], **arguments, *vec[MAXARGS];
54
55 #ifdef LOCALE
56     setlocale(LC_ALL, "");
57 #endif
58     invo_name = r1bindex (argv[0], '/');
59
60     /* read user profile/context */
61     context_read();
62
63     arguments = getarguments (invo_name, argc, argv, 1);
64     argp = arguments;
65
66     vec[vecp++] = invo_name;
67     vec[vecp++] = "-whom";
68     vec[vecp++] = "-library";
69     vec[vecp++] = getcpy (m_maildir (""));
70
71     while ((cp = *argp++)) {
72         if (*cp == '-') {
73             switch (smatch (++cp, switches)) {
74                 case AMBIGSW: 
75                     ambigsw (cp, switches);
76                     done (1);
77                 case UNKWNSW: 
78                     adios (NULL, "-%s unknown", cp);
79
80                 case HELPSW: 
81                     snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name);
82                     print_help (buf, switches, 1);
83                     done (1);
84                 case VERSIONSW:
85                     print_version(invo_name);
86                     done (1);
87
88                 case CHKSW: 
89                 case NOCHKSW: 
90                 case SNOOPSW:
91                     vec[vecp++] = --cp;
92                     continue;
93
94                 case DRAFTSW:
95                     msg = draft;
96                     continue;
97
98                 case DFOLDSW: 
99                     if (dfolder)
100                         adios (NULL, "only one draft folder at a time!");
101                     if (!(cp = *argp++) || *cp == '-')
102                         adios (NULL, "missing argument to %s", argp[-2]);
103                     dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
104                             *cp != '@' ? TFOLDER : TSUBCWF);
105                     continue;
106                 case DMSGSW: 
107                     if (dmsg)
108                         adios (NULL, "only one draft message at a time!");
109                     if (!(dmsg = *argp++) || *dmsg == '-')
110                         adios (NULL, "missing argument to %s", argp[-2]);
111                     continue;
112                 case NDFLDSW: 
113                     dfolder = NULL;
114                     isdf = NOTOK;
115                     continue;
116
117                 case ALIASW: 
118                 case CLIESW: 
119                 case SERVSW: 
120                     vec[vecp++] = --cp;
121                     if (!(cp = *argp++) || *cp == '-')
122                         adios (NULL, "missing argument to %s", argp[-2]);
123                     vec[vecp++] = cp;
124                     continue;
125             }
126         }
127         if (msg)
128             adios (NULL, "only one draft at a time!");
129         else
130             vec[vecp++] = msg = cp;
131     }
132
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             vec[vecp++] = "-alias";
139             vec[vecp++] = *ap;
140         }
141     }
142
143     if (msg == NULL) {
144 #ifdef  WHATNOW
145         if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0')
146 #endif  /* WHATNOW */
147             cp  = getcpy (m_draft (dfolder, dmsg, 1, &isdf));
148         msg = vec[vecp++] = cp;
149     }
150     if ((cp = getenv ("mhdist"))
151             && *cp
152             && (distsw = atoi (cp))
153             && (cp = getenv ("mhaltmsg"))
154             && *cp) {
155         if (distout (msg, cp, backup) == NOTOK)
156             done (1);
157         vec[vecp++] = "-dist";
158         distsw++;
159     }
160     vec[vecp] = NULL;
161
162     closefds (3);
163
164     if (distsw) {
165         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
166             sleep (5);
167     }
168
169     switch (distsw ? child_id : OK) {
170         case NOTOK:
171             advise (NULL, "unable to fork, so checking directly...");
172         case OK:
173             execvp (postproc, vec);
174             fprintf (stderr, "unable to exec ");
175             perror (postproc);
176             _exit (-1);
177
178         default:
179             SIGNAL (SIGHUP, SIG_IGN);
180             SIGNAL (SIGINT, SIG_IGN);
181             SIGNAL (SIGQUIT, SIG_IGN);
182             SIGNAL (SIGTERM, SIG_IGN);
183
184             status = pidwait(child_id, OK);
185
186             unlink (msg);
187             if (rename (backup, msg) == NOTOK)
188                 adios (msg, "unable to rename %s to", backup);
189             done (status);
190     }
191
192     return 0;  /* dead code to satisfy the compiler */
193 }