Python plugin: Improve error reporting during plugin init
[claws.git] / src / plugins / python / messageinfotype.c
index e796858ebbaac232b7f4204c6884e274105217a5..77d5724ed13f44d8e8fc419e4a5a643d9f0f600b 100644 (file)
 #include "claws-features.h"
 #endif
 
-#include <glib.h>
 #include <glib/gi18n.h>
 
 #include "messageinfotype.h"
 
 #include "common/tags.h"
+#include "common/defs.h"
 #include "mainwindow.h"
 #include "summaryview.h"
+#include "procheader.h"
 
 #include <structmember.h>
 
+#include <string.h>
+
+#define HEADER_CONTENT_SIZE BUFFSIZE
 
 typedef struct {
     PyObject_HEAD
     PyObject *from;
     PyObject *to;
+    PyObject *cc;
     PyObject *subject;
     PyObject *msgid;
     PyObject *filepath;
@@ -47,6 +52,7 @@ static void MessageInfo_dealloc(clawsmail_MessageInfoObject* self)
 {
   Py_XDECREF(self->from);
   Py_XDECREF(self->to);
+  Py_XDECREF(self->cc);
   Py_XDECREF(self->subject);
   Py_XDECREF(self->msgid);
   self->ob_type->tp_free((PyObject*)self);
@@ -60,6 +66,9 @@ static int MessageInfo_init(clawsmail_MessageInfoObject *self, PyObject *args, P
   Py_INCREF(Py_None);
   self->to = Py_None;
 
+  Py_INCREF(Py_None);
+  self->cc = Py_None;
+
   Py_INCREF(Py_None);
   self->subject = Py_None;
 
@@ -202,6 +211,44 @@ static PyObject* remove_tag(PyObject *self, PyObject *args)
   return add_or_remove_tag(self, args, FALSE);
 }
 
+static PyObject* get_header(PyObject *self, PyObject *args)
+{
+  int retval;
+  const char *header_str;
+  char *header_str_dup;
+  MsgInfo *msginfo;
+  gchar header_content[HEADER_CONTENT_SIZE];
+
+  retval = PyArg_ParseTuple(args, "s", &header_str);
+  if(!retval)
+    return NULL;
+
+  msginfo = ((clawsmail_MessageInfoObject*)self)->msginfo;
+
+  header_str_dup = g_strdup(header_str);
+  retval = procheader_get_header_from_msginfo(msginfo, header_content, HEADER_CONTENT_SIZE, header_str);
+  g_free(header_str_dup);
+  if(retval == 0) {
+    PyObject *header_content_object;
+    gchar *content_start;
+
+    /* the string is now Header: Value. Strip the Header: part */
+    content_start = strstr(header_content, ":");
+    if(content_start == NULL)
+      content_start = header_content;
+    else
+      content_start++;
+    /* strip leading spaces */
+    while(*content_start == ' ')
+      content_start++;
+    header_content_object = Py_BuildValue("s", content_start);
+    return header_content_object;
+  }
+  else {
+    Py_RETURN_NONE;
+  }
+}
+
 
 static PyMethodDef MessageInfo_methods[] = {
   {"is_new",  is_new, METH_NOARGS,
@@ -209,7 +256,7 @@ static PyMethodDef MessageInfo_methods[] = {
    "\n"
    "Returns True if the new flag of the message is set."},
 
-  {"is_unread",  is_unread, METH_NOARGS, 
+  {"is_unread",  is_unread, METH_NOARGS,
    "is_unread() - checks if the message is unread\n"
    "\n"
    "Returns True if the unread flag of the message is set."},
@@ -251,15 +298,25 @@ static PyMethodDef MessageInfo_methods[] = {
    "Remove a tag from this message. If the tag is not set, a KeyError exception is raised.\n"
    "If the tag does not exist, a ValueError exception is raised."},
 
+   {"get_header",  get_header, METH_VARARGS,
+    "get_header(name) - get a message header with a given name\n"
+    "\n"
+    "Get a message header content with a given name. If the header does not exist,\n"
+    "the value 'None' is returned. If multiple headers with the same name exist,\n"
+    "the first one is returned."},
+
   {NULL}
 };
 
 static PyMemberDef MessageInfo_members[] = {
-    {"From", T_OBJECT_EX, offsetof(clawsmail_MessageInfoObject, from), 0,
-     "From - the From header of the message"},
+    { "From", T_OBJECT_EX, offsetof(clawsmail_MessageInfoObject, from), 0,
+        "From - the From header of the message" },
+
+    { "To", T_OBJECT_EX, offsetof(clawsmail_MessageInfoObject, to), 0,
+        "To - the To header of the message" },
 
-    {"To", T_OBJECT_EX, offsetof(clawsmail_MessageInfoObject, to), 0,
-     "To - the To header of the message"},
+    { "Cc", T_OBJECT_EX, offsetof(clawsmail_MessageInfoObject, cc), 0,
+        "Cc - the Cc header of the message" },
 
     {"Subject", T_OBJECT_EX, offsetof(clawsmail_MessageInfoObject, subject), 0,
      "Subject - the subject header of the message"},
@@ -316,14 +373,14 @@ static PyTypeObject clawsmail_MessageInfoType = {
     0,                         /* tp_new */
 };
 
-PyMODINIT_FUNC initmessageinfo(PyObject *module)
+gboolean cmpy_add_messageinfo(PyObject *module)
 {
-    clawsmail_MessageInfoType.tp_new = PyType_GenericNew;
-    if(PyType_Ready(&clawsmail_MessageInfoType) < 0)
-        return;
+  clawsmail_MessageInfoType.tp_new = PyType_GenericNew;
+  if(PyType_Ready(&clawsmail_MessageInfoType) < 0)
+    return FALSE;
 
-    Py_INCREF(&clawsmail_MessageInfoType);
-    PyModule_AddObject(module, "MessageInfo", (PyObject*)&clawsmail_MessageInfoType);
+  Py_INCREF(&clawsmail_MessageInfoType);
+  return (PyModule_AddObject(module, "MessageInfo", (PyObject*)&clawsmail_MessageInfoType) == 0);
 }
 
 #define MSGINFO_STRING_TO_PYTHON_MESSAGEINFO_MEMBER(fis, pms)     \
@@ -347,6 +404,7 @@ static gboolean update_members(clawsmail_MessageInfoObject *ff, MsgInfo *msginfo
 
   MSGINFO_STRING_TO_PYTHON_MESSAGEINFO_MEMBER(msginfo->from, "From");
   MSGINFO_STRING_TO_PYTHON_MESSAGEINFO_MEMBER(msginfo->to, "To");
+  MSGINFO_STRING_TO_PYTHON_MESSAGEINFO_MEMBER(msginfo->cc, "Cc");
   MSGINFO_STRING_TO_PYTHON_MESSAGEINFO_MEMBER(msginfo->subject, "Subject");
   MSGINFO_STRING_TO_PYTHON_MESSAGEINFO_MEMBER(msginfo->msgid, "MessageID");