]>
diplodocus.org Git - nmh/blob - uip/viamail.c
045f43feab3d6c5570c1c9a788567dc17c1c66bf
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_help.h"
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
17 #include "h/mhparse.h"
20 #include "sbr/m_mktemp.h"
21 #include "sbr/base64.h"
23 #define VIAMAIL_SWITCHES \
24 X("to mailpath", 0, TOSW) \
25 X("from mailpath", 0, FROMSW) \
26 X("subject subject", 0, SUBJECTSW) \
27 X("parameters arguments", 0, PARAMSW) \
28 X("description text", 0, DESCRIPTSW) \
29 X("comment text", 0, COMMENTSW) \
30 X("delay seconds", 0, DELAYSW) \
31 X("verbose", 0, VERBSW) \
32 X("noverbose", 0, NVERBSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
35 X("debug", -5, DEBUGSW) \
37 #define X(sw, minchars, id) id,
38 DEFINE_SWITCH_ENUM(VIAMAIL
);
41 #define X(sw, minchars, id) { sw, minchars, id },
42 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
52 static int via_mail (char *, char *, char *, char *, char *, int, char *);
56 main (int argc
, char **argv
)
59 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
60 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
61 char *cp
, buf
[BUFSIZ
];
62 char **argp
, **arguments
;
64 if (nmh_init(argv
[0], true, false)) { return 1; }
66 arguments
= getarguments (invo_name
, argc
, argv
, 0);
69 while ((cp
= *argp
++)) {
71 switch (smatch (++cp
, switches
)) {
73 ambigsw (cp
, switches
);
76 die("-%s unknown", cp
);
79 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
80 print_help (buf
, switches
, 1);
83 print_version(invo_name
);
88 die("missing argument to %s", argp
[-2]);
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]);
107 if (!(cp
= *argp
++) || *cp
== '-')
108 die("missing argument to %s", argp
[-2]);
111 * If there is an error, just reset the delay parameter
112 * to -1. We will set a default delay later.
114 if (sscanf (cp
, "%d", &delay
) != 1)
119 die("missing argument to %s", argp
[-2]);
137 die("missing -viamail \"mailpath\" switch");
139 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
140 return 0; /* dead code to satisfy the compiler */
149 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
150 char *cmntsw
, int delay
, char *fromsw
)
153 char tmpfil
[BUFSIZ
], *program
;
160 umask (~m_gmprot ());
162 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
163 die("unable to create temporary file in %s", get_temp_dir());
165 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
167 if (!strchr(mailsw
, '@'))
168 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
169 fprintf (fp
, "To: %s\n", mailsw
);
172 fprintf (fp
, "Subject: %s\n", subjsw
);
175 if (!strchr(fromsw
, '@'))
176 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
177 fprintf (fp
, "From: %s\n", fromsw
);
180 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
181 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
184 fprintf (fp
, "; %s", parmsw
);
187 fprintf (fp
, "\n\t(%s)", cmntsw
);
190 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
192 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
195 adios (tmpfil
, "error writing to");
197 writeBase64aux (stdin
, fp
, 0);
199 adios (tmpfil
, "error writing to");
201 if (fstat (fileno (fp
), &st
) == NOTOK
)
202 adios ("failed", "fstat of %s", tmpfil
);
211 vec
= argsplit(postproc
, &program
, &vecp
);
213 vec
[vecp
++] = "-verbose";
215 if ((cp
= context_find ("credentials"))) {
216 /* post doesn't read context so need to pass credentials. */
217 vec
[vecp
++] = "-credentials";
221 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
231 if (m_unlink (tmpfil
) == -1)
232 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);