e906b1d3c928d0e9031e37692d222f18df0b1f04
[mmh] / uip / mhmail.c
1
2 /*
3  * mhmail.c -- simple mail program
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 <h/utils.h>
15 #include <signal.h>
16
17 static struct swit switches[] = {
18 #define BODYSW             0
19     { "body text", 0 },
20 #define CCSW               1
21     { "cc addrs ...", 0 },
22 #define FROMSW             2
23     { "from addr", 0 },
24 #define SUBJSW             3
25     { "subject text", 0 },
26 #define VERSIONSW          4
27     { "version", 0 },
28 #define HELPSW             5
29     { "help", 0 },
30 #define RESNDSW            6
31     { "resent", -6 },
32 #define QUEUESW            7
33     { "queued", -6 },
34     { NULL, 0 }
35 };
36
37 static char tmpfil[BUFSIZ];
38
39 /*
40  * static prototypes
41  */
42 static RETSIGTYPE intrser (int);
43
44
45 int
46 main (int argc, char **argv)
47 {
48     pid_t child_id;
49     int status, i, iscc = 0, nvec;
50     int queued = 0, resent = 0, somebody;
51     char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
52     char *from = NULL, *body = NULL, **argp, **arguments;
53     char *vec[5], buf[BUFSIZ];
54     FILE *out;
55     char *tfile = NULL;
56
57 #ifdef LOCALE
58     setlocale(LC_ALL, "");
59 #endif
60     invo_name = r1bindex (argv[0], '/');
61
62     /* foil search of user profile/context */
63     if (context_foil (NULL) == -1)
64         done (1);
65
66     /* If no arguments, just incorporate new mail */
67     if (argc == 1) {
68         execlp (incproc, r1bindex (incproc, '/'), NULL);
69         adios (incproc, "unable to exec");
70     }
71
72     arguments = getarguments (invo_name, argc, argv, 0);
73     argp = arguments;
74
75     while ((cp = *argp++)) {
76         if (*cp == '-') {
77             switch (smatch (++cp, switches)) {
78                 case AMBIGSW: 
79                     ambigsw (cp, switches);
80                     done (1);
81                 case UNKWNSW: 
82                     adios (NULL, "-%s unknown", cp);
83
84                 case HELPSW: 
85                     snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
86                         invo_name);
87                     print_help (buf, switches, 0);
88                     done (1);
89                 case VERSIONSW:
90                     print_version(invo_name);
91                     done (1);
92
93                 case FROMSW: 
94                     if (!(from = *argp++) || *from == '-')
95                         adios (NULL, "missing argument to %s", argp[-2]);
96                     continue;
97
98                 case BODYSW: 
99                     if (!(body = *argp++) || *body == '-')
100                         adios (NULL, "missing argument to %s", argp[-2]);
101                     continue;
102
103                 case CCSW: 
104                     iscc++;
105                     continue;
106
107                 case SUBJSW: 
108                     if (!(subject = *argp++) || *subject == '-')
109                         adios (NULL, "missing argument to %s", argp[-2]);
110                     continue;
111
112                 case RESNDSW: 
113                     resent++;
114                     continue;
115
116                 case QUEUESW: 
117                     queued++;
118                     continue;
119             }
120         }
121         if (iscc)
122             cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
123         else
124             tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
125     }
126
127     if (tolist == NULL)
128         adios (NULL, "usage: %s addrs ... [switches]", invo_name);
129
130     tfile = m_mktemp2(NULL, invo_name, NULL, &out);
131     if (tfile == NULL) adios("mhmail", "unable to create temporary file");
132     chmod(tfile, 0600);
133     strncpy (tmpfil, tfile, sizeof(tmpfil));
134
135     SIGNAL2 (SIGINT, intrser);
136
137     fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
138     if (cclist)
139         fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
140     if (subject)
141         fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
142     if (from)
143         fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
144     if (!resent)
145         fputs ("\n", out);
146
147     if (body) {
148         fprintf (out, "%s", body);
149         if (*body && *(body + strlen (body) - 1) != '\n')
150             fputs ("\n", out);
151     } else {
152         for (somebody = 0;
153                 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
154                 somebody++)
155             if (fwrite (buf, sizeof(*buf), i, out) != i)
156                 adios (tmpfil, "error writing");
157         if (!somebody) {
158             unlink (tmpfil);
159             done (1);
160         }
161     }
162     fclose (out);
163
164     nvec = 0;
165     vec[nvec++] = r1bindex (postproc, '/');
166     vec[nvec++] = tmpfil;
167     if (resent)
168         vec[nvec++] = "-dist";
169     if (queued)
170         vec[nvec++] = "-queued";
171     vec[nvec] = NULL;
172
173     for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
174         sleep (5);
175
176     if (child_id == NOTOK) {
177         /* report failure and then send it */
178         adios (NULL, "unable to fork");
179     } else if (child_id) {
180         /* parent process */
181         if ((status = pidXwait(child_id, postproc))) {
182             fprintf (stderr, "Letter saved in dead.letter\n");
183             execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
184             execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
185             perror ("mv");
186             _exit (-1);
187         }
188         unlink (tmpfil);
189         done (status ? 1 : 0);
190     } else {
191         /* child process */
192         execvp (postproc, vec);
193         fprintf (stderr, "unable to exec ");
194         perror (postproc);
195         _exit (-1);
196     }
197
198     return 0;  /* dead code to satisfy the compiler */
199 }
200
201
202 static RETSIGTYPE
203 intrser (int i)
204 {
205 #ifndef RELIABLE_SIGNALS
206     if (i)
207         SIGNAL (i, SIG_IGN);
208 #endif
209
210     unlink (tmpfil);
211     done (i != 0 ? 1 : 0);
212 }
213