]>
diplodocus.org Git - nmh/blob - uip/send.c
d4cfd2e9bbd4d3acac753c6bae1d70314a98cdf1
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/print_help.h"
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
22 # define SASLminc(a) (a)
23 #else /* CYRUS_SASL */
24 # define SASLminc(a) 0
25 #endif /* CYRUS_SASL */
28 # define TLSminc(a) (a)
29 #else /* TLS_SUPPORT */
31 #endif /* TLS_SUPPORT */
33 #define SEND_SWITCHES \
34 X("alias aliasfile", 0, ALIASW) \
35 X("debug", -5, DEBUGSW) \
36 X("draft", 0, DRAFTSW) \
37 X("draftfolder +folder", 6, DFOLDSW) \
38 X("draftmessage msg", 6, DMSGSW) \
39 X("nodraftfolder", 0, NDFLDSW) \
40 X("filter filterfile", 0, FILTSW) \
41 X("nofilter", 0, NFILTSW) \
42 X("format", 0, FRMTSW) \
43 X("noformat", 0, NFRMTSW) \
44 X("forward", 0, FORWSW) \
45 X("noforward", 0, NFORWSW) \
46 X("mime", 0, MIMESW) \
47 X("nomime", 0, NMIMESW) \
48 X("msgid", 0, MSGDSW) \
49 X("nomsgid", 0, NMSGDSW) \
50 X("push", 0, PUSHSW) \
51 X("nopush", 0, NPUSHSW) \
52 X("split seconds", 0, SPLITSW) \
53 X("unique", -6, UNIQSW) \
54 X("nounique", -8, NUNIQSW) \
55 X("verbose", 0, VERBSW) \
56 X("noverbose", 0, NVERBSW) \
57 X("watch", 0, WATCSW) \
58 X("nowatch", 0, NWATCSW) \
59 X("width columns", 0, WIDTHSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
62 X("dashstuffing", -12, BITSTUFFSW) \
63 X("nodashstuffing", -14, NBITSTUFFSW) \
64 X("client host", -6, CLIESW) \
65 X("server host", 6, SERVSW) \
66 X("snoop", 5, SNOOPSW) \
67 X("sasl", SASLminc(4), SASLSW) \
68 X("nosasl", SASLminc(6), NOSASLSW) \
69 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
70 X("authservice", SASLminc(0), AUTHSERVICESW) \
71 X("user username", SASLminc(-4), USERSW) \
72 X("port server-port-name/number", 4, PORTSW) \
73 X("tls", TLSminc(-3), TLSSW) \
74 X("initialtls", TLSminc(-10), INITTLSSW) \
75 X("notls", TLSminc(-5), NTLSSW) \
76 X("certverify", TLSminc(-10), CERTVERSW) \
77 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
78 X("sendmail program", 0, MTSSM) \
79 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
80 X("messageid localname|random", 2, MESSAGEIDSW) \
82 #define X(sw, minchars, id) id,
83 DEFINE_SWITCH_ENUM(SEND
);
86 #define X(sw, minchars, id) { sw, minchars, id },
87 DEFINE_SWITCH_ARRAY(SEND
, switches
);
90 #define USE_SWITCHES \
93 X("list", 0, LISTDSW) \
95 #define X(sw, minchars, id) id,
96 DEFINE_SWITCH_ENUM(USE
);
99 #define X(sw, minchars, id) { sw, minchars, id },
100 DEFINE_SWITCH_ARRAY(USE
, anyl
);
103 extern int debugsw
; /* from sendsbr.c */
111 extern char *altmsg
; /* .. */
112 extern char *annotext
;
113 extern char *distfile
;
117 main (int argc
, char **argv
)
122 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
123 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
124 char *msgs
[MAXARGS
], **vec
;
125 const char *user
= NULL
, *saslmech
= NULL
;
128 char *auth_svc
= NULL
;
130 if (nmh_init(argv
[0], true, true)) { return 1; }
132 arguments
= getarguments (invo_name
, argc
, argv
, 1);
135 vec
= argsplit(postproc
, &program
, &vecp
);
137 vec
[vecp
++] = "-library";
138 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
140 if ((cp
= context_find ("fileproc"))) {
141 vec
[vecp
++] = "-fileproc";
145 if ((cp
= context_find ("mhlproc"))) {
146 vec
[vecp
++] = "-mhlproc";
150 if ((cp
= context_find ("credentials"))) {
151 /* post doesn't read context so need to pass credentials. */
152 vec
[vecp
++] = "-credentials";
156 while ((cp
= *argp
++)) {
158 switch (smatch (++cp
, switches
)) {
160 ambigsw (cp
, switches
);
163 die("-%s unknown\n", cp
);
166 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
167 print_help (buf
, switches
, 1);
170 print_version(invo_name
);
174 msgs
[msgp
++] = draft
;
179 die("only one draft folder at a time!");
180 if (!(cp
= *argp
++) || *cp
== '-')
181 die("missing argument to %s", argp
[-2]);
182 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
183 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
203 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
204 die("missing argument to %s", argp
[-2]);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
273 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
274 die("missing argument to %s", argp
[-2]);
276 die("not built with OAuth support");
281 if (!(saslmech
= *argp
) || *saslmech
== '-')
282 die("missing argument to %s", argp
[-1]);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 die("missing argument to %s", argp
[-2]);
306 * check for "Aliasfile:" profile entry
308 if ((cp
= context_find ("Aliasfile"))) {
311 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
312 vec
[vecp
++] = "-alias";
317 if (dfolder
== NULL
) {
319 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
320 if (stat (msgs
[0], &st
) == NOTOK
)
321 adios (msgs
[0], "unable to stat draft file");
322 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
323 for (status
= LISTDSW
; status
!= YESW
;) {
324 if (!(argp
= read_switch_multiword (cp
, anyl
)))
326 switch (status
= smatch (*argp
, anyl
)) {
332 showfile (++argp
, msgs
[0]);
340 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
341 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER
));
348 msgs
[msgp
++] = "cur";
349 maildir
= m_maildir (dfolder
);
351 if (chdir (maildir
) == NOTOK
)
352 adios (maildir
, "unable to change directory to");
354 /* read folder and create message structure */
355 if (!(mp
= folder_read (dfolder
, 1)))
356 die("unable to read folder %s", dfolder
);
358 /* check for empty folder */
360 die("no messages in %s", dfolder
);
362 /* parse all the message ranges/sequences and set SELECTED */
363 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
364 if (!m_convert (mp
, msgs
[msgnum
]))
366 seq_setprev (mp
); /* set the previous-sequence */
368 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
369 if (is_selected (mp
, msgnum
)) {
370 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
371 unset_exists (mp
, msgnum
);
375 mp
->msgflags
|= SEQMOD
;
383 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
384 if ((cp
= context_find ("signature")) && *cp
)
385 setenv("SIGNATURE", cp
, 1);
387 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
388 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
389 adios (msgs
[msgnum
], "unable to stat draft file");
391 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
393 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
395 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
396 altmsg
= NULL
; /* used by dist interface - see below */
398 if ((cp
= getenv ("mhdist"))
402 vec
[vecp
++] = "-dist";
403 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
404 die("unable to create temporary file");
406 distfile
= mh_xstrdup(cp
);
407 (void) m_unlink(distfile
);
408 if (link (altmsg
, distfile
) == NOTOK
) {
409 /* Cygwin with FAT32 filesystem produces EPERM. */
410 if (errno
!= EXDEV
&& errno
!= EPERM
412 && errno
!= EISREMOTE
413 #endif /* EISREMOTE */
415 adios (distfile
, "unable to link %s to", altmsg
);
417 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
418 die("unable to create temporary file in %s",
421 distfile
= mh_xstrdup(cp
);
426 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
427 adios (altmsg
, "unable to open");
429 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
430 adios (distfile
, "unable to write");
431 cpydata (in
, out
, altmsg
, distfile
);
441 if (auth_svc
== NULL
) {
442 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
443 die("must specify -authservice with -saslmech xoauth2");
447 die("must specify -user with -saslmech xoauth2");
451 NMH_UNUSED(auth_svc
);
453 NMH_UNUSED(saslmech
);
454 #endif /* OAUTH_SUPPORT */
456 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
467 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
468 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
480 context_save (); /* save the context file */