]>
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/arglist.h"
12 #include "sbr/error.h"
18 #include "h/mhparse.h"
21 #include "sbr/m_mktemp.h"
22 #include "sbr/base64.h"
24 #define VIAMAIL_SWITCHES \
25 X("to mailpath", 0, TOSW) \
26 X("from mailpath", 0, FROMSW) \
27 X("subject subject", 0, SUBJECTSW) \
28 X("parameters arguments", 0, PARAMSW) \
29 X("description text", 0, DESCRIPTSW) \
30 X("comment text", 0, COMMENTSW) \
31 X("delay seconds", 0, DELAYSW) \
32 X("verbose", 0, VERBSW) \
33 X("noverbose", 0, NVERBSW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("debug", -5, DEBUGSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(VIAMAIL
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
53 static int via_mail (char *, char *, char *, char *, char *, int, char *);
57 main (int argc
, char **argv
)
60 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
61 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
62 char *cp
, buf
[BUFSIZ
];
63 char **argp
, **arguments
;
65 if (nmh_init(argv
[0], true, false)) { return 1; }
67 arguments
= getarguments (invo_name
, argc
, argv
, 0);
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
77 die("-%s unknown", cp
);
80 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
81 print_help (buf
, switches
, 1);
84 print_version(invo_name
);
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]);
105 die("missing argument to %s", argp
[-2]);
108 if (!(cp
= *argp
++) || *cp
== '-')
109 die("missing argument to %s", argp
[-2]);
112 * If there is an error, just reset the delay parameter
113 * to -1. We will set a default delay later.
115 if (sscanf (cp
, "%d", &delay
) != 1)
120 die("missing argument to %s", argp
[-2]);
138 die("missing -viamail \"mailpath\" switch");
140 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
141 return 0; /* dead code to satisfy the compiler */
150 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
151 char *cmntsw
, int delay
, char *fromsw
)
154 char tmpfil
[BUFSIZ
], *program
;
161 umask (~m_gmprot ());
163 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
164 die("unable to create temporary file in %s", get_temp_dir());
166 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
168 if (!strchr(mailsw
, '@'))
169 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
170 fprintf (fp
, "To: %s\n", mailsw
);
173 fprintf (fp
, "Subject: %s\n", subjsw
);
176 if (!strchr(fromsw
, '@'))
177 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
178 fprintf (fp
, "From: %s\n", fromsw
);
181 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
182 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
185 fprintf (fp
, "; %s", parmsw
);
188 fprintf (fp
, "\n\t(%s)", cmntsw
);
191 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
193 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
196 adios (tmpfil
, "error writing to");
198 writeBase64aux (stdin
, fp
, 0);
200 adios (tmpfil
, "error writing to");
202 if (fstat (fileno (fp
), &st
) == NOTOK
)
203 adios ("failed", "fstat of %s", tmpfil
);
212 vec
= argsplit(postproc
, &program
, &vecp
);
214 vec
[vecp
++] = "-verbose";
216 if ((cp
= context_find ("credentials"))) {
217 /* post doesn't read context so need to pass credentials. */
218 vec
[vecp
++] = "-credentials";
222 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
232 if (m_unlink (tmpfil
) == -1)
233 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);