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