]>
diplodocus.org Git - nmh/blob - uip/viamail.c
3324d46bfbc0f2a0f5314c6002dd828f1d6a1720
1 /* viamail.c -- send multiple files in a MIME message
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
15 #include <h/mhparse.h>
18 #include "sbr/m_mktemp.h"
19 #include "sbr/base64.h"
21 #define VIAMAIL_SWITCHES \
22 X("to mailpath", 0, TOSW) \
23 X("from mailpath", 0, FROMSW) \
24 X("subject subject", 0, SUBJECTSW) \
25 X("parameters arguments", 0, PARAMSW) \
26 X("description text", 0, DESCRIPTSW) \
27 X("comment text", 0, COMMENTSW) \
28 X("delay seconds", 0, DELAYSW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("version", 0, VERSIONSW) \
32 X("help", 0, HELPSW) \
33 X("debug", -5, DEBUGSW) \
35 #define X(sw, minchars, id) id,
36 DEFINE_SWITCH_ENUM(VIAMAIL
);
39 #define X(sw, minchars, id) { sw, minchars, id },
40 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
50 static int via_mail (char *, char *, char *, char *, char *, int, char *);
54 main (int argc
, char **argv
)
57 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
58 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
59 char *cp
, buf
[BUFSIZ
];
60 char **argp
, **arguments
;
62 if (nmh_init(argv
[0], true, false)) { return 1; }
64 arguments
= getarguments (invo_name
, argc
, argv
, 0);
67 while ((cp
= *argp
++)) {
69 switch (smatch (++cp
, switches
)) {
71 ambigsw (cp
, switches
);
74 die("-%s unknown", cp
);
77 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
78 print_help (buf
, switches
, 1);
81 print_version(invo_name
);
86 die("missing argument to %s", argp
[-2]);
90 die("missing argument to %s", argp
[-2]);
94 die("missing argument to %s", argp
[-2]);
98 die("missing argument to %s", argp
[-2]);
102 die("missing argument to %s", argp
[-2]);
105 if (!(cp
= *argp
++) || *cp
== '-')
106 die("missing argument to %s", argp
[-2]);
109 * If there is an error, just reset the delay parameter
110 * to -1. We will set a default delay later.
112 if (sscanf (cp
, "%d", &delay
) != 1)
117 die("missing argument to %s", argp
[-2]);
135 die("missing -viamail \"mailpath\" switch");
137 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
138 return 0; /* dead code to satisfy the compiler */
147 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
148 char *cmntsw
, int delay
, char *fromsw
)
151 char tmpfil
[BUFSIZ
], *program
;
158 umask (~m_gmprot ());
160 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
161 die("unable to create temporary file in %s", get_temp_dir());
163 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
165 if (!strchr(mailsw
, '@'))
166 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
167 fprintf (fp
, "To: %s\n", mailsw
);
170 fprintf (fp
, "Subject: %s\n", subjsw
);
173 if (!strchr(fromsw
, '@'))
174 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
175 fprintf (fp
, "From: %s\n", fromsw
);
178 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
179 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
182 fprintf (fp
, "; %s", parmsw
);
185 fprintf (fp
, "\n\t(%s)", cmntsw
);
188 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
190 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
193 adios (tmpfil
, "error writing to");
195 writeBase64aux (stdin
, fp
, 0);
197 adios (tmpfil
, "error writing to");
199 if (fstat (fileno (fp
), &st
) == NOTOK
)
200 adios ("failed", "fstat of %s", tmpfil
);
209 vec
= argsplit(postproc
, &program
, &vecp
);
211 vec
[vecp
++] = "-verbose";
213 if ((cp
= context_find ("credentials"))) {
214 /* post doesn't read context so need to pass credentials. */
215 vec
[vecp
++] = "-credentials";
219 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
229 if (m_unlink (tmpfil
) == -1)
230 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);