]>
diplodocus.org Git - nmh/blob - uip/viamail.c
fb60ec30dc4e6eeb2bce9b30a960c1ce864ee9c5
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"
24 #include "h/mhparse.h"
27 #include "sbr/m_mktemp.h"
28 #include "sbr/base64.h"
30 #define VIAMAIL_SWITCHES \
31 X("to mailpath", 0, TOSW) \
32 X("from mailpath", 0, FROMSW) \
33 X("subject subject", 0, SUBJECTSW) \
34 X("parameters arguments", 0, PARAMSW) \
35 X("description text", 0, DESCRIPTSW) \
36 X("comment text", 0, COMMENTSW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41 X("debug", -5, DEBUGSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(VIAMAIL
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
57 static int via_mail (char *, char *, char *, char *, char *, char *);
61 main (int argc
, char **argv
)
63 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
64 char *f4
= NULL
, *f5
= NULL
, *f6
= 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]);
112 die("missing argument to %s", argp
[-2]);
130 die("missing -viamail \"mailpath\" switch");
132 via_mail (f1
, f2
, f3
, f4
, f5
, f6
);
133 return 0; /* dead code to satisfy the compiler */
142 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
143 char *cmntsw
, char *fromsw
)
146 char tmpfil
[BUFSIZ
], *program
;
153 umask (~m_gmprot ());
155 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
156 die("unable to create temporary file in %s", get_temp_dir());
158 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
160 if (!strchr(mailsw
, '@'))
161 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
162 fprintf (fp
, "To: %s\n", mailsw
);
165 fprintf (fp
, "Subject: %s\n", subjsw
);
168 if (!strchr(fromsw
, '@'))
169 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
170 fprintf (fp
, "From: %s\n", fromsw
);
173 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
174 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
177 fprintf (fp
, "; %s", parmsw
);
180 fprintf (fp
, "\n\t(%s)", cmntsw
);
183 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
185 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
188 adios (tmpfil
, "error writing to");
190 writeBase64aux (stdin
, fp
, 0);
192 adios (tmpfil
, "error writing to");
194 if (fstat (fileno (fp
), &st
) == NOTOK
)
195 adios ("failed", "fstat of %s", tmpfil
);
199 vec
= argsplit(postproc
, &program
, &vecp
);
201 vec
[vecp
++] = "-verbose";
203 if ((cp
= context_find ("credentials"))) {
204 /* post doesn't read context so need to pass credentials. */
205 vec
[vecp
++] = "-credentials";
209 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
219 if (m_unlink (tmpfil
) == -1)
220 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);