]>
diplodocus.org Git - nmh/blob - uip/send.c
c12908f46884511fbd38e7ebdbcc8568e17de97f
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("unique", -6, UNIQSW) \
73 X("nounique", -8, NUNIQSW) \
74 X("verbose", 0, VERBSW) \
75 X("noverbose", 0, NVERBSW) \
76 X("watch", 0, WATCSW) \
77 X("nowatch", 0, NWATCSW) \
78 X("width columns", 0, WIDTHSW) \
79 X("version", 0, VERSIONSW) \
80 X("help", 0, HELPSW) \
81 X("dashstuffing", -12, BITSTUFFSW) \
82 X("nodashstuffing", -14, NBITSTUFFSW) \
83 X("client host", -6, CLIESW) \
84 X("server host", 6, SERVSW) \
85 X("snoop", 5, SNOOPSW) \
86 X("sasl", SASLminc(4), SASLSW) \
87 X("nosasl", SASLminc(6), NOSASLSW) \
88 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
89 X("authservice", SASLminc(0), AUTHSERVICESW) \
90 X("user username", SASLminc(-4), USERSW) \
91 X("port server-port-name/number", 4, PORTSW) \
92 X("tls", TLSminc(-3), TLSSW) \
93 X("initialtls", TLSminc(-10), INITTLSSW) \
94 X("notls", TLSminc(-5), NTLSSW) \
95 X("certverify", TLSminc(-10), CERTVERSW) \
96 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
97 X("sendmail program", 0, MTSSM) \
98 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
99 X("messageid localname|random", 2, MESSAGEIDSW) \
101 #define X(sw, minchars, id) id,
102 DEFINE_SWITCH_ENUM(SEND
);
105 #define X(sw, minchars, id) { sw, minchars, id },
106 DEFINE_SWITCH_ARRAY(SEND
, switches
);
109 #define USE_SWITCHES \
112 X("list", 0, LISTDSW) \
114 #define X(sw, minchars, id) id,
115 DEFINE_SWITCH_ENUM(USE
);
118 #define X(sw, minchars, id) { sw, minchars, id },
119 DEFINE_SWITCH_ARRAY(USE
, anyl
);
122 extern int debugsw
; /* from sendsbr.c */
129 extern char *altmsg
; /* .. */
130 extern char *annotext
;
131 extern char *distfile
;
135 main (int argc
, char **argv
)
140 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
141 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
142 char *msgs
[MAXARGS
], **vec
;
143 const char *user
= NULL
, *saslmech
= NULL
;
146 char *auth_svc
= NULL
;
148 if (nmh_init(argv
[0], true, true)) { return 1; }
150 arguments
= getarguments (invo_name
, argc
, argv
, 1);
153 vec
= argsplit(postproc
, &program
, &vecp
);
155 vec
[vecp
++] = "-library";
156 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
158 if ((cp
= context_find ("fileproc"))) {
159 vec
[vecp
++] = "-fileproc";
163 if ((cp
= context_find ("mhlproc"))) {
164 vec
[vecp
++] = "-mhlproc";
168 if ((cp
= context_find ("credentials"))) {
169 /* post doesn't read context so need to pass credentials. */
170 vec
[vecp
++] = "-credentials";
174 while ((cp
= *argp
++)) {
176 switch (smatch (++cp
, switches
)) {
178 ambigsw (cp
, switches
);
181 die("-%s unknown\n", cp
);
184 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
185 print_help (buf
, switches
, 1);
188 print_version(invo_name
);
192 msgs
[msgp
++] = draft
;
197 die("only one draft folder at a time!");
198 if (!(cp
= *argp
++) || *cp
== '-')
199 die("missing argument to %s", argp
[-2]);
200 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
201 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 die("missing argument to %s", argp
[-2]);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 die("missing argument to %s", argp
[-2]);
286 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
287 die("missing argument to %s", argp
[-2]);
289 die("not built with OAuth support");
294 if (!(saslmech
= *argp
) || *saslmech
== '-')
295 die("missing argument to %s", argp
[-1]);
308 if (!(cp
= *argp
++) || *cp
== '-')
309 die("missing argument to %s", argp
[-2]);
319 * check for "Aliasfile:" profile entry
321 if ((cp
= context_find ("Aliasfile"))) {
324 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
325 vec
[vecp
++] = "-alias";
330 if (dfolder
== NULL
) {
332 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
333 if (stat (msgs
[0], &st
) == NOTOK
)
334 adios (msgs
[0], "unable to stat draft file");
335 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
336 for (status
= LISTDSW
; status
!= YESW
;) {
337 if (!(argp
= read_switch_multiword (cp
, anyl
)))
339 switch (status
= smatch (*argp
, anyl
)) {
345 showfile (++argp
, msgs
[0]);
353 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
354 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
357 if (!context_find ("path"))
358 free (path ("./", TFOLDER
));
361 msgs
[msgp
++] = "cur";
362 maildir
= m_maildir (dfolder
);
364 if (chdir (maildir
) == NOTOK
)
365 adios (maildir
, "unable to change directory to");
367 /* read folder and create message structure */
368 if (!(mp
= folder_read (dfolder
, 1)))
369 die("unable to read folder %s", dfolder
);
371 /* check for empty folder */
373 die("no messages in %s", dfolder
);
375 /* parse all the message ranges/sequences and set SELECTED */
376 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
377 if (!m_convert (mp
, msgs
[msgnum
]))
379 seq_setprev (mp
); /* set the previous-sequence */
381 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
382 if (is_selected (mp
, msgnum
)) {
383 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
384 unset_exists (mp
, msgnum
);
388 mp
->msgflags
|= SEQMOD
;
396 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
397 if ((cp
= context_find ("signature")) && *cp
)
398 setenv("SIGNATURE", cp
, 1);
400 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
401 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
402 adios (msgs
[msgnum
], "unable to stat draft file");
404 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
406 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
408 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
409 altmsg
= NULL
; /* used by dist interface - see below */
411 if ((cp
= getenv ("mhdist"))
415 vec
[vecp
++] = "-dist";
416 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
417 die("unable to create temporary file");
419 distfile
= mh_xstrdup(cp
);
420 (void) m_unlink(distfile
);
421 if (link (altmsg
, distfile
) == NOTOK
) {
422 /* Cygwin with FAT32 filesystem produces EPERM. */
423 if (errno
!= EXDEV
&& errno
!= EPERM
425 && errno
!= EISREMOTE
426 #endif /* EISREMOTE */
428 adios (distfile
, "unable to link %s to", altmsg
);
430 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
431 die("unable to create temporary file in %s",
434 distfile
= mh_xstrdup(cp
);
439 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
440 adios (altmsg
, "unable to open");
442 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
443 adios (distfile
, "unable to write");
444 cpydata (in
, out
, altmsg
, distfile
);
454 if (auth_svc
== NULL
) {
455 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
456 die("must specify -authservice with -saslmech xoauth2");
460 die("must specify -user with -saslmech xoauth2");
464 NMH_UNUSED(auth_svc
);
466 NMH_UNUSED(saslmech
);
467 #endif /* OAUTH_SUPPORT */
469 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
479 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
480 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
492 context_save (); /* save the context file */