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