Rudely fix the RFC 5322 dot-atom, the "Dr. Problem" (Dr. Werner Fink)
[s-mailx.git] / cmd_resend.c
blob055c094a4a683678b6678d650039fcb9c7756ecf
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 - 2016 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 char *cpo, c;
152 head->h_charset = cp = mime_param_get("charset", cp);
153 for(cpo = cp; (c = *cpo) != '\0'; ++cpo)
154 *cpo = lowerconv(c);
156 jleave:
157 NYD_LEAVE;
160 static int
161 _list_reply(int *msgvec, enum header_flags hf)
163 struct header head;
164 struct message *mp;
165 char const *reply_to, *rcv, *cp;
166 enum gfield gf;
167 struct name *rt, *mft, *np;
168 int *save_msgvec;
169 NYD_ENTER;
171 /* TODO Since we may recur and do stuff with message lists we need to save
172 * TODO away the argument vector as long as that isn't done by machinery */
174 size_t i;
175 for (i = 0; msgvec[i] != 0; ++i)
177 ++i;
178 save_msgvec = ac_alloc(sizeof(*save_msgvec) * i);
179 while (i-- > 0)
180 save_msgvec[i] = msgvec[i];
181 msgvec = save_msgvec;
184 jnext_msg:
185 mp = message + *msgvec - 1;
186 touch(mp);
187 setdot(mp);
189 memset(&head, 0, sizeof head);
190 head.h_flags = hf;
192 head.h_subject = _reedit(hfield1("subject", mp));
193 gf = ok_blook(fullnames) ? GFULL : GSKIN;
194 rt = mft = NULL;
196 rcv = NULL;
197 if ((reply_to = hfield1("reply-to", mp)) != NULL &&
198 (cp = ok_vlook(reply_to_honour)) != NULL &&
199 (rt = checkaddrs(lextract(reply_to, GTO | gf), EACM_STRICT, NULL)
200 ) != NULL) {
201 char const *tr = _("Reply-To %s%s");
202 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
203 char *sp = salloc(l);
205 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..." : n_empty));
206 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0)
207 rcv = reply_to;
210 if (rcv == NULL && (rcv = hfield1("from", mp)) == NULL)
211 rcv = nameof(mp, 1);
213 /* Cc: */
214 np = NULL;
215 if (ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
216 np = lextract(cp, GCC | gf);
217 if ((cp = hfield1("cc", mp)) != NULL)
218 np = cat(np, lextract(cp, GCC | gf));
219 if (np != NULL)
220 head.h_cc = delete_alternates(np);
222 /* To: */
223 np = NULL;
224 if (rcv != NULL)
225 np = (rcv == reply_to) ? namelist_dup(rt, GTO | gf)
226 : lextract(rcv, GTO | gf);
227 if (!ok_blook(recipients_in_cc) && (cp = hfield1("to", mp)) != NULL)
228 np = cat(np, lextract(cp, GTO | gf));
229 /* Delete my name from reply list, and with it, all my alternate names */
230 np = delete_alternates(np);
231 if (count(np) == 0)
232 np = lextract(rcv, GTO | gf);
233 head.h_to = np;
235 /* The user may have send this to himself, don't ignore that */
236 namelist_vaporise_head(&head, EACM_NORMAL, FAL0, NULL);
237 if (head.h_to == NULL)
238 head.h_to = np;
240 /* Mail-Followup-To: */
241 mft = NULL;
242 if (ok_vlook(followup_to_honour) != NULL &&
243 (cp = hfield1("mail-followup-to", mp)) != NULL &&
244 (mft = np = checkaddrs(lextract(cp, GTO | gf), EACM_STRICT, NULL)
245 ) != NULL) {
246 char const *tr = _("Followup-To %s%s");
247 size_t l = strlen(tr) + strlen(np->n_name) + 3 +1;
248 char *sp = salloc(l);
250 snprintf(sp, l, tr, np->n_name, (np->n_flink != NULL ? "..." : n_empty));
251 if (quadify(ok_vlook(followup_to_honour), UIZ_MAX, sp, TRU1) > FAL0) {
252 head.h_cc = NULL;
253 head.h_to = np;
254 head.h_mft =
255 mft = namelist_vaporise_head(&head, EACM_STRICT, FAL0, NULL);
256 } else
257 mft = NULL;
260 /* Special massage for list (follow-up) messages */
261 if (mft != NULL || (hf & HF_LIST_REPLY) || ok_blook(followup_to)) {
262 /* Learn about a possibly sending mailing list; use do for break; */
263 if ((cp = hfield1("list-post", mp)) != NULL) do {
264 struct name *x;
266 if ((x = lextract(cp, GEXTRA | GSKIN)) == NULL || x->n_flink != NULL ||
267 (cp = url_mailto_to_address(x->n_name)) == NULL ||
268 /* XXX terribly wasteful to create a new name, and can't we find
269 * XXX a way to mitigate that?? */
270 is_addr_invalid(x = nalloc(cp, GEXTRA | GSKIN), EACM_STRICT)) {
271 if (options & OPT_D_V)
272 n_err(_("Message contains invalid List-Post: header\n"));
273 cp = NULL;
274 break;
276 cp = x->n_name;
278 /* A special case has been seen on e.g. ietf-announce@ietf.org:
279 * these usually post to multiple groups, with ietf-announce@
280 * in List-Post:, but with Reply-To: set to ietf@ietf.org (since
281 * -announce@ is only used for announcements, say).
282 * So our desire is to honour this request and actively overwrite
283 * List-Post: for our purpose; but only if its a single address.
284 * However, to avoid ambiguities with users that place themselve in
285 * Reply-To: and mailing lists which don't overwrite this (or only
286 * extend this, shall such exist), only do so if reply_to exists of
287 * a single address which points to the same domain as List-Post: */
288 if (reply_to != NULL && rt->n_flink == NULL &&
289 name_is_same_domain(x, rt))
290 cp = rt->n_name; /* rt is EACM_STRICT tested */
292 /* "Automatically `mlist'" the List-Post: address temporarily */
293 if (is_mlist(cp, FAL0) == MLIST_OTHER)
294 head.h_list_post = cp;
295 else
296 cp = NULL;
297 } while (0);
299 /* In case of list replies we actively sort out any non-list recipient,
300 * but _only_ if we did not honour a MFT:, assuming that members of MFT
301 * were there for a reason; cp is still List-Post:/eqivalent */
302 if ((hf & HF_LIST_REPLY) && mft == NULL) {
303 struct name *nhp = head.h_to;
304 head.h_to = NULL;
305 j_lt_redo:
306 while (nhp != NULL) {
307 np = nhp;
308 nhp = nhp->n_flink;
310 if ((cp != NULL && !asccasecmp(cp, np->n_name)) ||
311 is_mlist(np->n_name, FAL0) != MLIST_OTHER) {
312 np->n_type = (np->n_type & ~GMASK) | GTO;
313 np->n_flink = head.h_to;
314 head.h_to = np;
317 if ((nhp = head.h_cc) != NULL) {
318 head.h_cc = NULL;
319 goto j_lt_redo;
324 make_ref_and_cs(mp, &head);
326 if (ok_blook(quote_as_attachment)) {
327 head.h_attach = csalloc(1, sizeof *head.h_attach);
328 head.h_attach->a_msgno = *msgvec;
329 head.h_attach->a_content_description = _("Original message content");
332 if (mail1(&head, 1, mp, NULL, !!(hf & HF_RECIPIENT_RECORD), 0) == OKAY &&
333 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
334 mp->m_flag |= MANSWER | MANSWERED;
336 if (*++msgvec != 0) {
337 /* TODO message (error) ring.., less sleep */
338 printf(_("Waiting a second before proceeding to the next message..\n"));
339 fflush(stdout);
340 n_msleep(1000, FAL0);
341 goto jnext_msg;
344 ac_free(save_msgvec);
345 NYD_LEAVE;
346 return 0;
349 static int
350 (*_reply_or_Reply(char c))(int *, bool_t)
352 int (*rv)(int*, bool_t);
353 NYD_ENTER;
355 rv = (ok_blook(flipr) ^ (c == 'R')) ? &_Reply : &_reply;
356 NYD_LEAVE;
357 return rv;
360 static int
361 _reply(int *msgvec, bool_t recipient_record)
363 int rv;
364 NYD_ENTER;
366 rv = _list_reply(msgvec, recipient_record ? HF_RECIPIENT_RECORD : HF_NONE);
367 NYD_LEAVE;
368 return rv;
371 static int
372 _Reply(int *msgvec, bool_t recipient_record)
374 struct header head;
375 struct message *mp;
376 int *ap;
377 char *cp;
378 enum gfield gf;
379 NYD_ENTER;
381 memset(&head, 0, sizeof head);
382 gf = ok_blook(fullnames) ? GFULL : GSKIN;
384 for (ap = msgvec; *ap != 0; ++ap) {
385 char const *rp;
386 struct name *rt;
388 mp = message + *ap - 1;
389 touch(mp);
390 setdot(mp);
392 if ((rp = hfield1("reply-to", mp)) != NULL &&
393 (cp = ok_vlook(reply_to_honour)) != NULL &&
394 (rt = checkaddrs(lextract(rp, GTO | gf), EACM_STRICT, NULL)
395 ) != NULL) {
396 char const *tr = _("Reply-To %s%s");
397 size_t l = strlen(tr) + strlen(rt->n_name) + 3 +1;
398 char *sp = salloc(l);
400 snprintf(sp, l, tr, rt->n_name, (rt->n_flink != NULL ? "..."
401 : n_empty));
402 if (quadify(cp, UIZ_MAX, sp, TRU1) > FAL0) {
403 head.h_to = cat(head.h_to, rt);
404 continue;
408 if ((cp = hfield1("from", mp)) == NULL)
409 cp = nameof(mp, 2);
410 head.h_to = cat(head.h_to, lextract(cp, GTO | gf));
412 if (head.h_to == NULL)
413 goto jleave;
415 mp = message + msgvec[0] - 1;
416 head.h_subject = hfield1("subject", mp);
417 head.h_subject = _reedit(head.h_subject);
418 make_ref_and_cs(mp, &head);
420 if (ok_blook(quote_as_attachment)) {
421 head.h_attach = csalloc(1, sizeof *head.h_attach);
422 head.h_attach->a_msgno = *msgvec;
423 head.h_attach->a_content_description = _("Original message content");
426 if (mail1(&head, 1, mp, NULL, recipient_record, 0) == OKAY &&
427 ok_blook(markanswered) && !(mp->m_flag & MANSWERED))
428 mp->m_flag |= MANSWER | MANSWERED;
429 jleave:
430 NYD_LEAVE;
431 return 0;
434 static int
435 _fwd(char *str, int recipient_record)
437 struct header head;
438 int *msgvec, rv = 1;
439 char *recipient;
440 struct message *mp;
441 bool_t f, forward_as_attachment;
442 NYD_ENTER;
444 if ((recipient = laststring(str, &f, TRU1)) == NULL) {
445 puts(_("No recipient specified."));
446 goto jleave;
449 forward_as_attachment = ok_blook(forward_as_attachment);
450 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
452 if (!f) {
453 *msgvec = first(0, MMNORM);
454 if (*msgvec == 0) {
455 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
456 rv = 0;
457 goto jleave;
459 printf(_("No messages to forward.\n"));
460 goto jleave;
462 msgvec[1] = 0;
463 } else if (getmsglist(str, msgvec, 0) < 0)
464 goto jleave;
466 if (*msgvec == 0) {
467 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
468 rv = 0;
469 goto jleave;
471 printf(_("No applicable messages.\n"));
472 goto jleave;
474 if (msgvec[1] != 0) {
475 printf(_("Cannot forward multiple messages at once\n"));
476 goto jleave;
479 memset(&head, 0, sizeof head);
480 if ((head.h_to = lextract(recipient,
481 (GTO | (ok_blook(fullnames) ? GFULL : GSKIN)))) == NULL)
482 goto jleave;
484 mp = message + *msgvec - 1;
486 if (forward_as_attachment) {
487 head.h_attach = csalloc(1, sizeof *head.h_attach);
488 head.h_attach->a_msgno = *msgvec;
489 head.h_attach->a_content_description = _("Forwarded message");
490 } else {
491 touch(mp);
492 setdot(mp);
494 head.h_subject = hfield1("subject", mp);
495 head.h_subject = __fwdedit(head.h_subject);
496 mail1(&head, 1, (forward_as_attachment ? NULL : mp), NULL, recipient_record,
498 rv = 0;
499 jleave:
500 NYD_LEAVE;
501 return rv;
504 static char *
505 __fwdedit(char *subj)
507 struct str in, out;
508 char *newsubj = NULL;
509 NYD_ENTER;
511 if (subj == NULL || *subj == '\0')
512 goto jleave;
514 in.s = subj;
515 in.l = strlen(subj);
516 mime_fromhdr(&in, &out, TD_ISPR | TD_ICONV);
518 newsubj = salloc(out.l + 6);
519 memcpy(newsubj, "Fwd: ", 5); /* XXX localizable */
520 memcpy(newsubj + 5, out.s, out.l +1);
521 free(out.s);
522 jleave:
523 NYD_LEAVE;
524 return newsubj;
527 static int
528 _resend1(void *v, bool_t add_resent)
530 char *name, *str;
531 struct name *to, *sn;
532 int *ip, *msgvec;
533 bool_t f = TRU1;
534 NYD_ENTER;
536 str = v;
537 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
538 name = laststring(str, &f, TRU1);
539 if (name == NULL) {
540 puts(_("No recipient specified."));
541 goto jleave;
544 if (!f) {
545 *msgvec = first(0, MMNORM);
546 if (*msgvec == 0) {
547 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
548 f = FAL0;
549 goto jleave;
551 puts(_("No applicable messages."));
552 goto jleave;
554 msgvec[1] = 0;
555 } else if (getmsglist(str, msgvec, 0) < 0)
556 goto jleave;
558 if (*msgvec == 0) {
559 if (pstate & (PS_HOOK_MASK | PS_ROBOT)) {
560 f = FAL0;
561 goto jleave;
563 printf("No applicable messages.\n");
564 goto jleave;
567 sn = nalloc(name, GTO | GSKIN);
568 to = usermap(sn, FAL0);
569 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
570 ++ip)
571 if (resend_msg(message + *ip - 1, to, add_resent) != OKAY)
572 goto jleave;
573 f = FAL0;
574 jleave:
575 NYD_LEAVE;
576 return (f != FAL0);
579 FL int
580 c_reply(void *v)
582 int rv;
583 NYD_ENTER;
585 rv = (*_reply_or_Reply('r'))(v, FAL0);
586 NYD_LEAVE;
587 return rv;
590 FL int
591 c_replyall(void *v)
593 int rv;
594 NYD_ENTER;
596 rv = _reply(v, FAL0);
597 NYD_LEAVE;
598 return rv;
601 FL int
602 c_replysender(void *v)
604 int rv;
605 NYD_ENTER;
607 rv = _Reply(v, FAL0);
608 NYD_LEAVE;
609 return rv;
612 FL int
613 c_Reply(void *v)
615 int rv;
616 NYD_ENTER;
618 rv = (*_reply_or_Reply('R'))(v, FAL0);
619 NYD_LEAVE;
620 return rv;
623 FL int
624 c_Lreply(void *v)
626 int rv;
627 NYD_ENTER;
629 rv = _list_reply(v, HF_LIST_REPLY);
630 NYD_LEAVE;
631 return rv;
634 FL int
635 c_followup(void *v)
637 int rv;
638 NYD_ENTER;
640 rv = (*_reply_or_Reply('r'))(v, TRU1);
641 NYD_LEAVE;
642 return rv;
645 FL int
646 c_followupall(void *v)
648 int rv;
649 NYD_ENTER;
651 rv = _reply(v, TRU1);
652 NYD_LEAVE;
653 return rv;
656 FL int
657 c_followupsender(void *v)
659 int rv;
660 NYD_ENTER;
662 rv = _Reply(v, TRU1);
663 NYD_LEAVE;
664 return rv;
667 FL int
668 c_Followup(void *v)
670 int rv;
671 NYD_ENTER;
673 rv = (*_reply_or_Reply('R'))(v, TRU1);
674 NYD_LEAVE;
675 return rv;
678 FL int
679 c_forward(void *v)
681 int rv;
682 NYD_ENTER;
684 rv = _fwd(v, 0);
685 NYD_LEAVE;
686 return rv;
689 FL int
690 c_Forward(void *v)
692 int rv;
693 NYD_ENTER;
695 rv = _fwd(v, 1);
696 NYD_LEAVE;
697 return rv;
700 FL int
701 c_resend(void *v)
703 int rv;
704 NYD_ENTER;
706 rv = _resend1(v, TRU1);
707 NYD_LEAVE;
708 return rv;
711 FL int
712 c_Resend(void *v)
714 int rv;
715 NYD_ENTER;
717 rv = _resend1(v, FAL0);
718 NYD_LEAVE;
719 return rv;
722 /* s-it-mode */