diff options
author | Wojciech Polak <polak@gnu.org> | 2009-07-29 15:27:55 +0200 |
---|---|---|
committer | Wojciech Polak <polak@gnu.org> | 2009-07-29 22:55:02 +0200 |
commit | da546ae1358654cb568b983a2bb2e79f2f508c2b (patch) | |
tree | 44d83fd227a97b2528ddecb1bf3fdf7f6ba646f8 /python/libmu_py | |
parent | fe1fe3832d7d6febcd163f4854f3a5438db2074c (diff) | |
download | mailutils-da546ae1358654cb568b983a2bb2e79f2f508c2b.tar.gz mailutils-da546ae1358654cb568b983a2bb2e79f2f508c2b.tar.bz2 |
Add SieveMachine to C++/Python.
* include/mailutils/cpp/sieve.h: New file.
* libmu_cpp/sieve.cc: New file.
* python/libmu_py/sieve.c: New file.
* python/mailutils/sieve.py: New file.
* include/mailutils/cstr.h: Do not use C++ keywords.
* include/mailutils/tls.h: Likewise.
Diffstat (limited to 'python/libmu_py')
-rw-r--r-- | python/libmu_py/Makefile.am | 2 | ||||
-rw-r--r-- | python/libmu_py/libmu_py.c | 2 | ||||
-rw-r--r-- | python/libmu_py/libmu_py.h | 2 | ||||
-rw-r--r-- | python/libmu_py/mailbox.c | 6 | ||||
-rw-r--r-- | python/libmu_py/sieve.c | 487 |
5 files changed, 499 insertions, 0 deletions
diff --git a/python/libmu_py/Makefile.am b/python/libmu_py/Makefile.am index 0b5480aa5..0aef8df60 100644 --- a/python/libmu_py/Makefile.am +++ b/python/libmu_py/Makefile.am @@ -45,6 +45,7 @@ libmu_py_la_SOURCES = \ nls.c \ script.c \ secret.c \ + sieve.c \ stream.c \ registrar.c \ url.c \ @@ -61,6 +62,7 @@ libmu_py_la_LIBADD = $(PYTHON_LIBS) @MU_COMMON_LIBRARIES@ \ ${MU_LIB_MAILDIR}\ ${MU_LIB_AUTH}\ ${MU_LIB_MAILER}\ + ${MU_LIB_SIEVE}\ @MU_AUTHLIBS@\ ${MU_LIB_MAILUTILS} diff --git a/python/libmu_py/libmu_py.c b/python/libmu_py/libmu_py.c index 7fd1cb3cb..35088b0bc 100644 --- a/python/libmu_py/libmu_py.c +++ b/python/libmu_py/libmu_py.c @@ -85,6 +85,7 @@ mu_py_init (void) mu_py_init_message (); mu_py_init_mime (); mu_py_init_secret (); + mu_py_init_sieve (); mu_py_init_stream (); mu_py_init_url (); } @@ -126,6 +127,7 @@ mu_py_attach_modules (void) _mu_py_attach_nls (); _mu_py_attach_registrar (); _mu_py_attach_secret (); + _mu_py_attach_sieve (); _mu_py_attach_stream (); _mu_py_attach_url (); _mu_py_attach_util (); diff --git a/python/libmu_py/libmu_py.h b/python/libmu_py/libmu_py.h index e30e32d5f..0a4df4047 100644 --- a/python/libmu_py/libmu_py.h +++ b/python/libmu_py/libmu_py.h @@ -44,6 +44,7 @@ #include <mailutils/registrar.h> #include <mailutils/tls.h> #include <mailutils/secret.h> +#include <mailutils/sieve.h> #include <mailutils/stream.h> #include <mailutils/url.h> #include <mailutils/python.h> @@ -77,6 +78,7 @@ extern void _mu_py_attach_mime (void); extern void _mu_py_attach_nls (void); extern void _mu_py_attach_registrar (void); extern void _mu_py_attach_secret (void); +extern void _mu_py_attach_sieve (void); extern void _mu_py_attach_stream (void); extern void _mu_py_attach_url (void); extern void _mu_py_attach_util (void); diff --git a/python/libmu_py/mailbox.c b/python/libmu_py/mailbox.c index 6874f81f6..f0579bb59 100644 --- a/python/libmu_py/mailbox.c +++ b/python/libmu_py/mailbox.c @@ -79,6 +79,12 @@ PyMailbox_NEW () return (PyMailbox *)PyObject_NEW (PyMailbox, &PyMailboxType); } +int +PyMailbox_Check (PyObject *x) +{ + return x->ob_type == &PyMailboxType; +} + static PyObject * api_mailbox_create (PyObject *self, PyObject *args) { diff --git a/python/libmu_py/sieve.c b/python/libmu_py/sieve.c new file mode 100644 index 000000000..38c1d370d --- /dev/null +++ b/python/libmu_py/sieve.c @@ -0,0 +1,487 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2009 Free Software Foundation, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301 USA +*/ + +#include "libmu_py.h" + +#define PY_MODULE "sieve" +#define PY_CSNAME "SieveMachineType" + +static PyObject * +_repr (PyObject *self) +{ + char buf[80]; + sprintf (buf, "<" PY_MODULE "." PY_CSNAME " instance at %p>", self); + return PyString_FromString (buf); +} + +static PyTypeObject PySieveMachineType = { + PyObject_HEAD_INIT(NULL) + 0, /* ob_size */ + PY_MODULE "." PY_CSNAME, /* tp_name */ + sizeof (PySieveMachine), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)_py_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr; __getattr__ */ + 0, /* tp_setattr; __setattr__ */ + 0, /* tp_compare; __cmp__ */ + _repr, /* tp_repr; __repr__ */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash; __hash__ */ + 0, /* tp_call; __call__ */ + _repr, /* tp_str; __str__ */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + "", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ +}; + +PySieveMachine * +PySieveMachine_NEW () +{ + return (PySieveMachine *)PyObject_NEW (PySieveMachine, &PySieveMachineType); +} + +int +PySieveMachine_Check (PyObject *x) +{ + return x->ob_type == &PySieveMachineType; +} + +struct _mu_py_sieve_logger { + PyObject *py_debug_printer; + PyObject *py_error_printer; + PyObject *py_parse_error_printer; + PyObject *py_action_printer; +}; + +static PyObject * +api_sieve_machine_init (PyObject *self, PyObject *args) +{ + int status; + PySieveMachine *py_mach; + struct _mu_py_sieve_logger *s; + + if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach)) + return NULL; + + if (!(s = calloc (1, sizeof (*s)))) + return NULL; + + status = mu_sieve_machine_init (&py_mach->mach, s); + return _ro (PyInt_FromLong (status)); +} + +static PyObject * +api_sieve_machine_destroy (PyObject *self, PyObject *args) +{ + PySieveMachine *py_mach; + + if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach)) + return NULL; + + if (py_mach->mach) { + struct _mu_py_sieve_logger *s = mu_sieve_get_data (py_mach->mach); + if (s) + free (s); + mu_sieve_machine_destroy (&py_mach->mach); + } + return _ro (Py_None); +} + +static PyObject * +api_sieve_compile (PyObject *self, PyObject *args) +{ + int status; + char *name; + PySieveMachine *py_mach; + + if (!PyArg_ParseTuple (args, "O!s", &PySieveMachineType, &py_mach, &name)) + return NULL; + + status = mu_sieve_compile (py_mach->mach, name); + return _ro (PyInt_FromLong (status)); +} + +static PyObject * +api_sieve_disass (PyObject *self, PyObject *args) +{ + int status; + PySieveMachine *py_mach; + + if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach)) + return NULL; + + status = mu_sieve_disass (py_mach->mach); + return _ro (PyInt_FromLong (status)); +} + +static PyObject * +api_sieve_mailbox (PyObject *self, PyObject *args) +{ + int status; + PySieveMachine *py_mach; + PyMailbox *py_mbox; + + if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_mbox)) + return NULL; + + if (!PyMailbox_Check ((PyObject *)py_mbox)) + { + PyErr_SetString (PyExc_TypeError, ""); + return NULL; + } + + status = mu_sieve_mailbox (py_mach->mach, py_mbox->mbox); + return _ro (PyInt_FromLong (status)); +} + +static PyObject * +api_sieve_message (PyObject *self, PyObject *args) +{ + int status; + PySieveMachine *py_mach; + PyMessage *py_msg; + + if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_msg)) + return NULL; + + if (!PyMessage_Check ((PyObject *)py_msg)) + { + PyErr_SetString (PyExc_TypeError, ""); + return NULL; + } + + status = mu_sieve_message (py_mach->mach, py_msg->msg); + return _ro (PyInt_FromLong (status)); +} + +static int +_sieve_error_printer (void *data, const char *fmt, va_list ap) +{ + char *buf = NULL; + size_t buflen = 0; + PyObject *py_args = PyTuple_New (1); + + if (py_args) + { + struct _mu_py_sieve_logger *s = data; + PyObject *py_fnc = s->py_error_printer; + + if (mu_vasnprintf (&buf, &buflen, fmt, ap)) + return; + PyTuple_SetItem (py_args, 0, PyString_FromString (buf ? buf : "")); + if (buf) + free (buf); + + if (py_fnc && PyCallable_Check (py_fnc)) + PyObject_CallObject (py_fnc, py_args); + + Py_DECREF (py_args); + } + return 0; +} + +static int +_sieve_parse_error_printer (void *data, const char *filename, int lineno, + const char *fmt, va_list ap) +{ + PyObject *py_args; + PyObject *py_dict; + + py_dict = PyDict_New (); + if (py_dict) + { + char *buf = NULL; + size_t buflen = 0; + + PyDict_SetItemString (py_dict, "filename", + PyString_FromString (filename)); + PyDict_SetItemString (py_dict, "lineno", PyInt_FromLong (lineno)); + + if (mu_vasnprintf (&buf, &buflen, fmt, ap)) + return; + PyDict_SetItemString (py_dict, "text", + PyString_FromString (buf ? buf : "")); + if (buf) + free (buf); + + py_args = PyTuple_New (1); + if (py_args) + { + struct _mu_py_sieve_logger *s = data; + PyObject *py_fnc = s->py_parse_error_printer; + + Py_INCREF (py_dict); + PyTuple_SetItem (py_args, 0, py_dict); + + if (py_fnc && PyCallable_Check (py_fnc)) + PyObject_CallObject (py_fnc, py_args); + + Py_DECREF (py_dict); + Py_DECREF (py_args); + } + } +} + +static int +_sieve_debug_printer (void *data, const char *fmt, va_list ap) +{ + char *buf = NULL; + size_t buflen = 0; + PyObject *py_args = PyTuple_New (1); + + if (py_args) + { + struct _mu_py_sieve_logger *s = data; + PyObject *py_fnc = s->py_debug_printer; + + if (mu_vasnprintf (&buf, &buflen, fmt, ap)) + return; + PyTuple_SetItem (py_args, 0, PyString_FromString (buf ? buf : "")); + if (buf) + free (buf); + + if (py_fnc && PyCallable_Check (py_fnc)) + PyObject_CallObject (py_fnc, py_args); + + Py_DECREF (py_args); + } + return 0; +} + +static void +_sieve_action_printer (void *data, const mu_sieve_locus_t *locus, + size_t msgno, mu_message_t msg, + const char *action, const char *fmt, va_list ap) +{ + PyObject *py_args; + PyObject *py_dict; + + py_dict = PyDict_New (); + if (py_dict) + { + char *buf = NULL; + size_t buflen = 0; + PyMessage *py_msg = PyMessage_NEW (); + + py_msg->msg = msg; + Py_INCREF (py_msg); + + PyDict_SetItemString (py_dict, "source_file", + PyString_FromString (locus->source_file)); + PyDict_SetItemString (py_dict, "source_line", + PyInt_FromLong (locus->source_line)); + PyDict_SetItemString (py_dict, "msgno", + PyInt_FromLong (msgno)); + PyDict_SetItemString (py_dict, "msg", (PyObject *)py_msg); + PyDict_SetItemString (py_dict, "action", + PyString_FromString (action ? action : "")); + + if (mu_vasnprintf (&buf, &buflen, fmt, ap)) + return; + PyDict_SetItemString (py_dict, "text", + PyString_FromString (buf ? buf : "")); + if (buf) + free (buf); + + py_args = PyTuple_New (1); + if (py_args) + { + struct _mu_py_sieve_logger *s = data; + PyObject *py_fnc = s->py_action_printer; + + Py_INCREF (py_dict); + PyTuple_SetItem (py_args, 0, py_dict); + + if (py_fnc && PyCallable_Check (py_fnc)) + PyObject_CallObject (py_fnc, py_args); + + Py_DECREF (py_dict); + Py_DECREF (py_args); + } + } +} + +static PyObject * +api_sieve_set_error (PyObject *self, PyObject *args) +{ + PySieveMachine *py_mach; + PyObject *py_fnc; + + if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc)) + return NULL; + + if (py_fnc && PyCallable_Check (py_fnc)) { + mu_sieve_machine_t mach = py_mach->mach; + struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach); + s->py_error_printer = py_fnc; + Py_INCREF (py_fnc); + mu_sieve_set_error (py_mach->mach, _sieve_error_printer); + } + else { + PyErr_SetString (PyExc_TypeError, ""); + return NULL; + } + return _ro (Py_None); +} + +static PyObject * +api_sieve_set_parse_error (PyObject *self, PyObject *args) +{ + PySieveMachine *py_mach; + PyObject *py_fnc; + + if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc)) + return NULL; + + if (py_fnc && PyCallable_Check (py_fnc)) { + mu_sieve_machine_t mach = py_mach->mach; + struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach); + s->py_parse_error_printer = py_fnc; + Py_INCREF (py_fnc); + mu_sieve_set_parse_error (py_mach->mach, _sieve_parse_error_printer); + } + else { + PyErr_SetString (PyExc_TypeError, ""); + return NULL; + } + return _ro (Py_None); +} + +static PyObject * +api_sieve_set_debug (PyObject *self, PyObject *args) +{ + PySieveMachine *py_mach; + PyObject *py_fnc; + + if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc)) + return NULL; + + if (py_fnc && PyCallable_Check (py_fnc)) { + mu_sieve_machine_t mach = py_mach->mach; + struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach); + s->py_debug_printer = py_fnc; + Py_INCREF (py_fnc); + mu_sieve_set_debug (py_mach->mach, _sieve_debug_printer); + } + else { + PyErr_SetString (PyExc_TypeError, ""); + return NULL; + } + return _ro (Py_None); +} + +static PyObject * +api_sieve_set_logger (PyObject *self, PyObject *args) +{ + PySieveMachine *py_mach; + PyObject *py_fnc; + + if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc)) + return NULL; + + if (py_fnc && PyCallable_Check (py_fnc)) { + mu_sieve_machine_t mach = py_mach->mach; + struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach); + s->py_action_printer = py_fnc; + Py_INCREF (py_fnc); + mu_sieve_set_logger (py_mach->mach, _sieve_action_printer); + } + else { + PyErr_SetString (PyExc_TypeError, ""); + return NULL; + } + return _ro (Py_None); +} + +static PyMethodDef methods[] = { + { "machine_init", (PyCFunction) api_sieve_machine_init, METH_VARARGS, + "Create and initialize new Sieve MACHine." }, + + { "machine_destroy", (PyCFunction) api_sieve_machine_destroy, METH_VARARGS, + "Destroy Sieve MACHine." }, + + { "compile", (PyCFunction) api_sieve_compile, METH_VARARGS, + "Compile the sieve script from the file NAME." }, + + { "disass", (PyCFunction) api_sieve_disass, METH_VARARGS, + "Dump the disassembled code of the sieve machine MACH." }, + + { "mailbox", (PyCFunction) api_sieve_mailbox, METH_VARARGS, + "Execute the code from the given instance of sieve machine " + "MACH over each message in the mailbox MBOX." }, + + { "message", (PyCFunction) api_sieve_message, METH_VARARGS, + "Execute the code from the given instance of sieve machine " + "MACH over the MESSAGE. " }, + + { "set_debug", (PyCFunction) api_sieve_set_debug, METH_VARARGS, + "" }, + + { "set_error", (PyCFunction) api_sieve_set_error, METH_VARARGS, + "" }, + + { "set_parse_error", (PyCFunction) api_sieve_set_parse_error, METH_VARARGS, + "" }, + + { "set_logger", (PyCFunction) api_sieve_set_logger, METH_VARARGS, + "" }, + + { NULL, NULL, 0, NULL } +}; + +int +mu_py_init_sieve (void) +{ + PySieveMachineType.tp_new = PyType_GenericNew; + return PyType_Ready (&PySieveMachineType); +} + +void +_mu_py_attach_sieve (void) +{ + PyObject *m; + if ((m = _mu_py_attach_module (PY_MODULE, methods))) + { + Py_INCREF (&PySieveMachineType); + PyModule_AddObject (m, PY_CSNAME, (PyObject *)&PySieveMachineType); + } +} |