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