]>
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.
14 #include "../sbr/m_maildir.h"
15 #include "../sbr/m_mktemp.h"
18 # define SASLminc(a) (a)
19 #else /* CYRUS_SASL */
20 # define SASLminc(a) 0
21 #endif /* CYRUS_SASL */
24 # define TLSminc(a) (a)
25 #else /* TLS_SUPPORT */
27 #endif /* TLS_SUPPORT */
29 #define SEND_SWITCHES \
30 X("alias aliasfile", 0, ALIASW) \
31 X("debug", -5, DEBUGSW) \
32 X("draft", 0, DRAFTSW) \
33 X("draftfolder +folder", 6, DFOLDSW) \
34 X("draftmessage msg", 6, DMSGSW) \
35 X("nodraftfolder", 0, NDFLDSW) \
36 X("filter filterfile", 0, FILTSW) \
37 X("nofilter", 0, NFILTSW) \
38 X("format", 0, FRMTSW) \
39 X("noformat", 0, NFRMTSW) \
40 X("forward", 0, FORWSW) \
41 X("noforward", 0, NFORWSW) \
42 X("mime", 0, MIMESW) \
43 X("nomime", 0, NMIMESW) \
44 X("msgid", 0, MSGDSW) \
45 X("nomsgid", 0, NMSGDSW) \
46 X("push", 0, PUSHSW) \
47 X("nopush", 0, NPUSHSW) \
48 X("split seconds", 0, SPLITSW) \
49 X("unique", -6, UNIQSW) \
50 X("nounique", -8, NUNIQSW) \
51 X("verbose", 0, VERBSW) \
52 X("noverbose", 0, NVERBSW) \
53 X("watch", 0, WATCSW) \
54 X("nowatch", 0, NWATCSW) \
55 X("width columns", 0, WIDTHSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
58 X("dashstuffing", -12, BITSTUFFSW) \
59 X("nodashstuffing", -14, NBITSTUFFSW) \
60 X("client host", -6, CLIESW) \
61 X("server host", 6, SERVSW) \
62 X("snoop", 5, SNOOPSW) \
63 X("sasl", SASLminc(4), SASLSW) \
64 X("nosasl", SASLminc(6), NOSASLSW) \
65 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
66 X("authservice", SASLminc(0), AUTHSERVICESW) \
67 X("user username", SASLminc(-4), USERSW) \
68 X("port server-port-name/number", 4, PORTSW) \
69 X("tls", TLSminc(-3), TLSSW) \
70 X("initialtls", TLSminc(-10), INITTLSSW) \
71 X("notls", TLSminc(-5), NTLSSW) \
72 X("certverify", TLSminc(-10), CERTVERSW) \
73 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
74 X("sendmail program", 0, MTSSM) \
75 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
76 X("messageid localname|random", 2, MESSAGEIDSW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(SEND
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(SEND
, switches
);
86 #define USE_SWITCHES \
89 X("list", 0, LISTDSW) \
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(USE
);
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(USE
, anyl
);
99 extern int debugsw
; /* from sendsbr.c */
107 extern char *altmsg
; /* .. */
108 extern char *annotext
;
109 extern char *distfile
;
113 main (int argc
, char **argv
)
115 int msgp
= 0, distsw
= 0, vecp
;
116 int isdf
= 0, mime
= 0;
118 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
119 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
120 char *msgs
[MAXARGS
], **vec
;
121 const char *user
= NULL
, *saslmech
= NULL
;
125 char *auth_svc
= NULL
;
127 if (nmh_init(argv
[0], 1)) { return 1; }
129 arguments
= getarguments (invo_name
, argc
, argv
, 1);
132 vec
= argsplit(postproc
, &program
, &vecp
);
134 vec
[vecp
++] = "-library";
135 vec
[vecp
++] = getcpy (m_maildir (""));
137 if ((cp
= context_find ("fileproc"))) {
138 vec
[vecp
++] = "-fileproc";
142 if ((cp
= context_find ("mhlproc"))) {
143 vec
[vecp
++] = "-mhlproc";
147 if ((cp
= context_find ("credentials"))) {
148 /* post doesn't read context so need to pass credentials. */
149 vec
[vecp
++] = "-credentials";
153 while ((cp
= *argp
++)) {
155 switch (smatch (++cp
, switches
)) {
157 ambigsw (cp
, switches
);
160 adios (NULL
, "-%s unknown\n", cp
);
163 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
164 print_help (buf
, switches
, 1);
167 print_version(invo_name
);
171 msgs
[msgp
++] = draft
;
176 adios (NULL
, "only one draft folder at a time!");
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
179 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
180 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
200 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
201 adios (NULL
, "missing argument to %s", argp
[-2]);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 adios (NULL
, "missing argument to %s", argp
[-2]);
273 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
274 adios (NULL
, "missing argument to %s", argp
[-2]);
276 adios (NULL
, "not built with OAuth support");
281 if (!(saslmech
= *argp
) || *saslmech
== '-')
282 adios (NULL
, "missing argument to %s", argp
[-1]);
295 if (!(cp
= *argp
++) || *cp
== '-')
296 adios (NULL
, "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
++] = getcpy (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
] = getcpy (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 adios (NULL
, "unable to read folder %s", dfolder
);
358 /* check for empty folder */
360 adios (NULL
, "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"))
400 && (distsw
= atoi (cp
))
402 vec
[vecp
++] = "-dist";
403 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
404 adios(NULL
, "unable to create temporary file in %s",
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 adios(NULL
, "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 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
448 adios (NULL
, "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 */