]>
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.
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/concat.h"
13 #include "sbr/smatch.h"
14 #include "sbr/context_find.h"
15 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
25 #include "h/mhparse.h"
28 #include "sbr/m_mktemp.h"
29 #include "sbr/base64.h"
31 #define VIAMAIL_SWITCHES \
32 X("to mailpath", 0, TOSW) \
33 X("from mailpath", 0, FROMSW) \
34 X("subject subject", 0, SUBJECTSW) \
35 X("parameters arguments", 0, PARAMSW) \
36 X("description text", 0, DESCRIPTSW) \
37 X("comment text", 0, COMMENTSW) \
38 X("delay seconds", 0, DELAYSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("debug", -5, DEBUGSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(VIAMAIL
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
60 static int via_mail (char *, char *, char *, char *, char *, int, char *);
64 main (int argc
, char **argv
)
67 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
68 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
69 char *cp
, buf
[BUFSIZ
];
70 char **argp
, **arguments
;
72 if (nmh_init(argv
[0], true, false)) { return 1; }
74 arguments
= getarguments (invo_name
, argc
, argv
, 0);
77 while ((cp
= *argp
++)) {
79 switch (smatch (++cp
, switches
)) {
81 ambigsw (cp
, switches
);
84 die("-%s unknown", cp
);
87 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
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]);
112 die("missing argument to %s", argp
[-2]);
115 if (!(cp
= *argp
++) || *cp
== '-')
116 die("missing argument to %s", argp
[-2]);
119 * If there is an error, just reset the delay parameter
120 * to -1. We will set a default delay later.
122 if (sscanf (cp
, "%d", &delay
) != 1)
127 die("missing argument to %s", argp
[-2]);
145 die("missing -viamail \"mailpath\" switch");
147 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
148 return 0; /* dead code to satisfy the compiler */
157 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
158 char *cmntsw
, int delay
, char *fromsw
)
161 char tmpfil
[BUFSIZ
], *program
;
168 umask (~m_gmprot ());
170 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
171 die("unable to create temporary file in %s", get_temp_dir());
173 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
175 if (!strchr(mailsw
, '@'))
176 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
177 fprintf (fp
, "To: %s\n", mailsw
);
180 fprintf (fp
, "Subject: %s\n", subjsw
);
183 if (!strchr(fromsw
, '@'))
184 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
185 fprintf (fp
, "From: %s\n", fromsw
);
188 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
189 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
192 fprintf (fp
, "; %s", parmsw
);
195 fprintf (fp
, "\n\t(%s)", cmntsw
);
198 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
200 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
203 adios (tmpfil
, "error writing to");
205 writeBase64aux (stdin
, fp
, 0);
207 adios (tmpfil
, "error writing to");
209 if (fstat (fileno (fp
), &st
) == NOTOK
)
210 adios ("failed", "fstat of %s", tmpfil
);
219 vec
= argsplit(postproc
, &program
, &vecp
);
221 vec
[vecp
++] = "-verbose";
223 if ((cp
= context_find ("credentials"))) {
224 /* post doesn't read context so need to pass credentials. */
225 vec
[vecp
++] = "-credentials";
229 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
239 if (m_unlink (tmpfil
) == -1)
240 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);