]>
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.
17 # define SASLminc(a) (a)
18 #else /* CYRUS_SASL */
19 # define SASLminc(a) 0
20 #endif /* CYRUS_SASL */
23 # define TLSminc(a) (a)
24 #else /* TLS_SUPPORT */
26 #endif /* TLS_SUPPORT */
28 #define SEND_SWITCHES \
29 X("alias aliasfile", 0, ALIASW) \
30 X("debug", -5, DEBUGSW) \
31 X("draft", 0, DRAFTSW) \
32 X("draftfolder +folder", 6, DFOLDSW) \
33 X("draftmessage msg", 6, DMSGSW) \
34 X("nodraftfolder", 0, NDFLDSW) \
35 X("filter filterfile", 0, FILTSW) \
36 X("nofilter", 0, NFILTSW) \
37 X("format", 0, FRMTSW) \
38 X("noformat", 0, NFRMTSW) \
39 X("forward", 0, FORWSW) \
40 X("noforward", 0, NFORWSW) \
41 X("mime", 0, MIMESW) \
42 X("nomime", 0, NMIMESW) \
43 X("msgid", 0, MSGDSW) \
44 X("nomsgid", 0, NMSGDSW) \
45 X("push", 0, PUSHSW) \
46 X("nopush", 0, NPUSHSW) \
47 X("split seconds", 0, SPLITSW) \
48 X("unique", -6, UNIQSW) \
49 X("nounique", -8, NUNIQSW) \
50 X("verbose", 0, VERBSW) \
51 X("noverbose", 0, NVERBSW) \
52 X("watch", 0, WATCSW) \
53 X("nowatch", 0, NWATCSW) \
54 X("width columns", 0, WIDTHSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
57 X("dashstuffing", -12, BITSTUFFSW) \
58 X("nodashstuffing", -14, NBITSTUFFSW) \
59 X("client host", -6, CLIESW) \
60 X("server host", 6, SERVSW) \
61 X("snoop", 5, SNOOPSW) \
62 X("sasl", SASLminc(4), SASLSW) \
63 X("nosasl", SASLminc(6), NOSASLSW) \
64 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
65 X("authservice", SASLminc(0), AUTHSERVICESW) \
66 X("user username", SASLminc(-4), USERSW) \
67 X("port server-port-name/number", 4, PORTSW) \
68 X("tls", TLSminc(-3), TLSSW) \
69 X("initialtls", TLSminc(-10), INITTLSSW) \
70 X("notls", TLSminc(-5), NTLSSW) \
71 X("certverify", TLSminc(-10), CERTVERSW) \
72 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
73 X("sendmail program", 0, MTSSM) \
74 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
75 X("messageid localname|random", 2, MESSAGEIDSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(SEND
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(SEND
, switches
);
85 #define USE_SWITCHES \
88 X("list", 0, LISTDSW) \
90 #define X(sw, minchars, id) id,
91 DEFINE_SWITCH_ENUM(USE
);
94 #define X(sw, minchars, id) { sw, minchars, id },
95 DEFINE_SWITCH_ARRAY(USE
, anyl
);
98 extern int debugsw
; /* from sendsbr.c */
106 extern char *altmsg
; /* .. */
107 extern char *annotext
;
108 extern char *distfile
;
112 main (int argc
, char **argv
)
114 int msgp
= 0, distsw
= 0, vecp
;
115 int isdf
= 0, mime
= 0;
117 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
118 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
119 char *msgs
[MAXARGS
], **vec
;
120 const char *user
= NULL
, *saslmech
= NULL
;
124 char *auth_svc
= NULL
;
126 if (nmh_init(argv
[0], 1)) { return 1; }
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
131 vec
= argsplit(postproc
, &program
, &vecp
);
133 vec
[vecp
++] = "-library";
134 vec
[vecp
++] = getcpy (m_maildir (""));
136 if ((cp
= context_find ("fileproc"))) {
137 vec
[vecp
++] = "-fileproc";
141 if ((cp
= context_find ("mhlproc"))) {
142 vec
[vecp
++] = "-mhlproc";
146 if ((cp
= context_find ("credentials"))) {
147 /* post doesn't read context so need to pass credentials. */
148 vec
[vecp
++] = "-credentials";
152 while ((cp
= *argp
++)) {
154 switch (smatch (++cp
, switches
)) {
156 ambigsw (cp
, switches
);
159 adios (NULL
, "-%s unknown\n", cp
);
162 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
163 print_help (buf
, switches
, 1);
166 print_version(invo_name
);
170 msgs
[msgp
++] = draft
;
175 adios (NULL
, "only one draft folder at a time!");
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
178 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
179 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
182 if (!(cp
= *argp
++) || *cp
== '-')
183 adios (NULL
, "missing argument to %s", argp
[-2]);
199 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
200 adios (NULL
, "missing argument to %s", argp
[-2]);
264 if (!(cp
= *argp
++) || *cp
== '-')
265 adios (NULL
, "missing argument to %s", argp
[-2]);
272 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
273 adios (NULL
, "missing argument to %s", argp
[-2]);
275 adios (NULL
, "not built with OAuth support");
280 if (!(saslmech
= *argp
) || *saslmech
== '-')
281 adios (NULL
, "missing argument to %s", argp
[-1]);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
305 * check for "Aliasfile:" profile entry
307 if ((cp
= context_find ("Aliasfile"))) {
310 for (ap
= brkstring(dp
= mh_xstrdup(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
311 vec
[vecp
++] = "-alias";
316 if (dfolder
== NULL
) {
318 msgs
[msgp
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
319 if (stat (msgs
[0], &st
) == NOTOK
)
320 adios (msgs
[0], "unable to stat draft file");
321 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
322 for (status
= LISTDSW
; status
!= YESW
;) {
323 if (!(argp
= read_switch_multiword (cp
, anyl
)))
325 switch (status
= smatch (*argp
, anyl
)) {
331 showfile (++argp
, msgs
[0]);
339 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
340 msgs
[msgnum
] = getcpy (m_maildir (msgs
[msgnum
]));
343 if (!context_find ("path"))
344 free (path ("./", TFOLDER
));
347 msgs
[msgp
++] = "cur";
348 maildir
= m_maildir (dfolder
);
350 if (chdir (maildir
) == NOTOK
)
351 adios (maildir
, "unable to change directory to");
353 /* read folder and create message structure */
354 if (!(mp
= folder_read (dfolder
, 1)))
355 adios (NULL
, "unable to read folder %s", dfolder
);
357 /* check for empty folder */
359 adios (NULL
, "no messages in %s", dfolder
);
361 /* parse all the message ranges/sequences and set SELECTED */
362 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
363 if (!m_convert (mp
, msgs
[msgnum
]))
365 seq_setprev (mp
); /* set the previous-sequence */
367 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
368 if (is_selected (mp
, msgnum
)) {
369 msgs
[msgp
++] = mh_xstrdup(m_name (msgnum
));
370 unset_exists (mp
, msgnum
);
374 mp
->msgflags
|= SEQMOD
;
382 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
383 if ((cp
= context_find ("signature")) && *cp
)
384 m_putenv ("SIGNATURE", cp
);
386 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
387 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
388 adios (msgs
[msgnum
], "unable to stat draft file");
390 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
392 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
394 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
395 altmsg
= NULL
; /* used by dist interface - see below */
397 if ((cp
= getenv ("mhdist"))
399 && (distsw
= atoi (cp
))
401 vec
[vecp
++] = "-dist";
402 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
403 adios(NULL
, "unable to create temporary file in %s",
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 adios(NULL
, "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 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
447 adios (NULL
, "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 */