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