From: Richard Russon Date: Tue, 8 Jan 2019 18:21:02 +0000 (+0000) Subject: bounce_message: factor out Context X-Git-Tag: 2019-10-25~373^2~19 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8baccedb2e39e39108a0bf5012deb20b3dc2da13;p=neomutt bounce_message: factor out Context --- diff --git a/commands.c b/commands.c index cb3ea615b..53fd3c2c9 100644 --- a/commands.c +++ b/commands.c @@ -363,48 +363,34 @@ int mutt_display_message(struct Email *cur) /** * ci_bounce_message - Bounce an email - * @param e Email to bounce + * @param m Mailbox + * @param el List of Emails to bounce */ -void ci_bounce_message(struct Email *e) +void ci_bounce_message(struct Mailbox *m, struct EmailList *el) { + if (!m || !el || STAILQ_EMPTY(el)) + return; + char prompt[SHORT_STRING]; char scratch[SHORT_STRING]; char buf[HUGE_STRING] = { 0 }; struct Address *addr = NULL; char *err = NULL; int rc; - int msgcount; // for L10N with ngettext + int msg_count = 0; - /* RFC5322 mandates a From: header, so warn before bouncing - * messages without one */ - if (e) + struct EmailNode *en = NULL; + STAILQ_FOREACH(en, el, entries) { - msgcount = 1; - if (!e->env->from) - { + /* RFC5322 mandates a From: header, + * so warn before bouncing messages without one */ + if (!en->email->env->from) mutt_error(_("Warning: message contains no From: header")); - } - } - else if (Context) - { - msgcount = 0; // count the precise number of messages. - for (rc = 0; rc < Context->mailbox->msg_count; rc++) - { - if (message_is_tagged(Context, rc) && !Context->mailbox->emails[rc]->env->from) - { - msgcount++; - if (!Context->mailbox->emails[rc]->env->from) - { - mutt_error(_("Warning: message contains no From: header")); - break; - } - } - } + + msg_count++; } - else - msgcount = 0; - if (e) + if (msg_count == 1) mutt_str_strfcpy(prompt, _("Bounce message to: "), sizeof(prompt)); else mutt_str_strfcpy(prompt, _("Bounce tagged messages to: "), sizeof(prompt)); @@ -435,7 +421,7 @@ void ci_bounce_message(struct Email *e) #define EXTRA_SPACE (15 + 7 + 2) snprintf(scratch, sizeof(scratch), - ngettext("Bounce message to %s", "Bounce messages to %s", msgcount), buf); + ngettext("Bounce message to %s", "Bounce messages to %s", msg_count), buf); if (mutt_strwidth(prompt) > MuttMessageWindow->cols - EXTRA_SPACE) { @@ -450,17 +436,33 @@ void ci_bounce_message(struct Email *e) { mutt_addr_free(&addr); mutt_window_clearline(MuttMessageWindow, 0); - mutt_message(ngettext("Message not bounced", "Messages not bounced", msgcount)); + mutt_message(ngettext("Message not bounced", "Messages not bounced", msg_count)); return; } mutt_window_clearline(MuttMessageWindow, 0); - rc = mutt_bounce_message(NULL, e, addr); + struct Message *msg = NULL; + STAILQ_FOREACH(en, el, entries) + { + msg = mx_msg_open(m, en->email->msgno); + if (!msg) + { + rc = -1; + break; + } + + rc = mutt_bounce_message(msg->fp, en->email, addr); + mx_msg_close(m, &msg); + + if (rc < 0) + break; + } + mutt_addr_free(&addr); /* If no error, or background, display message. */ if ((rc == 0) || (rc == S_BKG)) - mutt_message(ngettext("Message bounced", "Messages bounced", msgcount)); + mutt_message(ngettext("Message bounced", "Messages bounced", msg_count)); } /** @@ -490,7 +492,7 @@ static void pipe_set_flags(bool decode, bool print, int *cmflags, int *chflags) /** * pipe_msg - Pipe a message - * @param e Email + * @param e Email to pipe * @param fp File to write to * @param decode If true, decode the message * @param print If true, message is for printing diff --git a/commands.h b/commands.h index 6d18b0969..d6ecf2224 100644 --- a/commands.h +++ b/commands.h @@ -41,7 +41,7 @@ extern bool PrintDecode; extern bool PrintSplit; extern bool PromptAfter; -void ci_bounce_message(struct Email *e); +void ci_bounce_message(struct Mailbox *m, struct EmailList *el); void mutt_check_stats(void); bool mutt_check_traditional_pgp(struct Email *e, int *redraw); void mutt_display_address(struct Envelope *env); diff --git a/index.c b/index.c index 20cf45d64..4d82406d0 100644 --- a/index.c +++ b/index.c @@ -2919,12 +2919,17 @@ int mutt_index_menu(void) */ case OP_BOUNCE_MESSAGE: - + { CHECK_ATTACH; CHECK_MSGCOUNT; CHECK_VISIBLE; - ci_bounce_message(tag ? NULL : CUR_EMAIL); + + struct EmailList el = STAILQ_HEAD_INITIALIZER(el); + el_add_tagged(&el, Context, CUR_EMAIL, tag); + ci_bounce_message(Context->mailbox, &el); + el_free(&el); break; + } case OP_CREATE_ALIAS: diff --git a/pager.c b/pager.c index 65f1f7307..16ba39169 100644 --- a/pager.c +++ b/pager.c @@ -2930,13 +2930,21 @@ int mutt_pager(const char *banner, const char *fname, int flags, struct Pager *e */ case OP_BOUNCE_MESSAGE: + { + struct Mailbox *m = Context ? Context->mailbox : NULL; CHECK_MODE(IsEmail(extra) || IsMsgAttach(extra)) CHECK_ATTACH; if (IsMsgAttach(extra)) - mutt_attach_bounce(extra->fp, extra->actx, extra->bdy); + mutt_attach_bounce(m, extra->fp, extra->actx, extra->bdy); else - ci_bounce_message(extra->email); + { + struct EmailList el = STAILQ_HEAD_INITIALIZER(el); + el_add_email(&el, extra->email); + ci_bounce_message(m, &el); + el_free(&el); + } break; + } case OP_RESEND: CHECK_MODE(IsEmail(extra) || IsMsgAttach(extra)) diff --git a/recvattach.c b/recvattach.c index dc102a9f9..e8c3992a6 100644 --- a/recvattach.c +++ b/recvattach.c @@ -1526,7 +1526,7 @@ void mutt_view_attachments(struct Email *e) case OP_BOUNCE_MESSAGE: CHECK_ATTACH; - mutt_attach_bounce(CURATTACH->fp, actx, + mutt_attach_bounce(m, CURATTACH->fp, actx, menu->tagprefix ? NULL : CURATTACH->content); menu->redraw = REDRAW_FULL; break; diff --git a/recvcmd.c b/recvcmd.c index 6c632fa52..674113938 100644 --- a/recvcmd.c +++ b/recvcmd.c @@ -156,12 +156,16 @@ static short count_tagged_children(struct AttachCtx *actx, short i) /** * mutt_attach_bounce - Bounce function, from the attachment menu + * @param m Mailbox * @param fp Handle of message * @param actx Attachment context * @param cur Body of email */ -void mutt_attach_bounce(FILE *fp, struct AttachCtx *actx, struct Body *cur) +void mutt_attach_bounce(struct Mailbox *m, FILE *fp, struct AttachCtx *actx, struct Body *cur) { + if (!m || !fp || !actx) + return; + char prompt[STRING]; char buf[HUGE_STRING]; char *err = NULL; diff --git a/recvcmd.h b/recvcmd.h index 08a47464d..99d8aa773 100644 --- a/recvcmd.h +++ b/recvcmd.h @@ -32,7 +32,7 @@ struct Email; /* These Config Variables are only used in recvcmd.c */ extern unsigned char MimeForwardRest; -void mutt_attach_bounce(FILE *fp, struct AttachCtx *actx, struct Body *cur); +void mutt_attach_bounce(struct Mailbox *m, FILE *fp, struct AttachCtx *actx, struct Body *cur); void mutt_attach_resend(FILE *fp, struct AttachCtx *actx, struct Body *cur); void mutt_attach_forward(FILE *fp, struct Email *e, struct AttachCtx *actx, struct Body *cur, int flags); void mutt_attach_reply(FILE *fp, struct Email *e, struct AttachCtx *actx, struct Body *cur, int flags); diff --git a/sendlib.c b/sendlib.c index 42094d674..6dfd20135 100644 --- a/sendlib.c +++ b/sendlib.c @@ -2956,34 +2956,18 @@ void mutt_unprepare_envelope(struct Envelope *env) static int bounce_message(FILE *fp, struct Email *e, struct Address *to, const char *resent_from, struct Address *env_from) { - int rc = 0; - FILE *f = NULL; - char tempfile[PATH_MAX]; - struct Message *msg = NULL; - if (!e) - { - /* Try to bounce each message out, aborting if we get any failures. */ - for (int i = 0; i < Context->mailbox->msg_count; i++) - if (message_is_tagged(Context, i)) - rc |= bounce_message(fp, Context->mailbox->emails[i], to, resent_from, env_from); - return rc; - } - - /* If we failed to open a message, return with error */ - if (!fp && !(msg = mx_msg_open(Context->mailbox, e->msgno))) return -1; - if (!fp) - fp = msg->fp; + int rc = 0; + char tempfile[PATH_MAX]; mutt_mktemp(tempfile, sizeof(tempfile)); - f = mutt_file_fopen(tempfile, "w"); + FILE *f = mutt_file_fopen(tempfile, "w"); if (f) { char date[SHORT_STRING]; int ch_flags = CH_XMIT | CH_NONEWLINE | CH_NOQFROM; - char *msgid_str = NULL; if (!BounceDelivered) ch_flags |= CH_WEED_DELIVERED; @@ -2991,14 +2975,14 @@ static int bounce_message(FILE *fp, struct Email *e, struct Address *to, fseeko(fp, e->offset, SEEK_SET); fprintf(f, "Resent-From: %s", resent_from); fprintf(f, "\nResent-%s", mutt_date_make_date(date, sizeof(date))); - msgid_str = gen_msgid(); + char *msgid_str = gen_msgid(); fprintf(f, "Resent-Message-ID: %s\n", msgid_str); + FREE(&msgid_str); fputs("Resent-To: ", f); mutt_write_address_list(to, f, 11, 0); mutt_copy_header(fp, e, f, ch_flags, NULL); fputc('\n', f); mutt_file_copy_bytes(fp, f, e->content->length); - FREE(&msgid_str); if (mutt_file_fclose(&f) != 0) { mutt_perror(tempfile); @@ -3009,14 +2993,11 @@ static int bounce_message(FILE *fp, struct Email *e, struct Address *to, if (SmtpUrl) rc = mutt_smtp_send(env_from, to, NULL, NULL, tempfile, e->content->encoding == ENC_8BIT); else -#endif /* USE_SMTP */ +#endif rc = mutt_invoke_sendmail(env_from, to, NULL, NULL, tempfile, e->content->encoding == ENC_8BIT); } - if (msg) - mx_msg_close(Context->mailbox, &msg); - return rc; } @@ -3030,6 +3011,9 @@ static int bounce_message(FILE *fp, struct Email *e, struct Address *to, */ int mutt_bounce_message(FILE *fp, struct Email *e, struct Address *to) { + if (!fp || !e || !to) + return -1; + const char *fqdn = mutt_fqdn(true); char resent_from[STRING]; char *err = NULL;