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