]>
diplodocus.org Git - nmh/blob - uip/send.c
3 * send.c -- send a composed message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
19 # define SASLminc(a) (a)
20 #else /* CYRUS_SASL */
21 # define SASLminc(a) 0
22 #endif /* CYRUS_SASL */
25 # define TLSminc(a) (a)
26 #else /* TLS_SUPPORT */
28 #endif /* TLS_SUPPORT */
30 #define SEND_SWITCHES \
31 X("alias aliasfile", 0, ALIASW) \
32 X("debug", -5, DEBUGSW) \
33 X("draft", 0, DRAFTSW) \
34 X("draftfolder +folder", 6, DFOLDSW) \
35 X("draftmessage msg", 6, DMSGSW) \
36 X("nodraftfolder", 0, NDFLDSW) \
37 X("filter filterfile", 0, FILTSW) \
38 X("nofilter", 0, NFILTSW) \
39 X("format", 0, FRMTSW) \
40 X("noformat", 0, NFRMTSW) \
41 X("forward", 0, FORWSW) \
42 X("noforward", 0, NFORWSW) \
43 X("mime", 0, MIMESW) \
44 X("nomime", 0, NMIMESW) \
45 X("msgid", 0, MSGDSW) \
46 X("nomsgid", 0, NMSGDSW) \
47 X("push", 0, PUSHSW) \
48 X("nopush", 0, NPUSHSW) \
49 X("split seconds", 0, SPLITSW) \
50 X("unique", -6, UNIQSW) \
51 X("nounique", -8, NUNIQSW) \
52 X("verbose", 0, VERBSW) \
53 X("noverbose", 0, NVERBSW) \
54 X("watch", 0, WATCSW) \
55 X("nowatch", 0, NWATCSW) \
56 X("width columns", 0, WIDTHSW) \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59 X("dashstuffing", -12, BITSTUFFSW) \
60 X("nodashstuffing", -14, NBITSTUFFSW) \
61 X("client host", -6, CLIESW) \
62 X("server host", 6, SERVSW) \
63 X("snoop", 5, SNOOPSW) \
64 X("sasl", SASLminc(4), SASLSW) \
65 X("nosasl", SASLminc(6), NOSASLSW) \
66 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
67 X("authservice", SASLminc(0), AUTHSERVICESW) \
68 X("user username", SASLminc(-4), USERSW) \
69 X("port server-port-name/number", 4, PORTSW) \
70 X("tls", TLSminc(-3), TLSSW) \
71 X("initialtls", TLSminc(-10), INITTLSSW) \
72 X("notls", TLSminc(-5), NTLSSW) \
73 X("certverify", TLSminc(-10), CERTVERSW) \
74 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
75 X("sendmail program", 0, MTSSM) \
76 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
77 X("messageid localname|random", 2, MESSAGEIDSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(SEND
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(SEND
, switches
);
87 #define USE_SWITCHES \
90 X("list", 0, LISTDSW) \
92 #define X(sw, minchars, id) id,
93 DEFINE_SWITCH_ENUM(USE
);
96 #define X(sw, minchars, id) { sw, minchars, id },
97 DEFINE_SWITCH_ARRAY(USE
, anyl
);
100 extern int debugsw
; /* from sendsbr.c */
108 extern char *altmsg
; /* .. */
109 extern char *annotext
;
110 extern char *distfile
;
114 main (int argc
, char **argv
)
116 int msgp
= 0, distsw
= 0, vecp
;
117 int isdf
= 0, mime
= 0;
119 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
120 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
121 char *msgs
[MAXARGS
], **vec
;
122 const char *user
= NULL
, *saslmech
= NULL
;
126 char *auth_svc
= NULL
;
128 if (nmh_init(argv
[0], 1)) { return 1; }
130 arguments
= getarguments (invo_name
, argc
, argv
, 1);
133 vec
= argsplit(postproc
, &program
, &vecp
);
135 vec
[vecp
++] = "-library";
136 vec
[vecp
++] = getcpy (m_maildir (""));
138 if ((cp
= context_find ("fileproc"))) {
139 vec
[vecp
++] = "-fileproc";
143 if ((cp
= context_find ("mhlproc"))) {
144 vec
[vecp
++] = "-mhlproc";
148 if ((cp
= context_find ("credentials"))) {
149 /* post doesn't read context so need to pass credentials. */
150 vec
[vecp
++] = "-credentials";
154 while ((cp
= *argp
++)) {
156 switch (smatch (++cp
, switches
)) {
158 ambigsw (cp
, switches
);
161 adios (NULL
, "-%s unknown\n", cp
);
164 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
165 print_help (buf
, switches
, 1);
168 print_version(invo_name
);
172 msgs
[msgp
++] = draft
;
177 adios (NULL
, "only one draft folder at a time!");
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
180 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
181 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
201 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
202 adios (NULL
, "missing argument to %s", argp
[-2]);
266 if (!(cp
= *argp
++) || *cp
== '-')
267 adios (NULL
, "missing argument to %s", argp
[-2]);
274 if (!(auth_svc
= *argp
++) || *auth_svc
== '-')
275 adios (NULL
, "missing argument to %s", argp
[-2]);
277 adios (NULL
, "not built with OAuth support");
282 if (!(saslmech
= *argp
) || *saslmech
== '-')
283 adios (NULL
, "missing argument to %s", argp
[-1]);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 adios (NULL
, "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
++] = getcpy (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
] = getcpy (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 adios (NULL
, "unable to read folder %s", dfolder
);
359 /* check for empty folder */
361 adios (NULL
, "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 m_putenv ("SIGNATURE", cp
);
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"))
401 && (distsw
= atoi (cp
))
403 vec
[vecp
++] = "-dist";
404 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
405 adios(NULL
, "unable to create temporary file in %s",
408 distfile
= mh_xstrdup(cp
);
409 (void) m_unlink(distfile
);
410 if (link (altmsg
, distfile
) == NOTOK
) {
411 /* Cygwin with FAT32 filesystem produces EPERM. */
412 if (errno
!= EXDEV
&& errno
!= EPERM
414 && errno
!= EISREMOTE
415 #endif /* EISREMOTE */
417 adios (distfile
, "unable to link %s to", altmsg
);
419 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
420 adios(NULL
, "unable to create temporary file in %s",
423 distfile
= mh_xstrdup(cp
);
428 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
429 adios (altmsg
, "unable to open");
431 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
432 adios (distfile
, "unable to write");
433 cpydata (in
, out
, altmsg
, distfile
);
443 if (auth_svc
== NULL
) {
444 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
445 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
449 adios (NULL
, "must specify -user with -saslmech xoauth2");
453 NMH_UNUSED(auth_svc
);
455 NMH_UNUSED(saslmech
);
456 #endif /* OAUTH_SUPPORT */
458 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
469 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
470 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
482 context_save (); /* save the context file */