]>
diplodocus.org Git - nmh/blob - uip/viamail.c
f6d34c3fea6ee39c627e20405852ce54b0a34133
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/ambigsw.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/arglist.h"
13 #include "sbr/error.h"
19 #include "h/mhparse.h"
22 #include "sbr/m_mktemp.h"
23 #include "sbr/base64.h"
25 #define VIAMAIL_SWITCHES \
26 X("to mailpath", 0, TOSW) \
27 X("from mailpath", 0, FROMSW) \
28 X("subject subject", 0, SUBJECTSW) \
29 X("parameters arguments", 0, PARAMSW) \
30 X("description text", 0, DESCRIPTSW) \
31 X("comment text", 0, COMMENTSW) \
32 X("delay seconds", 0, DELAYSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37 X("debug", -5, DEBUGSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(VIAMAIL
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
54 static int via_mail (char *, char *, char *, char *, char *, int, char *);
58 main (int argc
, char **argv
)
61 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
62 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
63 char *cp
, buf
[BUFSIZ
];
64 char **argp
, **arguments
;
66 if (nmh_init(argv
[0], true, false)) { return 1; }
68 arguments
= getarguments (invo_name
, argc
, argv
, 0);
71 while ((cp
= *argp
++)) {
73 switch (smatch (++cp
, switches
)) {
75 ambigsw (cp
, switches
);
78 die("-%s unknown", cp
);
81 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
82 print_help (buf
, switches
, 1);
85 print_version(invo_name
);
90 die("missing argument to %s", argp
[-2]);
94 die("missing argument to %s", argp
[-2]);
98 die("missing argument to %s", argp
[-2]);
102 die("missing argument to %s", argp
[-2]);
106 die("missing argument to %s", argp
[-2]);
109 if (!(cp
= *argp
++) || *cp
== '-')
110 die("missing argument to %s", argp
[-2]);
113 * If there is an error, just reset the delay parameter
114 * to -1. We will set a default delay later.
116 if (sscanf (cp
, "%d", &delay
) != 1)
121 die("missing argument to %s", argp
[-2]);
139 die("missing -viamail \"mailpath\" switch");
141 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
142 return 0; /* dead code to satisfy the compiler */
151 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
152 char *cmntsw
, int delay
, char *fromsw
)
155 char tmpfil
[BUFSIZ
], *program
;
162 umask (~m_gmprot ());
164 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
165 die("unable to create temporary file in %s", get_temp_dir());
167 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
169 if (!strchr(mailsw
, '@'))
170 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
171 fprintf (fp
, "To: %s\n", mailsw
);
174 fprintf (fp
, "Subject: %s\n", subjsw
);
177 if (!strchr(fromsw
, '@'))
178 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
179 fprintf (fp
, "From: %s\n", fromsw
);
182 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
183 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
186 fprintf (fp
, "; %s", parmsw
);
189 fprintf (fp
, "\n\t(%s)", cmntsw
);
192 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
194 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
197 adios (tmpfil
, "error writing to");
199 writeBase64aux (stdin
, fp
, 0);
201 adios (tmpfil
, "error writing to");
203 if (fstat (fileno (fp
), &st
) == NOTOK
)
204 adios ("failed", "fstat of %s", tmpfil
);
213 vec
= argsplit(postproc
, &program
, &vecp
);
215 vec
[vecp
++] = "-verbose";
217 if ((cp
= context_find ("credentials"))) {
218 /* post doesn't read context so need to pass credentials. */
219 vec
[vecp
++] = "-credentials";
223 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
233 if (m_unlink (tmpfil
) == -1)
234 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);