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 static struct swit switches[] = {
25 { "from mailpath", 0 },
27 { "subject subject", 0 },
29 { "parameters arguments", 0 },
31 { "description text", 0 },
33 { "comment text", 0 },
35 { "delay seconds", 0 },
53 int ebcdicsw = 0; /* hack for linking purposes */
56 void set_endian (void);
59 int writeBase64aux (FILE *, FILE *);
64 static int via_mail (char *, char *, char *, char *, char *, int, char *);
68 main (int argc, char **argv)
71 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
72 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
73 char *cp, buf[BUFSIZ];
74 char **argp, **arguments;
77 setlocale(LC_ALL, "");
79 invo_name = r1bindex (argv[0], '/');
81 /* foil search of user profile/context */
82 if (context_foil (NULL) == -1)
85 arguments = getarguments (invo_name, argc, argv, 0);
88 while ((cp = *argp++)) {
90 switch (smatch (++cp, switches)) {
92 ambigsw (cp, switches);
95 adios (NULL, "-%s unknown", cp);
98 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
99 print_help (buf, switches, 1);
102 print_version(invo_name);
107 adios (NULL, "missing argument to %s", argp[-2]);
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]);
126 if (!(cp = *argp++) || *cp == '-')
127 adios (NULL, "missing argument to %s", argp[-2]);
130 * If there is an error, just reset the delay parameter
131 * to -1. We will set a default delay later.
133 if (sscanf (cp, "%d", &delay) != 1)
138 adios (NULL, "missing argument to %s", argp[-2]);
158 adios (NULL, "missing -viamail \"mailpath\" switch");
160 via_mail (f1, f2, f3, f4, f5, delay, f7);
161 return 0; /* dead code to satisfy the compiler */
170 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
171 char *cmntsw, int delay, char *fromsw)
173 int status, vecp = 1;
180 umask (~m_gmprot ());
182 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
183 if (tfile == NULL) adios("viamail", "unable to create temporary file");
185 strncpy (tmpfil, tfile, sizeof(tmpfil));
187 if (!strchr(mailsw, '@'))
188 mailsw = concat (mailsw, "@", LocalName (0), NULL);
189 fprintf (fp, "To: %s\n", mailsw);
192 fprintf (fp, "Subject: %s\n", subjsw);
195 if (!strchr(fromsw, '@'))
196 fromsw = concat (fromsw, "@", LocalName (0), NULL);
197 fprintf (fp, "From: %s\n", fromsw);
200 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
201 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
204 fprintf (fp, "; %s", parmsw);
207 fprintf (fp, "\n\t(%s)", cmntsw);
210 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
212 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
215 adios (tmpfil, "error writing to");
217 writeBase64aux (stdin, fp);
219 adios (tmpfil, "error writing to");
221 if (fstat (fileno (fp), &st) == NOTOK)
222 adios ("failed", "fstat of %s", tmpfil);
230 vec[0] = r1bindex (postproc, '/');
232 vec[vecp++] = "-verbose";
234 switch (sendsbr (vec, vecp, tmpfil, &st, 0, (char *)0, 0)) {
244 if (unlink (tmpfil) == -1)
245 advise (NULL, "unable to remove temp file %s", tmpfil);