Bump S-nail v14.9.4.ar ("(5th anniversary) Marsh tit"), 2017-09-18
[s-mailx.git] / cmd-resend.c
blob59361150c91a28d5e4dec8c20c4ba66c8d6b8cfb
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 /* Fetch these headers, as appropriate */
46 static struct name *a_crese_reply_to(struct message *mp);
47 static struct name *a_crese_mail_followup_to(struct message *mp);
49 /* We honoured Reply-To: and/or Mail-Followup-To:, but *recipients-in-cc* is
50 * set so try to keep "secondary" addressees in Cc:, if possible, */
51 static void a_crese_polite_rt_mft_move(struct message *mp, struct header *hp,
52 struct name *np);
54 /* References and charset, as appropriate */
55 static void a_crese_make_ref_and_cs(struct message *mp, struct header *head);
57 /* `reply' and `Lreply' workhorse */
58 static int a_crese_list_reply(int *msgvec, enum header_flags hf);
60 /* Get PTF to implementation of command c (i.e., take care for *flipr*) */
61 static int (*a_crese_reply_or_Reply(char c))(int *, bool_t);
63 /* Reply to a single message. Extract each name from the message header and
64 * send them off to mail1() */
65 static int a_crese_reply(int *msgvec, bool_t recipient_record);
67 /* Reply to a series of messages by simply mailing to the senders and not
68 * messing around with the To: and Cc: lists as in normal reply */
69 static int a_crese_Reply(int *msgvec, bool_t recipient_record);
71 /* Forward a message to a new recipient, in the sense of RFC 2822 */
72 static int a_crese_fwd(char *str, int recipient_record);
74 /* Modify the subject we are replying to to begin with Fwd: */
75 static char *a_crese__fwdedit(char *subj);
77 /* Do the real work of resending */
78 static int a_crese_resend1(void *v, bool_t add_resent);
80 static char *
81 a_crese_reedit(char const *subj){
82 char *newsubj;
83 NYD2_ENTER;
85 newsubj = NULL;
87 if(subj != NULL && *subj != '\0'){
88 struct str in, out;
89 size_t i;
90 char const *cp;
92 in.l = strlen(in.s = n_UNCONST(subj));
93 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
95 i = strlen(cp = subject_re_trim(out.s)) +1;
96 /* RFC mandates english "Re: " */
97 newsubj = n_autorec_alloc(sizeof("Re: ") -1 + i);
98 memcpy(newsubj, "Re: ", sizeof("Re: ") -1);
99 memcpy(&newsubj[sizeof("Re: ") -1], cp, i);
101 free(out.s);
103 NYD2_LEAVE;
104 return newsubj;
107 static struct name *
108 a_crese_reply_to(struct message *mp){
109 char const *cp, *cp2;
110 struct name *rt, *np;
111 enum gfield gf;
112 NYD2_ENTER;
114 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
115 rt = NULL;
117 if((cp = ok_vlook(reply_to_honour)) != NULL &&
118 (cp2 = hfield1("reply-to", mp)) != NULL &&
119 (rt = checkaddrs(lextract(cp2, GTO | gf), EACM_STRICT, NULL)) != NULL){
120 char *sp;
121 size_t l;
122 char const *tr;
124 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
125 fprintf(n_stdout, _("Reply-To: header contains:"));
126 for(np = rt; np != NULL; np = np->n_flink)
127 fprintf(n_stdout, " %s", np->n_name);
128 putc('\n', n_stdout);
131 tr = _("Reply-To %s%s");
132 l = strlen(tr) + strlen(rt->n_name) + 3 +1;
133 sp = n_lofi_alloc(l);
135 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : n_empty));
136 if(quadify(cp, UIZ_MAX, sp, TRU1) <= FAL0)
137 rt = NULL;
139 n_lofi_free(sp);
141 NYD2_LEAVE;
142 return rt;
145 static struct name *
146 a_crese_mail_followup_to(struct message *mp){
147 char const *cp, *cp2;
148 struct name *mft, *np;
149 enum gfield gf;
150 NYD2_ENTER;
152 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
153 mft = NULL;
155 if((cp = ok_vlook(followup_to_honour)) != NULL &&
156 (cp2 = hfield1("mail-followup-to", mp)) != NULL &&
157 (mft = checkaddrs(lextract(cp2, GTO | gf), EACM_STRICT,NULL)) != NULL){
158 char *sp;
159 size_t l;
160 char const *tr;
162 if((n_psonce & n_PSO_INTERACTIVE) && !(n_pstate & n_PS_ROBOT)){
163 fprintf(n_stdout, _("Mail-Followup-To: header contains:"));
164 for(np = mft; np != NULL; np = np->n_flink)
165 fprintf(n_stdout, " %s", np->n_name);
166 putc('\n', n_stdout);
169 tr = _("Followup-To %s%s");
170 l = strlen(tr) + strlen(mft->n_name) + 3 +1;
171 sp = n_lofi_alloc(l);
173 snprintf(sp, l, tr, mft->n_name, (mft->n_flink != NULL ? "..." : n_empty));
174 if(quadify(cp, UIZ_MAX, sp, TRU1) <= FAL0)
175 mft = NULL;
177 n_lofi_free(sp);
179 NYD2_LEAVE;
180 return mft;
183 static void
184 a_crese_polite_rt_mft_move(struct message *mp, struct header *hp,
185 struct name *np){
186 bool_t once;
187 NYD2_ENTER;
188 n_UNUSED(mp);
190 if(np == hp->h_to)
191 hp->h_to = NULL;
192 if(np == hp->h_cc)
193 hp->h_cc = NULL;
195 /* We may find that in the end To: is empty but Cc: is not, in which case we
196 * upgrade Cc: to To:, and jump back and redo the thing slightly different */
197 once = FAL0;
198 jredo:
199 while(np != NULL){
200 enum gfield gf;
201 struct name *nnp, **xpp, *xp;
203 nnp = np;
204 np = np->n_flink;
206 if(once){
207 gf = GTO;
208 xpp = &hp->h_to;
209 }else{
210 gf = GCC;
211 xpp = &hp->h_cc;
214 /* Try primary, then secondary */
215 for(xp = hp->h_mailx_orig_to; xp != NULL; xp = xp->n_flink)
216 if(!asccasecmp(xp->n_name, nnp->n_name))
217 goto jlink;
219 if(once){
220 gf = GCC;
221 xpp = &hp->h_cc;
224 for(xp = hp->h_mailx_orig_cc; xp != NULL; xp = xp->n_flink)
225 if(!asccasecmp(xp->n_name, nnp->n_name))
226 goto jlink;
228 /* If this receiver came in only via R-T: or M-F-T:, place her/him/it in
229 * To: due to lack of a better place */
230 gf = GTO;
231 xpp = &hp->h_to;
232 jlink:
233 /* Link it at the end to not loose original sort order */
234 if((xp = *xpp) != NULL)
235 while(xp->n_flink != NULL)
236 xp = xp->n_flink;
238 if((nnp->n_blink = xp) != NULL)
239 xp->n_flink = nnp;
240 else
241 *xpp = nnp;
242 nnp->n_flink = NULL;
243 nnp->n_type = (nnp->n_type & ~GMASK) | gf;
246 /* If afterwards only Cc: data remains, upgrade all of it to To: */
247 if(hp->h_to == NULL){
248 np = hp->h_cc;
249 hp->h_cc = NULL;
250 if(!once){
251 hp->h_to = NULL;
252 once = TRU1;
253 goto jredo;
255 hp->h_to = np;
257 NYD2_LEAVE;
260 static void
261 a_crese_make_ref_and_cs(struct message *mp, struct header *head) /* TODO ASAP */
263 char *oldref, *oldmsgid, *newref, *cp;
264 size_t oldreflen = 0, oldmsgidlen = 0, reflen;
265 unsigned i;
266 struct name *n;
267 NYD2_ENTER;
269 oldref = hfield1("references", mp);
270 oldmsgid = hfield1("message-id", mp);
271 if (oldmsgid == NULL || *oldmsgid == '\0') {
272 head->h_ref = NULL;
273 goto jleave;
276 reflen = 1;
277 if (oldref) {
278 oldreflen = strlen(oldref);
279 reflen += oldreflen + 2;
281 if (oldmsgid) {
282 oldmsgidlen = strlen(oldmsgid);
283 reflen += oldmsgidlen;
286 newref = smalloc(reflen);
287 if (oldref != NULL) {
288 memcpy(newref, oldref, oldreflen +1);
289 if (oldmsgid != NULL) {
290 newref[oldreflen++] = ',';
291 newref[oldreflen++] = ' ';
292 memcpy(newref + oldreflen, oldmsgid, oldmsgidlen +1);
294 } else if (oldmsgid)
295 memcpy(newref, oldmsgid, oldmsgidlen +1);
296 n = extract(newref, GREF);
297 free(newref);
299 /* Limit number of references TODO better on parser side */
300 while (n->n_flink != NULL)
301 n = n->n_flink;
302 for (i = 1; i <= REFERENCES_MAX; ++i) {
303 if (n->n_blink != NULL)
304 n = n->n_blink;
305 else
306 break;
308 n->n_blink = NULL;
309 head->h_ref = n;
310 if (ok_blook(reply_in_same_charset) &&
311 (cp = hfield1("content-type", mp)) != NULL){
312 if((head->h_charset = cp = mime_param_get("charset", cp)) != NULL){
313 char *cpo, c;
315 for(cpo = cp; (c = *cpo) != '\0'; ++cpo)
316 *cpo = lowerconv(c);
318 head->h_charset = n_charsetalias_expand(cp);
321 jleave:
322 NYD2_LEAVE;
325 static int
326 a_crese_list_reply(int *msgvec, enum header_flags hf){
327 struct header head;
328 struct message *mp;
329 char const *cp, *cp2;
330 enum gfield gf;
331 struct name *rt, *mft, *np;
332 int *save_msgvec;
333 NYD2_ENTER;
335 n_pstate_err_no = n_ERR_NONE;
337 /* TODO Since we may recur and do stuff with message lists we need to save
338 * TODO away the argument vector as long as that isn't done by machinery */
339 /* C99 */{
340 size_t i;
341 for(i = 0; msgvec[i] != 0; ++i)
343 ++i;
344 save_msgvec = n_lofi_alloc(sizeof(*save_msgvec) * i);
345 while(i-- > 0)
346 save_msgvec[i] = msgvec[i];
347 msgvec = save_msgvec;
350 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
352 jwork_msg:
353 n_autorec_relax_create();
354 mp = &message[*msgvec - 1];
355 touch(mp);
356 setdot(mp);
358 memset(&head, 0, sizeof head);
359 head.h_flags = hf;
360 head.h_subject = a_crese_reedit(hfield1("subject", mp));
361 head.h_mailx_command = (hf & HF_LIST_REPLY) ? "Lreply" : "reply";
362 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
363 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
364 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
365 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
367 /* First of all check for Reply-To: then Mail-Followup-To:, because these,
368 * if honoured, take precedence over anything else. We will join the
369 * resulting list together if so desired.
370 * So if we shall honour R-T: or M-F-T:, then these are our receivers! */
371 rt = a_crese_reply_to(mp);
372 mft = a_crese_mail_followup_to(mp);
374 if(rt != NULL || mft != NULL){
375 np = cat(rt, mft);
376 if(mft != NULL)
377 head.h_mft = namelist_dup(np, GTO | gf); /* xxx GTO: no "clone"! */
379 /* Optionally do not propagate a receiver that originally was in
380 * secondary Cc: to the primary To: list */
381 if(ok_blook(recipients_in_cc)){
382 a_crese_polite_rt_mft_move(mp, &head, np);
384 head.h_mailx_raw_cc = namelist_dup(head.h_cc, GCC | gf);
385 head.h_cc = n_alternates_remove(head.h_cc, FAL0);
386 }else
387 head.h_to = np;
389 head.h_mailx_raw_to = namelist_dup(head.h_to, GTO | gf);
390 head.h_to = n_alternates_remove(head.h_to, FAL0);
391 #ifdef HAVE_DEVEL
392 for(np = head.h_to; np != NULL; np = np->n_flink)
393 assert((np->n_type & GMASK) == GTO);
394 for(np = head.h_cc; np != NULL; np = np->n_flink)
395 assert((np->n_type & GMASK) == GCC);
396 #endif
397 goto jrecipients_done;
400 /* Otherwise do the normal From: / To: / Cc: dance */
402 if((cp2 = hfield1("from", mp)) == NULL)
403 cp2 = nameof(mp, 1);
405 /* Cc: */
406 np = NULL;
407 if(ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
408 np = lextract(cp, GCC | gf);
409 if((cp = hfield1("cc", mp)) != NULL){
410 struct name *x;
412 if((x = lextract(cp, GCC | gf)) != NULL)
413 np = cat(np, x);
415 if(np != NULL){
416 head.h_mailx_raw_cc = namelist_dup(np, GCC | gf);
417 head.h_cc = n_alternates_remove(np, FAL0);
420 /* To: */
421 np = NULL;
422 if(cp2 != NULL)
423 np = lextract(cp2, GTO | gf);
424 if(!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL){
425 struct name *x;
427 if((x = lextract(cp, GTO | gf)) != NULL)
428 np = cat(np, x);
430 /* Delete my name from reply list, and with it, all my alternate names */
431 if(np != NULL){
432 head.h_mailx_raw_to = namelist_dup(np, GTO | gf);
433 np = n_alternates_remove(np, FAL0);
434 /* The user may have send this to himself, don't ignore that */
435 if(count(np) == 0){
436 np = lextract(cp2, GTO | gf);
437 head.h_mailx_raw_to = namelist_dup(np, GTO | gf);
440 head.h_to = np;
442 jrecipients_done:
444 /* For list replies we want to automatically recognize the list address
445 * given in the List-Post: header, so that we will not throw away a possible
446 * corresponding receiver: temporarily "`mlist' the List-Post: address" */
447 if((hf & HF_LIST_REPLY) && (cp = hfield1("list-post", mp)) != NULL){
448 struct name *x;
450 if((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
451 (cp = url_mailto_to_address(x->n_name)) == NULL ||
452 /* XXX terribly wasteful to create a new name, and can't we find
453 * XXX a way to mitigate that?? */
454 is_addr_invalid(x = nalloc(cp, GEXTRA | GSKIN), EACM_STRICT)){
455 if(n_poption & n_PO_D_V)
456 n_err(_("Message contains invalid List-Post: header\n"));
457 }else{
458 /* A special case has been seen on e.g. ietf-announce@ietf.org:
459 * these usually post to multiple groups, with ietf-announce@
460 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
461 * -announce@ is only used for announcements, say).
462 * So our desire is to honour this request and actively overwrite
463 * List-Post: for our purpose; but only if its a single address.
464 * However, to avoid ambiguities with users that place themselve in
465 * Reply-To: and mailing lists which don't overwrite this (or only
466 * extend this, shall such exist), only do so if reply_to exists of
467 * a single address which points to the same domain as List-Post: */
468 if(rt != NULL && rt->n_flink == NULL &&
469 name_is_same_domain(x, rt))
470 cp = rt->n_name; /* rt is EACM_STRICT tested */
471 else
472 cp = x->n_name;
474 if(is_mlist(cp, FAL0) == MLIST_OTHER)
475 head.h_list_post = cp;
479 /* In case of list replies we actively sort out any non-list recipient */
480 if(hf & HF_LIST_REPLY){
481 struct name **nhpp, *nhp, *tail;
483 cp = head.h_list_post;
485 nhp = *(nhpp = &head.h_to);
486 head.h_to = NULL;
487 j_lt_redo:
488 for(tail = NULL; nhp != NULL;){
489 np = nhp;
490 nhp = nhp->n_flink;
492 if((cp != NULL && !asccasecmp(cp, np->n_name)) ||
493 is_mlist(np->n_name, FAL0) != MLIST_OTHER){
494 if((np->n_blink = tail) != NULL)
495 tail->n_flink = np;
496 else
497 *nhpp = np;
498 np->n_flink = NULL;
499 tail = np;
502 if(nhpp == &head.h_to){
503 nhp = *(nhpp = &head.h_cc);
504 head.h_cc = NULL;
505 goto j_lt_redo;
508 /* For `Lreply' only, fail immediately with DESTADDRREQ if there are no
509 * receivers at all! */
510 if(head.h_to == NULL && head.h_cc == NULL){
511 n_err(_("No recipients specified for `Lreply'\n"));
512 if(msgvec[1] == 0){
513 n_pstate_err_no = n_ERR_DESTADDRREQ;
514 msgvec = NULL;
515 goto jleave;
517 goto jskip_to_next;
521 /* Move Cc: to To: as appropriate! */
522 if(head.h_to == NULL && (np = head.h_cc) != NULL){
523 head.h_cc = NULL;
524 for(head.h_to = np; np != NULL; np = np->n_flink)
525 np->n_type = (np->n_type & ~GMASK) | GTO;
528 a_crese_make_ref_and_cs(mp, &head);
530 if(ok_blook(quote_as_attachment)){
531 head.h_attach = csalloc(1, sizeof *head.h_attach);
532 head.h_attach->a_msgno = *msgvec;
533 head.h_attach->a_content_description = _("Original message content");
536 if(mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), 0) != OKAY){
537 msgvec = NULL;
538 goto jleave;
540 if(ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
541 mp->m_flag |= MANSWER | MANSWERED;
542 n_autorec_relax_gut();
544 jskip_to_next:
545 if(*++msgvec != 0){
546 /* TODO message (error) ring.., less sleep */
547 if(n_psonce & n_PSO_INTERACTIVE){
548 fprintf(n_stdout,
549 _("Waiting a second before proceeding to the next message..\n"));
550 fflush(n_stdout);
551 n_msleep(1000, FAL0);
553 goto jwork_msg;
556 jleave:
557 n_lofi_free(save_msgvec);
558 NYD2_LEAVE;
559 return (msgvec == NULL);
562 static int
563 (*a_crese_reply_or_Reply(char c))(int *, bool_t){
564 int (*rv)(int*, bool_t);
565 NYD2_ENTER;
567 rv = (ok_blook(flipr) ^ (c == 'R')) ? &a_crese_Reply : &a_crese_reply;
568 NYD2_LEAVE;
569 return rv;
572 static int
573 a_crese_reply(int *msgvec, bool_t recipient_record){
574 int rv;
575 NYD2_ENTER;
577 rv = a_crese_list_reply(msgvec,
578 (recipient_record ? HF_RECIPIENT_RECORD : HF_NONE));
579 NYD2_LEAVE;
580 return rv;
583 static int
584 a_crese_Reply(int *msgvec, bool_t recipient_record){
585 struct header head;
586 struct message *mp;
587 int *ap;
588 enum gfield gf;
589 NYD2_ENTER;
591 memset(&head, 0, sizeof head);
592 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
594 for(ap = msgvec; *ap != 0; ++ap){
595 struct name *np;
597 mp = &message[*ap - 1];
598 touch(mp);
599 setdot(mp);
601 if((np = a_crese_reply_to(mp)) == NULL){
602 char *cp;
604 if((cp = hfield1("from", mp)) == NULL)
605 cp = nameof(mp, 2);
606 np = lextract(cp, GTO | gf);
608 head.h_to = cat(head.h_to, np);
611 mp = &message[msgvec[0] - 1];
612 head.h_subject = hfield1("subject", mp);
613 head.h_subject = a_crese_reedit(head.h_subject);
614 a_crese_make_ref_and_cs(mp, &head);
615 head.h_mailx_command = "Reply";
616 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
617 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
618 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
619 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
621 if(ok_blook(recipients_in_cc)){
622 a_crese_polite_rt_mft_move(mp, &head, head.h_to);
624 head.h_mailx_raw_cc = namelist_dup(head.h_cc, GCC | gf);
625 head.h_cc = n_alternates_remove(head.h_cc, FAL0);
627 head.h_mailx_raw_to = namelist_dup(head.h_to, GTO | gf);
628 head.h_to = n_alternates_remove(head.h_to, FAL0);
630 if(ok_blook(quote_as_attachment)){
631 head.h_attach = csalloc(1, sizeof *head.h_attach);
632 head.h_attach->a_msgno = *msgvec;
633 head.h_attach->a_content_description = _("Original message content");
636 if(mail1(&head, 1, mp, NULL, recipient_record, 0) != OKAY){
637 msgvec = NULL;
638 goto jleave;
641 if(ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
642 mp->m_flag |= MANSWER | MANSWERED;
643 jleave:
644 NYD2_LEAVE;
645 return (msgvec == NULL);
648 static int
649 a_crese_fwd(char *str, int recipient_record){
650 struct header head;
651 struct message *mp;
652 enum gfield gf;
653 bool_t f, forward_as_attachment;
654 char *recipient;
655 int rv, *msgvec;
656 NYD2_ENTER;
658 rv = 1;
660 if((recipient = laststring(str, &f, TRU1)) == NULL){
661 n_err(_("No recipient specified.\n"));
662 n_pstate_err_no = n_ERR_DESTADDRREQ;
663 goto jleave;
666 forward_as_attachment = ok_blook(forward_as_attachment);
667 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
668 msgvec = n_autorec_alloc((msgCount + 2) * sizeof *msgvec);
670 n_pstate_err_no = n_ERR_NODATA;
671 if(!f){
672 *msgvec = first(0, MMNORM);
673 if(*msgvec != 0)
674 msgvec[1] = 0;
675 }else if(getmsglist(str, msgvec, 0) < 0)
676 goto jleave;
678 if(*msgvec == 0){
679 n_err(_("No applicable messages.\n"));
680 goto jleave;
682 if(msgvec[1] != 0){
683 n_err(_("Cannot forward multiple messages at once\n"));
684 n_pstate_err_no = n_ERR_NOTSUP;
685 goto jleave;
688 memset(&head, 0, sizeof head);
689 head.h_to = lextract(recipient,
690 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)));
692 mp = &message[*msgvec - 1];
693 touch(mp);
694 setdot(mp);
695 head.h_subject = hfield1("subject", mp);
696 head.h_subject = a_crese__fwdedit(head.h_subject);
697 head.h_mailx_command = "forward";
698 head.h_mailx_raw_to = namelist_dup(head.h_to, GTO | gf);
699 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
700 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
701 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
702 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
704 if(forward_as_attachment){
705 head.h_attach = csalloc(1, sizeof *head.h_attach);
706 head.h_attach->a_msgno = *msgvec;
707 head.h_attach->a_content_description = _("Forwarded message");
710 rv = (mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL,
711 recipient_record, 1) != OKAY); /* reverse! */
712 jleave:
713 NYD2_LEAVE;
714 return rv;
717 static char *
718 a_crese__fwdedit(char *subj){
719 struct str in, out;
720 char *newsubj;
721 NYD2_ENTER;
723 newsubj = NULL;
725 if(subj == NULL || *subj == '\0')
726 goto jleave;
728 in.s = subj;
729 in.l = strlen(subj);
730 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
732 newsubj = n_autorec_alloc(out.l + 6);
733 if(!ascncasecmp(out.s, "Fwd: ", sizeof("Fwd: ") -1)) /* TODO EXTEND SUPP.. */
734 memcpy(newsubj, out.s, out.l +1);
735 else{
736 memcpy(newsubj, "Fwd: ", 5); /* TODO ..a la subject_re_trim()! */
737 memcpy(&newsubj[5], out.s, out.l +1);
740 free(out.s);
741 jleave:
742 NYD2_LEAVE;
743 return newsubj;
746 static int
747 a_crese_resend1(void *vp, bool_t add_resent){
748 struct header head;
749 struct name *myto, *myrawto;
750 enum gfield gf;
751 char *name, *str;
752 int *ip, *msgvec;
753 bool_t fail;
754 NYD2_ENTER;
756 fail = TRU1;
758 str = vp;
759 msgvec = n_autorec_alloc((msgCount + 2) * sizeof *msgvec);
760 name = laststring(str, &fail, TRU1);
761 if(name == NULL){
762 n_err(_("No recipient specified.\n"));
763 n_pstate_err_no = n_ERR_DESTADDRREQ;
764 goto jleave;
767 n_pstate_err_no = n_ERR_NODATA;
769 if(!fail){
770 *msgvec = first(0, MMNORM);
771 if(*msgvec != 0)
772 msgvec[1] = 0;
773 }else if(getmsglist(str, msgvec, 0) < 0)
774 goto jleave;
776 if(*msgvec == 0){
777 n_err(_("No applicable messages.\n"));
778 goto jleave;
781 fail = TRU1;
782 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
784 myrawto = nalloc(name, GTO | gf);
785 myto = usermap(namelist_dup(myrawto, myrawto->n_type), FAL0);
786 myto = n_alternates_remove(myto, TRU1);
787 if(myto == NULL){
788 n_pstate_err_no = n_ERR_DESTADDRREQ;
789 goto jleave;
792 n_autorec_relax_create();
793 for(ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
794 ++ip){
795 struct message *mp;
797 mp = &message[*ip - 1];
798 touch(mp);
799 setdot(mp);
801 memset(&head, 0, sizeof head);
802 head.h_to = myto;
803 head.h_mailx_command = "resend";
804 head.h_mailx_raw_to = myrawto;
805 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
806 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
807 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
808 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
810 if(resend_msg(mp, &head, add_resent) != OKAY){
811 /* n_autorec_relax_gut(); XXX but is handled automatically? */
812 goto jleave;
814 n_autorec_relax_unroll();
816 n_autorec_relax_gut();
818 fail = FAL0;
819 n_pstate_err_no = n_ERR_NONE;
820 jleave:
821 NYD2_LEAVE;
822 return (fail != FAL0);
825 FL int
826 c_reply(void *vp){
827 int rv;
828 NYD_ENTER;
830 rv = (*a_crese_reply_or_Reply('r'))(vp, FAL0);
831 NYD_LEAVE;
832 return rv;
835 FL int
836 c_replyall(void *vp){
837 int rv;
838 NYD_ENTER;
840 rv = a_crese_reply(vp, FAL0);
841 NYD_LEAVE;
842 return rv;
845 FL int
846 c_replysender(void *vp){
847 int rv;
848 NYD_ENTER;
850 rv = a_crese_Reply(vp, FAL0);
851 NYD_LEAVE;
852 return rv;
855 FL int
856 c_Reply(void *vp){
857 int rv;
858 NYD_ENTER;
860 rv = (*a_crese_reply_or_Reply('R'))(vp, FAL0);
861 NYD_LEAVE;
862 return rv;
865 FL int
866 c_Lreply(void *vp){
867 int rv;
868 NYD_ENTER;
870 rv = a_crese_list_reply(vp, HF_LIST_REPLY);
871 NYD_LEAVE;
872 return rv;
875 FL int
876 c_followup(void *vp){
877 int rv;
878 NYD_ENTER;
880 rv = (*a_crese_reply_or_Reply('r'))(vp, TRU1);
881 NYD_LEAVE;
882 return rv;
885 FL int
886 c_followupall(void *vp){
887 int rv;
888 NYD_ENTER;
890 rv = a_crese_reply(vp, TRU1);
891 NYD_LEAVE;
892 return rv;
895 FL int
896 c_followupsender(void *vp){
897 int rv;
898 NYD_ENTER;
900 rv = a_crese_Reply(vp, TRU1);
901 NYD_LEAVE;
902 return rv;
905 FL int
906 c_Followup(void *vp){
907 int rv;
908 NYD_ENTER;
910 rv = (*a_crese_reply_or_Reply('R'))(vp, TRU1);
911 NYD_LEAVE;
912 return rv;
915 FL int
916 c_forward(void *vp){
917 int rv;
918 NYD_ENTER;
920 rv = a_crese_fwd(vp, 0);
921 NYD_LEAVE;
922 return rv;
925 FL int
926 c_Forward(void *vp){
927 int rv;
928 NYD_ENTER;
930 rv = a_crese_fwd(vp, 1);
931 NYD_LEAVE;
932 return rv;
935 FL int
936 c_resend(void *vp){
937 int rv;
938 NYD_ENTER;
940 rv = a_crese_resend1(vp, TRU1);
941 NYD_LEAVE;
942 return rv;
945 FL int
946 c_Resend(void *vp){
947 int rv;
948 NYD_ENTER;
950 rv = a_crese_resend1(vp, FAL0);
951 NYD_LEAVE;
952 return rv;
955 /* s-it-mode */