]> diplodocus.org Git - nmh/blob - uip/send.c
With -messageid random, make the part after the @ more resemble a
[nmh] / uip / send.c
1
2 /*
3 * send.c -- send a composed 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 <errno.h>
13 #include <signal.h>
14
15
16 #ifndef CYRUS_SASL
17 # define SASLminc(a) (a)
18 #else /* CYRUS_SASL */
19 # define SASLminc(a) 0
20 #endif /* CYRUS_SASL */
21
22 #ifndef TLS_SUPPORT
23 # define TLSminc(a) (a)
24 #else /* TLS_SUPPORT */
25 # define TLSminc(a) 0
26 #endif /* TLS_SUPPORT */
27
28 static struct swit switches[] = {
29 #define ALIASW 0
30 { "alias aliasfile", 0 },
31 #define DEBUGSW 1
32 { "debug", -5 },
33 #define DRAFTSW 2
34 { "draft", 0 },
35 #define DFOLDSW 3
36 { "draftfolder +folder", 6 },
37 #define DMSGSW 4
38 { "draftmessage msg", 6 },
39 #define NDFLDSW 5
40 { "nodraftfolder", 0 },
41 #define FILTSW 6
42 { "filter filterfile", 0 },
43 #define NFILTSW 7
44 { "nofilter", 0 },
45 #define FRMTSW 8
46 { "format", 0 },
47 #define NFRMTSW 9
48 { "noformat", 0 },
49 #define FORWSW 10
50 { "forward", 0 },
51 #define NFORWSW 11
52 { "noforward", 0 },
53 #define MIMESW 12
54 { "mime", 0 },
55 #define NMIMESW 13
56 { "nomime", 0 },
57 #define MSGDSW 14
58 { "msgid", 0 },
59 #define NMSGDSW 15
60 { "nomsgid", 0 },
61 #define PUSHSW 16
62 { "push", 0 },
63 #define NPUSHSW 17
64 { "nopush", 0 },
65 #define SPLITSW 18
66 { "split seconds", 0 },
67 #define UNIQSW 19
68 { "unique", -6 },
69 #define NUNIQSW 20
70 { "nounique", -8 },
71 #define VERBSW 21
72 { "verbose", 0 },
73 #define NVERBSW 22
74 { "noverbose", 0 },
75 #define WATCSW 23
76 { "watch", 0 },
77 #define NWATCSW 24
78 { "nowatch", 0 },
79 #define WIDTHSW 25
80 { "width columns", 0 },
81 #define VERSIONSW 26
82 { "version", 0 },
83 #define HELPSW 27
84 { "help", 0 },
85 #define BITSTUFFSW 28
86 { "dashstuffing", -12 },
87 #define NBITSTUFFSW 29
88 { "nodashstuffing", -14 },
89 #define MAILSW 30
90 { "mail", -4 },
91 #define SAMLSW 31
92 { "saml", -4 },
93 #define SENDSW 32
94 { "send", -4 },
95 #define SOMLSW 33
96 { "soml", -4 },
97 #define CLIESW 34
98 { "client host", -6 },
99 #define SERVSW 35
100 { "server host", 6 },
101 #define SNOOPSW 36
102 { "snoop", 5 },
103 #define SASLSW 37
104 { "sasl", SASLminc(4) },
105 #define NOSASLSW 38
106 { "nosasl", SASLminc(-6) },
107 #define SASLMXSSFSW 39
108 { "saslmaxssf", SASLminc(-10) },
109 #define SASLMECHSW 40
110 { "saslmech mechanism", SASLminc(-5) },
111 #define USERSW 41
112 { "user username", SASLminc(-4) },
113 #define ATTACHSW 42
114 { "attach", 6 },
115 #define NOATTACHSW 43
116 { "noattach", 0 },
117 #define ATTACHFORMATSW 44
118 { "attachformat", 7 },
119 #define PORTSW 45
120 { "port server-port-name/number" , 4 },
121 #define TLSSW 46
122 { "tls", TLSminc(-3) },
123 #define NTLSSW 47
124 { "notls", TLSminc(-5) },
125 #define MTSSW 48
126 { "mts smtp|sendmail/smtp|sendmail/pipe", 2 },
127 #define MESSAGEIDSW 49
128 { "messageid localname|random", 2 },
129 { NULL, 0 }
130 };
131
132 static struct swit anyl[] = {
133 #define NOSW 0
134 { "no", 0 },
135 #define YESW 1
136 { "yes", 0 },
137 #define LISTDSW 2
138 { "list", 0 },
139 { NULL, 0 }
140 };
141
142 extern int debugsw; /* from sendsbr.c */
143 extern int forwsw;
144 extern int inplace;
145 extern int pushsw;
146 extern int splitsw;
147 extern int unique;
148 extern int verbsw;
149
150 extern char *altmsg; /* .. */
151 extern char *annotext;
152 extern char *distfile;
153
154
155 int
156 main (int argc, char **argv)
157 {
158 int msgp = 0, distsw = 0, vecp = 1;
159 int isdf = 0, mime = 0;
160 int msgnum, status;
161 char *cp, *dfolder = NULL, *maildir = NULL;
162 char buf[BUFSIZ], **ap, **argp, **arguments;
163 char *msgs[MAXARGS], *vec[MAXARGS];
164 struct msgs *mp;
165 struct stat st;
166 char *attach = NMH_ATTACH_HEADER; /* header field name for attachments */
167 int attachformat = 1; /* mhbuild format specifier for attachments */
168
169 #ifdef LOCALE
170 setlocale(LC_ALL, "");
171 #endif
172 invo_name = r1bindex (argv[0], '/');
173
174 /* read user profile/context */
175 context_read();
176
177 arguments = getarguments (invo_name, argc, argv, 1);
178 argp = arguments;
179
180 vec[vecp++] = "-library";
181 vec[vecp++] = getcpy (m_maildir (""));
182
183 if ((cp = context_find ("fileproc"))) {
184 vec[vecp++] = "-fileproc";
185 vec[vecp++] = cp;
186 }
187
188 if ((cp = context_find ("mhlproc"))) {
189 vec[vecp++] = "-mhlproc";
190 vec[vecp++] = cp;
191 }
192
193 while ((cp = *argp++)) {
194 if (*cp == '-') {
195 switch (smatch (++cp, switches)) {
196 case AMBIGSW:
197 ambigsw (cp, switches);
198 done (1);
199 case UNKWNSW:
200 adios (NULL, "-%s unknown\n", cp);
201
202 case HELPSW:
203 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
204 print_help (buf, switches, 1);
205 done (0);
206 case VERSIONSW:
207 print_version(invo_name);
208 done (0);
209
210 case DRAFTSW:
211 msgs[msgp++] = draft;
212 continue;
213
214 case DFOLDSW:
215 if (dfolder)
216 adios (NULL, "only one draft folder at a time!");
217 if (!(cp = *argp++) || *cp == '-')
218 adios (NULL, "missing argument to %s", argp[-2]);
219 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
220 *cp != '@' ? TFOLDER : TSUBCWF);
221 continue;
222 case DMSGSW:
223 if (!(cp = *argp++) || *cp == '-')
224 adios (NULL, "missing argument to %s", argp[-2]);
225 msgs[msgp++] = cp;
226 continue;
227 case NDFLDSW:
228 dfolder = NULL;
229 isdf = NOTOK;
230 continue;
231
232 case PUSHSW:
233 pushsw++;
234 continue;
235 case NPUSHSW:
236 pushsw = 0;
237 continue;
238
239 case SPLITSW:
240 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
241 adios (NULL, "missing argument to %s", argp[-2]);
242 continue;
243
244 case UNIQSW:
245 unique++;
246 continue;
247 case NUNIQSW:
248 unique = 0;
249 continue;
250
251 case FORWSW:
252 forwsw++;
253 continue;
254 case NFORWSW:
255 forwsw = 0;
256 continue;
257
258 case VERBSW:
259 verbsw++;
260 vec[vecp++] = --cp;
261 continue;
262 case NVERBSW:
263 verbsw = 0;
264 vec[vecp++] = --cp;
265 continue;
266
267 case MIMESW:
268 mime++;
269 vec[vecp++] = --cp;
270 continue;
271 case NMIMESW:
272 mime = 0;
273 vec[vecp++] = --cp;
274 continue;
275
276 case DEBUGSW:
277 debugsw++; /* fall */
278 case NFILTSW:
279 case FRMTSW:
280 case NFRMTSW:
281 case BITSTUFFSW:
282 case NBITSTUFFSW:
283 case MSGDSW:
284 case NMSGDSW:
285 case WATCSW:
286 case NWATCSW:
287 case MAILSW:
288 case SAMLSW:
289 case SENDSW:
290 case SOMLSW:
291 case SNOOPSW:
292 case SASLSW:
293 case NOSASLSW:
294 case TLSSW:
295 case NTLSSW:
296 vec[vecp++] = --cp;
297 continue;
298
299 case ALIASW:
300 case FILTSW:
301 case WIDTHSW:
302 case CLIESW:
303 case SERVSW:
304 case SASLMECHSW:
305 case SASLMXSSFSW:
306 case USERSW:
307 case PORTSW:
308 case MTSSW:
309 case MESSAGEIDSW:
310 vec[vecp++] = --cp;
311 if (!(cp = *argp++) || *cp == '-')
312 adios (NULL, "missing argument to %s", argp[-2]);
313 vec[vecp++] = cp;
314 continue;
315
316 case ATTACHSW:
317 if (!(attach = *argp++) || *attach == '-')
318 adios (NULL, "missing argument to %s", argp[-2]);
319 continue;
320 case NOATTACHSW:
321 attach = NULL;
322 continue;
323
324 case ATTACHFORMATSW:
325 if (! *argp || **argp == '-')
326 adios (NULL, "missing argument to %s", argp[-1]);
327 else {
328 attachformat = atoi (*argp);
329 if (attachformat < 0 ||
330 attachformat > ATTACHFORMATS - 1) {
331 advise (NULL, "unsupported attachformat %d",
332 attachformat);
333 continue;
334 }
335 }
336 ++argp;
337 continue;
338 }
339 } else {
340 msgs[msgp++] = cp;
341 }
342 }
343
344 /*
345 * check for "Aliasfile:" profile entry
346 */
347 if ((cp = context_find ("Aliasfile"))) {
348 char *dp = NULL;
349
350 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
351 vec[vecp++] = "-alias";
352 vec[vecp++] = *ap;
353 }
354 }
355
356 if (dfolder == NULL) {
357 if (msgp == 0) {
358 #ifdef WHATNOW
359 if ((cp = getenv ("mhdraft")) && *cp) {
360 msgs[msgp++] = cp;
361 goto go_to_it;
362 }
363 #endif /* WHATNOW */
364 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
365 if (stat (msgs[0], &st) == NOTOK)
366 adios (msgs[0], "unable to stat draft file");
367 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
368 for (status = LISTDSW; status != YESW;) {
369 if (!(argp = getans (cp, anyl)))
370 done (1);
371 switch (status = smatch (*argp, anyl)) {
372 case NOSW:
373 done (0);
374 case YESW:
375 break;
376 case LISTDSW:
377 showfile (++argp, msgs[0]);
378 break;
379 default:
380 advise (NULL, "say what?");
381 break;
382 }
383 }
384 } else {
385 for (msgnum = 0; msgnum < msgp; msgnum++)
386 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
387 }
388 } else {
389 if (!context_find ("path"))
390 free (path ("./", TFOLDER));
391
392 if (!msgp)
393 msgs[msgp++] = "cur";
394 maildir = m_maildir (dfolder);
395
396 if (chdir (maildir) == NOTOK)
397 adios (maildir, "unable to change directory to");
398
399 /* read folder and create message structure */
400 if (!(mp = folder_read (dfolder)))
401 adios (NULL, "unable to read folder %s", dfolder);
402
403 /* check for empty folder */
404 if (mp->nummsg == 0)
405 adios (NULL, "no messages in %s", dfolder);
406
407 /* parse all the message ranges/sequences and set SELECTED */
408 for (msgnum = 0; msgnum < msgp; msgnum++)
409 if (!m_convert (mp, msgs[msgnum]))
410 done (1);
411 seq_setprev (mp); /* set the previous-sequence */
412
413 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
414 if (is_selected (mp, msgnum)) {
415 msgs[msgp++] = getcpy (m_name (msgnum));
416 unset_exists (mp, msgnum);
417 }
418 }
419
420 mp->msgflags |= SEQMOD;
421 seq_save (mp);
422 }
423
424 #ifdef WHATNOW
425 go_to_it:
426 #endif /* WHATNOW */
427
428 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
429 if ((cp = context_find ("signature")) && *cp)
430 m_putenv ("SIGNATURE", cp);
431
432 for (msgnum = 0; msgnum < msgp; msgnum++)
433 if (stat (msgs[msgnum], &st) == NOTOK)
434 adios (msgs[msgnum], "unable to stat draft file");
435
436 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
437 annotext = NULL;
438 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
439 inplace = atoi (cp);
440 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
441 altmsg = NULL; /* used by dist interface - see below */
442
443 if ((cp = getenv ("mhdist"))
444 && *cp
445 && (distsw = atoi (cp))
446 && altmsg) {
447 vec[vecp++] = "-dist";
448 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
449 unlink(distfile);
450 if (link (altmsg, distfile) == NOTOK) {
451 /* Cygwin with FAT32 filesystem produces EPERM. */
452 if (errno != EXDEV && errno != EPERM
453 #ifdef EISREMOTE
454 && errno != EISREMOTE
455 #endif /* EISREMOTE */
456 )
457 adios (distfile, "unable to link %s to", altmsg);
458 free (distfile);
459 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
460 {
461 int in, out;
462 struct stat st;
463
464 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
465 adios (altmsg, "unable to open");
466 fstat(in, &st);
467 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
468 adios (distfile, "unable to write");
469 cpydata (in, out, altmsg, distfile);
470 close (in);
471 close (out);
472 }
473 }
474 } else {
475 distfile = NULL;
476 }
477
478 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
479 st.st_mtime = 0;
480 st.st_dev = 0;
481 st.st_ino = 0;
482 }
483 if (pushsw)
484 push ();
485
486 status = 0;
487 vec[0] = r1bindex (postproc, '/');
488 closefds (3);
489
490 for (msgnum = 0; msgnum < msgp; msgnum++) {
491 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
492 attachformat)) {
493 case DONE:
494 done (++status);
495 case NOTOK:
496 status++; /* fall */
497 case OK:
498 break;
499 }
500 }
501
502 context_save (); /* save the context file */
503 done (status);
504 return 1;
505 }