]>
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/context_find.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
20 #include "h/mhparse.h"
23 #include "sbr/m_mktemp.h"
24 #include "sbr/base64.h"
26 #define VIAMAIL_SWITCHES \
27 X("to mailpath", 0, TOSW) \
28 X("from mailpath", 0, FROMSW) \
29 X("subject subject", 0, SUBJECTSW) \
30 X("parameters arguments", 0, PARAMSW) \
31 X("description text", 0, DESCRIPTSW) \
32 X("comment text", 0, COMMENTSW) \
33 X("delay seconds", 0, DELAYSW) \
34 X("verbose", 0, VERBSW) \
35 X("noverbose", 0, NVERBSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 X("debug", -5, DEBUGSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(VIAMAIL
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
55 static int via_mail (char *, char *, char *, char *, char *, int, char *);
59 main (int argc
, char **argv
)
62 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
63 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
64 char *cp
, buf
[BUFSIZ
];
65 char **argp
, **arguments
;
67 if (nmh_init(argv
[0], true, false)) { return 1; }
69 arguments
= getarguments (invo_name
, argc
, argv
, 0);
72 while ((cp
= *argp
++)) {
74 switch (smatch (++cp
, switches
)) {
76 ambigsw (cp
, switches
);
79 die("-%s unknown", cp
);
82 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
83 print_help (buf
, switches
, 1);
86 print_version(invo_name
);
91 die("missing argument to %s", argp
[-2]);
95 die("missing argument to %s", argp
[-2]);
99 die("missing argument to %s", argp
[-2]);
103 die("missing argument to %s", argp
[-2]);
107 die("missing argument to %s", argp
[-2]);
110 if (!(cp
= *argp
++) || *cp
== '-')
111 die("missing argument to %s", argp
[-2]);
114 * If there is an error, just reset the delay parameter
115 * to -1. We will set a default delay later.
117 if (sscanf (cp
, "%d", &delay
) != 1)
122 die("missing argument to %s", argp
[-2]);
140 die("missing -viamail \"mailpath\" switch");
142 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
143 return 0; /* dead code to satisfy the compiler */
152 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
153 char *cmntsw
, int delay
, char *fromsw
)
156 char tmpfil
[BUFSIZ
], *program
;
163 umask (~m_gmprot ());
165 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
166 die("unable to create temporary file in %s", get_temp_dir());
168 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
170 if (!strchr(mailsw
, '@'))
171 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
172 fprintf (fp
, "To: %s\n", mailsw
);
175 fprintf (fp
, "Subject: %s\n", subjsw
);
178 if (!strchr(fromsw
, '@'))
179 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
180 fprintf (fp
, "From: %s\n", fromsw
);
183 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
184 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
187 fprintf (fp
, "; %s", parmsw
);
190 fprintf (fp
, "\n\t(%s)", cmntsw
);
193 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
195 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
198 adios (tmpfil
, "error writing to");
200 writeBase64aux (stdin
, fp
, 0);
202 adios (tmpfil
, "error writing to");
204 if (fstat (fileno (fp
), &st
) == NOTOK
)
205 adios ("failed", "fstat of %s", tmpfil
);
214 vec
= argsplit(postproc
, &program
, &vecp
);
216 vec
[vecp
++] = "-verbose";
218 if ((cp
= context_find ("credentials"))) {
219 /* post doesn't read context so need to pass credentials. */
220 vec
[vecp
++] = "-credentials";
224 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
234 if (m_unlink (tmpfil
) == -1)
235 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);