]>
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/cpydata.h"
19 #include "sbr/m_draft.h"
20 #include "sbr/m_convert.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_find.h"
24 #include "sbr/brkstring.h"
25 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/arglist.h"
31 #include "sbr/error.h"
38 #include "sbr/m_maildir.h"
39 #include "sbr/m_mktemp.h"
42 # define SASLminc(a) (a)
43 #else /* CYRUS_SASL */
44 # define SASLminc(a) 0
45 #endif /* CYRUS_SASL */
48 # define TLSminc(a) (a)
49 #else /* TLS_SUPPORT */
51 #endif /* TLS_SUPPORT */
53 #define SEND_SWITCHES \
54 X("alias aliasfile", 0, ALIASW) \
55 X("debug", -5, DEBUGSW) \
56 X("draft", 0, DRAFTSW) \
57 X("draftfolder +folder", 6, DFOLDSW) \
58 X("draftmessage msg", 6, DMSGSW) \
59 X("nodraftfolder", 0, NDFLDSW) \
60 X("filter filterfile", 0, FILTSW) \
61 X("nofilter", 0, NFILTSW) \
62 X("format", 0, FRMTSW) \
63 X("noformat", 0, NFRMTSW) \
64 X("forward", 0, FORWSW) \
65 X("noforward", 0, NFORWSW) \
66 X("mime", 0, MIMESW) \
67 X("nomime", 0, NMIMESW) \
68 X("msgid", 0, MSGDSW) \
69 X("nomsgid", 0, NMSGDSW) \
70 X("push", 0, PUSHSW) \
71 X("nopush", 0, NPUSHSW) \
72 X("split seconds", 0, SPLITSW) \
73 X("unique", -6, UNIQSW) \
74 X("nounique", -8, NUNIQSW) \
75 X("verbose", 0, VERBSW) \
76 X("noverbose", 0, NVERBSW) \
77 X("watch", 0, WATCSW) \
78 X("nowatch", 0, NWATCSW) \
79 X("width columns", 0, WIDTHSW) \
80 X("version", 0, VERSIONSW) \
81 X("help", 0, HELPSW) \
82 X("dashstuffing", -12, BITSTUFFSW) \
83 X("nodashstuffing", -14, NBITSTUFFSW) \
84 X("client host", -6, CLIESW) \
85 X("server host", 6, SERVSW) \
86 X("snoop", 5, SNOOPSW) \
87 X("sasl", SASLminc(4), SASLSW) \
88 X("nosasl", SASLminc(6), NOSASLSW) \
89 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
90 X("authservice", SASLminc(0), AUTHSERVICESW) \
91 X("user username", SASLminc(-4), USERSW) \
92 X("port server-port-name/number", 4, PORTSW) \
93 X("tls", TLSminc(-3), TLSSW) \
94 X("initialtls", TLSminc(-10), INITTLSSW) \
95 X("notls", TLSminc(-5), NTLSSW) \
96 X("certverify", TLSminc(-10), CERTVERSW) \
97 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
98 X("sendmail program", 0, MTSSM) \
99 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
100 X("messageid localname|random", 2, MESSAGEIDSW) \
102 #define X(sw, minchars, id) id,
103 DEFINE_SWITCH_ENUM(SEND
);
106 #define X(sw, minchars, id) { sw, minchars, id },
107 DEFINE_SWITCH_ARRAY(SEND
, switches
);
110 #define USE_SWITCHES \
113 X("list", 0, LISTDSW) \
115 #define X(sw, minchars, id) id,
116 DEFINE_SWITCH_ENUM(USE
);
119 #define X(sw, minchars, id) { sw, minchars, id },
120 DEFINE_SWITCH_ARRAY(USE
, anyl
);
123 extern int debugsw
; /* from sendsbr.c */
131 extern char *altmsg
; /* .. */
132 extern char *annotext
;
133 extern char *distfile
;
137 main (int argc
, char **argv
)
142 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
143 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
144 char *msgs
[MAXARGS
], **vec
;
145 const char *user
= NULL
, *saslmech
= NULL
;
148 char *auth_svc
= NULL
;
150 if (nmh_init(argv
[0], true, true)) { return 1; }
152 arguments
= getarguments (invo_name
, argc
, argv
, 1);
155 vec
= argsplit(postproc
, &program
, &vecp
);
157 vec
[vecp
++] = "-library";
158 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
160 if ((cp
= context_find ("fileproc"))) {
161 vec
[vecp
++] = "-fileproc";
165 if ((cp
= context_find ("mhlproc"))) {
166 vec
[vecp
++] = "-mhlproc";
170 if ((cp
= context_find ("credentials"))) {
171 /* post doesn't read context so need to pass credentials. */
172 vec
[vecp
++] = "-credentials";
176 while ((cp
= *argp
++)) {
178 switch (smatch (++cp
, switches
)) {
180 ambigsw (cp
, switches
);
183 die("-%s unknown\n", cp
);
186 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
187 print_help (buf
, switches
, 1);
190 print_version(invo_name
);
194 msgs
[msgp
++] = draft
;
199 die("only one draft folder at a time!");
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
202 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
203 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 die("missing argument to %s", argp
[-2]);
223 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
224 die("missing argument to %s", argp
[-2]);
285 if (!(cp
= *argp
++) || *cp
== '-')
286 die("missing argument to %s", argp
[-2]);
293 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
294 die("missing argument to %s", argp
[-2]);
296 die("not built with OAuth support");
301 if (!(saslmech
= *argp
) || *saslmech
== '-')
302 die("missing argument to %s", argp
[-1]);
315 if (!(cp
= *argp
++) || *cp
== '-')
316 die("missing argument to %s", argp
[-2]);
326 * check for "Aliasfile:" profile entry
328 if ((cp
= context_find ("Aliasfile"))) {
331 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
332 vec
[vecp
++] = "-alias";
337 if (dfolder
== NULL
) {
339 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
340 if (stat (msgs
[0], &st
) == NOTOK
)
341 adios (msgs
[0], "unable to stat draft file");
342 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
343 for (status
= LISTDSW
; status
!= YESW
;) {
344 if (!(argp
= read_switch_multiword (cp
, anyl
)))
346 switch (status
= smatch (*argp
, anyl
)) {
352 showfile (++argp
, msgs
[0]);
360 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
361 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
364 if (!context_find ("path"))
365 free (path ("./", TFOLDER
));
368 msgs
[msgp
++] = "cur";
369 maildir
= m_maildir (dfolder
);
371 if (chdir (maildir
) == NOTOK
)
372 adios (maildir
, "unable to change directory to");
374 /* read folder and create message structure */
375 if (!(mp
= folder_read (dfolder
, 1)))
376 die("unable to read folder %s", dfolder
);
378 /* check for empty folder */
380 die("no messages in %s", dfolder
);
382 /* parse all the message ranges/sequences and set SELECTED */
383 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
384 if (!m_convert (mp
, msgs
[msgnum
]))
386 seq_setprev (mp
); /* set the previous-sequence */
388 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
389 if (is_selected (mp
, msgnum
)) {
390 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
391 unset_exists (mp
, msgnum
);
395 mp
->msgflags
|= SEQMOD
;
403 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
404 if ((cp
= context_find ("signature")) && *cp
)
405 setenv("SIGNATURE", cp
, 1);
407 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
408 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
409 adios (msgs
[msgnum
], "unable to stat draft file");
411 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
413 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
415 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
416 altmsg
= NULL
; /* used by dist interface - see below */
418 if ((cp
= getenv ("mhdist"))
422 vec
[vecp
++] = "-dist";
423 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
424 die("unable to create temporary file");
426 distfile
= mh_xstrdup(cp
);
427 (void) m_unlink(distfile
);
428 if (link (altmsg
, distfile
) == NOTOK
) {
429 /* Cygwin with FAT32 filesystem produces EPERM. */
430 if (errno
!= EXDEV
&& errno
!= EPERM
432 && errno
!= EISREMOTE
433 #endif /* EISREMOTE */
435 adios (distfile
, "unable to link %s to", altmsg
);
437 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
438 die("unable to create temporary file in %s",
441 distfile
= mh_xstrdup(cp
);
446 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
447 adios (altmsg
, "unable to open");
449 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
450 adios (distfile
, "unable to write");
451 cpydata (in
, out
, altmsg
, distfile
);
461 if (auth_svc
== NULL
) {
462 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
463 die("must specify -authservice with -saslmech xoauth2");
467 die("must specify -user with -saslmech xoauth2");
471 NMH_UNUSED(auth_svc
);
473 NMH_UNUSED(saslmech
);
474 #endif /* OAUTH_SUPPORT */
476 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
486 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
487 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
499 context_save (); /* save the context file */