]>
diplodocus.org Git - nmh/blob - uip/send.c
1 /* send.c -- send a composed message
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.
9 #include "sbr/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_find.h"
12 #include "sbr/brkstring.h"
13 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/m_mktemp.h"
30 # define SASLminc(a) (a)
31 #else /* CYRUS_SASL */
32 # define SASLminc(a) 0
33 #endif /* CYRUS_SASL */
36 # define TLSminc(a) (a)
37 #else /* TLS_SUPPORT */
39 #endif /* TLS_SUPPORT */
41 #define SEND_SWITCHES \
42 X("alias aliasfile", 0, ALIASW) \
43 X("debug", -5, DEBUGSW) \
44 X("draft", 0, DRAFTSW) \
45 X("draftfolder +folder", 6, DFOLDSW) \
46 X("draftmessage msg", 6, DMSGSW) \
47 X("nodraftfolder", 0, NDFLDSW) \
48 X("filter filterfile", 0, FILTSW) \
49 X("nofilter", 0, NFILTSW) \
50 X("format", 0, FRMTSW) \
51 X("noformat", 0, NFRMTSW) \
52 X("forward", 0, FORWSW) \
53 X("noforward", 0, NFORWSW) \
54 X("mime", 0, MIMESW) \
55 X("nomime", 0, NMIMESW) \
56 X("msgid", 0, MSGDSW) \
57 X("nomsgid", 0, NMSGDSW) \
58 X("push", 0, PUSHSW) \
59 X("nopush", 0, NPUSHSW) \
60 X("split seconds", 0, SPLITSW) \
61 X("unique", -6, UNIQSW) \
62 X("nounique", -8, NUNIQSW) \
63 X("verbose", 0, VERBSW) \
64 X("noverbose", 0, NVERBSW) \
65 X("watch", 0, WATCSW) \
66 X("nowatch", 0, NWATCSW) \
67 X("width columns", 0, WIDTHSW) \
68 X("version", 0, VERSIONSW) \
69 X("help", 0, HELPSW) \
70 X("dashstuffing", -12, BITSTUFFSW) \
71 X("nodashstuffing", -14, NBITSTUFFSW) \
72 X("client host", -6, CLIESW) \
73 X("server host", 6, SERVSW) \
74 X("snoop", 5, SNOOPSW) \
75 X("sasl", SASLminc(4), SASLSW) \
76 X("nosasl", SASLminc(6), NOSASLSW) \
77 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
78 X("authservice", SASLminc(0), AUTHSERVICESW) \
79 X("user username", SASLminc(-4), USERSW) \
80 X("port server-port-name/number", 4, PORTSW) \
81 X("tls", TLSminc(-3), TLSSW) \
82 X("initialtls", TLSminc(-10), INITTLSSW) \
83 X("notls", TLSminc(-5), NTLSSW) \
84 X("certverify", TLSminc(-10), CERTVERSW) \
85 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
86 X("sendmail program", 0, MTSSM) \
87 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
88 X("messageid localname|random", 2, MESSAGEIDSW) \
90 #define X(sw, minchars, id) id,
91 DEFINE_SWITCH_ENUM(SEND
);
94 #define X(sw, minchars, id) { sw, minchars, id },
95 DEFINE_SWITCH_ARRAY(SEND
, switches
);
98 #define USE_SWITCHES \
101 X("list", 0, LISTDSW) \
103 #define X(sw, minchars, id) id,
104 DEFINE_SWITCH_ENUM(USE
);
107 #define X(sw, minchars, id) { sw, minchars, id },
108 DEFINE_SWITCH_ARRAY(USE
, anyl
);
111 extern int debugsw
; /* from sendsbr.c */
119 extern char *altmsg
; /* .. */
120 extern char *annotext
;
121 extern char *distfile
;
125 main (int argc
, char **argv
)
130 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
131 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
132 char *msgs
[MAXARGS
], **vec
;
133 const char *user
= NULL
, *saslmech
= NULL
;
136 char *auth_svc
= NULL
;
138 if (nmh_init(argv
[0], true, true)) { return 1; }
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 vec
= argsplit(postproc
, &program
, &vecp
);
145 vec
[vecp
++] = "-library";
146 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
148 if ((cp
= context_find ("fileproc"))) {
149 vec
[vecp
++] = "-fileproc";
153 if ((cp
= context_find ("mhlproc"))) {
154 vec
[vecp
++] = "-mhlproc";
158 if ((cp
= context_find ("credentials"))) {
159 /* post doesn't read context so need to pass credentials. */
160 vec
[vecp
++] = "-credentials";
164 while ((cp
= *argp
++)) {
166 switch (smatch (++cp
, switches
)) {
168 ambigsw (cp
, switches
);
171 die("-%s unknown\n", cp
);
174 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
175 print_help (buf
, switches
, 1);
178 print_version(invo_name
);
182 msgs
[msgp
++] = draft
;
187 die("only one draft folder at a time!");
188 if (!(cp
= *argp
++) || *cp
== '-')
189 die("missing argument to %s", argp
[-2]);
190 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
191 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
211 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
212 die("missing argument to %s", argp
[-2]);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
281 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
282 die("missing argument to %s", argp
[-2]);
284 die("not built with OAuth support");
289 if (!(saslmech
= *argp
) || *saslmech
== '-')
290 die("missing argument to %s", argp
[-1]);
303 if (!(cp
= *argp
++) || *cp
== '-')
304 die("missing argument to %s", argp
[-2]);
314 * check for "Aliasfile:" profile entry
316 if ((cp
= context_find ("Aliasfile"))) {
319 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
320 vec
[vecp
++] = "-alias";
325 if (dfolder
== NULL
) {
327 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
328 if (stat (msgs
[0], &st
) == NOTOK
)
329 adios (msgs
[0], "unable to stat draft file");
330 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
331 for (status
= LISTDSW
; status
!= YESW
;) {
332 if (!(argp
= read_switch_multiword (cp
, anyl
)))
334 switch (status
= smatch (*argp
, anyl
)) {
340 showfile (++argp
, msgs
[0]);
348 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
349 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
352 if (!context_find ("path"))
353 free (path ("./", TFOLDER
));
356 msgs
[msgp
++] = "cur";
357 maildir
= m_maildir (dfolder
);
359 if (chdir (maildir
) == NOTOK
)
360 adios (maildir
, "unable to change directory to");
362 /* read folder and create message structure */
363 if (!(mp
= folder_read (dfolder
, 1)))
364 die("unable to read folder %s", dfolder
);
366 /* check for empty folder */
368 die("no messages in %s", dfolder
);
370 /* parse all the message ranges/sequences and set SELECTED */
371 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
372 if (!m_convert (mp
, msgs
[msgnum
]))
374 seq_setprev (mp
); /* set the previous-sequence */
376 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
377 if (is_selected (mp
, msgnum
)) {
378 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
379 unset_exists (mp
, msgnum
);
383 mp
->msgflags
|= SEQMOD
;
391 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
392 if ((cp
= context_find ("signature")) && *cp
)
393 setenv("SIGNATURE", cp
, 1);
395 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
396 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
397 adios (msgs
[msgnum
], "unable to stat draft file");
399 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
401 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
403 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
404 altmsg
= NULL
; /* used by dist interface - see below */
406 if ((cp
= getenv ("mhdist"))
410 vec
[vecp
++] = "-dist";
411 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
412 die("unable to create temporary file");
414 distfile
= mh_xstrdup(cp
);
415 (void) m_unlink(distfile
);
416 if (link (altmsg
, distfile
) == NOTOK
) {
417 /* Cygwin with FAT32 filesystem produces EPERM. */
418 if (errno
!= EXDEV
&& errno
!= EPERM
420 && errno
!= EISREMOTE
421 #endif /* EISREMOTE */
423 adios (distfile
, "unable to link %s to", altmsg
);
425 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
426 die("unable to create temporary file in %s",
429 distfile
= mh_xstrdup(cp
);
434 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
435 adios (altmsg
, "unable to open");
437 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
438 adios (distfile
, "unable to write");
439 cpydata (in
, out
, altmsg
, distfile
);
449 if (auth_svc
== NULL
) {
450 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
451 die("must specify -authservice with -saslmech xoauth2");
455 die("must specify -user with -saslmech xoauth2");
459 NMH_UNUSED(auth_svc
);
461 NMH_UNUSED(saslmech
);
462 #endif /* OAUTH_SUPPORT */
464 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
475 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
476 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
488 context_save (); /* save the context file */