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