]>
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.
9 #include "sbr/smatch.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
21 #include "h/mhparse.h"
24 #include "sbr/m_mktemp.h"
25 #include "sbr/base64.h"
27 #define VIAMAIL_SWITCHES \
28 X("to mailpath", 0, TOSW) \
29 X("from mailpath", 0, FROMSW) \
30 X("subject subject", 0, SUBJECTSW) \
31 X("parameters arguments", 0, PARAMSW) \
32 X("description text", 0, DESCRIPTSW) \
33 X("comment text", 0, COMMENTSW) \
34 X("delay seconds", 0, DELAYSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("debug", -5, DEBUGSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(VIAMAIL
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
56 static int via_mail (char *, char *, char *, char *, char *, int, char *);
60 main (int argc
, char **argv
)
63 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
64 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
65 char *cp
, buf
[BUFSIZ
];
66 char **argp
, **arguments
;
68 if (nmh_init(argv
[0], true, false)) { return 1; }
70 arguments
= getarguments (invo_name
, argc
, argv
, 0);
73 while ((cp
= *argp
++)) {
75 switch (smatch (++cp
, switches
)) {
77 ambigsw (cp
, switches
);
80 die("-%s unknown", cp
);
83 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
84 print_help (buf
, switches
, 1);
87 print_version(invo_name
);
92 die("missing argument to %s", argp
[-2]);
96 die("missing argument to %s", argp
[-2]);
100 die("missing argument to %s", argp
[-2]);
104 die("missing argument to %s", argp
[-2]);
108 die("missing argument to %s", argp
[-2]);
111 if (!(cp
= *argp
++) || *cp
== '-')
112 die("missing argument to %s", argp
[-2]);
115 * If there is an error, just reset the delay parameter
116 * to -1. We will set a default delay later.
118 if (sscanf (cp
, "%d", &delay
) != 1)
123 die("missing argument to %s", argp
[-2]);
141 die("missing -viamail \"mailpath\" switch");
143 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
144 return 0; /* dead code to satisfy the compiler */
153 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
154 char *cmntsw
, int delay
, char *fromsw
)
157 char tmpfil
[BUFSIZ
], *program
;
164 umask (~m_gmprot ());
166 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
167 die("unable to create temporary file in %s", get_temp_dir());
169 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
171 if (!strchr(mailsw
, '@'))
172 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
173 fprintf (fp
, "To: %s\n", mailsw
);
176 fprintf (fp
, "Subject: %s\n", subjsw
);
179 if (!strchr(fromsw
, '@'))
180 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
181 fprintf (fp
, "From: %s\n", fromsw
);
184 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
185 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
188 fprintf (fp
, "; %s", parmsw
);
191 fprintf (fp
, "\n\t(%s)", cmntsw
);
194 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
196 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
199 adios (tmpfil
, "error writing to");
201 writeBase64aux (stdin
, fp
, 0);
203 adios (tmpfil
, "error writing to");
205 if (fstat (fileno (fp
), &st
) == NOTOK
)
206 adios ("failed", "fstat of %s", tmpfil
);
215 vec
= argsplit(postproc
, &program
, &vecp
);
217 vec
[vecp
++] = "-verbose";
219 if ((cp
= context_find ("credentials"))) {
220 /* post doesn't read context so need to pass credentials. */
221 vec
[vecp
++] = "-credentials";
225 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
235 if (m_unlink (tmpfil
) == -1)
236 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);