use a semi-shallo copy of the message to add own stuff without changing the original struct message msg.

MIME-13
Roker 3 years ago
parent d63610178c
commit 4b3b9d323b

@ -84,7 +84,61 @@ namespace pEpMIME
return o;
}
}
// a shallow copy of pEpEngine's struct message with own opt_fields
class MessageCopy
{
public:
MessageCopy(const message* _msg_orig)
: msg_orig(_msg_orig)
, msg_copy(*msg_orig)
{
if(msg_orig->opt_fields)
{
msg_copy.opt_fields = stringpair_list_dup(msg_orig->opt_fields);
}
}
void add_header(sv key, sv value)
{
stringpair_t* sp = new_stringpair(key.data(), value.data());
if(sp==nullptr)
{
throw std::runtime_error("Out of memory. new_stringpair() in add_header() fails.");
}
auto success = stringpair_list_add(msg_copy.opt_fields, sp);
if(!success)
{
free_stringpair(sp);
throw std::runtime_error("Out of memory. stringpair_list_add() in add_header() fails.");
}
// there were no opt_fields, yet? So set them to the "last" (and only) element:
if(msg_copy.opt_fields == nullptr)
{
msg_copy.opt_fields = success;
}
}
// Neither copy nor move!
MessageCopy(const MessageCopy&) = delete;
MessageCopy(MessageCopy&&) = delete;
~MessageCopy()
{
free_stringpair_list(msg_copy.opt_fields);
msg_copy.opt_fields = nullptr;
}
operator const message* () const { return &msg_copy; }
const message* operator->() const { return &msg_copy; }
private:
const message* msg_orig;
message msg_copy;
};
} // end of anonymous namespace
template<class LineEnding>
@ -153,12 +207,18 @@ message* parse_message(const char* mime_text, size_t length, bool* raise_attachm
}
char* generate_message(const message* msg, bool omit_fields, bool has_pEp_msg_attachment)
char* generate_message(const message* msg_orig, bool omit_fields, bool has_pEp_msg_attachment)
{
if(msg == nullptr)
if(msg_orig == nullptr)
return nullptr;
LOG << "GEN_MSG omit=" << omit_fields << ", has_pEp_msg_att=" << has_pEp_msg_attachment << std::endl;
MessageCopy msg{msg_orig};
if(has_pEp_msg_attachment)
{
msg.add_header(Pseudo_Header_Forwarded, "no");
}
const auto attachments = parse_attachments(msg->attachments);
const unsigned inline_attachments = std::count_if(
@ -206,7 +266,7 @@ char* generate_message(const message* msg, bool omit_fields, bool has_pEp_msg_at
default:
throw std::logic_error("Determinant ouf of range 0...15: det=" + std::to_string(det) );
}
return new_string( smsg.data(), smsg.size() ); // make a C-compatible copy allocated by the "right" allocator. *sigh*
}

Loading…
Cancel
Save