]>
diplodocus.org Git - nmh/blob - uip/send.c
0a893a1e67ac9fb42271286eb058fbfd9093c4a6
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/m_draft.h"
10 #include "sbr/m_convert.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_find.h"
14 #include "sbr/brkstring.h"
15 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/arglist.h"
21 #include "sbr/error.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/m_mktemp.h"
32 # define SASLminc(a) (a)
33 #else /* CYRUS_SASL */
34 # define SASLminc(a) 0
35 #endif /* CYRUS_SASL */
38 # define TLSminc(a) (a)
39 #else /* TLS_SUPPORT */
41 #endif /* TLS_SUPPORT */
43 #define SEND_SWITCHES \
44 X("alias aliasfile", 0, ALIASW) \
45 X("debug", -5, DEBUGSW) \
46 X("draft", 0, DRAFTSW) \
47 X("draftfolder +folder", 6, DFOLDSW) \
48 X("draftmessage msg", 6, DMSGSW) \
49 X("nodraftfolder", 0, NDFLDSW) \
50 X("filter filterfile", 0, FILTSW) \
51 X("nofilter", 0, NFILTSW) \
52 X("format", 0, FRMTSW) \
53 X("noformat", 0, NFRMTSW) \
54 X("forward", 0, FORWSW) \
55 X("noforward", 0, NFORWSW) \
56 X("mime", 0, MIMESW) \
57 X("nomime", 0, NMIMESW) \
58 X("msgid", 0, MSGDSW) \
59 X("nomsgid", 0, NMSGDSW) \
60 X("push", 0, PUSHSW) \
61 X("nopush", 0, NPUSHSW) \
62 X("split seconds", 0, SPLITSW) \
63 X("unique", -6, UNIQSW) \
64 X("nounique", -8, NUNIQSW) \
65 X("verbose", 0, VERBSW) \
66 X("noverbose", 0, NVERBSW) \
67 X("watch", 0, WATCSW) \
68 X("nowatch", 0, NWATCSW) \
69 X("width columns", 0, WIDTHSW) \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
72 X("dashstuffing", -12, BITSTUFFSW) \
73 X("nodashstuffing", -14, NBITSTUFFSW) \
74 X("client host", -6, CLIESW) \
75 X("server host", 6, SERVSW) \
76 X("snoop", 5, SNOOPSW) \
77 X("sasl", SASLminc(4), SASLSW) \
78 X("nosasl", SASLminc(6), NOSASLSW) \
79 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
80 X("authservice", SASLminc(0), AUTHSERVICESW) \
81 X("user username", SASLminc(-4), USERSW) \
82 X("port server-port-name/number", 4, PORTSW) \
83 X("tls", TLSminc(-3), TLSSW) \
84 X("initialtls", TLSminc(-10), INITTLSSW) \
85 X("notls", TLSminc(-5), NTLSSW) \
86 X("certverify", TLSminc(-10), CERTVERSW) \
87 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
88 X("sendmail program", 0, MTSSM) \
89 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
90 X("messageid localname|random", 2, MESSAGEIDSW) \
92 #define X(sw, minchars, id) id,
93 DEFINE_SWITCH_ENUM(SEND
);
96 #define X(sw, minchars, id) { sw, minchars, id },
97 DEFINE_SWITCH_ARRAY(SEND
, switches
);
100 #define USE_SWITCHES \
103 X("list", 0, LISTDSW) \
105 #define X(sw, minchars, id) id,
106 DEFINE_SWITCH_ENUM(USE
);
109 #define X(sw, minchars, id) { sw, minchars, id },
110 DEFINE_SWITCH_ARRAY(USE
, anyl
);
113 extern int debugsw
; /* from sendsbr.c */
121 extern char *altmsg
; /* .. */
122 extern char *annotext
;
123 extern char *distfile
;
127 main (int argc
, char **argv
)
132 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
133 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
134 char *msgs
[MAXARGS
], **vec
;
135 const char *user
= NULL
, *saslmech
= NULL
;
138 char *auth_svc
= NULL
;
140 if (nmh_init(argv
[0], true, true)) { return 1; }
142 arguments
= getarguments (invo_name
, argc
, argv
, 1);
145 vec
= argsplit(postproc
, &program
, &vecp
);
147 vec
[vecp
++] = "-library";
148 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
150 if ((cp
= context_find ("fileproc"))) {
151 vec
[vecp
++] = "-fileproc";
155 if ((cp
= context_find ("mhlproc"))) {
156 vec
[vecp
++] = "-mhlproc";
160 if ((cp
= context_find ("credentials"))) {
161 /* post doesn't read context so need to pass credentials. */
162 vec
[vecp
++] = "-credentials";
166 while ((cp
= *argp
++)) {
168 switch (smatch (++cp
, switches
)) {
170 ambigsw (cp
, switches
);
173 die("-%s unknown\n", cp
);
176 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
177 print_help (buf
, switches
, 1);
180 print_version(invo_name
);
184 msgs
[msgp
++] = draft
;
189 die("only one draft folder at a time!");
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
193 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
213 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
214 die("missing argument to %s", argp
[-2]);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 die("missing argument to %s", argp
[-2]);
283 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
284 die("missing argument to %s", argp
[-2]);
286 die("not built with OAuth support");
291 if (!(saslmech
= *argp
) || *saslmech
== '-')
292 die("missing argument to %s", argp
[-1]);
305 if (!(cp
= *argp
++) || *cp
== '-')
306 die("missing argument to %s", argp
[-2]);
316 * check for "Aliasfile:" profile entry
318 if ((cp
= context_find ("Aliasfile"))) {
321 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
322 vec
[vecp
++] = "-alias";
327 if (dfolder
== NULL
) {
329 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
330 if (stat (msgs
[0], &st
) == NOTOK
)
331 adios (msgs
[0], "unable to stat draft file");
332 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
333 for (status
= LISTDSW
; status
!= YESW
;) {
334 if (!(argp
= read_switch_multiword (cp
, anyl
)))
336 switch (status
= smatch (*argp
, anyl
)) {
342 showfile (++argp
, msgs
[0]);
350 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
351 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
354 if (!context_find ("path"))
355 free (path ("./", TFOLDER
));
358 msgs
[msgp
++] = "cur";
359 maildir
= m_maildir (dfolder
);
361 if (chdir (maildir
) == NOTOK
)
362 adios (maildir
, "unable to change directory to");
364 /* read folder and create message structure */
365 if (!(mp
= folder_read (dfolder
, 1)))
366 die("unable to read folder %s", dfolder
);
368 /* check for empty folder */
370 die("no messages in %s", dfolder
);
372 /* parse all the message ranges/sequences and set SELECTED */
373 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
374 if (!m_convert (mp
, msgs
[msgnum
]))
376 seq_setprev (mp
); /* set the previous-sequence */
378 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
379 if (is_selected (mp
, msgnum
)) {
380 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
381 unset_exists (mp
, msgnum
);
385 mp
->msgflags
|= SEQMOD
;
393 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
394 if ((cp
= context_find ("signature")) && *cp
)
395 setenv("SIGNATURE", cp
, 1);
397 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
398 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
399 adios (msgs
[msgnum
], "unable to stat draft file");
401 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
403 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
405 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
406 altmsg
= NULL
; /* used by dist interface - see below */
408 if ((cp
= getenv ("mhdist"))
412 vec
[vecp
++] = "-dist";
413 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
414 die("unable to create temporary file");
416 distfile
= mh_xstrdup(cp
);
417 (void) m_unlink(distfile
);
418 if (link (altmsg
, distfile
) == NOTOK
) {
419 /* Cygwin with FAT32 filesystem produces EPERM. */
420 if (errno
!= EXDEV
&& errno
!= EPERM
422 && errno
!= EISREMOTE
423 #endif /* EISREMOTE */
425 adios (distfile
, "unable to link %s to", altmsg
);
427 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
428 die("unable to create temporary file in %s",
431 distfile
= mh_xstrdup(cp
);
436 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
437 adios (altmsg
, "unable to open");
439 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
440 adios (distfile
, "unable to write");
441 cpydata (in
, out
, altmsg
, distfile
);
451 if (auth_svc
== NULL
) {
452 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
453 die("must specify -authservice with -saslmech xoauth2");
457 die("must specify -user with -saslmech xoauth2");
461 NMH_UNUSED(auth_svc
);
463 NMH_UNUSED(saslmech
);
464 #endif /* OAUTH_SUPPORT */
466 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
477 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
478 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
490 context_save (); /* save the context file */