Add enum n_fopen_state, rename Zopen()->n_fopen_any(), etc..
[s-mailx.git] / cmd-resend.c
blobe5eb43e8e56a854e2d644b863e5a6026f3e788e1
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ All sorts of `reply', `resend', `forward', and similar user commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE cmd_resend
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* Modify subject we reply to to begin with Re: if it does not already */
43 static char *a_crese_reedit(char const *subj);
45 static void make_ref_and_cs(struct message *mp, struct header *head);
47 /* `reply' and `Lreply' workhorse */
48 static int _list_reply(int *msgvec, enum header_flags hf);
50 /* Get PTF to implementation of command c (i.e., take care for *flipr*) */
51 static int (* _reply_or_Reply(char c))(int *, bool_t);
53 /* Reply to a single message. Extract each name from the message header and
54 * send them off to mail1() */
55 static int _reply(int *msgvec, bool_t recipient_record);
57 /* Reply to a series of messages by simply mailing to the senders and not
58 * messing around with the To: and Cc: lists as in normal reply */
59 static int _Reply(int *msgvec, bool_t recipient_record);
61 /* Forward a message to a new recipient, in the sense of RFC 2822 */
62 static int _fwd(char *str, int recipient_record);
64 /* Modify the subject we are replying to to begin with Fwd: */
65 static char * __fwdedit(char *subj);
67 /* Do the real work of resending */
68 static int _resend1(void *v, bool_t add_resent);
70 static char *
71 a_crese_reedit(char const *subj){
72 char *newsubj;
73 NYD_ENTER;
75 newsubj = NULL;
77 if(subj != NULL && *subj != '\0'){
78 struct str in, out;
79 size_t i;
80 char const *cp;
82 in.l = strlen(in.s = n_UNCONST(subj));
83 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
85 i = strlen(cp = subject_re_trim(out.s)) +1;
86 /* RFC mandates english "Re: " */
87 newsubj = salloc(sizeof("Re: ") -1 + i);
88 memcpy(newsubj, "Re: ", sizeof("Re: ") -1);
89 memcpy(&newsubj[sizeof("Re: ") -1], cp, i);
91 free(out.s);
93 NYD_LEAVE;
94 return newsubj;
97 static void
98 make_ref_and_cs(struct message *mp, struct header *head) /* TODO rewrite FAST */
100 char *oldref, *oldmsgid, *newref, *cp;
101 size_t oldreflen = 0, oldmsgidlen = 0, reflen;
102 unsigned i;
103 struct name *n;
104 NYD_ENTER;
106 oldref = hfield1("references", mp);
107 oldmsgid = hfield1("message-id", mp);
108 if (oldmsgid == NULL || *oldmsgid == '\0') {
109 head->h_ref = NULL;
110 goto jleave;
113 reflen = 1;
114 if (oldref) {
115 oldreflen = strlen(oldref);
116 reflen += oldreflen + 2;
118 if (oldmsgid) {
119 oldmsgidlen = strlen(oldmsgid);
120 reflen += oldmsgidlen;
123 newref = smalloc(reflen);
124 if (oldref != NULL) {
125 memcpy(newref, oldref, oldreflen +1);
126 if (oldmsgid != NULL) {
127 newref[oldreflen++] = ',';
128 newref[oldreflen++] = ' ';
129 memcpy(newref + oldreflen, oldmsgid, oldmsgidlen +1);
131 } else if (oldmsgid)
132 memcpy(newref, oldmsgid, oldmsgidlen +1);
133 n = extract(newref, GREF);
134 free(newref);
136 /* Limit number of references TODO better on parser side */
137 while (n->n_flink != NULL)
138 n = n->n_flink;
139 for (i = 1; i <= REFERENCES_MAX; ++i) {
140 if (n->n_blink != NULL)
141 n = n->n_blink;
142 else
143 break;
145 n->n_blink = NULL;
146 head->h_ref = n;
147 if (ok_blook(reply_in_same_charset) &&
148 (cp = hfield1("content-type", mp)) != NULL){
149 if((head->h_charset = cp = mime_param_get("charset", cp)) != NULL){
150 char *cpo, c;
152 for(cpo = cp; (c = *cpo) != '\0'; ++cpo)
153 *cpo = lowerconv(c);
155 head->h_charset = n_charsetalias_expand(cp);
158 jleave:
159 NYD_LEAVE;
162 static int
163 _list_reply(int *msgvec, enum header_flags hf)
165 struct header head;
166 struct message *mp;
167 char const *reply_to, *rcv, *cp;
168 enum gfield gf;
169 struct name *rt, *mft, *np;
170 int *save_msgvec;
171 NYD_ENTER;
173 /* TODO Since we may recur and do stuff with message lists we need to save
174 * TODO away the argument vector as long as that isn't done by machinery */
176 size_t i;
177 for (i = 0; msgvec[i] != 0; ++i)
179 ++i;
180 save_msgvec = ac_alloc(sizeof(*save_msgvec) * i);
181 while (i-- > 0)
182 save_msgvec[i] = msgvec[i];
183 msgvec = save_msgvec;
186 jnext_msg:
187 mp = message + *msgvec - 1;
188 touch(mp);
189 setdot(mp);
191 memset(&head, 0, sizeof head);
192 head.h_flags = hf;
194 head.h_subject = a_crese_reedit(hfield1("subject", mp));
195 gf = ok_blook(fullnames) ? GFULL : GSKIN;
196 rt = mft = NULL;
198 rcv = NULL;
199 if ((reply_to = hfield1("reply-to", mp)) != NULL &&
200 (cp = ok_vlook(reply_to_honour)) != NULL &&
201 (rt = checkaddrs(lextract(reply_to, GTO | gf), EACM_STRICT, NULL)
202 ) != NULL) {
203 char const *tr = _("Reply-To %s%s");
204 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
205 char *sp = salloc(l);
207 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : n_empty));
208 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0)
209 rcv = reply_to;
212 if (rcv == NULL && (rcv = hfield1("from", mp)) == NULL)
213 rcv = nameof(mp, 1);
215 /* Cc: */
216 np = NULL;
217 if (ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
218 np = lextract(cp, GCC | gf);
219 if ((cp = hfield1("cc", mp)) != NULL)
220 np = cat(np, lextract(cp, GCC | gf));
221 if (np != NULL)
222 head.h_cc = delete_alternates(np);
224 /* To: */
225 np = NULL;
226 if (rcv != NULL)
227 np = (rcv == reply_to) ? namelist_dup(rt, GTO | gf)
228 : lextract(rcv, GTO | gf);
229 if (!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
230 np = cat(np, lextract(cp, GTO | gf));
231 /* Delete my name from reply list, and with it, all my alternate names */
232 np = delete_alternates(np);
233 if (count(np) == 0)
234 np = lextract(rcv, GTO | gf);
235 head.h_to = np;
237 /* The user may have send this to himself, don't ignore that */
238 namelist_vaporise_head(&head, EACM_NORMAL, FAL0, NULL);
239 if (head.h_to == NULL)
240 head.h_to = np;
242 /* Mail-Followup-To: */
243 mft = NULL;
244 if (ok_vlook(followup_to_honour) != NULL &&
245 (cp = hfield1("mail-followup-to", mp)) != NULL &&
246 (mft = np = checkaddrs(lextract(cp, GTO | gf), EACM_STRICT, NULL)
247 ) != NULL) {
248 char const *tr = _("Followup-To %s%s");
249 size_t l = strlen(tr) + strlen(np->n_name) + 3 +1;
250 char *sp = salloc(l);
252 snprintf(sp, l, tr, np->n_name, (np->n_flink != NULL ? "..." : n_empty));
253 if (quadify(ok_vlook(followup_to_honour), UIZ_MAX, sp, TRU1) > FAL0) {
254 head.h_cc = NULL;
255 head.h_to = np;
256 head.h_mft =
257 mft = namelist_vaporise_head(&head, EACM_STRICT, FAL0, NULL);
258 } else
259 mft = NULL;
262 /* Special massage for list (follow-up) messages */
263 if (mft != NULL || (hf & HF_LIST_REPLY) || ok_blook(followup_to)) {
264 /* Learn about a possibly sending mailing list; use do for break; */
265 if ((cp = hfield1("list-post", mp)) != NULL) do {
266 struct name *x;
268 if ((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
269 (cp = url_mailto_to_address(x->n_name)) == NULL ||
270 /* XXX terribly wasteful to create a new name, and can't we find
271 * XXX a way to mitigate that?? */
272 is_addr_invalid(x = nalloc(cp, GEXTRA | GSKIN), EACM_STRICT)) {
273 if(n_poption & n_PO_D_V)
274 n_err(_("Message contains invalid List-Post: header\n"));
275 cp = NULL;
276 break;
278 cp = x->n_name;
280 /* A special case has been seen on e.g. ietf-announce@ietf.org:
281 * these usually post to multiple groups, with ietf-announce@
282 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
283 * -announce@ is only used for announcements, say).
284 * So our desire is to honour this request and actively overwrite
285 * List-Post: for our purpose; but only if its a single address.
286 * However, to avoid ambiguities with users that place themselve in
287 * Reply-To: and mailing lists which don't overwrite this (or only
288 * extend this, shall such exist), only do so if reply_to exists of
289 * a single address which points to the same domain as List-Post: */
290 if (reply_to != NULL && rt->n_flink == NULL &&
291 name_is_same_domain(x, rt))
292 cp = rt->n_name; /* rt is EACM_STRICT tested */
294 /* "Automatically `mlist'" the List-Post: address temporarily */
295 if (is_mlist(cp, FAL0) == MLIST_OTHER)
296 head.h_list_post = cp;
297 else
298 cp = NULL;
299 } while (0);
301 /* In case of list replies we actively sort out any non-list recipient,
302 * but _only_ if we did not honour a MFT:, assuming that members of MFT
303 * were there for a reason; cp is still List-Post:/eqivalent */
304 if ((hf & HF_LIST_REPLY) && mft == NULL) {
305 struct name *nhp = head.h_to;
306 head.h_to = NULL;
307 j_lt_redo:
308 while (nhp != NULL) {
309 np = nhp;
310 nhp = nhp->n_flink;
312 if ((cp != NULL && !asccasecmp(cp, np->n_name)) ||
313 is_mlist(np->n_name, FAL0) != MLIST_OTHER) {
314 np->n_type = (np->n_type & ~GMASK) | GTO;
315 np->n_flink = head.h_to;
316 head.h_to = np;
319 if ((nhp = head.h_cc) != NULL) {
320 head.h_cc = NULL;
321 goto j_lt_redo;
326 make_ref_and_cs(mp, &head);
328 if (ok_blook(quote_as_attachment)) {
329 head.h_attach = csalloc(1, sizeof *head.h_attach);
330 head.h_attach->a_msgno = *msgvec;
331 head.h_attach->a_content_description = _("Original message content");
334 if (mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), 0) != OKAY) {
335 msgvec = NULL;
336 goto jleave;
338 if(ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
339 mp->m_flag |= MANSWER | MANSWERED;
341 if (*++msgvec != 0) {
342 /* TODO message (error) ring.., less sleep */
343 if(n_psonce & n_PSO_INTERACTIVE){
344 fprintf(n_stdout,
345 _("Waiting a second before proceeding to the next message..\n"));
346 fflush(n_stdout);
347 n_msleep(1000, FAL0);
349 goto jnext_msg;
352 jleave:
353 ac_free(save_msgvec);
354 NYD_LEAVE;
355 return (msgvec == NULL);
358 static int
359 (*_reply_or_Reply(char c))(int *, bool_t)
361 int (*rv)(int*, bool_t);
362 NYD_ENTER;
364 rv = (ok_blook(flipr) ^ (c == 'R')) ? &_Reply : &_reply;
365 NYD_LEAVE;
366 return rv;
369 static int
370 _reply(int *msgvec, bool_t recipient_record)
372 int rv;
373 NYD_ENTER;
375 rv = _list_reply(msgvec, recipient_record ? HF_RECIPIENT_RECORD : HF_NONE);
376 NYD_LEAVE;
377 return rv;
380 static int
381 _Reply(int *msgvec, bool_t recipient_record)
383 struct header head;
384 struct message *mp;
385 int *ap;
386 char *cp;
387 enum gfield gf;
388 NYD_ENTER;
390 memset(&head, 0, sizeof head);
391 gf = ok_blook(fullnames) ? GFULL : GSKIN;
393 for (ap = msgvec; *ap != 0; ++ap) {
394 char const *rp;
395 struct name *rt;
397 mp = message + *ap - 1;
398 touch(mp);
399 setdot(mp);
401 if ((rp = hfield1("reply-to", mp)) != NULL &&
402 (cp = ok_vlook(reply_to_honour)) != NULL &&
403 (rt = checkaddrs(lextract(rp, GTO | gf), EACM_STRICT, NULL)
404 ) != NULL) {
405 char const *tr = _("Reply-To %s%s");
406 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
407 char *sp = salloc(l);
409 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..."
410 : n_empty));
411 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0) {
412 head.h_to = cat(head.h_to, rt);
413 continue;
417 if ((cp = hfield1("from", mp)) == NULL)
418 cp = nameof(mp, 2);
419 head.h_to = cat(head.h_to, lextract(cp, GTO | gf));
421 if (head.h_to == NULL)
422 goto jleave;
424 mp = message + msgvec[0] - 1;
425 head.h_subject = hfield1("subject", mp);
426 head.h_subject = a_crese_reedit(head.h_subject);
427 make_ref_and_cs(mp, &head);
429 if (ok_blook(quote_as_attachment)) {
430 head.h_attach = csalloc(1, sizeof *head.h_attach);
431 head.h_attach->a_msgno = *msgvec;
432 head.h_attach->a_content_description = _("Original message content");
435 if (mail1(&head, 1, mp, NULL, recipient_record, 0) != OKAY) {
436 msgvec = NULL;
437 goto jleave;
440 if(ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
441 mp->m_flag |= MANSWER | MANSWERED;
442 jleave:
443 NYD_LEAVE;
444 return (msgvec == NULL);
447 static int
448 _fwd(char *str, int recipient_record)
450 struct header head;
451 int *msgvec, rv = 1;
452 char *recipient;
453 struct message *mp;
454 bool_t f, forward_as_attachment;
455 NYD_ENTER;
457 if ((recipient = laststring(str, &f, TRU1)) == NULL) {
458 fputs(_("No recipient specified.\n"), n_stdout);
459 goto jleave;
462 forward_as_attachment = ok_blook(forward_as_attachment);
463 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
465 if (!f) {
466 *msgvec = first(0, MMNORM);
467 if (*msgvec == 0) {
468 if (n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) {
469 rv = 0;
470 goto jleave;
472 fprintf(n_stdout, _("No messages to forward.\n"));
473 goto jleave;
475 msgvec[1] = 0;
476 } else if (getmsglist(str, msgvec, 0) < 0)
477 goto jleave;
479 if (*msgvec == 0) {
480 if (n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) {
481 rv = 0;
482 goto jleave;
484 fprintf(n_stdout, _("No applicable messages.\n"));
485 goto jleave;
487 if (msgvec[1] != 0) {
488 fprintf(n_stdout, _("Cannot forward multiple messages at once\n"));
489 goto jleave;
492 memset(&head, 0, sizeof head);
493 if ((head.h_to = lextract(recipient,
494 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)))) == NULL)
495 goto jleave;
497 mp = message + *msgvec - 1;
499 if (forward_as_attachment) {
500 head.h_attach = csalloc(1, sizeof *head.h_attach);
501 head.h_attach->a_msgno = *msgvec;
502 head.h_attach->a_content_description = _("Forwarded message");
503 } else {
504 touch(mp);
505 setdot(mp);
507 head.h_subject = hfield1("subject", mp);
508 head.h_subject = __fwdedit(head.h_subject);
509 rv = (mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL,
510 recipient_record, 1) != OKAY); /* reverse! */
511 jleave:
512 NYD_LEAVE;
513 return rv;
516 static char *
517 __fwdedit(char *subj)
519 struct str in, out;
520 char *newsubj = NULL;
521 NYD_ENTER;
523 if (subj == NULL || *subj == '\0')
524 goto jleave;
526 in.s = subj;
527 in.l = strlen(subj);
528 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
530 newsubj = salloc(out.l + 6);
531 memcpy(newsubj, "Fwd: ", 5); /* XXX localizable */
532 memcpy(newsubj + 5, out.s, out.l +1);
533 free(out.s);
534 jleave:
535 NYD_LEAVE;
536 return newsubj;
539 static int
540 _resend1(void *v, bool_t add_resent)
542 char *name, *str;
543 struct name *to, *sn;
544 int *ip, *msgvec;
545 bool_t f = TRU1;
546 NYD_ENTER;
548 str = v;
549 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
550 name = laststring(str, &f, TRU1);
551 if (name == NULL) {
552 fputs(_("No recipient specified.\n"), n_stdout);
553 goto jleave;
556 if (!f) {
557 *msgvec = first(0, MMNORM);
558 if (*msgvec == 0) {
559 if (n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) {
560 f = FAL0;
561 goto jleave;
563 fputs(_("No applicable messages.\n"), n_stdout);
564 goto jleave;
566 msgvec[1] = 0;
567 } else if (getmsglist(str, msgvec, 0) < 0)
568 goto jleave;
570 if (*msgvec == 0) {
571 if (n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) {
572 f = FAL0;
573 goto jleave;
575 fprintf(n_stdout, "No applicable messages.\n");
576 goto jleave;
579 sn = nalloc(name, GTO | GSKIN);
580 to = usermap(sn, FAL0);
581 f = TRU1;
582 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
583 ++ip)
584 if (resend_msg(message + *ip - 1, to, add_resent) != OKAY)
585 goto jleave;
586 f = FAL0;
587 jleave:
588 NYD_LEAVE;
589 return (f != FAL0);
592 FL int
593 c_reply(void *v)
595 int rv;
596 NYD_ENTER;
598 rv = (*_reply_or_Reply('r'))(v, FAL0);
599 NYD_LEAVE;
600 return rv;
603 FL int
604 c_replyall(void *v)
606 int rv;
607 NYD_ENTER;
609 rv = _reply(v, FAL0);
610 NYD_LEAVE;
611 return rv;
614 FL int
615 c_replysender(void *v)
617 int rv;
618 NYD_ENTER;
620 rv = _Reply(v, FAL0);
621 NYD_LEAVE;
622 return rv;
625 FL int
626 c_Reply(void *v)
628 int rv;
629 NYD_ENTER;
631 rv = (*_reply_or_Reply('R'))(v, FAL0);
632 NYD_LEAVE;
633 return rv;
636 FL int
637 c_Lreply(void *v)
639 int rv;
640 NYD_ENTER;
642 rv = _list_reply(v, HF_LIST_REPLY);
643 NYD_LEAVE;
644 return rv;
647 FL int
648 c_followup(void *v)
650 int rv;
651 NYD_ENTER;
653 rv = (*_reply_or_Reply('r'))(v, TRU1);
654 NYD_LEAVE;
655 return rv;
658 FL int
659 c_followupall(void *v)
661 int rv;
662 NYD_ENTER;
664 rv = _reply(v, TRU1);
665 NYD_LEAVE;
666 return rv;
669 FL int
670 c_followupsender(void *v)
672 int rv;
673 NYD_ENTER;
675 rv = _Reply(v, TRU1);
676 NYD_LEAVE;
677 return rv;
680 FL int
681 c_Followup(void *v)
683 int rv;
684 NYD_ENTER;
686 rv = (*_reply_or_Reply('R'))(v, TRU1);
687 NYD_LEAVE;
688 return rv;
691 FL int
692 c_forward(void *v)
694 int rv;
695 NYD_ENTER;
697 rv = _fwd(v, 0);
698 NYD_LEAVE;
699 return rv;
702 FL int
703 c_Forward(void *v)
705 int rv;
706 NYD_ENTER;
708 rv = _fwd(v, 1);
709 NYD_LEAVE;
710 return rv;
713 FL int
714 c_resend(void *v)
716 int rv;
717 NYD_ENTER;
719 rv = _resend1(v, TRU1);
720 NYD_LEAVE;
721 return rv;
724 FL int
725 c_Resend(void *v)
727 int rv;
728 NYD_ENTER;
730 rv = _resend1(v, FAL0);
731 NYD_LEAVE;
732 return rv;
735 /* s-it-mode */