*quote-as-attachment*: handle in compose-mode, very last..
[s-mailx.git] / cmd-resend.c
blob8b7b8bcb9b6211c13cf3afc41d7b1c21461dafc8
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 - 2018 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(void *vp, bool_t 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 n_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(n_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,
174 (mft->n_flink != NULL ? "..." : n_empty));
175 if(n_quadify(cp, UIZ_MAX, sp, TRU1) <= FAL0)
176 mft = NULL;
178 n_lofi_free(sp);
180 NYD2_LEAVE;
181 return mft;
184 static void
185 a_crese_polite_rt_mft_move(struct message *mp, struct header *hp,
186 struct name *np){
187 bool_t once;
188 NYD2_ENTER;
189 n_UNUSED(mp);
191 if(np == hp->h_to)
192 hp->h_to = NULL;
193 if(np == hp->h_cc)
194 hp->h_cc = NULL;
196 /* We may find that in the end To: is empty but Cc: is not, in which case we
197 * upgrade Cc: to To:, and jump back and redo the thing slightly different */
198 once = FAL0;
199 jredo:
200 while(np != NULL){
201 enum gfield gf;
202 struct name *nnp, **xpp, *xp;
204 nnp = np;
205 np = np->n_flink;
207 if(once){
208 gf = GTO;
209 xpp = &hp->h_to;
210 }else{
211 gf = GCC;
212 xpp = &hp->h_cc;
215 /* Try primary, then secondary */
216 for(xp = hp->h_mailx_orig_to; xp != NULL; xp = xp->n_flink)
217 if(!asccasecmp(xp->n_name, nnp->n_name))
218 goto jlink;
220 if(once){
221 gf = GCC;
222 xpp = &hp->h_cc;
225 for(xp = hp->h_mailx_orig_cc; xp != NULL; xp = xp->n_flink)
226 if(!asccasecmp(xp->n_name, nnp->n_name))
227 goto jlink;
229 /* If this receiver came in only via R-T: or M-F-T:, place her/him/it in
230 * To: due to lack of a better place */
231 gf = GTO;
232 xpp = &hp->h_to;
233 jlink:
234 /* Link it at the end to not loose original sort order */
235 if((xp = *xpp) != NULL)
236 while(xp->n_flink != NULL)
237 xp = xp->n_flink;
239 if((nnp->n_blink = xp) != NULL)
240 xp->n_flink = nnp;
241 else
242 *xpp = nnp;
243 nnp->n_flink = NULL;
244 nnp->n_type = (nnp->n_type & ~GMASK) | gf;
247 /* If afterwards only Cc: data remains, upgrade all of it to To: */
248 if(hp->h_to == NULL){
249 np = hp->h_cc;
250 hp->h_cc = NULL;
251 if(!once){
252 hp->h_to = NULL;
253 once = TRU1;
254 goto jredo;
255 }else
256 for(hp->h_to = np; np != NULL; np = np->n_flink)
257 np->n_type = (np->n_type & ~GMASK) | GTO;
259 NYD2_LEAVE;
262 static void
263 a_crese_make_ref_and_cs(struct message *mp, struct header *head) /* TODO ASAP */
265 char const *ccp;
266 char *oldref, *oldmsgid, *newref;
267 size_t oldreflen = 0, oldmsgidlen = 0, reflen;
268 unsigned i;
269 struct name *n;
270 NYD2_ENTER;
272 oldref = hfield1("references", mp);
273 oldmsgid = hfield1("message-id", mp);
274 if (oldmsgid == NULL || *oldmsgid == '\0') {
275 head->h_ref = NULL;
276 goto jleave;
279 reflen = 1;
280 if (oldref) {
281 oldreflen = strlen(oldref);
282 reflen += oldreflen + 2;
284 if (oldmsgid) {
285 oldmsgidlen = strlen(oldmsgid);
286 reflen += oldmsgidlen;
289 newref = n_alloc(reflen);
290 if (oldref != NULL) {
291 memcpy(newref, oldref, oldreflen +1);
292 if (oldmsgid != NULL) {
293 newref[oldreflen++] = ',';
294 newref[oldreflen++] = ' ';
295 memcpy(newref + oldreflen, oldmsgid, oldmsgidlen +1);
297 } else if (oldmsgid)
298 memcpy(newref, oldmsgid, oldmsgidlen +1);
299 n = extract(newref, GREF);
300 n_free(newref);
302 /* Limit number of references TODO better on parser side */
303 while (n->n_flink != NULL)
304 n = n->n_flink;
305 for (i = 1; i <= REFERENCES_MAX; ++i) {
306 if (n->n_blink != NULL)
307 n = n->n_blink;
308 else
309 break;
311 n->n_blink = NULL;
312 head->h_ref = n;
313 if (ok_blook(reply_in_same_charset) &&
314 (ccp = hfield1("content-type", mp)) != NULL){
315 if((head->h_charset = ccp = mime_param_get("charset", ccp)) != NULL){
316 if((ccp = n_iconv_normalize_name(ccp)) != NULL)
317 ccp = n_charsetalias_expand(ccp);
318 head->h_charset = ccp;
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 NYD2_ENTER;
334 n_pstate_err_no = n_ERR_NONE;
336 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
338 jwork_msg:
339 n_autorec_relax_create();
340 mp = &message[*msgvec - 1];
341 touch(mp);
342 setdot(mp);
344 memset(&head, 0, sizeof head);
345 head.h_flags = hf;
346 head.h_subject = a_crese_reedit(hfield1("subject", mp));
347 head.h_mailx_command = (hf & HF_LIST_REPLY) ? "Lreply" : "reply";
348 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
349 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
350 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
351 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
353 /* First of all check for Reply-To: then Mail-Followup-To:, because these,
354 * if honoured, take precedence over anything else. We will join the
355 * resulting list together if so desired.
356 * So if we shall honour R-T: or M-F-T:, then these are our receivers! */
357 rt = a_crese_reply_to(mp);
358 mft = a_crese_mail_followup_to(mp);
360 if(rt != NULL || mft != NULL){
361 np = cat(rt, mft);
362 if(mft != NULL)
363 head.h_mft = namelist_dup(np, GTO | gf); /* xxx GTO: no "clone"! */
365 /* Optionally do not propagate a receiver that originally was in
366 * secondary Cc: to the primary To: list */
367 if(ok_blook(recipients_in_cc)){
368 a_crese_polite_rt_mft_move(mp, &head, np);
370 head.h_mailx_raw_cc = namelist_dup(head.h_cc, GCC | gf);
371 head.h_cc = n_alternates_remove(head.h_cc, FAL0);
372 }else
373 head.h_to = np;
375 head.h_mailx_raw_to = namelist_dup(head.h_to, GTO | gf);
376 head.h_to = n_alternates_remove(head.h_to, FAL0);
377 #ifdef HAVE_DEVEL
378 for(np = head.h_to; np != NULL; np = np->n_flink)
379 assert((np->n_type & GMASK) == GTO);
380 for(np = head.h_cc; np != NULL; np = np->n_flink)
381 assert((np->n_type & GMASK) == GCC);
382 #endif
383 goto jrecipients_done;
386 /* Otherwise do the normal From: / To: / Cc: dance */
388 cp2 = n_header_senderfield_of(mp);
390 /* Cc: */
391 np = NULL;
392 if(ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
393 np = lextract(cp, GCC | gf);
394 if((cp = hfield1("cc", mp)) != NULL){
395 struct name *x;
397 if((x = lextract(cp, GCC | gf)) != NULL)
398 np = cat(np, x);
400 if(np != NULL){
401 head.h_mailx_raw_cc = namelist_dup(np, GCC | gf);
402 head.h_cc = n_alternates_remove(np, FAL0);
405 /* To: */
406 np = NULL;
407 if(cp2 != NULL)
408 np = lextract(cp2, GTO | gf);
409 if(!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL){
410 struct name *x;
412 if((x = lextract(cp, GTO | gf)) != NULL)
413 np = cat(np, x);
415 /* Delete my name from reply list, and with it, all my alternate names */
416 if(np != NULL){
417 head.h_mailx_raw_to = namelist_dup(np, GTO | gf);
418 np = n_alternates_remove(np, FAL0);
419 /* The user may have send this to himself, don't ignore that */
420 if(count(np) == 0){
421 np = lextract(cp2, GTO | gf);
422 head.h_mailx_raw_to = namelist_dup(np, GTO | gf);
425 head.h_to = np;
427 jrecipients_done:
429 /* For list replies we want to automatically recognize the list address
430 * given in the List-Post: header, so that we will not throw away a possible
431 * corresponding receiver: temporarily "`mlist' the List-Post: address" */
432 if((hf & HF_LIST_REPLY) && (cp = hfield1("list-post", mp)) != NULL){
433 struct name *x;
435 if((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
436 (cp = url_mailto_to_address(x->n_name)) == NULL ||
437 /* XXX terribly wasteful to create a new name, and can't we find
438 * XXX a way to mitigate that?? */
439 is_addr_invalid(x = nalloc(cp, GEXTRA | GSKIN), EACM_STRICT)){
440 if(n_poption & n_PO_D_V)
441 n_err(_("Message contains invalid List-Post: header\n"));
442 }else{
443 /* A special case has been seen on e.g. ietf-announce@ietf.org:
444 * these usually post to multiple groups, with ietf-announce@
445 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
446 * -announce@ is only used for announcements, say).
447 * So our desire is to honour this request and actively overwrite
448 * List-Post: for our purpose; but only if its a single address.
449 * However, to avoid ambiguities with users that place themselve in
450 * Reply-To: and mailing lists which don't overwrite this (or only
451 * extend this, shall such exist), only do so if reply_to exists of
452 * a single address which points to the same domain as List-Post: */
453 if(rt != NULL && rt->n_flink == NULL &&
454 name_is_same_domain(x, rt))
455 cp = rt->n_name; /* rt is EACM_STRICT tested */
456 else
457 cp = x->n_name;
459 /* XXX is_mlist_mp()?? */
460 if(is_mlist(cp, FAL0) == MLIST_OTHER)
461 head.h_list_post = cp;
465 /* In case of list replies we actively sort out any non-list recipient */
466 if(hf & HF_LIST_REPLY){
467 struct name **nhpp, *nhp, *tail;
469 cp = head.h_list_post;
471 nhp = *(nhpp = &head.h_to);
472 head.h_to = NULL;
473 j_lt_redo:
474 for(tail = NULL; nhp != NULL;){
475 np = nhp;
476 nhp = nhp->n_flink;
478 /* XXX is_mlist_mp()?? */
479 if((cp != NULL && !asccasecmp(cp, np->n_name)) ||
480 is_mlist(np->n_name, FAL0) != MLIST_OTHER){
481 if((np->n_blink = tail) != NULL)
482 tail->n_flink = np;
483 else
484 *nhpp = np;
485 np->n_flink = NULL;
486 tail = np;
489 if(nhpp == &head.h_to){
490 nhp = *(nhpp = &head.h_cc);
491 head.h_cc = NULL;
492 goto j_lt_redo;
495 /* For `Lreply' only, fail immediately with DESTADDRREQ if there are no
496 * receivers at all! */
497 if(head.h_to == NULL && head.h_cc == NULL){
498 n_err(_("No recipients specified for `Lreply'\n"));
499 if(msgvec[1] == 0){
500 n_pstate_err_no = n_ERR_DESTADDRREQ;
501 msgvec = NULL;
502 goto jleave;
504 goto jskip_to_next;
508 /* Move Cc: to To: as appropriate! */
509 if(head.h_to == NULL && (np = head.h_cc) != NULL){
510 head.h_cc = NULL;
511 for(head.h_to = np; np != NULL; np = np->n_flink)
512 np->n_type = (np->n_type & ~GMASK) | GTO;
515 a_crese_make_ref_and_cs(mp, &head);
517 if(mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), FAL0) != OKAY){
518 msgvec = NULL;
519 goto jleave;
521 if(ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
522 mp->m_flag |= MANSWER | MANSWERED;
523 n_autorec_relax_gut();
525 jskip_to_next:
526 if(*++msgvec != 0){
527 /* TODO message (error) ring.., less sleep */
528 if(n_psonce & n_PSO_INTERACTIVE){
529 fprintf(n_stdout,
530 _("Waiting a second before proceeding to the next message..\n"));
531 fflush(n_stdout);
532 n_msleep(1000, FAL0);
534 goto jwork_msg;
537 jleave:
538 NYD2_LEAVE;
539 return (msgvec == NULL);
542 static int
543 (*a_crese_reply_or_Reply(char c))(int *, bool_t){
544 int (*rv)(int*, bool_t);
545 NYD2_ENTER;
547 rv = (ok_blook(flipr) ^ (c == 'R')) ? &a_crese_Reply : &a_crese_reply;
548 NYD2_LEAVE;
549 return rv;
552 static int
553 a_crese_reply(int *msgvec, bool_t recipient_record){
554 int rv;
555 NYD2_ENTER;
557 rv = a_crese_list_reply(msgvec,
558 (recipient_record ? HF_RECIPIENT_RECORD : HF_NONE));
559 NYD2_LEAVE;
560 return rv;
563 static int
564 a_crese_Reply(int *msgvec, bool_t recipient_record){
565 struct header head;
566 struct message *mp;
567 int *ap;
568 enum gfield gf;
569 NYD2_ENTER;
571 memset(&head, 0, sizeof head);
572 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
574 for(ap = msgvec; *ap != 0; ++ap){
575 struct name *np;
577 mp = &message[*ap - 1];
578 touch(mp);
579 setdot(mp);
581 if((np = a_crese_reply_to(mp)) == NULL)
582 np = lextract(n_header_senderfield_of(mp), GTO | gf);
583 head.h_to = cat(head.h_to, np);
586 mp = &message[msgvec[0] - 1];
587 head.h_subject = hfield1("subject", mp);
588 head.h_subject = a_crese_reedit(head.h_subject);
589 a_crese_make_ref_and_cs(mp, &head);
590 head.h_mailx_command = "Reply";
591 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
592 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
593 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
594 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
596 if(ok_blook(recipients_in_cc)){
597 a_crese_polite_rt_mft_move(mp, &head, head.h_to);
599 head.h_mailx_raw_cc = namelist_dup(head.h_cc, GCC | gf);
600 head.h_cc = n_alternates_remove(head.h_cc, FAL0);
602 head.h_mailx_raw_to = namelist_dup(head.h_to, GTO | gf);
603 head.h_to = n_alternates_remove(head.h_to, FAL0);
605 if(ok_blook(quote_as_attachment)){
606 head.h_attach = n_autorec_calloc(1, sizeof *head.h_attach);
607 head.h_attach->a_msgno = *msgvec;
608 head.h_attach->a_content_description = _("Original message content");
611 if(mail1(&head, 1, mp, NULL, recipient_record, FAL0) != OKAY){
612 msgvec = NULL;
613 goto jleave;
616 if(ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
617 mp->m_flag |= MANSWER | MANSWERED;
618 jleave:
619 NYD2_LEAVE;
620 return (msgvec == NULL);
623 static int
624 a_crese_fwd(void *vp, bool_t recipient_record){
625 struct header head;
626 struct message *mp;
627 enum gfield gf;
628 bool_t forward_as_attachment;
629 int *msgvec, rv;
630 struct n_cmd_arg *cap;
631 struct n_cmd_arg_ctx *cacp;
632 NYD2_ENTER;
634 cacp = vp;
635 cap = cacp->cac_arg;
636 msgvec = cap->ca_arg.ca_msglist;
637 cap = cap->ca_next;
638 rv = 1;
640 if(cap->ca_arg.ca_str.s[0] == '\0'){
641 if(!(n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) || (n_poption & n_PO_D_V))
642 n_err(_("No recipient specified.\n"));
643 n_pstate_err_no = n_ERR_DESTADDRREQ;
644 goto jleave;
647 forward_as_attachment = ok_blook(forward_as_attachment);
648 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
650 memset(&head, 0, sizeof head);
651 head.h_to = lextract(cap->ca_arg.ca_str.s,
652 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)));
654 mp = &message[*msgvec - 1];
655 touch(mp);
656 setdot(mp);
657 head.h_subject = hfield1("subject", mp);
658 head.h_subject = a_crese__fwdedit(head.h_subject);
659 head.h_mailx_command = "forward";
660 head.h_mailx_raw_to = namelist_dup(head.h_to, GTO | gf);
661 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
662 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
663 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
664 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
666 if(forward_as_attachment){
667 head.h_attach = n_autorec_calloc(1, sizeof *head.h_attach);
668 head.h_attach->a_msgno = *msgvec;
669 head.h_attach->a_content_description = _("Forwarded message");
672 rv = (mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL,
673 recipient_record, TRU1) != OKAY); /* reverse! */
674 jleave:
675 NYD2_LEAVE;
676 return rv;
679 static char *
680 a_crese__fwdedit(char *subj){
681 struct str in, out;
682 char *newsubj;
683 NYD2_ENTER;
685 newsubj = NULL;
687 if(subj == NULL || *subj == '\0')
688 goto jleave;
690 in.s = subj;
691 in.l = strlen(subj);
692 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
694 newsubj = n_autorec_alloc(out.l + 6);
695 if(!ascncasecmp(out.s, "Fwd: ", sizeof("Fwd: ") -1)) /* TODO EXTEND SUPP.. */
696 memcpy(newsubj, out.s, out.l +1);
697 else{
698 memcpy(newsubj, "Fwd: ", 5); /* TODO ..a la subject_re_trim()! */
699 memcpy(&newsubj[5], out.s, out.l +1);
702 n_free(out.s);
703 jleave:
704 NYD2_LEAVE;
705 return newsubj;
708 static int
709 a_crese_resend1(void *vp, bool_t add_resent){
710 struct header head;
711 struct name *myto, *myrawto;
712 enum gfield gf;
713 int *msgvec, rv, *ip;
714 struct n_cmd_arg *cap;
715 struct n_cmd_arg_ctx *cacp;
716 NYD2_ENTER;
718 cacp = vp;
719 cap = cacp->cac_arg;
720 msgvec = cap->ca_arg.ca_msglist;
721 cap = cap->ca_next;
722 rv = 1;
723 n_pstate_err_no = n_ERR_DESTADDRREQ;
725 if(cap->ca_arg.ca_str.s[0] == '\0'){
726 if(!(n_pstate & (n_PS_HOOK_MASK | n_PS_ROBOT)) || (n_poption & n_PO_D_V))
727 n_err(_("No recipient specified.\n"));
728 goto jleave;
731 gf = ok_blook(fullnames) ? GFULL | GSKIN : GSKIN;
733 myrawto = nalloc(cap->ca_arg.ca_str.s, GTO | gf);
734 myto = usermap(namelist_dup(myrawto, myrawto->n_type), FAL0);
735 myto = n_alternates_remove(myto, TRU1);
736 if(myto == NULL)
737 goto jleave;
739 n_autorec_relax_create();
740 for(ip = msgvec; *ip != 0; ++ip){
741 struct message *mp;
743 mp = &message[*ip - 1];
744 touch(mp);
745 setdot(mp);
747 memset(&head, 0, sizeof head);
748 head.h_to = myto;
749 head.h_mailx_command = "resend";
750 head.h_mailx_raw_to = myrawto;
751 head.h_mailx_orig_from = lextract(hfield1("from", mp), GIDENT | gf);
752 head.h_mailx_orig_to = lextract(hfield1("to", mp), GTO | gf);
753 head.h_mailx_orig_cc = lextract(hfield1("cc", mp), GCC | gf);
754 head.h_mailx_orig_bcc = lextract(hfield1("bcc", mp), GBCC | gf);
756 if(resend_msg(mp, &head, add_resent) != OKAY){
757 /* n_autorec_relax_gut(); XXX but is handled automatically? */
758 goto jleave;
760 n_autorec_relax_unroll();
762 n_autorec_relax_gut();
764 n_pstate_err_no = n_ERR_NONE;
765 rv = 0;
766 jleave:
767 NYD2_LEAVE;
768 return rv;
771 FL int
772 c_reply(void *vp){
773 int rv;
774 NYD_ENTER;
776 rv = (*a_crese_reply_or_Reply('r'))(vp, FAL0);
777 NYD_LEAVE;
778 return rv;
781 FL int
782 c_replyall(void *vp){
783 int rv;
784 NYD_ENTER;
786 rv = a_crese_reply(vp, FAL0);
787 NYD_LEAVE;
788 return rv;
791 FL int
792 c_replysender(void *vp){
793 int rv;
794 NYD_ENTER;
796 rv = a_crese_Reply(vp, FAL0);
797 NYD_LEAVE;
798 return rv;
801 FL int
802 c_Reply(void *vp){
803 int rv;
804 NYD_ENTER;
806 rv = (*a_crese_reply_or_Reply('R'))(vp, FAL0);
807 NYD_LEAVE;
808 return rv;
811 FL int
812 c_Lreply(void *vp){
813 int rv;
814 NYD_ENTER;
816 rv = a_crese_list_reply(vp, HF_LIST_REPLY);
817 NYD_LEAVE;
818 return rv;
821 FL int
822 c_followup(void *vp){
823 int rv;
824 NYD_ENTER;
826 rv = (*a_crese_reply_or_Reply('r'))(vp, TRU1);
827 NYD_LEAVE;
828 return rv;
831 FL int
832 c_followupall(void *vp){
833 int rv;
834 NYD_ENTER;
836 rv = a_crese_reply(vp, TRU1);
837 NYD_LEAVE;
838 return rv;
841 FL int
842 c_followupsender(void *vp){
843 int rv;
844 NYD_ENTER;
846 rv = a_crese_Reply(vp, TRU1);
847 NYD_LEAVE;
848 return rv;
851 FL int
852 c_Followup(void *vp){
853 int rv;
854 NYD_ENTER;
856 rv = (*a_crese_reply_or_Reply('R'))(vp, TRU1);
857 NYD_LEAVE;
858 return rv;
861 FL int
862 c_forward(void *vp){
863 int rv;
864 NYD_ENTER;
866 rv = a_crese_fwd(vp, FAL0);
867 NYD_LEAVE;
868 return rv;
871 FL int
872 c_Forward(void *vp){
873 int rv;
874 NYD_ENTER;
876 rv = a_crese_fwd(vp, TRU1);
877 NYD_LEAVE;
878 return rv;
881 FL int
882 c_resend(void *vp){
883 int rv;
884 NYD_ENTER;
886 rv = a_crese_resend1(vp, TRU1);
887 NYD_LEAVE;
888 return rv;
891 FL int
892 c_Resend(void *vp){
893 int rv;
894 NYD_ENTER;
896 rv = a_crese_resend1(vp, FAL0);
897 NYD_LEAVE;
898 return rv;
901 /* s-it-mode */