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