No need to define the month and day names a second time
[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 #include <fcntl.h>
14 #include <unistd.h>
15 #include <sys/stat.h>
16 #include <locale.h>
17 #include <sysexits.h>
18
19 static struct swit switches[] = {
20 #define BODYSW  0
21         { "bodytext text", 0 },
22 #define CCSW  1
23         { "cc addrs ...", 0 },
24 #define FROMSW  2
25         { "from addr", 0 },
26 #define SUBJSW  3
27         { "subject text", 0 },
28 #define VERSIONSW  4
29         { "Version", 0 },
30 #define HELPSW  5
31         { "help", 0 },
32         { NULL, 0 }
33 };
34
35 static char tmpfil[BUFSIZ];
36
37 /*
38 ** static prototypes
39 */
40 static void intrser(int);
41
42
43 int
44 main(int argc, char **argv)
45 {
46         int status, iscc = 0, nvec;
47         char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
48         char *from = NULL, *body = NULL, **argp, **arguments;
49         char *vec[5], buf[BUFSIZ];
50         FILE *out;
51         char *tfile = NULL;
52
53         setlocale(LC_ALL, "");
54         invo_name = mhbasename(argv[0]);
55
56         /* Without arguments, exit. */
57         if (argc == 1) {
58                 adios(EX_USAGE, NULL, "no interactive mail shell. Use inc/scan/show instead.");
59         }
60
61         context_read();
62
63         arguments = getarguments(invo_name, argc, argv, 0);
64         argp = arguments;
65
66         while ((cp = *argp++)) {
67                 if (*cp == '-') {
68                         switch (smatch(++cp, switches)) {
69                         case AMBIGSW:
70                                 ambigsw(cp, switches);
71                                 exit(EX_USAGE);
72                         case UNKWNSW:
73                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
74
75                         case HELPSW:
76                                 snprintf(buf, sizeof(buf),
77                                                 "%s addrs... [switches]",
78                                                 invo_name);
79                                 print_help(buf, switches, 0);
80                                 exit(argc == 2 ? EX_OK : EX_USAGE);
81                         case VERSIONSW:
82                                 print_version(invo_name);
83                                 exit(argc == 2 ? EX_OK : EX_USAGE);
84
85                         case FROMSW:
86                                 if (!(from = *argp++) || *from == '-')
87                                         adios(EX_USAGE, NULL, "missing argument to %s",
88                                                         argp[-2]);
89                                 continue;
90
91                         case BODYSW:
92                                 if (!(body = *argp++) || *body == '-')
93                                         adios(EX_USAGE, NULL, "missing argument to %s",
94                                                         argp[-2]);
95                                 continue;
96
97                         case CCSW:
98                                 iscc++;
99                                 continue;
100
101                         case SUBJSW:
102                                 if (!(subject = *argp++) || *subject == '-')
103                                         adios(EX_USAGE, NULL, "missing argument to %s",
104                                                         argp[-2]);
105                                 continue;
106                         }
107                 }
108                 if (iscc)
109                         cclist = cclist ? add(cp, add(", ", cclist)) :
110                                         mh_xstrdup(cp);
111                 else
112                         tolist = tolist ? add(cp, add(", ", tolist)) :
113                                         mh_xstrdup(cp);
114         }
115
116         if (tolist == NULL)
117                 adios(EX_USAGE, NULL, "usage: %s addrs ... [switches]", invo_name);
118
119         tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
120         if (tfile == NULL)
121                 adios(EX_CANTCREAT, "mhmail", "unable to create temporary file");
122         chmod(tfile, 0600);
123         strncpy(tmpfil, tfile, sizeof(tmpfil));
124
125         SIGNAL2(SIGINT, intrser);
126
127         fprintf(out, "To: %s\n", tolist);
128         if (cclist)
129                 fprintf(out, "Cc: %s\n", cclist);
130         if (subject)
131                 fprintf(out, "Subject: %s\n", subject);
132         if (from)
133                 fprintf(out, "From: %s\n", from);
134         fputs("\n", out);
135
136         if (body) {
137                 fprintf(out, "%s", body);
138                 if (*body && body[strlen(body) - 1] != '\n')
139                         fputs("\n", out);
140         } else {
141                 int empty = 1;
142
143                 while (fgets(buf, sizeof buf, stdin)) {
144                         if (buf[0]=='.' && buf[1]=='\n') {
145                                 /* A period alone on a line means EOF. */
146                                 break;
147                         }
148                         empty = 0;
149                         if (fputs(buf, out) == EOF) {
150                                 adios(EX_IOERR, tmpfil, "error writing");
151                         }
152                 }
153                 if (empty) {
154                         unlink(tmpfil);
155                         adios(EX_DATAERR, NULL, "not sending message with empty body");
156                 }
157         }
158         fclose(out);
159
160         nvec = 0;
161         vec[nvec++] = "spost";
162         vec[nvec++] = tmpfil;
163         vec[nvec] = NULL;
164
165         if ((status = execprog(*vec, vec))) {
166                 /* spost failed, save draft as dead.letter */
167                 int in, out;
168
169                 in = open(tmpfil, O_RDONLY);
170                 out = creat("dead.letter", 0600);
171                 if (in == -1 || out == -1) {
172                         fprintf(stderr, "Letter left at %s.\n",
173                                         tmpfil);
174                         exit(status);
175                 }
176                 cpydata(in, out, tmpfil, "dead.letter");
177                 close(in);
178                 close(out);
179                 fprintf(stderr, "Letter saved in dead.letter\n");
180         }
181         unlink(tmpfil);
182         exit(status);
183 }
184
185
186 static void
187 intrser(int i)
188 {
189         unlink(tmpfil);
190         _exit(EX_IOERR);
191 }
192