3 * viamail.c -- send multiple files in a MIME message
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.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
23 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
27 static struct swit switches[] = {
31 { "from mailpath", 0 },
33 { "subject subject", 0 },
35 { "parameters arguments", 0 },
37 { "description text", 0 },
39 { "comment text", 0 },
41 { "delay seconds", 0 },
59 int ebcdicsw = 0; /* hack for linking purposes */
62 void set_endian (void);
65 int writeBase64aux (FILE *, FILE *);
70 static int via_mail (char *, char *, char *, char *, char *, int, char *);
74 main (int argc, char **argv)
77 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
78 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
79 char *cp, buf[BUFSIZ];
80 char **argp, **arguments;
83 setlocale(LC_ALL, "");
85 invo_name = r1bindex (argv[0], '/');
87 /* foil search of user profile/context */
88 if (context_foil (NULL) == -1)
91 arguments = getarguments (invo_name, argc, argv, 0);
94 while ((cp = *argp++)) {
96 switch (smatch (++cp, switches)) {
98 ambigsw (cp, switches);
101 adios (NULL, "-%s unknown", cp);
104 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
105 print_help (buf, switches, 1);
108 print_version(invo_name);
113 adios (NULL, "missing argument to %s", argp[-2]);
117 adios (NULL, "missing argument to %s", argp[-2]);
121 adios (NULL, "missing argument to %s", argp[-2]);
125 adios (NULL, "missing argument to %s", argp[-2]);
129 adios (NULL, "missing argument to %s", argp[-2]);
132 if (!(cp = *argp++) || *cp == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
136 * If there is an error, just reset the delay parameter
137 * to -1. We will set a default delay later.
139 if (sscanf (cp, "%d", &delay) != 1)
144 adios (NULL, "missing argument to %s", argp[-2]);
164 adios (NULL, "missing -viamail \"mailpath\" switch");
166 via_mail (f1, f2, f3, f4, f5, delay, f7);
167 return 0; /* dead code to satisfy the compiler */
176 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
177 char *cmntsw, int delay, char *fromsw)
179 int status, vecp = 1;
185 umask (~m_gmprot ());
187 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
188 if ((fp = fopen (tmpfil, "w+")) == NULL)
189 adios (tmpfil, "unable to open for writing");
190 chmod (tmpfil, 0600);
192 if (!strchr(mailsw, '@'))
193 mailsw = concat (mailsw, "@", LocalName (), NULL);
194 fprintf (fp, "To: %s\n", mailsw);
197 fprintf (fp, "Subject: %s\n", subjsw);
200 if (!strchr(fromsw, '@'))
201 fromsw = concat (fromsw, "@", LocalName (), NULL);
202 fprintf (fp, "From: %s\n", fromsw);
205 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
206 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
209 fprintf (fp, "; %s", parmsw);
212 fprintf (fp, "\n\t(%s)", cmntsw);
215 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
217 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
220 adios (tmpfil, "error writing to");
222 writeBase64aux (stdin, fp);
224 adios (tmpfil, "error writing to");
226 if (fstat (fileno (fp), &st) == NOTOK)
227 adios ("failed", "fstat of %s", tmpfil);
235 vec[0] = r1bindex (postproc, '/');
237 vec[vecp++] = "-verbose";
239 switch (sendsbr (vec, vecp, tmpfil, &st, 0, (char *)0, 0)) {
249 if (unlink (tmpfil) == -1)
250 advise (NULL, "unable to remove temp file %s", tmpfil);
251 return done (status);