Browse Source

some MIME parsing and composing

PYADPT-55
Volker Birk 5 years ago
parent
commit
8981f4e741
3 changed files with 67 additions and 0 deletions
  1. +62
    -0
      src/message.cc
  2. +3
    -0
      src/message.hh
  3. +2
    -0
      src/pEpmodule.cc

+ 62
- 0
src/message.cc View File

@ -4,6 +4,7 @@
#include <string.h>
#include <stdexcept>
#include <sstream>
#include <pEp/mime.h>
namespace pEp {
namespace PythonAdapter {
@ -112,6 +113,34 @@ namespace pEp {
throw bad_alloc();
}
Message::Message(string mimetext)
{
_msg = NULL;
PEP_STATUS status = mime_decode_message(mimetext.c_str(),
mimetext.size(), &_msg);
switch (status) {
case PEP_STATUS_OK:
if (_msg)
return;
_msg = new_message(PEP_dir_incoming);
break;
case PEP_BUFFER_TOO_SMALL:
throw runtime_error("mime_decode_message: buffer too small");
case PEP_CANNOT_CREATE_TEMP_FILE:
throw runtime_error("mime_decode_message: cannot create temp file");
case PEP_OUT_OF_MEMORY:
throw bad_alloc();
default:
stringstream build;
build << "mime_decode_message: unknown error (" << (int) status << ")";
throw runtime_error(build.str());
}
}
Message::Message(const Message& second)
: _msg(message_dup(second._msg))
{
@ -155,6 +184,39 @@ namespace pEp {
return _msg;
}
string Message::_str()
{
if (!(_msg->from && _msg->from->address && _msg->from->address[0]))
return "< incomplete pEp.Message object: from missing >";
char *mimetext;
string result;
PEP_STATUS status = mime_encode_message(_msg, false, &mimetext);
switch (status) {
case PEP_STATUS_OK:
result = mimetext;
free(mimetext);
break;
case PEP_BUFFER_TOO_SMALL:
result = "< Message MIME error: buffer too small >";
break;
case PEP_CANNOT_CREATE_TEMP_FILE:
result = "< Message MIME error: cannot create temp file >";
break;
case PEP_OUT_OF_MEMORY:
throw bad_alloc();
default:
result = "< Message MIME error: unknown >";
}
return result;
}
tuple Message::attachments()
{
list l;


+ 3
- 0
src/message.hh View File

@ -52,6 +52,7 @@ namespace pEp {
};
Message(PEP_msg_direction dir = PEP_dir_outgoing);
Message(string mimetext);
Message(const Message& second);
Message(message *ident);
~Message();
@ -59,6 +60,8 @@ namespace pEp {
void attach(message *ident);
message *detach();
string _str();
PEP_msg_direction dir() { return _msg->dir; }
void dir(PEP_msg_direction value) { _msg->dir = value; }


+ 2
- 0
src/pEpmodule.cc View File

@ -93,6 +93,8 @@ BOOST_PYTHON_MODULE(pEp)
((PyTypeObject *)(void *)blob_class.ptr())->tp_as_buffer = &Message::Blob::bp;
auto message_class = class_<Message>("Message", "p≡p message")
.def(init<string>())
.def("__str__", &Message::_str)
.add_property("dir", (int(Message::*)())
(PEP_msg_direction(Message::*)()) &Message::dir,
(void(Message::*)(int))


Loading…
Cancel
Save