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