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