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 },
60 int ebcdicsw = 0; /* hack for linking purposes */
63 void set_endian (void);
66 int writeBase64aux (FILE *, FILE *);
71 static int via_mail (char *, char *, char *, char *, char *, int, char *);
75 main (int argc, char **argv)
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;
84 setlocale(LC_ALL, "");
86 invo_name = r1bindex (argv[0], '/');
88 /* foil search of user profile/context */
89 if (context_foil (NULL) == -1)
92 arguments = getarguments (invo_name, argc, argv, 0);
95 while ((cp = *argp++)) {
97 switch (smatch (++cp, switches)) {
99 ambigsw (cp, switches);
102 adios (NULL, "-%s unknown", cp);
105 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
106 print_help (buf, switches, 1);
109 print_version(invo_name);
114 adios (NULL, "missing argument to %s", argp[-2]);
118 adios (NULL, "missing argument to %s", argp[-2]);
122 adios (NULL, "missing argument to %s", argp[-2]);
126 adios (NULL, "missing argument to %s", argp[-2]);
130 adios (NULL, "missing argument to %s", argp[-2]);
133 if (!(cp = *argp++) || *cp == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
137 * If there is an error, just reset the delay parameter
138 * to -1. We will set a default delay later.
140 if (sscanf (cp, "%d", &delay) != 1)
145 adios (NULL, "missing argument to %s", argp[-2]);
165 adios (NULL, "missing -viamail \"mailpath\" switch");
167 via_mail (f1, f2, f3, f4, f5, delay, f7);
168 return 0; /* dead code to satisfy the compiler */
177 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
178 char *cmntsw, int delay, char *fromsw)
180 int status, vecp = 1;
186 umask (~m_gmprot ());
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);
193 if (!strchr(mailsw, '@'))
194 mailsw = concat (mailsw, "@", LocalName (), NULL);
195 fprintf (fp, "To: %s\n", mailsw);
198 fprintf (fp, "Subject: %s\n", subjsw);
201 if (!strchr(fromsw, '@'))
202 fromsw = concat (fromsw, "@", LocalName (), NULL);
203 fprintf (fp, "From: %s\n", fromsw);
206 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
207 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
210 fprintf (fp, "; %s", parmsw);
213 fprintf (fp, "\n\t(%s)", cmntsw);
216 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
218 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
221 adios (tmpfil, "error writing to");
223 writeBase64aux (stdin, fp);
225 adios (tmpfil, "error writing to");
227 if (fstat (fileno (fp), &st) == NOTOK)
228 adios ("failed", "fstat of %s", tmpfil);
236 vec[0] = r1bindex (postproc, '/');
238 vec[vecp++] = "-verbose";
240 switch (sendsbr (vec, vecp, tmpfil, &st, 0, (char *)0)) {
250 if (unlink (tmpfil) == -1)
251 advise (NULL, "unable to remove temp file %s", tmpfil);
252 return done (status);