Commit da546ae1 da546ae1358654cb568b983a2bb2e79f2f508c2b by Wojciech Polak

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.
1 parent fe1fe383
......@@ -64,5 +64,6 @@ LDADD =\
${MU_LIB_MAILDIR}\
${MU_LIB_AUTH}\
${MU_LIB_MAILER}\
${MU_LIB_SIEVE}\
@MU_AUTHLIBS@\
${MU_LIB_MAILUTILS}
......
......@@ -66,7 +66,7 @@ main (int argc, char *argv[])
{
string folder;
string ref;
char *pattern = "*";
const char *pattern = "*";
int level = 0;
switch (argc)
......@@ -87,6 +87,6 @@ main (int argc, char *argv[])
register_all_mbox_formats ();
return ls_folders (folder, ref, pattern, level);
return ls_folders (folder, ref, (char *)pattern, level);
}
......
......@@ -41,7 +41,7 @@ main (int argc, char *argv[])
{
int opt;
int optdebug = 0;
char *optmailer = "sendmail:";
const char *optmailer = "sendmail:";
char *optfrom = 0;
while ((opt = getopt (argc, argv, "hdm:f:")) != -1)
......
......@@ -53,6 +53,7 @@ if __name__ == '__main__':
ref = sys.argv[2]
fname = sys.argv[1]
elif argc == 2:
ref = None
fname = sys.argv[1]
else:
print "usage: lsf folder [ref] [pattern] [recursion-level]"
......
......@@ -38,6 +38,7 @@ MU_CXX_INCLUDES = \
pop3.h\
registrar.h\
secret.h\
sieve.h\
stream.h\
url.h
......
......@@ -18,8 +18,8 @@
Boston, MA 02110-1301 USA
*/
#ifndef _ADDRESS_H
#define _ADDRESS_H
#ifndef _MUCPP_ADDRESS_H
#define _MUCPP_ADDRESS_H
#include <ostream>
#include <errno.h>
......@@ -72,5 +72,5 @@ class Address
}
#endif // not _ADDRESS_H
#endif // not _MUCPP_ADDRESS_H
......
......@@ -37,6 +37,8 @@ class MailboxBase
protected:
mu_mailbox_t mbox;
friend class SieveMachine;
public:
void open ();
void open (int flag);
......
......@@ -36,6 +36,7 @@
#include <mailutils/cpp/pop3.h>
#include <mailutils/cpp/registrar.h>
#include <mailutils/cpp/secret.h>
#include <mailutils/cpp/sieve.h>
#include <mailutils/cpp/stream.h>
#include <mailutils/cpp/url.h>
......
......@@ -42,6 +42,7 @@ class Message
friend class MailboxBase;
friend class Mailer;
friend class Mime;
friend class SieveMachine;
public:
Message ();
......
/*
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
*/
#ifndef _MUCPP_SIEVE_H
#define _MUCPP_SIEVE_H
#include <string>
#include <errno.h>
#include <mailutils/sieve.h>
#include <mailutils/cpp/error.h>
#include <mailutils/cpp/mailbox.h>
#include <mailutils/cpp/message.h>
namespace mailutils
{
class SieveMachine
{
protected:
mu_sieve_machine_t mach;
public:
SieveMachine ();
SieveMachine (const mu_sieve_machine_t);
~SieveMachine ();
SieveMachine& operator = (const SieveMachine&);
void compile (const std::string& name);
void disass ();
void mailbox (const Mailbox& mbox);
void message (const Message& msg);
void set_debug (mu_sieve_printf_t printer);
void set_error (mu_sieve_printf_t printer);
void set_parse_error (mu_sieve_parse_error_t printer);
void set_logger (mu_sieve_action_log_t printer);
};
}
#endif // not _MUCPP_SIEVE_H
......@@ -28,15 +28,15 @@ int mu_strupper (char *);
int mu_c_strcasecmp (const char *a, const char *b);
int mu_c_strncasecmp (const char *a, const char *b, size_t n);
size_t mu_rtrim_class (char *str, int class);
size_t mu_rtrim_class (char *str, int __class);
size_t mu_rtrim_cset (char *str, const char *cset);
size_t mu_ltrim_class (char *str, int class);
size_t mu_ltrim_class (char *str, int __class);
size_t mu_ltrim_cset (char *str, const char *cset);
char *mu_str_skip_class (const char *str, int class);
char *mu_str_skip_class (const char *str, int __class);
char *mu_str_skip_cset (const char *str, const char *cset);
char *mu_str_skip_class_comp (const char *str, int class);
char *mu_str_skip_class_comp (const char *str, int __class);
char *mu_str_skip_cset_comp (const char *str, const char *cset);
char *mu_str_stripws (char *string);
......
......@@ -117,6 +117,11 @@ typedef struct {
typedef struct {
PyObject_HEAD;
mu_sieve_machine_t mach;
} PySieveMachine;
typedef struct {
PyObject_HEAD;
mu_stream_t stm;
} PyStream;
......@@ -143,6 +148,7 @@ extern int mu_py_init_mailcap (void);
extern int mu_py_init_message (void);
extern int mu_py_init_mime (void);
extern int mu_py_init_secret (void);
extern int mu_py_init_sieve (void);
extern int mu_py_init_stream (void);
extern int mu_py_init_url (void);
......@@ -175,6 +181,7 @@ extern int PyAuthority_Check (PyObject *x);
extern int PyTicket_Check (PyObject *x);
extern int PyWicket_Check (PyObject *x);
extern int PyAuthData_Check (PyObject *x);
extern int PyMailbox_Check (PyObject *x);
extern int PyMessage_Check (PyObject *x);
extern int PySecret_Check (PyObject *x);
extern int PyStream_Check (PyObject *x);
......
......@@ -52,7 +52,7 @@ extern void mu_deinit_tls_libs (void);
typedef int (*mu_tls_readline_fn) (void *iodata);
typedef int (*mu_tls_writeline_fn) (void *iodata, char *buf);
typedef void (*mu_tls_stream_ctl_fn) (void *iodata, mu_stream_t *pold,
mu_stream_t new);
mu_stream_t __new);
extern int mu_tls_begin (void *iodata, mu_tls_readline_fn reader,
mu_tls_writeline_fn writer,
......
......@@ -43,6 +43,7 @@ libmu_cpp_la_SOURCES = \
pop3.cc\
registrar.cc\
secret.cc\
sieve.cc\
stream.cc\
url.cc
......
/*
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 <mailutils/cpp/sieve.h>
using namespace mailutils;
//
// SieveMachine
//
SieveMachine :: SieveMachine ()
{
int status = mu_sieve_machine_init (&mach, NULL);
if (status)
throw Exception ("SieveMachine::SieveMachine", status);
}
SieveMachine :: SieveMachine (const mu_sieve_machine_t mach)
{
if (mach == 0)
throw Exception ("SieveMachine::SieveMachine", EINVAL);
this->mach = mach;
}
SieveMachine :: ~SieveMachine ()
{
if (mach)
mu_sieve_machine_destroy (&mach);
}
SieveMachine&
SieveMachine :: operator = (const SieveMachine& m)
{
if (this != &m)
{
if (this->mach)
mu_sieve_machine_destroy (&this->mach);
mu_sieve_machine_dup (m.mach, &this->mach);
}
return *this;
}
void
SieveMachine :: compile (const std::string& name)
{
int status = mu_sieve_compile (mach, name.c_str ());
if (status)
throw Exception ("SieveMachine::compile", status);
}
void
SieveMachine :: disass ()
{
int status = mu_sieve_disass (mach);
if (status)
throw Exception ("SieveMachine::disass", status);
}
void
SieveMachine :: mailbox (const Mailbox& mbox)
{
int status = mu_sieve_mailbox (mach, mbox.mbox);
if (status)
throw Exception ("SieveMachine::mailbox", status);
}
void
SieveMachine :: message (const Message& msg)
{
int status = mu_sieve_message (mach, msg.msg);
if (status)
throw Exception ("SieveMachine::message", status);
}
void
SieveMachine :: set_debug (mu_sieve_printf_t printer)
{
mu_sieve_set_debug (mach, printer);
}
void
SieveMachine :: set_error (mu_sieve_printf_t printer)
{
mu_sieve_set_error (mach, printer);
}
void
SieveMachine :: set_parse_error (mu_sieve_parse_error_t printer)
{
mu_sieve_set_parse_error (mach, printer);
}
void
SieveMachine :: set_logger (mu_sieve_action_log_t printer)
{
mu_sieve_set_logger (mach, printer);
}
......@@ -614,8 +614,6 @@ mu_sieve_compile (mu_sieve_machine_t mach, const char *name)
rc = 1;
mu_sieve_machine_finish (mach);
if (rc)
mu_sieve_machine_destroy (&mach);
return rc;
}
......@@ -639,8 +637,6 @@ mu_sieve_compile_buffer (mu_sieve_machine_t mach,
rc = 1;
mu_sieve_machine_finish (mach);
if (rc)
mu_sieve_machine_destroy (&mach);
return rc;
}
......
......@@ -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}
......
......@@ -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 ();
......
......@@ -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);
......
......@@ -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)
{
......
/*
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);
}
}
......@@ -37,6 +37,7 @@ MU_PY_PKGPYTHON_LIST=\
nls.py \
registrar.py \
secret.py \
sieve.py \
stream.py \
url.py \
util.py
......
......@@ -35,6 +35,7 @@ __all__ = [
"nls",
"registrar",
"secret",
"sieve",
"stream",
"url",
"util",
......
......@@ -43,6 +43,7 @@ class MailcapError (Error): pass
class MessageError (Error): pass
class MimeError (Error): pass
class SecretError (Error): pass
class SieveMachineError (Error): pass
class StreamError (Error): pass
class UrlError (Error): pass
......
#
# 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
#
from mailutils.c_api import sieve
from mailutils.error import SieveMachineError
class Machine:
def __init__ (self):
self.mach = sieve.SieveMachineType ()
status = sieve.machine_init (self.mach)
if status:
raise SieveMachineError (status)
def __del__ (self):
sieve.machine_destroy (self.mach)
del self.mach
def set_debug (self, fnc):
status = sieve.set_debug (self.mach, fnc)
if status:
raise SieveMachineError (status)
def set_error (self, fnc):
status = sieve.set_error (self.mach, fnc)
if status:
raise SieveMachineError (status)
def set_parse_error (self, fnc):
status = sieve.set_parse_error (self.mach, fnc)
if status:
raise SieveMachineError (status)
def set_logger (self, fnc):
status = sieve.set_logger (self.mach, fnc)
if status:
raise SieveMachineError (status)
def compile (self, name):
"""Compile the sieve script from the file NAME."""
status = sieve.compile (self.mach, name)
if status:
raise SieveMachineError (status)
def disass (self):
"""Dump the disassembled code of the sieve machine."""
status = sieve.disass (self.mach)
if status:
raise SieveMachineError (status)
def mailbox (self, mbox):
"""Execute the code from the given instance of sieve machine
over each message in the mailbox."""
status = sieve.mailbox (self.mach, mbox.mbox)
if status:
raise SieveMachineError (status)
def message (self, msg):
"""Execute the code from the given instance of sieve machine
over the MSG."""
status = sieve.message (self.mach, msg.msg)
if status:
raise SieveMachineError (status)