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