e9b26b76e3eb63c3cfae266cb75aa82de1181504
[mmh] / uip / viamail.c
1
2 /*
3  * viamail.c -- send multiple files in a MIME message
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <errno.h>
15 #include <signal.h>
16 #include <h/mts.h>
17 #include <h/tws.h>
18 #include <h/mime.h>
19 #include <h/mhparse.h>
20
21 static struct swit switches[] = {
22 #define TOSW                    0
23     { "to mailpath", 0 },
24 #define FROMSW                  1
25     { "from mailpath", 0 },
26 #define SUBJECTSW               2
27     { "subject subject", 0 },
28 #define PARAMSW                 3
29     { "parameters arguments", 0 },
30 #define DESCRIPTSW              4
31     { "description text", 0 },
32 #define COMMENTSW               5
33     { "comment text", 0 },
34 #define DELAYSW                 6
35     { "delay seconds", 0 },
36 #define VERBSW                  7
37     { "verbose", 0 },
38 #define NVERBSW                 8
39     { "noverbose", 0 },
40 #define VERSIONSW               9
41     { "version", 0 },
42 #define HELPSW                 10
43     { "help", 0 },
44 #define DEBUGSW                11
45     { "debug", -5 },
46     { NULL, 0 }
47 };
48
49 extern int debugsw;
50 extern int splitsw;
51 extern int verbsw;
52
53 int ebcdicsw = 0;       /* hack for linking purposes */
54
55 /*
56  * static prototypes
57  */
58 static int via_mail (char *, char *, char *, char *, char *, int, char *);
59
60
61 int
62 main (int argc, char **argv)
63 {
64     int delay = 0;
65     char *f1 = NULL, *f2 = NULL, *f3 = NULL;
66     char *f4 = NULL, *f5 = NULL, *f7 = NULL;
67     char *cp, buf[BUFSIZ];
68     char **argp, **arguments;
69
70 #ifdef LOCALE
71     setlocale(LC_ALL, "");
72 #endif
73     invo_name = r1bindex (argv[0], '/');
74
75     /* foil search of user profile/context */
76     if (context_foil (NULL) == -1)
77         done (1);
78
79     arguments = getarguments (invo_name, argc, argv, 0);
80     argp = arguments;
81
82     while ((cp = *argp++)) {
83         if (*cp == '-') {
84             switch (smatch (++cp, switches)) {
85             case AMBIGSW: 
86                 ambigsw (cp, switches);
87                 done (1);
88             case UNKWNSW: 
89                 adios (NULL, "-%s unknown", cp);
90
91             case HELPSW: 
92                 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
93                 print_help (buf, switches, 1);
94                 done (0);
95             case VERSIONSW:
96                 print_version(invo_name);
97                 done (0);
98     
99             case TOSW:
100                 if (!(f1 = *argp++))
101                     adios (NULL, "missing argument to %s", argp[-2]);
102                 continue;
103             case SUBJECTSW:
104                 if (!(f2 = *argp++))
105                     adios (NULL, "missing argument to %s", argp[-2]);
106                 continue;
107             case PARAMSW:
108                 if (!(f3 = *argp++))
109                     adios (NULL, "missing argument to %s", argp[-2]);
110                 continue;
111             case DESCRIPTSW:
112                 if (!(f4 = *argp++))
113                     adios (NULL, "missing argument to %s", argp[-2]);
114                 continue;
115             case COMMENTSW:
116                 if (!(f5 = *argp++))
117                     adios (NULL, "missing argument to %s", argp[-2]);
118                 continue;
119             case DELAYSW:
120                 if (!(cp = *argp++) || *cp == '-')
121                     adios (NULL, "missing argument to %s", argp[-2]);
122
123                 /*
124                  * If there is an error, just reset the delay parameter
125                  * to -1.  We will set a default delay later.
126                  */
127                 if (sscanf (cp, "%d", &delay) != 1)
128                     delay = -1;
129                 continue;
130             case FROMSW:
131                 if (!(f7 = *argp++))
132                     adios (NULL, "missing argument to %s", argp[-2]);
133                 continue;
134
135             case VERBSW: 
136                 verbsw = 1;
137                 continue;
138             case NVERBSW: 
139                 verbsw = 0;
140                 continue;
141
142             case DEBUGSW:
143                 debugsw = 1;
144                 continue;
145             }
146         }
147     }
148
149     if (!f1)
150         adios (NULL, "missing -viamail \"mailpath\" switch");
151
152     via_mail (f1, f2, f3, f4, f5, delay, f7);
153     return 0;  /* dead code to satisfy the compiler */
154 }
155
156
157 /*
158  * VIAMAIL
159  */
160
161 static int
162 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
163           char *cmntsw, int delay, char *fromsw)
164 {
165     int status, vecp = 1;
166     char tmpfil[BUFSIZ];
167     char *vec[MAXARGS];
168     struct stat st;
169     FILE *fp;
170     char *tfile = NULL;
171
172     umask (~m_gmprot ());
173
174     tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
175     if (tfile == NULL) adios("viamail", "unable to create temporary file");
176     chmod(tfile, 0600);
177     strncpy (tmpfil, tfile, sizeof(tmpfil));
178
179     if (!strchr(mailsw, '@'))
180         mailsw = concat (mailsw, "@", LocalName (0), NULL);
181     fprintf (fp, "To: %s\n", mailsw);
182
183     if (subjsw)
184         fprintf (fp, "Subject: %s\n", subjsw);
185
186     if (fromsw) {
187         if (!strchr(fromsw, '@'))
188             fromsw = concat (fromsw, "@", LocalName (0), NULL);
189         fprintf (fp, "From: %s\n", fromsw);
190     }
191
192     fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
193     fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
194
195     if (parmsw)
196         fprintf (fp, "; %s", parmsw);
197
198     if (cmntsw)
199         fprintf (fp, "\n\t(%s)", cmntsw);
200
201     if (descsw)
202         fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
203
204     fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
205
206     if (fflush (fp))
207         adios (tmpfil, "error writing to");
208
209     writeBase64aux (stdin, fp);
210     if (fflush (fp))
211         adios (tmpfil, "error writing to");
212
213     if (fstat (fileno (fp), &st) == NOTOK)
214         adios ("failed", "fstat of %s", tmpfil);
215
216     if (delay < 0)
217         splitsw = 10;
218     else
219         splitsw = delay;
220
221     status = 0;
222     vec[0] = r1bindex (postproc, '/');
223     if (verbsw)
224         vec[vecp++] = "-verbose";
225
226     switch (sendsbr (vec, vecp, tmpfil, &st, 0, (char *)0, 0)) {
227         case DONE:
228         case NOTOK:
229             status++;
230             break;
231         case OK:
232             break;
233     }
234
235     fclose (fp);
236     if (unlink (tmpfil) == -1)
237         advise (NULL, "unable to remove temp file %s", tmpfil);
238     done (status);
239     return 1;
240 }