]>
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.
10 #include "sbr/m_name.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/read_switch_multiword.h"
13 #include "sbr/concat.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/showfile.h"
17 #include "sbr/smatch.h"
18 #include "sbr/closefds.h"
19 #include "sbr/cpydata.h"
20 #include "sbr/m_draft.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/folder_read.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_find.h"
25 #include "sbr/brkstring.h"
26 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/arglist.h"
32 #include "sbr/error.h"
39 #include "sbr/m_maildir.h"
40 #include "sbr/m_mktemp.h"
43 # define SASLminc(a) (a)
44 #else /* CYRUS_SASL */
45 # define SASLminc(a) 0
46 #endif /* CYRUS_SASL */
49 # define TLSminc(a) (a)
50 #else /* TLS_SUPPORT */
52 #endif /* TLS_SUPPORT */
54 #define SEND_SWITCHES \
55 X("alias aliasfile", 0, ALIASW) \
56 X("debug", -5, DEBUGSW) \
57 X("draft", 0, DRAFTSW) \
58 X("draftfolder +folder", 6, DFOLDSW) \
59 X("draftmessage msg", 6, DMSGSW) \
60 X("nodraftfolder", 0, NDFLDSW) \
61 X("filter filterfile", 0, FILTSW) \
62 X("nofilter", 0, NFILTSW) \
63 X("format", 0, FRMTSW) \
64 X("noformat", 0, NFRMTSW) \
65 X("forward", 0, FORWSW) \
66 X("noforward", 0, NFORWSW) \
67 X("mime", 0, MIMESW) \
68 X("nomime", 0, NMIMESW) \
69 X("msgid", 0, MSGDSW) \
70 X("nomsgid", 0, NMSGDSW) \
71 X("push", 0, PUSHSW) \
72 X("nopush", 0, NPUSHSW) \
73 X("split seconds", 0, SPLITSW) \
74 X("unique", -6, UNIQSW) \
75 X("nounique", -8, NUNIQSW) \
76 X("verbose", 0, VERBSW) \
77 X("noverbose", 0, NVERBSW) \
78 X("watch", 0, WATCSW) \
79 X("nowatch", 0, NWATCSW) \
80 X("width columns", 0, WIDTHSW) \
81 X("version", 0, VERSIONSW) \
82 X("help", 0, HELPSW) \
83 X("dashstuffing", -12, BITSTUFFSW) \
84 X("nodashstuffing", -14, NBITSTUFFSW) \
85 X("client host", -6, CLIESW) \
86 X("server host", 6, SERVSW) \
87 X("snoop", 5, SNOOPSW) \
88 X("sasl", SASLminc(4), SASLSW) \
89 X("nosasl", SASLminc(6), NOSASLSW) \
90 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
91 X("authservice", SASLminc(0), AUTHSERVICESW) \
92 X("user username", SASLminc(-4), USERSW) \
93 X("port server-port-name/number", 4, PORTSW) \
94 X("tls", TLSminc(-3), TLSSW) \
95 X("initialtls", TLSminc(-10), INITTLSSW) \
96 X("notls", TLSminc(-5), NTLSSW) \
97 X("certverify", TLSminc(-10), CERTVERSW) \
98 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
99 X("sendmail program", 0, MTSSM) \
100 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
101 X("messageid localname|random", 2, MESSAGEIDSW) \
103 #define X(sw, minchars, id) id,
104 DEFINE_SWITCH_ENUM(SEND
);
107 #define X(sw, minchars, id) { sw, minchars, id },
108 DEFINE_SWITCH_ARRAY(SEND
, switches
);
111 #define USE_SWITCHES \
114 X("list", 0, LISTDSW) \
116 #define X(sw, minchars, id) id,
117 DEFINE_SWITCH_ENUM(USE
);
120 #define X(sw, minchars, id) { sw, minchars, id },
121 DEFINE_SWITCH_ARRAY(USE
, anyl
);
124 extern int debugsw
; /* from sendsbr.c */
132 extern char *altmsg
; /* .. */
133 extern char *annotext
;
134 extern char *distfile
;
138 main (int argc
, char **argv
)
143 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
144 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
145 char *msgs
[MAXARGS
], **vec
;
146 const char *user
= NULL
, *saslmech
= NULL
;
149 char *auth_svc
= NULL
;
151 if (nmh_init(argv
[0], true, true)) { return 1; }
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
156 vec
= argsplit(postproc
, &program
, &vecp
);
158 vec
[vecp
++] = "-library";
159 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
161 if ((cp
= context_find ("fileproc"))) {
162 vec
[vecp
++] = "-fileproc";
166 if ((cp
= context_find ("mhlproc"))) {
167 vec
[vecp
++] = "-mhlproc";
171 if ((cp
= context_find ("credentials"))) {
172 /* post doesn't read context so need to pass credentials. */
173 vec
[vecp
++] = "-credentials";
177 while ((cp
= *argp
++)) {
179 switch (smatch (++cp
, switches
)) {
181 ambigsw (cp
, switches
);
184 die("-%s unknown\n", cp
);
187 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
188 print_help (buf
, switches
, 1);
191 print_version(invo_name
);
195 msgs
[msgp
++] = draft
;
200 die("only one draft folder at a time!");
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
203 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
204 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
207 if (!(cp
= *argp
++) || *cp
== '-')
208 die("missing argument to %s", argp
[-2]);
224 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
225 die("missing argument to %s", argp
[-2]);
286 if (!(cp
= *argp
++) || *cp
== '-')
287 die("missing argument to %s", argp
[-2]);
294 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
295 die("missing argument to %s", argp
[-2]);
297 die("not built with OAuth support");
302 if (!(saslmech
= *argp
) || *saslmech
== '-')
303 die("missing argument to %s", argp
[-1]);
316 if (!(cp
= *argp
++) || *cp
== '-')
317 die("missing argument to %s", argp
[-2]);
327 * check for "Aliasfile:" profile entry
329 if ((cp
= context_find ("Aliasfile"))) {
332 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
333 vec
[vecp
++] = "-alias";
338 if (dfolder
== NULL
) {
340 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
341 if (stat (msgs
[0], &st
) == NOTOK
)
342 adios (msgs
[0], "unable to stat draft file");
343 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
344 for (status
= LISTDSW
; status
!= YESW
;) {
345 if (!(argp
= read_switch_multiword (cp
, anyl
)))
347 switch (status
= smatch (*argp
, anyl
)) {
353 showfile (++argp
, msgs
[0]);
361 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
362 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
365 if (!context_find ("path"))
366 free (path ("./", TFOLDER
));
369 msgs
[msgp
++] = "cur";
370 maildir
= m_maildir (dfolder
);
372 if (chdir (maildir
) == NOTOK
)
373 adios (maildir
, "unable to change directory to");
375 /* read folder and create message structure */
376 if (!(mp
= folder_read (dfolder
, 1)))
377 die("unable to read folder %s", dfolder
);
379 /* check for empty folder */
381 die("no messages in %s", dfolder
);
383 /* parse all the message ranges/sequences and set SELECTED */
384 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
385 if (!m_convert (mp
, msgs
[msgnum
]))
387 seq_setprev (mp
); /* set the previous-sequence */
389 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
390 if (is_selected (mp
, msgnum
)) {
391 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
392 unset_exists (mp
, msgnum
);
396 mp
->msgflags
|= SEQMOD
;
404 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
405 if ((cp
= context_find ("signature")) && *cp
)
406 setenv("SIGNATURE", cp
, 1);
408 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
409 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
410 adios (msgs
[msgnum
], "unable to stat draft file");
412 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
414 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
416 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
417 altmsg
= NULL
; /* used by dist interface - see below */
419 if ((cp
= getenv ("mhdist"))
423 vec
[vecp
++] = "-dist";
424 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
425 die("unable to create temporary file");
427 distfile
= mh_xstrdup(cp
);
428 (void) m_unlink(distfile
);
429 if (link (altmsg
, distfile
) == NOTOK
) {
430 /* Cygwin with FAT32 filesystem produces EPERM. */
431 if (errno
!= EXDEV
&& errno
!= EPERM
433 && errno
!= EISREMOTE
434 #endif /* EISREMOTE */
436 adios (distfile
, "unable to link %s to", altmsg
);
438 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
439 die("unable to create temporary file in %s",
442 distfile
= mh_xstrdup(cp
);
447 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
448 adios (altmsg
, "unable to open");
450 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
451 adios (distfile
, "unable to write");
452 cpydata (in
, out
, altmsg
, distfile
);
462 if (auth_svc
== NULL
) {
463 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
464 die("must specify -authservice with -saslmech xoauth2");
468 die("must specify -user with -saslmech xoauth2");
472 NMH_UNUSED(auth_svc
);
474 NMH_UNUSED(saslmech
);
475 #endif /* OAUTH_SUPPORT */
477 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
488 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
489 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
501 context_save (); /* save the context file */