]>
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("attach", -6, ATTACHSW) \
70 X("noattach", -8, NOATTACHSW) \
71 X("attachformat", 7, ATTACHFORMATSW) \
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("sendmail program", 0, MTSSM) \
77 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
78 X("messageid localname|random", 2, MESSAGEIDSW) \
80 #define X(sw, minchars, id) id,
81 DEFINE_SWITCH_ENUM(SEND
);
84 #define X(sw, minchars, id) { sw, minchars, id },
85 DEFINE_SWITCH_ARRAY(SEND
, switches
);
88 #define USE_SWITCHES \
91 X("list", 0, LISTDSW) \
93 #define X(sw, minchars, id) id,
94 DEFINE_SWITCH_ENUM(USE
);
97 #define X(sw, minchars, id) { sw, minchars, id },
98 DEFINE_SWITCH_ARRAY(USE
, anyl
);
101 extern int debugsw
; /* from sendsbr.c */
109 extern char *altmsg
; /* .. */
110 extern char *annotext
;
111 extern char *distfile
;
115 main (int argc
, char **argv
)
117 int msgp
= 0, distsw
= 0, vecp
;
118 int isdf
= 0, mime
= 0;
120 char *cp
, *dfolder
= NULL
, *maildir
= NULL
;
121 char buf
[BUFSIZ
], **ap
, **argp
, **arguments
, *program
;
122 char *msgs
[MAXARGS
], **vec
;
123 const char *user
= NULL
, *saslmech
= NULL
;
127 char *auth_svc
= NULL
;
129 if (nmh_init(argv
[0], 1)) { return 1; }
131 arguments
= getarguments (invo_name
, argc
, argv
, 1);
134 vec
= argsplit(postproc
, &program
, &vecp
);
136 vec
[vecp
++] = "-library";
137 vec
[vecp
++] = getcpy (m_maildir (""));
139 if ((cp
= context_find ("fileproc"))) {
140 vec
[vecp
++] = "-fileproc";
144 if ((cp
= context_find ("mhlproc"))) {
145 vec
[vecp
++] = "-mhlproc";
149 if ((cp
= context_find ("credentials"))) {
150 /* post doesn't read context so need to pass credentials. */
151 vec
[vecp
++] = "-credentials";
155 while ((cp
= *argp
++)) {
157 switch (smatch (++cp
, switches
)) {
159 ambigsw (cp
, switches
);
162 adios (NULL
, "-%s unknown\n", cp
);
165 snprintf (buf
, sizeof(buf
), "%s [file] [switches]", invo_name
);
166 print_help (buf
, switches
, 1);
169 print_version(invo_name
);
173 msgs
[msgp
++] = draft
;
178 adios (NULL
, "only one draft folder at a time!");
179 if (!(cp
= *argp
++) || *cp
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
181 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
182 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
202 if (!(cp
= *argp
++) || sscanf (cp
, "%d", &splitsw
) != 1)
203 adios (NULL
, "missing argument to %s", argp
[-2]);
244 debugsw
++; /* fall */
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
[-2]);
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
300 advise(NULL
, "The -attach switch is deprecated");
303 advise(NULL
, "The -noattach switch is deprecated");
307 advise(NULL
, "The -attachformat switch is deprecated");
316 * check for "Aliasfile:" profile entry
318 if ((cp
= context_find ("Aliasfile"))) {
321 for (ap
= brkstring(dp
= getcpy(cp
), " ", "\n"); ap
&& *ap
; ap
++) {
322 vec
[vecp
++] = "-alias";
327 if (dfolder
== NULL
) {
329 msgs
[msgp
++] = getcpy (m_draft (NULL
, NULL
, 1, &isdf
));
330 if (stat (msgs
[0], &st
) == NOTOK
)
331 adios (msgs
[0], "unable to stat draft file");
332 cp
= concat ("Use \"", msgs
[0], "\"? ", NULL
);
333 for (status
= LISTDSW
; status
!= YESW
;) {
334 if (!(argp
= read_switch_multiword (cp
, anyl
)))
336 switch (status
= smatch (*argp
, anyl
)) {
342 showfile (++argp
, msgs
[0]);
345 advise (NULL
, "say what?");
350 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
351 msgs
[msgnum
] = getcpy (m_maildir (msgs
[msgnum
]));
354 if (!context_find ("path"))
355 free (path ("./", TFOLDER
));
358 msgs
[msgp
++] = "cur";
359 maildir
= m_maildir (dfolder
);
361 if (chdir (maildir
) == NOTOK
)
362 adios (maildir
, "unable to change directory to");
364 /* read folder and create message structure */
365 if (!(mp
= folder_read (dfolder
, 1)))
366 adios (NULL
, "unable to read folder %s", dfolder
);
368 /* check for empty folder */
370 adios (NULL
, "no messages in %s", dfolder
);
372 /* parse all the message ranges/sequences and set SELECTED */
373 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
374 if (!m_convert (mp
, msgs
[msgnum
]))
376 seq_setprev (mp
); /* set the previous-sequence */
378 for (msgp
= 0, msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
379 if (is_selected (mp
, msgnum
)) {
380 msgs
[msgp
++] = getcpy (m_name (msgnum
));
381 unset_exists (mp
, msgnum
);
385 mp
->msgflags
|= SEQMOD
;
393 if ((cp
= getenv ("SIGNATURE")) == NULL
|| *cp
== 0)
394 if ((cp
= context_find ("signature")) && *cp
)
395 m_putenv ("SIGNATURE", cp
);
397 for (msgnum
= 0; msgnum
< msgp
; msgnum
++)
398 if (stat (msgs
[msgnum
], &st
) == NOTOK
)
399 adios (msgs
[msgnum
], "unable to stat draft file");
401 if ((annotext
= getenv ("mhannotate")) == NULL
|| *annotext
== 0)
403 if (annotext
&& ((cp
= getenv ("mhinplace")) != NULL
&& *cp
!= 0))
405 if ((altmsg
= getenv ("mhaltmsg")) == NULL
|| *altmsg
== 0)
406 altmsg
= NULL
; /* used by dist interface - see below */
408 if ((cp
= getenv ("mhdist"))
410 && (distsw
= atoi (cp
))
412 vec
[vecp
++] = "-dist";
413 if ((cp
= m_mktemp2(altmsg
, invo_name
, NULL
, NULL
)) == NULL
) {
414 adios(NULL
, "unable to create temporary file in %s",
417 distfile
= getcpy (cp
);
418 (void) m_unlink(distfile
);
419 if (link (altmsg
, distfile
) == NOTOK
) {
420 /* Cygwin with FAT32 filesystem produces EPERM. */
421 if (errno
!= EXDEV
&& errno
!= EPERM
423 && errno
!= EISREMOTE
424 #endif /* EISREMOTE */
426 adios (distfile
, "unable to link %s to", altmsg
);
428 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
429 adios(NULL
, "unable to create temporary file in %s",
432 distfile
= getcpy (cp
);
437 if ((in
= open (altmsg
, O_RDONLY
)) == NOTOK
)
438 adios (altmsg
, "unable to open");
440 if ((out
= creat (distfile
, (int) st
.st_mode
& 0777)) == NOTOK
)
441 adios (distfile
, "unable to write");
442 cpydata (in
, out
, altmsg
, distfile
);
452 if (auth_svc
== NULL
) {
453 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
454 adios (NULL
, "must specify -authservice with -saslmech xoauth2");
458 adios (NULL
, "must specify -user with -saslmech xoauth2");
462 NMH_UNUSED(auth_svc
);
464 NMH_UNUSED(saslmech
);
465 #endif /* OAUTH_SUPPORT */
467 if (altmsg
== NULL
|| stat (altmsg
, &st
) == NOTOK
) {
478 for (msgnum
= 0; msgnum
< msgp
; msgnum
++) {
479 switch (sendsbr (vec
, vecp
, program
, msgs
[msgnum
], &st
, 1, auth_svc
)) {
489 context_save (); /* save the context file */