]>
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.
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_mktemp.h"
25 # define SASLminc(a) (a)
26 #else /* CYRUS_SASL */
27 # define SASLminc(a) 0
28 #endif /* CYRUS_SASL */
31 # define TLSminc(a) (a)
32 #else /* TLS_SUPPORT */
34 #endif /* TLS_SUPPORT */
36 #define SEND_SWITCHES \
37 X("alias aliasfile", 0, ALIASW) \
38 X("debug", -5, DEBUGSW) \
39 X("draft", 0, DRAFTSW) \
40 X("draftfolder +folder", 6, DFOLDSW) \
41 X("draftmessage msg", 6, DMSGSW) \
42 X("nodraftfolder", 0, NDFLDSW) \
43 X("filter filterfile", 0, FILTSW) \
44 X("nofilter", 0, NFILTSW) \
45 X("format", 0, FRMTSW) \
46 X("noformat", 0, NFRMTSW) \
47 X("forward", 0, FORWSW) \
48 X("noforward", 0, NFORWSW) \
49 X("mime", 0, MIMESW) \
50 X("nomime", 0, NMIMESW) \
51 X("msgid", 0, MSGDSW) \
52 X("nomsgid", 0, NMSGDSW) \
53 X("push", 0, PUSHSW) \
54 X("nopush", 0, NPUSHSW) \
55 X("split seconds", 0, SPLITSW) \
56 X("unique", -6, UNIQSW) \
57 X("nounique", -8, NUNIQSW) \
58 X("verbose", 0, VERBSW) \
59 X("noverbose", 0, NVERBSW) \
60 X("watch", 0, WATCSW) \
61 X("nowatch", 0, NWATCSW) \
62 X("width columns", 0, WIDTHSW) \
63 X("version", 0, VERSIONSW) \
64 X("help", 0, HELPSW) \
65 X("dashstuffing", -12, BITSTUFFSW) \
66 X("nodashstuffing", -14, NBITSTUFFSW) \
67 X("client host", -6, CLIESW) \
68 X("server host", 6, SERVSW) \
69 X("snoop", 5, SNOOPSW) \
70 X("sasl", SASLminc(4), SASLSW) \
71 X("nosasl", SASLminc(6), NOSASLSW) \
72 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
73 X("authservice", SASLminc(0), AUTHSERVICESW) \
74 X("user username", SASLminc(-4), USERSW) \
75 X("port server-port-name/number", 4, PORTSW) \
76 X("tls", TLSminc(-3), TLSSW) \
77 X("initialtls", TLSminc(-10), INITTLSSW) \
78 X("notls", TLSminc(-5), NTLSSW) \
79 X("certverify", TLSminc(-10), CERTVERSW) \
80 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
81 X("sendmail program", 0, MTSSM) \
82 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
83 X("messageid localname|random", 2, MESSAGEIDSW) \
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(SEND
);
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(SEND
, switches
);
93 #define USE_SWITCHES \
96 X("list", 0, LISTDSW) \
98 #define X(sw, minchars, id) id,
99 DEFINE_SWITCH_ENUM(USE
);
102 #define X(sw, minchars, id) { sw, minchars, id },
103 DEFINE_SWITCH_ARRAY(USE
, anyl
);
106 extern int debugsw
; /* from sendsbr.c */
114 extern char *altmsg
; /* .. */
115 extern char *annotext
;
116 extern char *distfile
;
120 main (int argc
, char **argv
)
125 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
126 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
127 char *msgs
[MAXARGS
], **vec
;
128 const char *user
= NULL
, *saslmech
= NULL
;
131 char *auth_svc
= NULL
;
133 if (nmh_init(argv
[0], true, true)) { return 1; }
135 arguments
= getarguments (invo_name
, argc
, argv
, 1);
138 vec
= argsplit(postproc
, &program
, &vecp
);
140 vec
[vecp
++] = "-library";
141 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
143 if ((cp
= context_find ("fileproc"))) {
144 vec
[vecp
++] = "-fileproc";
148 if ((cp
= context_find ("mhlproc"))) {
149 vec
[vecp
++] = "-mhlproc";
153 if ((cp
= context_find ("credentials"))) {
154 /* post doesn't read context so need to pass credentials. */
155 vec
[vecp
++] = "-credentials";
159 while ((cp
= *argp
++)) {
161 switch (smatch (++cp
, switches
)) {
163 ambigsw (cp
, switches
);
166 die("-%s unknown\n", cp
);
169 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
170 print_help (buf
, switches
, 1);
173 print_version(invo_name
);
177 msgs
[msgp
++] = draft
;
182 die("only one draft folder at a time!");
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
186 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
206 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
207 die("missing argument to %s", argp
[-2]);
268 if (!(cp
= *argp
++) || *cp
== '-')
269 die("missing argument to %s", argp
[-2]);
276 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
277 die("missing argument to %s", argp
[-2]);
279 die("not built with OAuth support");
284 if (!(saslmech
= *argp
) || *saslmech
== '-')
285 die("missing argument to %s", argp
[-1]);
298 if (!(cp
= *argp
++) || *cp
== '-')
299 die("missing argument to %s", argp
[-2]);
309 * check for "Aliasfile:" profile entry
311 if ((cp
= context_find ("Aliasfile"))) {
314 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
315 vec
[vecp
++] = "-alias";
320 if (dfolder
== NULL
) {
322 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
323 if (stat (msgs
[0], &st
) == NOTOK
)
324 adios (msgs
[0], "unable to stat draft file");
325 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
326 for (status
= LISTDSW
; status
!= YESW
;) {
327 if (!(argp
= read_switch_multiword (cp
, anyl
)))
329 switch (status
= smatch (*argp
, anyl
)) {
335 showfile (++argp
, msgs
[0]);
343 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
344 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
347 if (!context_find ("path"))
348 free (path ("./", TFOLDER
));
351 msgs
[msgp
++] = "cur";
352 maildir
= m_maildir (dfolder
);
354 if (chdir (maildir
) == NOTOK
)
355 adios (maildir
, "unable to change directory to");
357 /* read folder and create message structure */
358 if (!(mp
= folder_read (dfolder
, 1)))
359 die("unable to read folder %s", dfolder
);
361 /* check for empty folder */
363 die("no messages in %s", dfolder
);
365 /* parse all the message ranges/sequences and set SELECTED */
366 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
367 if (!m_convert (mp
, msgs
[msgnum
]))
369 seq_setprev (mp
); /* set the previous-sequence */
371 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
372 if (is_selected (mp
, msgnum
)) {
373 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
374 unset_exists (mp
, msgnum
);
378 mp
->msgflags
|= SEQMOD
;
386 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
387 if ((cp
= context_find ("signature")) && *cp
)
388 setenv("SIGNATURE", cp
, 1);
390 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
391 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
392 adios (msgs
[msgnum
], "unable to stat draft file");
394 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
396 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
398 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
399 altmsg
= NULL
; /* used by dist interface - see below */
401 if ((cp
= getenv ("mhdist"))
405 vec
[vecp
++] = "-dist";
406 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
407 die("unable to create temporary file");
409 distfile
= mh_xstrdup(cp
);
410 (void) m_unlink(distfile
);
411 if (link (altmsg
, distfile
) == NOTOK
) {
412 /* Cygwin with FAT32 filesystem produces EPERM. */
413 if (errno
!= EXDEV
&& errno
!= EPERM
415 && errno
!= EISREMOTE
416 #endif /* EISREMOTE */
418 adios (distfile
, "unable to link %s to", altmsg
);
420 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
421 die("unable to create temporary file in %s",
424 distfile
= mh_xstrdup(cp
);
429 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
430 adios (altmsg
, "unable to open");
432 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
433 adios (distfile
, "unable to write");
434 cpydata (in
, out
, altmsg
, distfile
);
444 if (auth_svc
== NULL
) {
445 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
446 die("must specify -authservice with -saslmech xoauth2");
450 die("must specify -user with -saslmech xoauth2");
454 NMH_UNUSED(auth_svc
);
456 NMH_UNUSED(saslmech
);
457 #endif /* OAUTH_SUPPORT */
459 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
470 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
471 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
483 context_save (); /* save the context file */