3 * viamail.c -- send multiple files in a MIME message
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.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
25 static struct swit switches[] = {
29 { "from mailpath", 0 },
31 { "subject subject", 0 },
33 { "parameters arguments", 0 },
35 { "description text", 0 },
37 { "comment text", 0 },
39 { "delay seconds", 0 },
57 int ebcdicsw = 0; /* hack for linking purposes */
60 void set_endian (void);
63 int writeBase64aux (FILE *, FILE *);
68 static int via_mail (char *, char *, char *, char *, char *, int, char *);
72 main (int argc, char **argv)
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;
81 setlocale(LC_ALL, "");
83 invo_name = r1bindex (argv[0], '/');
85 /* foil search of user profile/context */
86 if (context_foil (NULL) == -1)
89 arguments = getarguments (invo_name, argc, argv, 0);
92 while ((cp = *argp++)) {
94 switch (smatch (++cp, switches)) {
96 ambigsw (cp, switches);
99 adios (NULL, "-%s unknown", cp);
102 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
103 print_help (buf, switches, 1);
106 print_version(invo_name);
111 adios (NULL, "missing argument to %s", argp[-2]);
115 adios (NULL, "missing argument to %s", argp[-2]);
119 adios (NULL, "missing argument to %s", argp[-2]);
123 adios (NULL, "missing argument to %s", argp[-2]);
127 adios (NULL, "missing argument to %s", argp[-2]);
130 if (!(cp = *argp++) || *cp == '-')
131 adios (NULL, "missing argument to %s", argp[-2]);
134 * If there is an error, just reset the delay parameter
135 * to -1. We will set a default delay later.
137 if (sscanf (cp, "%d", &delay) != 1)
142 adios (NULL, "missing argument to %s", argp[-2]);
162 adios (NULL, "missing -viamail \"mailpath\" switch");
164 via_mail (f1, f2, f3, f4, f5, delay, f7);
165 return 0; /* dead code to satisfy the compiler */
174 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
175 char *cmntsw, int delay, char *fromsw)
177 int status, vecp = 1;
184 umask (~m_gmprot ());
186 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
187 if (tfile == NULL) adios("viamail", "unable to create temporary file");
189 strncpy (tmpfil, tfile, sizeof(tmpfil));
191 if (!strchr(mailsw, '@'))
192 mailsw = concat (mailsw, "@", LocalName (), NULL);
193 fprintf (fp, "To: %s\n", mailsw);
196 fprintf (fp, "Subject: %s\n", subjsw);
199 if (!strchr(fromsw, '@'))
200 fromsw = concat (fromsw, "@", LocalName (), NULL);
201 fprintf (fp, "From: %s\n", fromsw);
204 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
205 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
208 fprintf (fp, "; %s", parmsw);
211 fprintf (fp, "\n\t(%s)", cmntsw);
214 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
216 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
219 adios (tmpfil, "error writing to");
221 writeBase64aux (stdin, fp);
223 adios (tmpfil, "error writing to");
225 if (fstat (fileno (fp), &st) == NOTOK)
226 adios ("failed", "fstat of %s", tmpfil);
234 vec[0] = r1bindex (postproc, '/');
236 vec[vecp++] = "-verbose";
238 switch (sendsbr (vec, vecp, tmpfil, &st, 0, (char *)0, 0)) {
248 if (unlink (tmpfil) == -1)
249 advise (NULL, "unable to remove temp file %s", tmpfil);