]>
diplodocus.org Git - nmh/blob - uip/viamail.c
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.
14 #include <h/mhparse.h>
17 #include "sbr/m_mktemp.h"
18 #include "sbr/base64.h"
20 #define VIAMAIL_SWITCHES \
21 X("to mailpath", 0, TOSW) \
22 X("from mailpath", 0, FROMSW) \
23 X("subject subject", 0, SUBJECTSW) \
24 X("parameters arguments", 0, PARAMSW) \
25 X("description text", 0, DESCRIPTSW) \
26 X("comment text", 0, COMMENTSW) \
27 X("delay seconds", 0, DELAYSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
32 X("debug", -5, DEBUGSW) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(VIAMAIL
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
49 static int via_mail (char *, char *, char *, char *, char *, int, char *);
53 main (int argc
, char **argv
)
56 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
57 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
58 char *cp
, buf
[BUFSIZ
];
59 char **argp
, **arguments
;
61 if (nmh_init(argv
[0], true, false)) { return 1; }
63 arguments
= getarguments (invo_name
, argc
, argv
, 0);
66 while ((cp
= *argp
++)) {
68 switch (smatch (++cp
, switches
)) {
70 ambigsw (cp
, switches
);
73 die("-%s unknown", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
77 print_help (buf
, switches
, 1);
80 print_version(invo_name
);
85 die("missing argument to %s", argp
[-2]);
89 die("missing argument to %s", argp
[-2]);
93 die("missing argument to %s", argp
[-2]);
97 die("missing argument to %s", argp
[-2]);
101 die("missing argument to %s", argp
[-2]);
104 if (!(cp
= *argp
++) || *cp
== '-')
105 die("missing argument to %s", argp
[-2]);
108 * If there is an error, just reset the delay parameter
109 * to -1. We will set a default delay later.
111 if (sscanf (cp
, "%d", &delay
) != 1)
116 die("missing argument to %s", argp
[-2]);
134 die("missing -viamail \"mailpath\" switch");
136 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
137 return 0; /* dead code to satisfy the compiler */
146 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
147 char *cmntsw
, int delay
, char *fromsw
)
150 char tmpfil
[BUFSIZ
], *program
;
157 umask (~m_gmprot ());
159 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
160 die("unable to create temporary file in %s", get_temp_dir());
162 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
164 if (!strchr(mailsw
, '@'))
165 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
166 fprintf (fp
, "To: %s\n", mailsw
);
169 fprintf (fp
, "Subject: %s\n", subjsw
);
172 if (!strchr(fromsw
, '@'))
173 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
174 fprintf (fp
, "From: %s\n", fromsw
);
177 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
178 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
181 fprintf (fp
, "; %s", parmsw
);
184 fprintf (fp
, "\n\t(%s)", cmntsw
);
187 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
189 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
192 adios (tmpfil
, "error writing to");
194 writeBase64aux (stdin
, fp
, 0);
196 adios (tmpfil
, "error writing to");
198 if (fstat (fileno (fp
), &st
) == NOTOK
)
199 adios ("failed", "fstat of %s", tmpfil
);
208 vec
= argsplit(postproc
, &program
, &vecp
);
210 vec
[vecp
++] = "-verbose";
212 if ((cp
= context_find ("credentials"))) {
213 /* post doesn't read context so need to pass credentials. */
214 vec
[vecp
++] = "-credentials";
218 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
228 if (m_unlink (tmpfil
) == -1)
229 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);