]>
diplodocus.org Git - nmh/blob - uip/send.c
e6a74f3197479f260a3ae80d764c5505280faa1a
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/showfile.h"
10 #include "sbr/smatch.h"
11 #include "sbr/closefds.h"
12 #include "sbr/cpydata.h"
13 #include "sbr/m_draft.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_find.h"
18 #include "sbr/brkstring.h"
19 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/arglist.h"
25 #include "sbr/error.h"
32 #include "sbr/m_maildir.h"
33 #include "sbr/m_mktemp.h"
36 # define SASLminc(a) (a)
37 #else /* CYRUS_SASL */
38 # define SASLminc(a) 0
39 #endif /* CYRUS_SASL */
42 # define TLSminc(a) (a)
43 #else /* TLS_SUPPORT */
45 #endif /* TLS_SUPPORT */
47 #define SEND_SWITCHES \
48 X("alias aliasfile", 0, ALIASW) \
49 X("debug", -5, DEBUGSW) \
50 X("draft", 0, DRAFTSW) \
51 X("draftfolder +folder", 6, DFOLDSW) \
52 X("draftmessage msg", 6, DMSGSW) \
53 X("nodraftfolder", 0, NDFLDSW) \
54 X("filter filterfile", 0, FILTSW) \
55 X("nofilter", 0, NFILTSW) \
56 X("format", 0, FRMTSW) \
57 X("noformat", 0, NFRMTSW) \
58 X("forward", 0, FORWSW) \
59 X("noforward", 0, NFORWSW) \
60 X("mime", 0, MIMESW) \
61 X("nomime", 0, NMIMESW) \
62 X("msgid", 0, MSGDSW) \
63 X("nomsgid", 0, NMSGDSW) \
64 X("push", 0, PUSHSW) \
65 X("nopush", 0, NPUSHSW) \
66 X("split seconds", 0, SPLITSW) \
67 X("unique", -6, UNIQSW) \
68 X("nounique", -8, NUNIQSW) \
69 X("verbose", 0, VERBSW) \
70 X("noverbose", 0, NVERBSW) \
71 X("watch", 0, WATCSW) \
72 X("nowatch", 0, NWATCSW) \
73 X("width columns", 0, WIDTHSW) \
74 X("version", 0, VERSIONSW) \
75 X("help", 0, HELPSW) \
76 X("dashstuffing", -12, BITSTUFFSW) \
77 X("nodashstuffing", -14, NBITSTUFFSW) \
78 X("client host", -6, CLIESW) \
79 X("server host", 6, SERVSW) \
80 X("snoop", 5, SNOOPSW) \
81 X("sasl", SASLminc(4), SASLSW) \
82 X("nosasl", SASLminc(6), NOSASLSW) \
83 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
84 X("authservice", SASLminc(0), AUTHSERVICESW) \
85 X("user username", SASLminc(-4), USERSW) \
86 X("port server-port-name/number", 4, PORTSW) \
87 X("tls", TLSminc(-3), TLSSW) \
88 X("initialtls", TLSminc(-10), INITTLSSW) \
89 X("notls", TLSminc(-5), NTLSSW) \
90 X("certverify", TLSminc(-10), CERTVERSW) \
91 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
92 X("sendmail program", 0, MTSSM) \
93 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
94 X("messageid localname|random", 2, MESSAGEIDSW) \
96 #define X(sw, minchars, id) id,
97 DEFINE_SWITCH_ENUM(SEND
);
100 #define X(sw, minchars, id) { sw, minchars, id },
101 DEFINE_SWITCH_ARRAY(SEND
, switches
);
104 #define USE_SWITCHES \
107 X("list", 0, LISTDSW) \
109 #define X(sw, minchars, id) id,
110 DEFINE_SWITCH_ENUM(USE
);
113 #define X(sw, minchars, id) { sw, minchars, id },
114 DEFINE_SWITCH_ARRAY(USE
, anyl
);
117 extern int debugsw
; /* from sendsbr.c */
125 extern char *altmsg
; /* .. */
126 extern char *annotext
;
127 extern char *distfile
;
131 main (int argc
, char **argv
)
136 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
137 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
138 char *msgs
[MAXARGS
], **vec
;
139 const char *user
= NULL
, *saslmech
= NULL
;
142 char *auth_svc
= NULL
;
144 if (nmh_init(argv
[0], true, true)) { return 1; }
146 arguments
= getarguments (invo_name
, argc
, argv
, 1);
149 vec
= argsplit(postproc
, &program
, &vecp
);
151 vec
[vecp
++] = "-library";
152 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
154 if ((cp
= context_find ("fileproc"))) {
155 vec
[vecp
++] = "-fileproc";
159 if ((cp
= context_find ("mhlproc"))) {
160 vec
[vecp
++] = "-mhlproc";
164 if ((cp
= context_find ("credentials"))) {
165 /* post doesn't read context so need to pass credentials. */
166 vec
[vecp
++] = "-credentials";
170 while ((cp
= *argp
++)) {
172 switch (smatch (++cp
, switches
)) {
174 ambigsw (cp
, switches
);
177 die("-%s unknown\n", cp
);
180 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
181 print_help (buf
, switches
, 1);
184 print_version(invo_name
);
188 msgs
[msgp
++] = draft
;
193 die("only one draft folder at a time!");
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
196 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
197 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
217 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
218 die("missing argument to %s", argp
[-2]);
279 if (!(cp
= *argp
++) || *cp
== '-')
280 die("missing argument to %s", argp
[-2]);
287 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
288 die("missing argument to %s", argp
[-2]);
290 die("not built with OAuth support");
295 if (!(saslmech
= *argp
) || *saslmech
== '-')
296 die("missing argument to %s", argp
[-1]);
309 if (!(cp
= *argp
++) || *cp
== '-')
310 die("missing argument to %s", argp
[-2]);
320 * check for "Aliasfile:" profile entry
322 if ((cp
= context_find ("Aliasfile"))) {
325 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
326 vec
[vecp
++] = "-alias";
331 if (dfolder
== NULL
) {
333 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
334 if (stat (msgs
[0], &st
) == NOTOK
)
335 adios (msgs
[0], "unable to stat draft file");
336 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
337 for (status
= LISTDSW
; status
!= YESW
;) {
338 if (!(argp
= read_switch_multiword (cp
, anyl
)))
340 switch (status
= smatch (*argp
, anyl
)) {
346 showfile (++argp
, msgs
[0]);
354 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
355 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
358 if (!context_find ("path"))
359 free (path ("./", TFOLDER
));
362 msgs
[msgp
++] = "cur";
363 maildir
= m_maildir (dfolder
);
365 if (chdir (maildir
) == NOTOK
)
366 adios (maildir
, "unable to change directory to");
368 /* read folder and create message structure */
369 if (!(mp
= folder_read (dfolder
, 1)))
370 die("unable to read folder %s", dfolder
);
372 /* check for empty folder */
374 die("no messages in %s", dfolder
);
376 /* parse all the message ranges/sequences and set SELECTED */
377 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
378 if (!m_convert (mp
, msgs
[msgnum
]))
380 seq_setprev (mp
); /* set the previous-sequence */
382 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
383 if (is_selected (mp
, msgnum
)) {
384 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
385 unset_exists (mp
, msgnum
);
389 mp
->msgflags
|= SEQMOD
;
397 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
398 if ((cp
= context_find ("signature")) && *cp
)
399 setenv("SIGNATURE", cp
, 1);
401 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
402 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
403 adios (msgs
[msgnum
], "unable to stat draft file");
405 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
407 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
409 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
410 altmsg
= NULL
; /* used by dist interface - see below */
412 if ((cp
= getenv ("mhdist"))
416 vec
[vecp
++] = "-dist";
417 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
418 die("unable to create temporary file");
420 distfile
= mh_xstrdup(cp
);
421 (void) m_unlink(distfile
);
422 if (link (altmsg
, distfile
) == NOTOK
) {
423 /* Cygwin with FAT32 filesystem produces EPERM. */
424 if (errno
!= EXDEV
&& errno
!= EPERM
426 && errno
!= EISREMOTE
427 #endif /* EISREMOTE */
429 adios (distfile
, "unable to link %s to", altmsg
);
431 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
432 die("unable to create temporary file in %s",
435 distfile
= mh_xstrdup(cp
);
440 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
441 adios (altmsg
, "unable to open");
443 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
444 adios (distfile
, "unable to write");
445 cpydata (in
, out
, altmsg
, distfile
);
455 if (auth_svc
== NULL
) {
456 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
457 die("must specify -authservice with -saslmech xoauth2");
461 die("must specify -user with -saslmech xoauth2");
465 NMH_UNUSED(auth_svc
);
467 NMH_UNUSED(saslmech
);
468 #endif /* OAUTH_SUPPORT */
470 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
481 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
482 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
494 context_save (); /* save the context file */