]>
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 #define VIAMAIL_SWITCHES \
17 X("to mailpath", 0, TOSW) \
18 X("from mailpath", 0, FROMSW) \
19 X("subject subject", 0, SUBJECTSW) \
20 X("parameters arguments", 0, PARAMSW) \
21 X("description text", 0, DESCRIPTSW) \
22 X("comment text", 0, COMMENTSW) \
23 X("delay seconds", 0, DELAYSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28 X("debug", -5, DEBUGSW) \
30 #define X(sw, minchars, id) id,
31 DEFINE_SWITCH_ENUM(VIAMAIL
);
34 #define X(sw, minchars, id) { sw, minchars, id },
35 DEFINE_SWITCH_ARRAY(VIAMAIL
, switches
);
45 static int via_mail (char *, char *, char *, char *, char *, int, char *);
49 main (int argc
, char **argv
)
52 char *f1
= NULL
, *f2
= NULL
, *f3
= NULL
;
53 char *f4
= NULL
, *f5
= NULL
, *f7
= NULL
;
54 char *cp
, buf
[BUFSIZ
];
55 char **argp
, **arguments
;
57 if (nmh_init(argv
[0], 2)) { return 1; }
59 arguments
= getarguments (invo_name
, argc
, argv
, 0);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 adios (NULL
, "-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
73 print_help (buf
, switches
, 1);
76 print_version(invo_name
);
81 adios (NULL
, "missing argument to %s", argp
[-2]);
85 adios (NULL
, "missing argument to %s", argp
[-2]);
89 adios (NULL
, "missing argument to %s", argp
[-2]);
93 adios (NULL
, "missing argument to %s", argp
[-2]);
97 adios (NULL
, "missing argument to %s", argp
[-2]);
100 if (!(cp
= *argp
++) || *cp
== '-')
101 adios (NULL
, "missing argument to %s", argp
[-2]);
104 * If there is an error, just reset the delay parameter
105 * to -1. We will set a default delay later.
107 if (sscanf (cp
, "%d", &delay
) != 1)
112 adios (NULL
, "missing argument to %s", argp
[-2]);
130 adios (NULL
, "missing -viamail \"mailpath\" switch");
132 via_mail (f1
, f2
, f3
, f4
, f5
, delay
, f7
);
133 return 0; /* dead code to satisfy the compiler */
142 via_mail (char *mailsw
, char *subjsw
, char *parmsw
, char *descsw
,
143 char *cmntsw
, int delay
, char *fromsw
)
146 char tmpfil
[BUFSIZ
], *program
;
153 umask (~m_gmprot ());
155 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, &fp
)) == NULL
) {
156 adios(NULL
, "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
);
204 vec
= argsplit(postproc
, &program
, &vecp
);
206 vec
[vecp
++] = "-verbose";
208 if ((cp
= context_find ("credentials"))) {
209 /* post doesn't read context so need to pass credentials. */
210 vec
[vecp
++] = "-credentials";
214 switch (sendsbr (vec
, vecp
, program
, tmpfil
, &st
, 0, NULL
)) {
224 if (m_unlink (tmpfil
) == -1)
225 advise (tmpfil
, "unable to remove temp file %s", tmpfil
);