]>
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/concat.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/showfile.h"
13 #include "sbr/smatch.h"
14 #include "sbr/closefds.h"
15 #include "sbr/cpydata.h"
16 #include "sbr/m_draft.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_find.h"
21 #include "sbr/brkstring.h"
22 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/arglist.h"
28 #include "sbr/error.h"
35 #include "sbr/m_maildir.h"
36 #include "sbr/m_mktemp.h"
39 # define SASLminc(a) (a)
40 #else /* CYRUS_SASL */
41 # define SASLminc(a) 0
42 #endif /* CYRUS_SASL */
45 # define TLSminc(a) (a)
46 #else /* TLS_SUPPORT */
48 #endif /* TLS_SUPPORT */
50 #define SEND_SWITCHES \
51 X("alias aliasfile", 0, ALIASW) \
52 X("debug", -5, DEBUGSW) \
53 X("draft", 0, DRAFTSW) \
54 X("draftfolder +folder", 6, DFOLDSW) \
55 X("draftmessage msg", 6, DMSGSW) \
56 X("nodraftfolder", 0, NDFLDSW) \
57 X("filter filterfile", 0, FILTSW) \
58 X("nofilter", 0, NFILTSW) \
59 X("format", 0, FRMTSW) \
60 X("noformat", 0, NFRMTSW) \
61 X("forward", 0, FORWSW) \
62 X("noforward", 0, NFORWSW) \
63 X("mime", 0, MIMESW) \
64 X("nomime", 0, NMIMESW) \
65 X("msgid", 0, MSGDSW) \
66 X("nomsgid", 0, NMSGDSW) \
67 X("push", 0, PUSHSW) \
68 X("nopush", 0, NPUSHSW) \
69 X("split seconds", 0, SPLITSW) \
70 X("unique", -6, UNIQSW) \
71 X("nounique", -8, NUNIQSW) \
72 X("verbose", 0, VERBSW) \
73 X("noverbose", 0, NVERBSW) \
74 X("watch", 0, WATCSW) \
75 X("nowatch", 0, NWATCSW) \
76 X("width columns", 0, WIDTHSW) \
77 X("version", 0, VERSIONSW) \
78 X("help", 0, HELPSW) \
79 X("dashstuffing", -12, BITSTUFFSW) \
80 X("nodashstuffing", -14, NBITSTUFFSW) \
81 X("client host", -6, CLIESW) \
82 X("server host", 6, SERVSW) \
83 X("snoop", 5, SNOOPSW) \
84 X("sasl", SASLminc(4), SASLSW) \
85 X("nosasl", SASLminc(6), NOSASLSW) \
86 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
87 X("authservice", SASLminc(0), AUTHSERVICESW) \
88 X("user username", SASLminc(-4), USERSW) \
89 X("port server-port-name/number", 4, PORTSW) \
90 X("tls", TLSminc(-3), TLSSW) \
91 X("initialtls", TLSminc(-10), INITTLSSW) \
92 X("notls", TLSminc(-5), NTLSSW) \
93 X("certverify", TLSminc(-10), CERTVERSW) \
94 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
95 X("sendmail program", 0, MTSSM) \
96 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
97 X("messageid localname|random", 2, MESSAGEIDSW) \
99 #define X(sw, minchars, id) id,
100 DEFINE_SWITCH_ENUM(SEND
);
103 #define X(sw, minchars, id) { sw, minchars, id },
104 DEFINE_SWITCH_ARRAY(SEND
, switches
);
107 #define USE_SWITCHES \
110 X("list", 0, LISTDSW) \
112 #define X(sw, minchars, id) id,
113 DEFINE_SWITCH_ENUM(USE
);
116 #define X(sw, minchars, id) { sw, minchars, id },
117 DEFINE_SWITCH_ARRAY(USE
, anyl
);
120 extern int debugsw
; /* from sendsbr.c */
128 extern char *altmsg
; /* .. */
129 extern char *annotext
;
130 extern char *distfile
;
134 main (int argc
, char **argv
)
139 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
140 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
141 char *msgs
[MAXARGS
], **vec
;
142 const char *user
= NULL
, *saslmech
= NULL
;
145 char *auth_svc
= NULL
;
147 if (nmh_init(argv
[0], true, true)) { return 1; }
149 arguments
= getarguments (invo_name
, argc
, argv
, 1);
152 vec
= argsplit(postproc
, &program
, &vecp
);
154 vec
[vecp
++] = "-library";
155 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
157 if ((cp
= context_find ("fileproc"))) {
158 vec
[vecp
++] = "-fileproc";
162 if ((cp
= context_find ("mhlproc"))) {
163 vec
[vecp
++] = "-mhlproc";
167 if ((cp
= context_find ("credentials"))) {
168 /* post doesn't read context so need to pass credentials. */
169 vec
[vecp
++] = "-credentials";
173 while ((cp
= *argp
++)) {
175 switch (smatch (++cp
, switches
)) {
177 ambigsw (cp
, switches
);
180 die("-%s unknown\n", cp
);
183 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
184 print_help (buf
, switches
, 1);
187 print_version(invo_name
);
191 msgs
[msgp
++] = draft
;
196 die("only one draft folder at a time!");
197 if (!(cp
= *argp
++) || *cp
== '-')
198 die("missing argument to %s", argp
[-2]);
199 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
200 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
203 if (!(cp
= *argp
++) || *cp
== '-')
204 die("missing argument to %s", argp
[-2]);
220 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
221 die("missing argument to %s", argp
[-2]);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 die("missing argument to %s", argp
[-2]);
290 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
291 die("missing argument to %s", argp
[-2]);
293 die("not built with OAuth support");
298 if (!(saslmech
= *argp
) || *saslmech
== '-')
299 die("missing argument to %s", argp
[-1]);
312 if (!(cp
= *argp
++) || *cp
== '-')
313 die("missing argument to %s", argp
[-2]);
323 * check for "Aliasfile:" profile entry
325 if ((cp
= context_find ("Aliasfile"))) {
328 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
329 vec
[vecp
++] = "-alias";
334 if (dfolder
== NULL
) {
336 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
337 if (stat (msgs
[0], &st
) == NOTOK
)
338 adios (msgs
[0], "unable to stat draft file");
339 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
340 for (status
= LISTDSW
; status
!= YESW
;) {
341 if (!(argp
= read_switch_multiword (cp
, anyl
)))
343 switch (status
= smatch (*argp
, anyl
)) {
349 showfile (++argp
, msgs
[0]);
357 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
358 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
361 if (!context_find ("path"))
362 free (path ("./", TFOLDER
));
365 msgs
[msgp
++] = "cur";
366 maildir
= m_maildir (dfolder
);
368 if (chdir (maildir
) == NOTOK
)
369 adios (maildir
, "unable to change directory to");
371 /* read folder and create message structure */
372 if (!(mp
= folder_read (dfolder
, 1)))
373 die("unable to read folder %s", dfolder
);
375 /* check for empty folder */
377 die("no messages in %s", dfolder
);
379 /* parse all the message ranges/sequences and set SELECTED */
380 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
381 if (!m_convert (mp
, msgs
[msgnum
]))
383 seq_setprev (mp
); /* set the previous-sequence */
385 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
386 if (is_selected (mp
, msgnum
)) {
387 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
388 unset_exists (mp
, msgnum
);
392 mp
->msgflags
|= SEQMOD
;
400 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
401 if ((cp
= context_find ("signature")) && *cp
)
402 setenv("SIGNATURE", cp
, 1);
404 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
405 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
406 adios (msgs
[msgnum
], "unable to stat draft file");
408 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
410 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
412 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
413 altmsg
= NULL
; /* used by dist interface - see below */
415 if ((cp
= getenv ("mhdist"))
419 vec
[vecp
++] = "-dist";
420 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
421 die("unable to create temporary file");
423 distfile
= mh_xstrdup(cp
);
424 (void) m_unlink(distfile
);
425 if (link (altmsg
, distfile
) == NOTOK
) {
426 /* Cygwin with FAT32 filesystem produces EPERM. */
427 if (errno
!= EXDEV
&& errno
!= EPERM
429 && errno
!= EISREMOTE
430 #endif /* EISREMOTE */
432 adios (distfile
, "unable to link %s to", altmsg
);
434 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
435 die("unable to create temporary file in %s",
438 distfile
= mh_xstrdup(cp
);
443 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
444 adios (altmsg
, "unable to open");
446 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
447 adios (distfile
, "unable to write");
448 cpydata (in
, out
, altmsg
, distfile
);
458 if (auth_svc
== NULL
) {
459 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
460 die("must specify -authservice with -saslmech xoauth2");
464 die("must specify -user with -saslmech xoauth2");
468 NMH_UNUSED(auth_svc
);
470 NMH_UNUSED(saslmech
);
471 #endif /* OAUTH_SUPPORT */
473 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
484 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
485 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
497 context_save (); /* save the context file */