Move #include from h/mh.h to source files
[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
18 static struct swit switches[] = {
19 #define BODYSW  0
20         { "bodytext text", 0 },
21 #define CCSW  1
22         { "cc addrs ...", 0 },
23 #define FROMSW  2
24         { "from addr", 0 },
25 #define SUBJSW  3
26         { "subject text", 0 },
27 #define VERSIONSW  4
28         { "Version", 0 },
29 #define HELPSW  5
30         { "help", 0 },
31         { NULL, 0 }
32 };
33
34 static char tmpfil[BUFSIZ];
35
36 /*
37 ** static prototypes
38 */
39 static void intrser(int);
40
41
42 int
43 main(int argc, char **argv)
44 {
45         int status, iscc = 0, nvec;
46         char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
47         char *from = NULL, *body = NULL, **argp, **arguments;
48         char *vec[5], buf[BUFSIZ];
49         FILE *out;
50         char *tfile = NULL;
51
52         setlocale(LC_ALL, "");
53         invo_name = mhbasename(argv[0]);
54
55         /* Without arguments, exit. */
56         if (argc == 1) {
57                 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
58         }
59
60         context_read();
61
62         arguments = getarguments(invo_name, argc, argv, 0);
63         argp = arguments;
64
65         while ((cp = *argp++)) {
66                 if (*cp == '-') {
67                         switch (smatch(++cp, switches)) {
68                         case AMBIGSW:
69                                 ambigsw(cp, switches);
70                                 /* sysexits.h EX_USAGE */
71                                 exit(1);
72                         case UNKWNSW:
73                                 adios(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(0);
81                         case VERSIONSW:
82                                 print_version(invo_name);
83                                 exit(0);
84
85                         case FROMSW:
86                                 if (!(from = *argp++) || *from == '-')
87                                         adios(NULL, "missing argument to %s",
88                                                         argp[-2]);
89                                 continue;
90
91                         case BODYSW:
92                                 if (!(body = *argp++) || *body == '-')
93                                         adios(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(NULL, "missing argument to %s",
104                                                         argp[-2]);
105                                 continue;
106                         }
107                 }
108                 if (iscc)
109                         cclist = cclist ? add(cp, add(", ", cclist)) :
110                                         getcpy(cp);
111                 else
112                         tolist = tolist ? add(cp, add(", ", tolist)) :
113                                         getcpy(cp);
114         }
115
116         if (tolist == NULL)
117                 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
118
119         tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
120         if (tfile == NULL)
121                 adios("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(tmpfil, "error writing");
151                         }
152                 }
153                 if (empty) {
154                         unlink(tmpfil);
155                         adios(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                         /* sysexits.h exit-status from spost */
175                         exit(status ? 1 : 0);
176                 }
177                 cpydata(in, out, tmpfil, "dead.letter");
178                 close(in);
179                 close(out);
180                 fprintf(stderr, "Letter saved in dead.letter\n");
181         }
182         unlink(tmpfil);
183         /* sysexits.h exit status from spost */
184         exit(status ? 1 : 0);
185 }
186
187
188 static void
189 intrser(int i)
190 {
191         unlink(tmpfil);
192         exit(i != 0 ? 1 : 0);
193 }
194