]>
diplodocus.org Git - nmh/blob - uip/send.c
f19ecba331a49dfde459486223c1fae8f48f7846
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_convert.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_find.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/arglist.h"
20 #include "sbr/error.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/m_mktemp.h"
31 # define SASLminc(a) (a)
32 #else /* CYRUS_SASL */
33 # define SASLminc(a) 0
34 #endif /* CYRUS_SASL */
37 # define TLSminc(a) (a)
38 #else /* TLS_SUPPORT */
40 #endif /* TLS_SUPPORT */
42 #define SEND_SWITCHES \
43 X("alias aliasfile", 0, ALIASW) \
44 X("debug", -5, DEBUGSW) \
45 X("draft", 0, DRAFTSW) \
46 X("draftfolder +folder", 6, DFOLDSW) \
47 X("draftmessage msg", 6, DMSGSW) \
48 X("nodraftfolder", 0, NDFLDSW) \
49 X("filter filterfile", 0, FILTSW) \
50 X("nofilter", 0, NFILTSW) \
51 X("format", 0, FRMTSW) \
52 X("noformat", 0, NFRMTSW) \
53 X("forward", 0, FORWSW) \
54 X("noforward", 0, NFORWSW) \
55 X("mime", 0, MIMESW) \
56 X("nomime", 0, NMIMESW) \
57 X("msgid", 0, MSGDSW) \
58 X("nomsgid", 0, NMSGDSW) \
59 X("push", 0, PUSHSW) \
60 X("nopush", 0, NPUSHSW) \
61 X("split seconds", 0, SPLITSW) \
62 X("unique", -6, UNIQSW) \
63 X("nounique", -8, NUNIQSW) \
64 X("verbose", 0, VERBSW) \
65 X("noverbose", 0, NVERBSW) \
66 X("watch", 0, WATCSW) \
67 X("nowatch", 0, NWATCSW) \
68 X("width columns", 0, WIDTHSW) \
69 X("version", 0, VERSIONSW) \
70 X("help", 0, HELPSW) \
71 X("dashstuffing", -12, BITSTUFFSW) \
72 X("nodashstuffing", -14, NBITSTUFFSW) \
73 X("client host", -6, CLIESW) \
74 X("server host", 6, SERVSW) \
75 X("snoop", 5, SNOOPSW) \
76 X("sasl", SASLminc(4), SASLSW) \
77 X("nosasl", SASLminc(6), NOSASLSW) \
78 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
79 X("authservice", SASLminc(0), AUTHSERVICESW) \
80 X("user username", SASLminc(-4), USERSW) \
81 X("port server-port-name/number", 4, PORTSW) \
82 X("tls", TLSminc(-3), TLSSW) \
83 X("initialtls", TLSminc(-10), INITTLSSW) \
84 X("notls", TLSminc(-5), NTLSSW) \
85 X("certverify", TLSminc(-10), CERTVERSW) \
86 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
87 X("sendmail program", 0, MTSSM) \
88 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
89 X("messageid localname|random", 2, MESSAGEIDSW) \
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(SEND
);
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(SEND
, switches
);
99 #define USE_SWITCHES \
102 X("list", 0, LISTDSW) \
104 #define X(sw, minchars, id) id,
105 DEFINE_SWITCH_ENUM(USE
);
108 #define X(sw, minchars, id) { sw, minchars, id },
109 DEFINE_SWITCH_ARRAY(USE
, anyl
);
112 extern int debugsw
; /* from sendsbr.c */
120 extern char *altmsg
; /* .. */
121 extern char *annotext
;
122 extern char *distfile
;
126 main (int argc
, char **argv
)
131 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
132 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
133 char *msgs
[MAXARGS
], **vec
;
134 const char *user
= NULL
, *saslmech
= NULL
;
137 char *auth_svc
= NULL
;
139 if (nmh_init(argv
[0], true, true)) { return 1; }
141 arguments
= getarguments (invo_name
, argc
, argv
, 1);
144 vec
= argsplit(postproc
, &program
, &vecp
);
146 vec
[vecp
++] = "-library";
147 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
149 if ((cp
= context_find ("fileproc"))) {
150 vec
[vecp
++] = "-fileproc";
154 if ((cp
= context_find ("mhlproc"))) {
155 vec
[vecp
++] = "-mhlproc";
159 if ((cp
= context_find ("credentials"))) {
160 /* post doesn't read context so need to pass credentials. */
161 vec
[vecp
++] = "-credentials";
165 while ((cp
= *argp
++)) {
167 switch (smatch (++cp
, switches
)) {
169 ambigsw (cp
, switches
);
172 die("-%s unknown\n", cp
);
175 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
176 print_help (buf
, switches
, 1);
179 print_version(invo_name
);
183 msgs
[msgp
++] = draft
;
188 die("only one draft folder at a time!");
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
192 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
212 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
213 die("missing argument to %s", argp
[-2]);
274 if (!(cp
= *argp
++) || *cp
== '-')
275 die("missing argument to %s", argp
[-2]);
282 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
283 die("missing argument to %s", argp
[-2]);
285 die("not built with OAuth support");
290 if (!(saslmech
= *argp
) || *saslmech
== '-')
291 die("missing argument to %s", argp
[-1]);
304 if (!(cp
= *argp
++) || *cp
== '-')
305 die("missing argument to %s", argp
[-2]);
315 * check for "Aliasfile:" profile entry
317 if ((cp
= context_find ("Aliasfile"))) {
320 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
321 vec
[vecp
++] = "-alias";
326 if (dfolder
== NULL
) {
328 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
329 if (stat (msgs
[0], &st
) == NOTOK
)
330 adios (msgs
[0], "unable to stat draft file");
331 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
332 for (status
= LISTDSW
; status
!= YESW
;) {
333 if (!(argp
= read_switch_multiword (cp
, anyl
)))
335 switch (status
= smatch (*argp
, anyl
)) {
341 showfile (++argp
, msgs
[0]);
349 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
350 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
353 if (!context_find ("path"))
354 free (path ("./", TFOLDER
));
357 msgs
[msgp
++] = "cur";
358 maildir
= m_maildir (dfolder
);
360 if (chdir (maildir
) == NOTOK
)
361 adios (maildir
, "unable to change directory to");
363 /* read folder and create message structure */
364 if (!(mp
= folder_read (dfolder
, 1)))
365 die("unable to read folder %s", dfolder
);
367 /* check for empty folder */
369 die("no messages in %s", dfolder
);
371 /* parse all the message ranges/sequences and set SELECTED */
372 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
373 if (!m_convert (mp
, msgs
[msgnum
]))
375 seq_setprev (mp
); /* set the previous-sequence */
377 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
378 if (is_selected (mp
, msgnum
)) {
379 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
380 unset_exists (mp
, msgnum
);
384 mp
->msgflags
|= SEQMOD
;
392 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
393 if ((cp
= context_find ("signature")) && *cp
)
394 setenv("SIGNATURE", cp
, 1);
396 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
397 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
398 adios (msgs
[msgnum
], "unable to stat draft file");
400 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
402 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
404 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
405 altmsg
= NULL
; /* used by dist interface - see below */
407 if ((cp
= getenv ("mhdist"))
411 vec
[vecp
++] = "-dist";
412 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
413 die("unable to create temporary file");
415 distfile
= mh_xstrdup(cp
);
416 (void) m_unlink(distfile
);
417 if (link (altmsg
, distfile
) == NOTOK
) {
418 /* Cygwin with FAT32 filesystem produces EPERM. */
419 if (errno
!= EXDEV
&& errno
!= EPERM
421 && errno
!= EISREMOTE
422 #endif /* EISREMOTE */
424 adios (distfile
, "unable to link %s to", altmsg
);
426 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
427 die("unable to create temporary file in %s",
430 distfile
= mh_xstrdup(cp
);
435 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
436 adios (altmsg
, "unable to open");
438 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
439 adios (distfile
, "unable to write");
440 cpydata (in
, out
, altmsg
, distfile
);
450 if (auth_svc
== NULL
) {
451 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
452 die("must specify -authservice with -saslmech xoauth2");
456 die("must specify -user with -saslmech xoauth2");
460 NMH_UNUSED(auth_svc
);
462 NMH_UNUSED(saslmech
);
463 #endif /* OAUTH_SUPPORT */
465 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
476 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
477 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
489 context_save (); /* save the context file */