]>
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.
14 #include <h/mhparse.h>
16 #include "sbr/m_mktemp.h"
17 #include "sbr/base64.h"
19 #define VIAMAIL_SWITCHES \
20 X("to mailpath", 0, TOSW) \
21 X("from mailpath", 0, FROMSW) \
22 X("subject subject", 0, SUBJECTSW) \
23 X("parameters arguments", 0, PARAMSW) \
24 X("description text", 0, DESCRIPTSW) \
25 X("comment text", 0, COMMENTSW) \
26 X("delay seconds", 0, DELAYSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
31 X("debug", -5, DEBUGSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(VIAMAIL
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
48 static int via_mail (char *, char *, char *, char *, char *, int, char *);
52 main (int argc
, char **argv
)
55 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
56 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
57 char *cp
, buf
[BUFSIZ
];
58 char **argp
, **arguments
;
60 if (nmh_init(argv
[0], 2)) { return 1; }
62 arguments
= getarguments (invo_name
, argc
, argv
, 0);
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
72 adios (NULL
, "-%s unknown", cp
);
75 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
84 adios (NULL
, "missing argument to %s", argp
[-2]);
88 adios (NULL
, "missing argument to %s", argp
[-2]);
92 adios (NULL
, "missing argument to %s", argp
[-2]);
96 adios (NULL
, "missing argument to %s", argp
[-2]);
100 adios (NULL
, "missing argument to %s", argp
[-2]);
103 if (!(cp
= *argp
++) || *cp
== '-')
104 adios (NULL
, "missing argument to %s", argp
[-2]);
107 * If there is an error, just reset the delay parameter
108 * to -1. We will set a default delay later.
110 if (sscanf (cp
, "%d", &delay
) != 1)
115 adios (NULL
, "missing argument to %s", argp
[-2]);
133 adios (NULL
, "missing -viamail \"mailpath\" switch");
135 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
136 return 0; /* dead code to satisfy the compiler */
145 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
146 char *cmntsw
, int delay
, char *fromsw
)
149 char tmpfil
[BUFSIZ
], *program
;
156 umask (~m_gmprot ());
158 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
159 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
161 strncpy (tmpfil
, tfile
, sizeof(tmpfil
));
163 if (!strchr(mailsw
, '@'))
164 mailsw
= concat (mailsw
, "@", LocalName (0), NULL
);
165 fprintf (fp
, "To: %s\n", mailsw
);
168 fprintf (fp
, "Subject: %s\n", subjsw
);
171 if (!strchr(fromsw
, '@'))
172 fromsw
= concat (fromsw
, "@", LocalName (0), NULL
);
173 fprintf (fp
, "From: %s\n", fromsw
);
176 fprintf (fp
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
177 fprintf (fp
, "%s: application/octet-stream", TYPE_FIELD
);
180 fprintf (fp
, "; %s", parmsw
);
183 fprintf (fp
, "\n\t(%s)", cmntsw
);
186 fprintf (fp
, "\n%s: %s", DESCR_FIELD
, descsw
);
188 fprintf (fp
, "\n%s: %s\n\n", ENCODING_FIELD
, "base64");
191 adios (tmpfil
, "error writing to");
193 writeBase64aux (stdin
, fp
, 0);
195 adios (tmpfil
, "error writing to");
197 if (fstat (fileno (fp
), &st
) == NOTOK
)
198 adios ("failed", "fstat of %s", tmpfil
);
207 vec
= argsplit(postproc
, &program
, &vecp
);
209 vec
[vecp
++] = "-verbose";
211 if ((cp
= context_find ("credentials"))) {
212 /* post doesn't read context so need to pass credentials. */
213 vec
[vecp
++] = "-credentials";
217 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
227 if (m_unlink (tmpfil
) == -1)
228 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);