]>
diplodocus.org Git - nmh/blob - uip/send.c
51286889fd11c59282a0882cd4e3206697d4c0ad
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_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/arglist.h"
12 #include "sbr/error.h"
19 #include "sbr/m_maildir.h"
20 #include "sbr/m_mktemp.h"
23 # define SASLminc(a) (a)
24 #else /* CYRUS_SASL */
25 # define SASLminc(a) 0
26 #endif /* CYRUS_SASL */
29 # define TLSminc(a) (a)
30 #else /* TLS_SUPPORT */
32 #endif /* TLS_SUPPORT */
34 #define SEND_SWITCHES \
35 X("alias aliasfile", 0, ALIASW) \
36 X("debug", -5, DEBUGSW) \
37 X("draft", 0, DRAFTSW) \
38 X("draftfolder +folder", 6, DFOLDSW) \
39 X("draftmessage msg", 6, DMSGSW) \
40 X("nodraftfolder", 0, NDFLDSW) \
41 X("filter filterfile", 0, FILTSW) \
42 X("nofilter", 0, NFILTSW) \
43 X("format", 0, FRMTSW) \
44 X("noformat", 0, NFRMTSW) \
45 X("forward", 0, FORWSW) \
46 X("noforward", 0, NFORWSW) \
47 X("mime", 0, MIMESW) \
48 X("nomime", 0, NMIMESW) \
49 X("msgid", 0, MSGDSW) \
50 X("nomsgid", 0, NMSGDSW) \
51 X("push", 0, PUSHSW) \
52 X("nopush", 0, NPUSHSW) \
53 X("split seconds", 0, SPLITSW) \
54 X("unique", -6, UNIQSW) \
55 X("nounique", -8, NUNIQSW) \
56 X("verbose", 0, VERBSW) \
57 X("noverbose", 0, NVERBSW) \
58 X("watch", 0, WATCSW) \
59 X("nowatch", 0, NWATCSW) \
60 X("width columns", 0, WIDTHSW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("dashstuffing", -12, BITSTUFFSW) \
64 X("nodashstuffing", -14, NBITSTUFFSW) \
65 X("client host", -6, CLIESW) \
66 X("server host", 6, SERVSW) \
67 X("snoop", 5, SNOOPSW) \
68 X("sasl", SASLminc(4), SASLSW) \
69 X("nosasl", SASLminc(6), NOSASLSW) \
70 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
71 X("authservice", SASLminc(0), AUTHSERVICESW) \
72 X("user username", SASLminc(-4), USERSW) \
73 X("port server-port-name/number", 4, PORTSW) \
74 X("tls", TLSminc(-3), TLSSW) \
75 X("initialtls", TLSminc(-10), INITTLSSW) \
76 X("notls", TLSminc(-5), NTLSSW) \
77 X("certverify", TLSminc(-10), CERTVERSW) \
78 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
79 X("sendmail program", 0, MTSSM) \
80 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
81 X("messageid localname|random", 2, MESSAGEIDSW) \
83 #define X(sw, minchars, id) id,
84 DEFINE_SWITCH_ENUM(SEND
);
87 #define X(sw, minchars, id) { sw, minchars, id },
88 DEFINE_SWITCH_ARRAY(SEND
, switches
);
91 #define USE_SWITCHES \
94 X("list", 0, LISTDSW) \
96 #define X(sw, minchars, id) id,
97 DEFINE_SWITCH_ENUM(USE
);
100 #define X(sw, minchars, id) { sw, minchars, id },
101 DEFINE_SWITCH_ARRAY(USE
, anyl
);
104 extern int debugsw
; /* from sendsbr.c */
112 extern char *altmsg
; /* .. */
113 extern char *annotext
;
114 extern char *distfile
;
118 main (int argc
, char **argv
)
123 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
124 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
125 char *msgs
[MAXARGS
], **vec
;
126 const char *user
= NULL
, *saslmech
= NULL
;
129 char *auth_svc
= NULL
;
131 if (nmh_init(argv
[0], true, true)) { return 1; }
133 arguments
= getarguments (invo_name
, argc
, argv
, 1);
136 vec
= argsplit(postproc
, &program
, &vecp
);
138 vec
[vecp
++] = "-library";
139 vec
[vecp
++] = mh_xstrdup(m_maildir(""));
141 if ((cp
= context_find ("fileproc"))) {
142 vec
[vecp
++] = "-fileproc";
146 if ((cp
= context_find ("mhlproc"))) {
147 vec
[vecp
++] = "-mhlproc";
151 if ((cp
= context_find ("credentials"))) {
152 /* post doesn't read context so need to pass credentials. */
153 vec
[vecp
++] = "-credentials";
157 while ((cp
= *argp
++)) {
159 switch (smatch (++cp
, switches
)) {
161 ambigsw (cp
, switches
);
164 die("-%s unknown\n", cp
);
167 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
168 print_help (buf
, switches
, 1);
171 print_version(invo_name
);
175 msgs
[msgp
++] = draft
;
180 die("only one draft folder at a time!");
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
184 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
204 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
205 die("missing argument to %s", argp
[-2]);
266 if (!(cp
= *argp
++) || *cp
== '-')
267 die("missing argument to %s", argp
[-2]);
274 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
275 die("missing argument to %s", argp
[-2]);
277 die("not built with OAuth support");
282 if (!(saslmech
= *argp
) || *saslmech
== '-')
283 die("missing argument to %s", argp
[-1]);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 die("missing argument to %s", argp
[-2]);
307 * check for "Aliasfile:" profile entry
309 if ((cp
= context_find ("Aliasfile"))) {
312 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
313 vec
[vecp
++] = "-alias";
318 if (dfolder
== NULL
) {
320 msgs
[msgp
++] = mh_xstrdup(m_draft(NULL
, NULL
, 1, &isdf
));
321 if (stat (msgs
[0], &st
) == NOTOK
)
322 adios (msgs
[0], "unable to stat draft file");
323 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
324 for (status
= LISTDSW
; status
!= YESW
;) {
325 if (!(argp
= read_switch_multiword (cp
, anyl
)))
327 switch (status
= smatch (*argp
, anyl
)) {
333 showfile (++argp
, msgs
[0]);
341 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
342 msgs
[msgnum
] = mh_xstrdup(m_maildir(msgs
[msgnum
]));
345 if (!context_find ("path"))
346 free (path ("./", TFOLDER
));
349 msgs
[msgp
++] = "cur";
350 maildir
= m_maildir (dfolder
);
352 if (chdir (maildir
) == NOTOK
)
353 adios (maildir
, "unable to change directory to");
355 /* read folder and create message structure */
356 if (!(mp
= folder_read (dfolder
, 1)))
357 die("unable to read folder %s", dfolder
);
359 /* check for empty folder */
361 die("no messages in %s", dfolder
);
363 /* parse all the message ranges/sequences and set SELECTED */
364 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
365 if (!m_convert (mp
, msgs
[msgnum
]))
367 seq_setprev (mp
); /* set the previous-sequence */
369 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
370 if (is_selected (mp
, msgnum
)) {
371 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
372 unset_exists (mp
, msgnum
);
376 mp
->msgflags
|= SEQMOD
;
384 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
385 if ((cp
= context_find ("signature")) && *cp
)
386 setenv("SIGNATURE", cp
, 1);
388 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
389 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
390 adios (msgs
[msgnum
], "unable to stat draft file");
392 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
394 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
396 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
397 altmsg
= NULL
; /* used by dist interface - see below */
399 if ((cp
= getenv ("mhdist"))
403 vec
[vecp
++] = "-dist";
404 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
405 die("unable to create temporary file");
407 distfile
= mh_xstrdup(cp
);
408 (void) m_unlink(distfile
);
409 if (link (altmsg
, distfile
) == NOTOK
) {
410 /* Cygwin with FAT32 filesystem produces EPERM. */
411 if (errno
!= EXDEV
&& errno
!= EPERM
413 && errno
!= EISREMOTE
414 #endif /* EISREMOTE */
416 adios (distfile
, "unable to link %s to", altmsg
);
418 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
419 die("unable to create temporary file in %s",
422 distfile
= mh_xstrdup(cp
);
427 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
428 adios (altmsg
, "unable to open");
430 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
431 adios (distfile
, "unable to write");
432 cpydata (in
, out
, altmsg
, distfile
);
442 if (auth_svc
== NULL
) {
443 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
444 die("must specify -authservice with -saslmech xoauth2");
448 die("must specify -user with -saslmech xoauth2");
452 NMH_UNUSED(auth_svc
);
454 NMH_UNUSED(saslmech
);
455 #endif /* OAUTH_SUPPORT */
457 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
468 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
469 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
481 context_save (); /* save the context file */