]> diplodocus.org Git - nmh/blob - uip/viamail.c
a096769b27b796a18d8c205544785b118409aebe
[nmh] / uip / viamail.c
1 /*
2 * viamail.c -- send multiple files in a MIME message
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/md5.h>
12 #include <h/mts.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16
17 #define VIAMAIL_SWITCHES \
18 X("to mailpath", 0, TOSW) \
19 X("from mailpath", 0, FROMSW) \
20 X("subject subject", 0, SUBJECTSW) \
21 X("parameters arguments", 0, PARAMSW) \
22 X("description text", 0, DESCRIPTSW) \
23 X("comment text", 0, COMMENTSW) \
24 X("delay seconds", 0, DELAYSW) \
25 X("verbose", 0, VERBSW) \
26 X("noverbose", 0, NVERBSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
29 X("debug", -5, DEBUGSW) \
30
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(VIAMAIL);
33 #undef X
34
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(VIAMAIL, switches);
37 #undef X
38
39 extern int debugsw;
40 extern int splitsw;
41 extern int verbsw;
42
43 /*
44 * static prototypes
45 */
46 static int via_mail (char *, char *, char *, char *, char *, int, char *);
47
48
49 int
50 main (int argc, char **argv)
51 {
52 int delay = 0;
53 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
54 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
55 char *cp, buf[BUFSIZ];
56 char **argp, **arguments;
57
58 if (nmh_init(argv[0], 2)) { return 1; }
59
60 arguments = getarguments (invo_name, argc, argv, 0);
61 argp = arguments;
62
63 while ((cp = *argp++)) {
64 if (*cp == '-') {
65 switch (smatch (++cp, switches)) {
66 case AMBIGSW:
67 ambigsw (cp, switches);
68 done (1);
69 case UNKWNSW:
70 adios (NULL, "-%s unknown", cp);
71
72 case HELPSW:
73 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
74 print_help (buf, switches, 1);
75 done (0);
76 case VERSIONSW:
77 print_version(invo_name);
78 done (0);
79
80 case TOSW:
81 if (!(f1 = *argp++))
82 adios (NULL, "missing argument to %s", argp[-2]);
83 continue;
84 case SUBJECTSW:
85 if (!(f2 = *argp++))
86 adios (NULL, "missing argument to %s", argp[-2]);
87 continue;
88 case PARAMSW:
89 if (!(f3 = *argp++))
90 adios (NULL, "missing argument to %s", argp[-2]);
91 continue;
92 case DESCRIPTSW:
93 if (!(f4 = *argp++))
94 adios (NULL, "missing argument to %s", argp[-2]);
95 continue;
96 case COMMENTSW:
97 if (!(f5 = *argp++))
98 adios (NULL, "missing argument to %s", argp[-2]);
99 continue;
100 case DELAYSW:
101 if (!(cp = *argp++) || *cp == '-')
102 adios (NULL, "missing argument to %s", argp[-2]);
103
104 /*
105 * If there is an error, just reset the delay parameter
106 * to -1. We will set a default delay later.
107 */
108 if (sscanf (cp, "%d", &delay) != 1)
109 delay = -1;
110 continue;
111 case FROMSW:
112 if (!(f7 = *argp++))
113 adios (NULL, "missing argument to %s", argp[-2]);
114 continue;
115
116 case VERBSW:
117 verbsw = 1;
118 continue;
119 case NVERBSW:
120 verbsw = 0;
121 continue;
122
123 case DEBUGSW:
124 debugsw = 1;
125 continue;
126 }
127 }
128 }
129
130 if (!f1)
131 adios (NULL, "missing -viamail \"mailpath\" switch");
132
133 via_mail (f1, f2, f3, f4, f5, delay, f7);
134 return 0; /* dead code to satisfy the compiler */
135 }
136
137
138 /*
139 * VIAMAIL
140 */
141
142 static int
143 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
144 char *cmntsw, int delay, char *fromsw)
145 {
146 int status, vecp;
147 char tmpfil[BUFSIZ], *program;
148 char **vec;
149 struct stat st;
150 FILE *fp;
151 char *tfile = NULL;
152 char *cp;
153
154 umask (~m_gmprot ());
155
156 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
157 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
158 }
159 strncpy (tmpfil, tfile, sizeof(tmpfil));
160
161 if (!strchr(mailsw, '@'))
162 mailsw = concat (mailsw, "@", LocalName (0), NULL);
163 fprintf (fp, "To: %s\n", mailsw);
164
165 if (subjsw)
166 fprintf (fp, "Subject: %s\n", subjsw);
167
168 if (fromsw) {
169 if (!strchr(fromsw, '@'))
170 fromsw = concat (fromsw, "@", LocalName (0), NULL);
171 fprintf (fp, "From: %s\n", fromsw);
172 }
173
174 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
175 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
176
177 if (parmsw)
178 fprintf (fp, "; %s", parmsw);
179
180 if (cmntsw)
181 fprintf (fp, "\n\t(%s)", cmntsw);
182
183 if (descsw)
184 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
185
186 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
187
188 if (fflush (fp))
189 adios (tmpfil, "error writing to");
190
191 writeBase64aux (stdin, fp, 0);
192 if (fflush (fp))
193 adios (tmpfil, "error writing to");
194
195 if (fstat (fileno (fp), &st) == NOTOK)
196 adios ("failed", "fstat of %s", tmpfil);
197
198 if (delay < 0)
199 splitsw = 10;
200 else
201 splitsw = delay;
202
203 status = 0;
204
205 vec = argsplit(postproc, &program, &vecp);
206 if (verbsw)
207 vec[vecp++] = "-verbose";
208
209 if ((cp = context_find ("credentials"))) {
210 /* post doesn't read context so need to pass credentials. */
211 vec[vecp++] = "-credentials";
212 vec[vecp++] = cp;
213 }
214
215 switch (sendsbr (vec, vecp, program, tmpfil, &st, 0, NULL)) {
216 case DONE:
217 case NOTOK:
218 status++;
219 break;
220 case OK:
221 break;
222 }
223
224 fclose (fp);
225 if (m_unlink (tmpfil) == -1)
226 advise (tmpfil, "unable to remove temp file %s", tmpfil);
227 done (status);
228 return 1;
229 }