FIX [ee4de6e4] and finally get asccaseprefix() right!
[s-mailx.git] / cmd_resend.c
blob25b1326727be1e9f81168d87c08b1dffec2c9bf2
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 * _reedit(char *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 _reedit(char *subj)
73 struct str in, out;
74 char *newsubj = NULL;
75 NYD_ENTER;
77 if (subj == NULL || *subj == '\0')
78 goto jleave;
80 in.s = subj;
81 in.l = strlen(subj);
82 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
84 if ((newsubj = subject_re_trim(out.s)) != out.s)
85 newsubj = savestr(out.s);
86 else {
87 /* RFC mandates english "Re: " */
88 newsubj = salloc(out.l + 4 +1);
89 sstpcpy(sstpcpy(newsubj, "Re: "), out.s);
92 free(out.s);
93 jleave:
94 NYD_LEAVE;
95 return newsubj;
98 static void
99 make_ref_and_cs(struct message *mp, struct header *head) /* TODO rewrite FAST */
101 char *oldref, *oldmsgid, *newref, *cp;
102 size_t oldreflen = 0, oldmsgidlen = 0, reflen;
103 unsigned i;
104 struct name *n;
105 NYD_ENTER;
107 oldref = hfield1("references", mp);
108 oldmsgid = hfield1("message-id", mp);
109 if (oldmsgid == NULL || *oldmsgid == '\0') {
110 head->h_ref = NULL;
111 goto jleave;
114 reflen = 1;
115 if (oldref) {
116 oldreflen = strlen(oldref);
117 reflen += oldreflen + 2;
119 if (oldmsgid) {
120 oldmsgidlen = strlen(oldmsgid);
121 reflen += oldmsgidlen;
124 newref = smalloc(reflen);
125 if (oldref != NULL) {
126 memcpy(newref, oldref, oldreflen +1);
127 if (oldmsgid != NULL) {
128 newref[oldreflen++] = ',';
129 newref[oldreflen++] = ' ';
130 memcpy(newref + oldreflen, oldmsgid, oldmsgidlen +1);
132 } else if (oldmsgid)
133 memcpy(newref, oldmsgid, oldmsgidlen +1);
134 n = extract(newref, GREF);
135 free(newref);
137 /* Limit number of references TODO better on parser side */
138 while (n->n_flink != NULL)
139 n = n->n_flink;
140 for (i = 1; i <= REFERENCES_MAX; ++i) {
141 if (n->n_blink != NULL)
142 n = n->n_blink;
143 else
144 break;
146 n->n_blink = NULL;
147 head->h_ref = n;
148 if (ok_blook(reply_in_same_charset) &&
149 (cp = hfield1("content-type", mp)) != NULL){
150 if((head->h_charset = cp = mime_param_get("charset", cp)) != NULL){
151 char *cpo, c;
153 for(cpo = cp; (c = *cpo) != '\0'; ++cpo)
154 *cpo = lowerconv(c);
157 jleave:
158 NYD_LEAVE;
161 static int
162 _list_reply(int *msgvec, enum header_flags hf)
164 struct header head;
165 struct message *mp;
166 char const *reply_to, *rcv, *cp;
167 enum gfield gf;
168 struct name *rt, *mft, *np;
169 int *save_msgvec;
170 NYD_ENTER;
172 /* TODO Since we may recur and do stuff with message lists we need to save
173 * TODO away the argument vector as long as that isn't done by machinery */
175 size_t i;
176 for (i = 0; msgvec[i] != 0; ++i)
178 ++i;
179 save_msgvec = ac_alloc(sizeof(*save_msgvec) * i);
180 while (i-- > 0)
181 save_msgvec[i] = msgvec[i];
182 msgvec = save_msgvec;
185 jnext_msg:
186 mp = message + *msgvec - 1;
187 touch(mp);
188 setdot(mp);
190 memset(&head, 0, sizeof head);
191 head.h_flags = hf;
193 head.h_subject = _reedit(hfield1("subject", mp));
194 gf = ok_blook(fullnames) ? GFULL : GSKIN;
195 rt = mft = NULL;
197 rcv = NULL;
198 if ((reply_to = hfield1("reply-to", mp)) != NULL &&
199 (cp = ok_vlook(reply_to_honour)) != NULL &&
200 (rt = checkaddrs(lextract(reply_to, GTO | gf), EACM_STRICT, NULL)
201 ) != NULL) {
202 char const *tr = _("Reply-To %s%s");
203 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
204 char *sp = salloc(l);
206 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : n_empty));
207 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0)
208 rcv = reply_to;
211 if (rcv == NULL && (rcv = hfield1("from", mp)) == NULL)
212 rcv = nameof(mp, 1);
214 /* Cc: */
215 np = NULL;
216 if (ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
217 np = lextract(cp, GCC | gf);
218 if ((cp = hfield1("cc", mp)) != NULL)
219 np = cat(np, lextract(cp, GCC | gf));
220 if (np != NULL)
221 head.h_cc = delete_alternates(np);
223 /* To: */
224 np = NULL;
225 if (rcv != NULL)
226 np = (rcv == reply_to) ? namelist_dup(rt, GTO | gf)
227 : lextract(rcv, GTO | gf);
228 if (!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
229 np = cat(np, lextract(cp, GTO | gf));
230 /* Delete my name from reply list, and with it, all my alternate names */
231 np = delete_alternates(np);
232 if (count(np) == 0)
233 np = lextract(rcv, GTO | gf);
234 head.h_to = np;
236 /* The user may have send this to himself, don't ignore that */
237 namelist_vaporise_head(&head, EACM_NORMAL, FAL0, NULL);
238 if (head.h_to == NULL)
239 head.h_to = np;
241 /* Mail-Followup-To: */
242 mft = NULL;
243 if (ok_vlook(followup_to_honour) != NULL &&
244 (cp = hfield1("mail-followup-to", mp)) != NULL &&
245 (mft = np = checkaddrs(lextract(cp, GTO | gf), EACM_STRICT, NULL)
246 ) != NULL) {
247 char const *tr = _("Followup-To %s%s");
248 size_t l = strlen(tr) + strlen(np->n_name) + 3 +1;
249 char *sp = salloc(l);
251 snprintf(sp, l, tr, np->n_name, (np->n_flink != NULL ? "..." : n_empty));
252 if (quadify(ok_vlook(followup_to_honour), UIZ_MAX, sp, TRU1) > FAL0) {
253 head.h_cc = NULL;
254 head.h_to = np;
255 head.h_mft =
256 mft = namelist_vaporise_head(&head, EACM_STRICT, FAL0, NULL);
257 } else
258 mft = NULL;
261 /* Special massage for list (follow-up) messages */
262 if (mft != NULL || (hf & HF_LIST_REPLY) || ok_blook(followup_to)) {
263 /* Learn about a possibly sending mailing list; use do for break; */
264 if ((cp = hfield1("list-post", mp)) != NULL) do {
265 struct name *x;
267 if ((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
268 (cp = url_mailto_to_address(x->n_name)) == NULL ||
269 /* XXX terribly wasteful to create a new name, and can't we find
270 * XXX a way to mitigate that?? */
271 is_addr_invalid(x = nalloc(cp, GEXTRA | GSKIN), EACM_STRICT)) {
272 if (options & OPT_D_V)
273 n_err(_("Message contains invalid List-Post: header\n"));
274 cp = NULL;
275 break;
277 cp = x->n_name;
279 /* A special case has been seen on e.g. ietf-announce@ietf.org:
280 * these usually post to multiple groups, with ietf-announce@
281 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
282 * -announce@ is only used for announcements, say).
283 * So our desire is to honour this request and actively overwrite
284 * List-Post: for our purpose; but only if its a single address.
285 * However, to avoid ambiguities with users that place themselve in
286 * Reply-To: and mailing lists which don't overwrite this (or only
287 * extend this, shall such exist), only do so if reply_to exists of
288 * a single address which points to the same domain as List-Post: */
289 if (reply_to != NULL && rt->n_flink == NULL &&
290 name_is_same_domain(x, rt))
291 cp = rt->n_name; /* rt is EACM_STRICT tested */
293 /* "Automatically `mlist'" the List-Post: address temporarily */
294 if (is_mlist(cp, FAL0) == MLIST_OTHER)
295 head.h_list_post = cp;
296 else
297 cp = NULL;
298 } while (0);
300 /* In case of list replies we actively sort out any non-list recipient,
301 * but _only_ if we did not honour a MFT:, assuming that members of MFT
302 * were there for a reason; cp is still List-Post:/eqivalent */
303 if ((hf & HF_LIST_REPLY) && mft == NULL) {
304 struct name *nhp = head.h_to;
305 head.h_to = NULL;
306 j_lt_redo:
307 while (nhp != NULL) {
308 np = nhp;
309 nhp = nhp->n_flink;
311 if ((cp != NULL && !asccasecmp(cp, np->n_name)) ||
312 is_mlist(np->n_name, FAL0) != MLIST_OTHER) {
313 np->n_type = (np->n_type & ~GMASK) | GTO;
314 np->n_flink = head.h_to;
315 head.h_to = np;
318 if ((nhp = head.h_cc) != NULL) {
319 head.h_cc = NULL;
320 goto j_lt_redo;
325 make_ref_and_cs(mp, &head);
327 if (ok_blook(quote_as_attachment)) {
328 head.h_attach = csalloc(1, sizeof *head.h_attach);
329 head.h_attach->a_msgno = *msgvec;
330 head.h_attach->a_content_description = _("Original message content");
333 if (mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), 0) == OKAY &&
334 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
335 mp->m_flag |= MANSWER | MANSWERED;
337 if (*++msgvec != 0) {
338 /* TODO message (error) ring.., less sleep */
339 printf(_("Waiting a second before proceeding to the next message..\n"));
340 fflush(stdout);
341 n_msleep(1000, FAL0);
342 goto jnext_msg;
345 ac_free(save_msgvec);
346 NYD_LEAVE;
347 return 0;
350 static int
351 (*_reply_or_Reply(char c))(int *, bool_t)
353 int (*rv)(int*, bool_t);
354 NYD_ENTER;
356 rv = (ok_blook(flipr) ^ (c == 'R')) ? &_Reply : &_reply;
357 NYD_LEAVE;
358 return rv;
361 static int
362 _reply(int *msgvec, bool_t recipient_record)
364 int rv;
365 NYD_ENTER;
367 rv = _list_reply(msgvec, recipient_record ? HF_RECIPIENT_RECORD : HF_NONE);
368 NYD_LEAVE;
369 return rv;
372 static int
373 _Reply(int *msgvec, bool_t recipient_record)
375 struct header head;
376 struct message *mp;
377 int *ap;
378 char *cp;
379 enum gfield gf;
380 NYD_ENTER;
382 memset(&head, 0, sizeof head);
383 gf = ok_blook(fullnames) ? GFULL : GSKIN;
385 for (ap = msgvec; *ap != 0; ++ap) {
386 char const *rp;
387 struct name *rt;
389 mp = message + *ap - 1;
390 touch(mp);
391 setdot(mp);
393 if ((rp = hfield1("reply-to", mp)) != NULL &&
394 (cp = ok_vlook(reply_to_honour)) != NULL &&
395 (rt = checkaddrs(lextract(rp, GTO | gf), EACM_STRICT, NULL)
396 ) != NULL) {
397 char const *tr = _("Reply-To %s%s");
398 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
399 char *sp = salloc(l);
401 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..."
402 : n_empty));
403 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0) {
404 head.h_to = cat(head.h_to, rt);
405 continue;
409 if ((cp = hfield1("from", mp)) == NULL)
410 cp = nameof(mp, 2);
411 head.h_to = cat(head.h_to, lextract(cp, GTO | gf));
413 if (head.h_to == NULL)
414 goto jleave;
416 mp = message + msgvec[0] - 1;
417 head.h_subject = hfield1("subject", mp);
418 head.h_subject = _reedit(head.h_subject);
419 make_ref_and_cs(mp, &head);
421 if (ok_blook(quote_as_attachment)) {
422 head.h_attach = csalloc(1, sizeof *head.h_attach);
423 head.h_attach->a_msgno = *msgvec;
424 head.h_attach->a_content_description = _("Original message content");
427 if (mail1(&head, 1, mp, NULL, recipient_record, 0) == OKAY &&
428 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
429 mp->m_flag |= MANSWER | MANSWERED;
430 jleave:
431 NYD_LEAVE;
432 return 0;
435 static int
436 _fwd(char *str, int recipient_record)
438 struct header head;
439 int *msgvec, rv = 1;
440 char *recipient;
441 struct message *mp;
442 bool_t f, forward_as_attachment;
443 NYD_ENTER;
445 if ((recipient = laststring(str, &f, TRU1)) == NULL) {
446 puts(_("No recipient specified."));
447 goto jleave;
450 forward_as_attachment = ok_blook(forward_as_attachment);
451 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
453 if (!f) {
454 *msgvec = first(0, MMNORM);
455 if (*msgvec == 0) {
456 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
457 rv = 0;
458 goto jleave;
460 printf(_("No messages to forward.\n"));
461 goto jleave;
463 msgvec[1] = 0;
464 } else if (getmsglist(str, msgvec, 0) < 0)
465 goto jleave;
467 if (*msgvec == 0) {
468 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
469 rv = 0;
470 goto jleave;
472 printf(_("No applicable messages.\n"));
473 goto jleave;
475 if (msgvec[1] != 0) {
476 printf(_("Cannot forward multiple messages at once\n"));
477 goto jleave;
480 memset(&head, 0, sizeof head);
481 if ((head.h_to = lextract(recipient,
482 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)))) == NULL)
483 goto jleave;
485 mp = message + *msgvec - 1;
487 if (forward_as_attachment) {
488 head.h_attach = csalloc(1, sizeof *head.h_attach);
489 head.h_attach->a_msgno = *msgvec;
490 head.h_attach->a_content_description = _("Forwarded message");
491 } else {
492 touch(mp);
493 setdot(mp);
495 head.h_subject = hfield1("subject", mp);
496 head.h_subject = __fwdedit(head.h_subject);
497 mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL, recipient_record,
499 rv = 0;
500 jleave:
501 NYD_LEAVE;
502 return rv;
505 static char *
506 __fwdedit(char *subj)
508 struct str in, out;
509 char *newsubj = NULL;
510 NYD_ENTER;
512 if (subj == NULL || *subj == '\0')
513 goto jleave;
515 in.s = subj;
516 in.l = strlen(subj);
517 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
519 newsubj = salloc(out.l + 6);
520 memcpy(newsubj, "Fwd: ", 5); /* XXX localizable */
521 memcpy(newsubj + 5, out.s, out.l +1);
522 free(out.s);
523 jleave:
524 NYD_LEAVE;
525 return newsubj;
528 static int
529 _resend1(void *v, bool_t add_resent)
531 char *name, *str;
532 struct name *to, *sn;
533 int *ip, *msgvec;
534 bool_t f = TRU1;
535 NYD_ENTER;
537 str = v;
538 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
539 name = laststring(str, &f, TRU1);
540 if (name == NULL) {
541 puts(_("No recipient specified."));
542 goto jleave;
545 if (!f) {
546 *msgvec = first(0, MMNORM);
547 if (*msgvec == 0) {
548 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
549 f = FAL0;
550 goto jleave;
552 puts(_("No applicable messages."));
553 goto jleave;
555 msgvec[1] = 0;
556 } else if (getmsglist(str, msgvec, 0) < 0)
557 goto jleave;
559 if (*msgvec == 0) {
560 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
561 f = FAL0;
562 goto jleave;
564 printf("No applicable messages.\n");
565 goto jleave;
568 sn = nalloc(name, GTO | GSKIN);
569 to = usermap(sn, FAL0);
570 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
571 ++ip)
572 if (resend_msg(message + *ip - 1, to, add_resent) != OKAY)
573 goto jleave;
574 f = FAL0;
575 jleave:
576 NYD_LEAVE;
577 return (f != FAL0);
580 FL int
581 c_reply(void *v)
583 int rv;
584 NYD_ENTER;
586 rv = (*_reply_or_Reply('r'))(v, FAL0);
587 NYD_LEAVE;
588 return rv;
591 FL int
592 c_replyall(void *v)
594 int rv;
595 NYD_ENTER;
597 rv = _reply(v, FAL0);
598 NYD_LEAVE;
599 return rv;
602 FL int
603 c_replysender(void *v)
605 int rv;
606 NYD_ENTER;
608 rv = _Reply(v, FAL0);
609 NYD_LEAVE;
610 return rv;
613 FL int
614 c_Reply(void *v)
616 int rv;
617 NYD_ENTER;
619 rv = (*_reply_or_Reply('R'))(v, FAL0);
620 NYD_LEAVE;
621 return rv;
624 FL int
625 c_Lreply(void *v)
627 int rv;
628 NYD_ENTER;
630 rv = _list_reply(v, HF_LIST_REPLY);
631 NYD_LEAVE;
632 return rv;
635 FL int
636 c_followup(void *v)
638 int rv;
639 NYD_ENTER;
641 rv = (*_reply_or_Reply('r'))(v, TRU1);
642 NYD_LEAVE;
643 return rv;
646 FL int
647 c_followupall(void *v)
649 int rv;
650 NYD_ENTER;
652 rv = _reply(v, TRU1);
653 NYD_LEAVE;
654 return rv;
657 FL int
658 c_followupsender(void *v)
660 int rv;
661 NYD_ENTER;
663 rv = _Reply(v, TRU1);
664 NYD_LEAVE;
665 return rv;
668 FL int
669 c_Followup(void *v)
671 int rv;
672 NYD_ENTER;
674 rv = (*_reply_or_Reply('R'))(v, TRU1);
675 NYD_LEAVE;
676 return rv;
679 FL int
680 c_forward(void *v)
682 int rv;
683 NYD_ENTER;
685 rv = _fwd(v, 0);
686 NYD_LEAVE;
687 return rv;
690 FL int
691 c_Forward(void *v)
693 int rv;
694 NYD_ENTER;
696 rv = _fwd(v, 1);
697 NYD_LEAVE;
698 return rv;
701 FL int
702 c_resend(void *v)
704 int rv;
705 NYD_ENTER;
707 rv = _resend1(v, TRU1);
708 NYD_LEAVE;
709 return rv;
712 FL int
713 c_Resend(void *v)
715 int rv;
716 NYD_ENTER;
718 rv = _resend1(v, FAL0);
719 NYD_LEAVE;
720 return rv;
723 /* s-it-mode */