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 =\ ...@@ -64,5 +64,6 @@ LDADD =\
64 ${MU_LIB_MAILDIR}\ 64 ${MU_LIB_MAILDIR}\
65 ${MU_LIB_AUTH}\ 65 ${MU_LIB_AUTH}\
66 ${MU_LIB_MAILER}\ 66 ${MU_LIB_MAILER}\
67 ${MU_LIB_SIEVE}\
67 @MU_AUTHLIBS@\ 68 @MU_AUTHLIBS@\
68 ${MU_LIB_MAILUTILS} 69 ${MU_LIB_MAILUTILS}
......
...@@ -66,7 +66,7 @@ main (int argc, char *argv[]) ...@@ -66,7 +66,7 @@ main (int argc, char *argv[])
66 { 66 {
67 string folder; 67 string folder;
68 string ref; 68 string ref;
69 char *pattern = "*"; 69 const char *pattern = "*";
70 int level = 0; 70 int level = 0;
71 71
72 switch (argc) 72 switch (argc)
...@@ -87,6 +87,6 @@ main (int argc, char *argv[]) ...@@ -87,6 +87,6 @@ main (int argc, char *argv[])
87 87
88 register_all_mbox_formats (); 88 register_all_mbox_formats ();
89 89
90 return ls_folders (folder, ref, pattern, level); 90 return ls_folders (folder, ref, (char *)pattern, level);
91 } 91 }
92 92
......
...@@ -41,7 +41,7 @@ main (int argc, char *argv[]) ...@@ -41,7 +41,7 @@ main (int argc, char *argv[])
41 { 41 {
42 int opt; 42 int opt;
43 int optdebug = 0; 43 int optdebug = 0;
44 char *optmailer = "sendmail:"; 44 const char *optmailer = "sendmail:";
45 char *optfrom = 0; 45 char *optfrom = 0;
46 46
47 while ((opt = getopt (argc, argv, "hdm:f:")) != -1) 47 while ((opt = getopt (argc, argv, "hdm:f:")) != -1)
......
...@@ -53,6 +53,7 @@ if __name__ == '__main__': ...@@ -53,6 +53,7 @@ if __name__ == '__main__':
53 ref = sys.argv[2] 53 ref = sys.argv[2]
54 fname = sys.argv[1] 54 fname = sys.argv[1]
55 elif argc == 2: 55 elif argc == 2:
56 ref = None
56 fname = sys.argv[1] 57 fname = sys.argv[1]
57 else: 58 else:
58 print "usage: lsf folder [ref] [pattern] [recursion-level]" 59 print "usage: lsf folder [ref] [pattern] [recursion-level]"
......
...@@ -38,6 +38,7 @@ MU_CXX_INCLUDES = \ ...@@ -38,6 +38,7 @@ MU_CXX_INCLUDES = \
38 pop3.h\ 38 pop3.h\
39 registrar.h\ 39 registrar.h\
40 secret.h\ 40 secret.h\
41 sieve.h\
41 stream.h\ 42 stream.h\
42 url.h 43 url.h
43 44
......
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
18 Boston, MA 02110-1301 USA 18 Boston, MA 02110-1301 USA
19 */ 19 */
20 20
21 #ifndef _ADDRESS_H 21 #ifndef _MUCPP_ADDRESS_H
22 #define _ADDRESS_H 22 #define _MUCPP_ADDRESS_H
23 23
24 #include <ostream> 24 #include <ostream>
25 #include <errno.h> 25 #include <errno.h>
...@@ -72,5 +72,5 @@ class Address ...@@ -72,5 +72,5 @@ class Address
72 72
73 } 73 }
74 74
75 #endif // not _ADDRESS_H 75 #endif // not _MUCPP_ADDRESS_H
76 76
......
...@@ -37,6 +37,8 @@ class MailboxBase ...@@ -37,6 +37,8 @@ class MailboxBase
37 protected: 37 protected:
38 mu_mailbox_t mbox; 38 mu_mailbox_t mbox;
39 39
40 friend class SieveMachine;
41
40 public: 42 public:
41 void open (); 43 void open ();
42 void open (int flag); 44 void open (int flag);
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
36 #include <mailutils/cpp/pop3.h> 36 #include <mailutils/cpp/pop3.h>
37 #include <mailutils/cpp/registrar.h> 37 #include <mailutils/cpp/registrar.h>
38 #include <mailutils/cpp/secret.h> 38 #include <mailutils/cpp/secret.h>
39 #include <mailutils/cpp/sieve.h>
39 #include <mailutils/cpp/stream.h> 40 #include <mailutils/cpp/stream.h>
40 #include <mailutils/cpp/url.h> 41 #include <mailutils/cpp/url.h>
41 42
......
...@@ -42,6 +42,7 @@ class Message ...@@ -42,6 +42,7 @@ class Message
42 friend class MailboxBase; 42 friend class MailboxBase;
43 friend class Mailer; 43 friend class Mailer;
44 friend class Mime; 44 friend class Mime;
45 friend class SieveMachine;
45 46
46 public: 47 public:
47 Message (); 48 Message ();
......
1 /*
2 GNU Mailutils -- a suite of utilities for electronic mail
3 Copyright (C) 2009 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _MUCPP_SIEVE_H
22 #define _MUCPP_SIEVE_H
23
24 #include <string>
25 #include <errno.h>
26 #include <mailutils/sieve.h>
27 #include <mailutils/cpp/error.h>
28 #include <mailutils/cpp/mailbox.h>
29 #include <mailutils/cpp/message.h>
30
31 namespace mailutils
32 {
33
34 class SieveMachine
35 {
36 protected:
37 mu_sieve_machine_t mach;
38
39 public:
40 SieveMachine ();
41 SieveMachine (const mu_sieve_machine_t);
42 ~SieveMachine ();
43
44 SieveMachine& operator = (const SieveMachine&);
45
46 void compile (const std::string& name);
47 void disass ();
48 void mailbox (const Mailbox& mbox);
49 void message (const Message& msg);
50 void set_debug (mu_sieve_printf_t printer);
51 void set_error (mu_sieve_printf_t printer);
52 void set_parse_error (mu_sieve_parse_error_t printer);
53 void set_logger (mu_sieve_action_log_t printer);
54 };
55
56 }
57
58 #endif // not _MUCPP_SIEVE_H
59
...@@ -28,15 +28,15 @@ int mu_strupper (char *); ...@@ -28,15 +28,15 @@ int mu_strupper (char *);
28 int mu_c_strcasecmp (const char *a, const char *b); 28 int mu_c_strcasecmp (const char *a, const char *b);
29 int mu_c_strncasecmp (const char *a, const char *b, size_t n); 29 int mu_c_strncasecmp (const char *a, const char *b, size_t n);
30 30
31 size_t mu_rtrim_class (char *str, int class); 31 size_t mu_rtrim_class (char *str, int __class);
32 size_t mu_rtrim_cset (char *str, const char *cset); 32 size_t mu_rtrim_cset (char *str, const char *cset);
33 size_t mu_ltrim_class (char *str, int class); 33 size_t mu_ltrim_class (char *str, int __class);
34 size_t mu_ltrim_cset (char *str, const char *cset); 34 size_t mu_ltrim_cset (char *str, const char *cset);
35 35
36 char *mu_str_skip_class (const char *str, int class); 36 char *mu_str_skip_class (const char *str, int __class);
37 char *mu_str_skip_cset (const char *str, const char *cset); 37 char *mu_str_skip_cset (const char *str, const char *cset);
38 38
39 char *mu_str_skip_class_comp (const char *str, int class); 39 char *mu_str_skip_class_comp (const char *str, int __class);
40 char *mu_str_skip_cset_comp (const char *str, const char *cset); 40 char *mu_str_skip_cset_comp (const char *str, const char *cset);
41 41
42 char *mu_str_stripws (char *string); 42 char *mu_str_stripws (char *string);
......
...@@ -117,6 +117,11 @@ typedef struct { ...@@ -117,6 +117,11 @@ typedef struct {
117 117
118 typedef struct { 118 typedef struct {
119 PyObject_HEAD; 119 PyObject_HEAD;
120 mu_sieve_machine_t mach;
121 } PySieveMachine;
122
123 typedef struct {
124 PyObject_HEAD;
120 mu_stream_t stm; 125 mu_stream_t stm;
121 } PyStream; 126 } PyStream;
122 127
...@@ -143,6 +148,7 @@ extern int mu_py_init_mailcap (void); ...@@ -143,6 +148,7 @@ extern int mu_py_init_mailcap (void);
143 extern int mu_py_init_message (void); 148 extern int mu_py_init_message (void);
144 extern int mu_py_init_mime (void); 149 extern int mu_py_init_mime (void);
145 extern int mu_py_init_secret (void); 150 extern int mu_py_init_secret (void);
151 extern int mu_py_init_sieve (void);
146 extern int mu_py_init_stream (void); 152 extern int mu_py_init_stream (void);
147 extern int mu_py_init_url (void); 153 extern int mu_py_init_url (void);
148 154
...@@ -175,6 +181,7 @@ extern int PyAuthority_Check (PyObject *x); ...@@ -175,6 +181,7 @@ extern int PyAuthority_Check (PyObject *x);
175 extern int PyTicket_Check (PyObject *x); 181 extern int PyTicket_Check (PyObject *x);
176 extern int PyWicket_Check (PyObject *x); 182 extern int PyWicket_Check (PyObject *x);
177 extern int PyAuthData_Check (PyObject *x); 183 extern int PyAuthData_Check (PyObject *x);
184 extern int PyMailbox_Check (PyObject *x);
178 extern int PyMessage_Check (PyObject *x); 185 extern int PyMessage_Check (PyObject *x);
179 extern int PySecret_Check (PyObject *x); 186 extern int PySecret_Check (PyObject *x);
180 extern int PyStream_Check (PyObject *x); 187 extern int PyStream_Check (PyObject *x);
......
...@@ -52,7 +52,7 @@ extern void mu_deinit_tls_libs (void); ...@@ -52,7 +52,7 @@ extern void mu_deinit_tls_libs (void);
52 typedef int (*mu_tls_readline_fn) (void *iodata); 52 typedef int (*mu_tls_readline_fn) (void *iodata);
53 typedef int (*mu_tls_writeline_fn) (void *iodata, char *buf); 53 typedef int (*mu_tls_writeline_fn) (void *iodata, char *buf);
54 typedef void (*mu_tls_stream_ctl_fn) (void *iodata, mu_stream_t *pold, 54 typedef void (*mu_tls_stream_ctl_fn) (void *iodata, mu_stream_t *pold,
55 mu_stream_t new); 55 mu_stream_t __new);
56 56
57 extern int mu_tls_begin (void *iodata, mu_tls_readline_fn reader, 57 extern int mu_tls_begin (void *iodata, mu_tls_readline_fn reader,
58 mu_tls_writeline_fn writer, 58 mu_tls_writeline_fn writer,
......
...@@ -43,6 +43,7 @@ libmu_cpp_la_SOURCES = \ ...@@ -43,6 +43,7 @@ libmu_cpp_la_SOURCES = \
43 pop3.cc\ 43 pop3.cc\
44 registrar.cc\ 44 registrar.cc\
45 secret.cc\ 45 secret.cc\
46 sieve.cc\
46 stream.cc\ 47 stream.cc\
47 url.cc 48 url.cc
48 49
......
1 /*
2 GNU Mailutils -- a suite of utilities for electronic mail
3 Copyright (C) 2009 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA
19 */
20
21 #include <mailutils/cpp/sieve.h>
22
23 using namespace mailutils;
24
25 //
26 // SieveMachine
27 //
28
29 SieveMachine :: SieveMachine ()
30 {
31 int status = mu_sieve_machine_init (&mach, NULL);
32 if (status)
33 throw Exception ("SieveMachine::SieveMachine", status);
34 }
35
36 SieveMachine :: SieveMachine (const mu_sieve_machine_t mach)
37 {
38 if (mach == 0)
39 throw Exception ("SieveMachine::SieveMachine", EINVAL);
40
41 this->mach = mach;
42 }
43
44 SieveMachine :: ~SieveMachine ()
45 {
46 if (mach)
47 mu_sieve_machine_destroy (&mach);
48 }
49
50 SieveMachine&
51 SieveMachine :: operator = (const SieveMachine& m)
52 {
53 if (this != &m)
54 {
55 if (this->mach)
56 mu_sieve_machine_destroy (&this->mach);
57 mu_sieve_machine_dup (m.mach, &this->mach);
58 }
59 return *this;
60 }
61
62 void
63 SieveMachine :: compile (const std::string& name)
64 {
65 int status = mu_sieve_compile (mach, name.c_str ());
66 if (status)
67 throw Exception ("SieveMachine::compile", status);
68 }
69
70 void
71 SieveMachine :: disass ()
72 {
73 int status = mu_sieve_disass (mach);
74 if (status)
75 throw Exception ("SieveMachine::disass", status);
76 }
77
78 void
79 SieveMachine :: mailbox (const Mailbox& mbox)
80 {
81 int status = mu_sieve_mailbox (mach, mbox.mbox);
82 if (status)
83 throw Exception ("SieveMachine::mailbox", status);
84 }
85
86 void
87 SieveMachine :: message (const Message& msg)
88 {
89 int status = mu_sieve_message (mach, msg.msg);
90 if (status)
91 throw Exception ("SieveMachine::message", status);
92 }
93
94 void
95 SieveMachine :: set_debug (mu_sieve_printf_t printer)
96 {
97 mu_sieve_set_debug (mach, printer);
98 }
99
100 void
101 SieveMachine :: set_error (mu_sieve_printf_t printer)
102 {
103 mu_sieve_set_error (mach, printer);
104 }
105
106 void
107 SieveMachine :: set_parse_error (mu_sieve_parse_error_t printer)
108 {
109 mu_sieve_set_parse_error (mach, printer);
110 }
111
112 void
113 SieveMachine :: set_logger (mu_sieve_action_log_t printer)
114 {
115 mu_sieve_set_logger (mach, printer);
116 }
117
...@@ -614,8 +614,6 @@ mu_sieve_compile (mu_sieve_machine_t mach, const char *name) ...@@ -614,8 +614,6 @@ mu_sieve_compile (mu_sieve_machine_t mach, const char *name)
614 rc = 1; 614 rc = 1;
615 615
616 mu_sieve_machine_finish (mach); 616 mu_sieve_machine_finish (mach);
617 if (rc)
618 mu_sieve_machine_destroy (&mach);
619 return rc; 617 return rc;
620 } 618 }
621 619
...@@ -639,8 +637,6 @@ mu_sieve_compile_buffer (mu_sieve_machine_t mach, ...@@ -639,8 +637,6 @@ mu_sieve_compile_buffer (mu_sieve_machine_t mach,
639 rc = 1; 637 rc = 1;
640 638
641 mu_sieve_machine_finish (mach); 639 mu_sieve_machine_finish (mach);
642 if (rc)
643 mu_sieve_machine_destroy (&mach);
644 return rc; 640 return rc;
645 } 641 }
646 642
......
...@@ -45,6 +45,7 @@ libmu_py_la_SOURCES = \ ...@@ -45,6 +45,7 @@ libmu_py_la_SOURCES = \
45 nls.c \ 45 nls.c \
46 script.c \ 46 script.c \
47 secret.c \ 47 secret.c \
48 sieve.c \
48 stream.c \ 49 stream.c \
49 registrar.c \ 50 registrar.c \
50 url.c \ 51 url.c \
...@@ -61,6 +62,7 @@ libmu_py_la_LIBADD = $(PYTHON_LIBS) @MU_COMMON_LIBRARIES@ \ ...@@ -61,6 +62,7 @@ libmu_py_la_LIBADD = $(PYTHON_LIBS) @MU_COMMON_LIBRARIES@ \
61 ${MU_LIB_MAILDIR}\ 62 ${MU_LIB_MAILDIR}\
62 ${MU_LIB_AUTH}\ 63 ${MU_LIB_AUTH}\
63 ${MU_LIB_MAILER}\ 64 ${MU_LIB_MAILER}\
65 ${MU_LIB_SIEVE}\
64 @MU_AUTHLIBS@\ 66 @MU_AUTHLIBS@\
65 ${MU_LIB_MAILUTILS} 67 ${MU_LIB_MAILUTILS}
66 68
......
...@@ -85,6 +85,7 @@ mu_py_init (void) ...@@ -85,6 +85,7 @@ mu_py_init (void)
85 mu_py_init_message (); 85 mu_py_init_message ();
86 mu_py_init_mime (); 86 mu_py_init_mime ();
87 mu_py_init_secret (); 87 mu_py_init_secret ();
88 mu_py_init_sieve ();
88 mu_py_init_stream (); 89 mu_py_init_stream ();
89 mu_py_init_url (); 90 mu_py_init_url ();
90 } 91 }
...@@ -126,6 +127,7 @@ mu_py_attach_modules (void) ...@@ -126,6 +127,7 @@ mu_py_attach_modules (void)
126 _mu_py_attach_nls (); 127 _mu_py_attach_nls ();
127 _mu_py_attach_registrar (); 128 _mu_py_attach_registrar ();
128 _mu_py_attach_secret (); 129 _mu_py_attach_secret ();
130 _mu_py_attach_sieve ();
129 _mu_py_attach_stream (); 131 _mu_py_attach_stream ();
130 _mu_py_attach_url (); 132 _mu_py_attach_url ();
131 _mu_py_attach_util (); 133 _mu_py_attach_util ();
......
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
44 #include <mailutils/registrar.h> 44 #include <mailutils/registrar.h>
45 #include <mailutils/tls.h> 45 #include <mailutils/tls.h>
46 #include <mailutils/secret.h> 46 #include <mailutils/secret.h>
47 #include <mailutils/sieve.h>
47 #include <mailutils/stream.h> 48 #include <mailutils/stream.h>
48 #include <mailutils/url.h> 49 #include <mailutils/url.h>
49 #include <mailutils/python.h> 50 #include <mailutils/python.h>
...@@ -77,6 +78,7 @@ extern void _mu_py_attach_mime (void); ...@@ -77,6 +78,7 @@ extern void _mu_py_attach_mime (void);
77 extern void _mu_py_attach_nls (void); 78 extern void _mu_py_attach_nls (void);
78 extern void _mu_py_attach_registrar (void); 79 extern void _mu_py_attach_registrar (void);
79 extern void _mu_py_attach_secret (void); 80 extern void _mu_py_attach_secret (void);
81 extern void _mu_py_attach_sieve (void);
80 extern void _mu_py_attach_stream (void); 82 extern void _mu_py_attach_stream (void);
81 extern void _mu_py_attach_url (void); 83 extern void _mu_py_attach_url (void);
82 extern void _mu_py_attach_util (void); 84 extern void _mu_py_attach_util (void);
......
...@@ -79,6 +79,12 @@ PyMailbox_NEW () ...@@ -79,6 +79,12 @@ PyMailbox_NEW ()
79 return (PyMailbox *)PyObject_NEW (PyMailbox, &PyMailboxType); 79 return (PyMailbox *)PyObject_NEW (PyMailbox, &PyMailboxType);
80 } 80 }
81 81
82 int
83 PyMailbox_Check (PyObject *x)
84 {
85 return x->ob_type == &PyMailboxType;
86 }
87
82 static PyObject * 88 static PyObject *
83 api_mailbox_create (PyObject *self, PyObject *args) 89 api_mailbox_create (PyObject *self, PyObject *args)
84 { 90 {
......
1 /*
2 GNU Mailutils -- a suite of utilities for electronic mail
3 Copyright (C) 2009 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA
19 */
20
21 #include "libmu_py.h"
22
23 #define PY_MODULE "sieve"
24 #define PY_CSNAME "SieveMachineType"
25
26 static PyObject *
27 _repr (PyObject *self)
28 {
29 char buf[80];
30 sprintf (buf, "<" PY_MODULE "." PY_CSNAME " instance at %p>", self);
31 return PyString_FromString (buf);
32 }
33
34 static PyTypeObject PySieveMachineType = {
35 PyObject_HEAD_INIT(NULL)
36 0, /* ob_size */
37 PY_MODULE "." PY_CSNAME, /* tp_name */
38 sizeof (PySieveMachine), /* tp_basicsize */
39 0, /* tp_itemsize */
40 (destructor)_py_dealloc, /* tp_dealloc */
41 0, /* tp_print */
42 0, /* tp_getattr; __getattr__ */
43 0, /* tp_setattr; __setattr__ */
44 0, /* tp_compare; __cmp__ */
45 _repr, /* tp_repr; __repr__ */
46 0, /* tp_as_number */
47 0, /* tp_as_sequence */
48 0, /* tp_as_mapping */
49 0, /* tp_hash; __hash__ */
50 0, /* tp_call; __call__ */
51 _repr, /* tp_str; __str__ */
52 0, /* tp_getattro */
53 0, /* tp_setattro */
54 0, /* tp_as_buffer */
55 Py_TPFLAGS_DEFAULT, /* tp_flags */
56 "", /* tp_doc */
57 0, /* tp_traverse */
58 0, /* tp_clear */
59 0, /* tp_richcompare */
60 0, /* tp_weaklistoffset */
61 0, /* tp_iter */
62 0, /* tp_iternext */
63 0, /* tp_methods */
64 0, /* tp_members */
65 0, /* tp_getset */
66 0, /* tp_base */
67 0, /* tp_dict */
68 0, /* tp_descr_get */
69 0, /* tp_descr_set */
70 0, /* tp_dictoffset */
71 0, /* tp_init */
72 0, /* tp_alloc */
73 0, /* tp_new */
74 };
75
76 PySieveMachine *
77 PySieveMachine_NEW ()
78 {
79 return (PySieveMachine *)PyObject_NEW (PySieveMachine, &PySieveMachineType);
80 }
81
82 int
83 PySieveMachine_Check (PyObject *x)
84 {
85 return x->ob_type == &PySieveMachineType;
86 }
87
88 struct _mu_py_sieve_logger {
89 PyObject *py_debug_printer;
90 PyObject *py_error_printer;
91 PyObject *py_parse_error_printer;
92 PyObject *py_action_printer;
93 };
94
95 static PyObject *
96 api_sieve_machine_init (PyObject *self, PyObject *args)
97 {
98 int status;
99 PySieveMachine *py_mach;
100 struct _mu_py_sieve_logger *s;
101
102 if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
103 return NULL;
104
105 if (!(s = calloc (1, sizeof (*s))))
106 return NULL;
107
108 status = mu_sieve_machine_init (&py_mach->mach, s);
109 return _ro (PyInt_FromLong (status));
110 }
111
112 static PyObject *
113 api_sieve_machine_destroy (PyObject *self, PyObject *args)
114 {
115 PySieveMachine *py_mach;
116
117 if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
118 return NULL;
119
120 if (py_mach->mach) {
121 struct _mu_py_sieve_logger *s = mu_sieve_get_data (py_mach->mach);
122 if (s)
123 free (s);
124 mu_sieve_machine_destroy (&py_mach->mach);
125 }
126 return _ro (Py_None);
127 }
128
129 static PyObject *
130 api_sieve_compile (PyObject *self, PyObject *args)
131 {
132 int status;
133 char *name;
134 PySieveMachine *py_mach;
135
136 if (!PyArg_ParseTuple (args, "O!s", &PySieveMachineType, &py_mach, &name))
137 return NULL;
138
139 status = mu_sieve_compile (py_mach->mach, name);
140 return _ro (PyInt_FromLong (status));
141 }
142
143 static PyObject *
144 api_sieve_disass (PyObject *self, PyObject *args)
145 {
146 int status;
147 PySieveMachine *py_mach;
148
149 if (!PyArg_ParseTuple (args, "O!", &PySieveMachineType, &py_mach))
150 return NULL;
151
152 status = mu_sieve_disass (py_mach->mach);
153 return _ro (PyInt_FromLong (status));
154 }
155
156 static PyObject *
157 api_sieve_mailbox (PyObject *self, PyObject *args)
158 {
159 int status;
160 PySieveMachine *py_mach;
161 PyMailbox *py_mbox;
162
163 if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_mbox))
164 return NULL;
165
166 if (!PyMailbox_Check ((PyObject *)py_mbox))
167 {
168 PyErr_SetString (PyExc_TypeError, "");
169 return NULL;
170 }
171
172 status = mu_sieve_mailbox (py_mach->mach, py_mbox->mbox);
173 return _ro (PyInt_FromLong (status));
174 }
175
176 static PyObject *
177 api_sieve_message (PyObject *self, PyObject *args)
178 {
179 int status;
180 PySieveMachine *py_mach;
181 PyMessage *py_msg;
182
183 if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_msg))
184 return NULL;
185
186 if (!PyMessage_Check ((PyObject *)py_msg))
187 {
188 PyErr_SetString (PyExc_TypeError, "");
189 return NULL;
190 }
191
192 status = mu_sieve_message (py_mach->mach, py_msg->msg);
193 return _ro (PyInt_FromLong (status));
194 }
195
196 static int
197 _sieve_error_printer (void *data, const char *fmt, va_list ap)
198 {
199 char *buf = NULL;
200 size_t buflen = 0;
201 PyObject *py_args = PyTuple_New (1);
202
203 if (py_args)
204 {
205 struct _mu_py_sieve_logger *s = data;
206 PyObject *py_fnc = s->py_error_printer;
207
208 if (mu_vasnprintf (&buf, &buflen, fmt, ap))
209 return;
210 PyTuple_SetItem (py_args, 0, PyString_FromString (buf ? buf : ""));
211 if (buf)
212 free (buf);
213
214 if (py_fnc && PyCallable_Check (py_fnc))
215 PyObject_CallObject (py_fnc, py_args);
216
217 Py_DECREF (py_args);
218 }
219 return 0;
220 }
221
222 static int
223 _sieve_parse_error_printer (void *data, const char *filename, int lineno,
224 const char *fmt, va_list ap)
225 {
226 PyObject *py_args;
227 PyObject *py_dict;
228
229 py_dict = PyDict_New ();
230 if (py_dict)
231 {
232 char *buf = NULL;
233 size_t buflen = 0;
234
235 PyDict_SetItemString (py_dict, "filename",
236 PyString_FromString (filename));
237 PyDict_SetItemString (py_dict, "lineno", PyInt_FromLong (lineno));
238
239 if (mu_vasnprintf (&buf, &buflen, fmt, ap))
240 return;
241 PyDict_SetItemString (py_dict, "text",
242 PyString_FromString (buf ? buf : ""));
243 if (buf)
244 free (buf);
245
246 py_args = PyTuple_New (1);
247 if (py_args)
248 {
249 struct _mu_py_sieve_logger *s = data;
250 PyObject *py_fnc = s->py_parse_error_printer;
251
252 Py_INCREF (py_dict);
253 PyTuple_SetItem (py_args, 0, py_dict);
254
255 if (py_fnc && PyCallable_Check (py_fnc))
256 PyObject_CallObject (py_fnc, py_args);
257
258 Py_DECREF (py_dict);
259 Py_DECREF (py_args);
260 }
261 }
262 }
263
264 static int
265 _sieve_debug_printer (void *data, const char *fmt, va_list ap)
266 {
267 char *buf = NULL;
268 size_t buflen = 0;
269 PyObject *py_args = PyTuple_New (1);
270
271 if (py_args)
272 {
273 struct _mu_py_sieve_logger *s = data;
274 PyObject *py_fnc = s->py_debug_printer;
275
276 if (mu_vasnprintf (&buf, &buflen, fmt, ap))
277 return;
278 PyTuple_SetItem (py_args, 0, PyString_FromString (buf ? buf : ""));
279 if (buf)
280 free (buf);
281
282 if (py_fnc && PyCallable_Check (py_fnc))
283 PyObject_CallObject (py_fnc, py_args);
284
285 Py_DECREF (py_args);
286 }
287 return 0;
288 }
289
290 static void
291 _sieve_action_printer (void *data, const mu_sieve_locus_t *locus,
292 size_t msgno, mu_message_t msg,
293 const char *action, const char *fmt, va_list ap)
294 {
295 PyObject *py_args;
296 PyObject *py_dict;
297
298 py_dict = PyDict_New ();
299 if (py_dict)
300 {
301 char *buf = NULL;
302 size_t buflen = 0;
303 PyMessage *py_msg = PyMessage_NEW ();
304
305 py_msg->msg = msg;
306 Py_INCREF (py_msg);
307
308 PyDict_SetItemString (py_dict, "source_file",
309 PyString_FromString (locus->source_file));
310 PyDict_SetItemString (py_dict, "source_line",
311 PyInt_FromLong (locus->source_line));
312 PyDict_SetItemString (py_dict, "msgno",
313 PyInt_FromLong (msgno));
314 PyDict_SetItemString (py_dict, "msg", (PyObject *)py_msg);
315 PyDict_SetItemString (py_dict, "action",
316 PyString_FromString (action ? action : ""));
317
318 if (mu_vasnprintf (&buf, &buflen, fmt, ap))
319 return;
320 PyDict_SetItemString (py_dict, "text",
321 PyString_FromString (buf ? buf : ""));
322 if (buf)
323 free (buf);
324
325 py_args = PyTuple_New (1);
326 if (py_args)
327 {
328 struct _mu_py_sieve_logger *s = data;
329 PyObject *py_fnc = s->py_action_printer;
330
331 Py_INCREF (py_dict);
332 PyTuple_SetItem (py_args, 0, py_dict);
333
334 if (py_fnc && PyCallable_Check (py_fnc))
335 PyObject_CallObject (py_fnc, py_args);
336
337 Py_DECREF (py_dict);
338 Py_DECREF (py_args);
339 }
340 }
341 }
342
343 static PyObject *
344 api_sieve_set_error (PyObject *self, PyObject *args)
345 {
346 PySieveMachine *py_mach;
347 PyObject *py_fnc;
348
349 if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc))
350 return NULL;
351
352 if (py_fnc && PyCallable_Check (py_fnc)) {
353 mu_sieve_machine_t mach = py_mach->mach;
354 struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach);
355 s->py_error_printer = py_fnc;
356 Py_INCREF (py_fnc);
357 mu_sieve_set_error (py_mach->mach, _sieve_error_printer);
358 }
359 else {
360 PyErr_SetString (PyExc_TypeError, "");
361 return NULL;
362 }
363 return _ro (Py_None);
364 }
365
366 static PyObject *
367 api_sieve_set_parse_error (PyObject *self, PyObject *args)
368 {
369 PySieveMachine *py_mach;
370 PyObject *py_fnc;
371
372 if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc))
373 return NULL;
374
375 if (py_fnc && PyCallable_Check (py_fnc)) {
376 mu_sieve_machine_t mach = py_mach->mach;
377 struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach);
378 s->py_parse_error_printer = py_fnc;
379 Py_INCREF (py_fnc);
380 mu_sieve_set_parse_error (py_mach->mach, _sieve_parse_error_printer);
381 }
382 else {
383 PyErr_SetString (PyExc_TypeError, "");
384 return NULL;
385 }
386 return _ro (Py_None);
387 }
388
389 static PyObject *
390 api_sieve_set_debug (PyObject *self, PyObject *args)
391 {
392 PySieveMachine *py_mach;
393 PyObject *py_fnc;
394
395 if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc))
396 return NULL;
397
398 if (py_fnc && PyCallable_Check (py_fnc)) {
399 mu_sieve_machine_t mach = py_mach->mach;
400 struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach);
401 s->py_debug_printer = py_fnc;
402 Py_INCREF (py_fnc);
403 mu_sieve_set_debug (py_mach->mach, _sieve_debug_printer);
404 }
405 else {
406 PyErr_SetString (PyExc_TypeError, "");
407 return NULL;
408 }
409 return _ro (Py_None);
410 }
411
412 static PyObject *
413 api_sieve_set_logger (PyObject *self, PyObject *args)
414 {
415 PySieveMachine *py_mach;
416 PyObject *py_fnc;
417
418 if (!PyArg_ParseTuple (args, "O!O", &PySieveMachineType, &py_mach, &py_fnc))
419 return NULL;
420
421 if (py_fnc && PyCallable_Check (py_fnc)) {
422 mu_sieve_machine_t mach = py_mach->mach;
423 struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach);
424 s->py_action_printer = py_fnc;
425 Py_INCREF (py_fnc);
426 mu_sieve_set_logger (py_mach->mach, _sieve_action_printer);
427 }
428 else {
429 PyErr_SetString (PyExc_TypeError, "");
430 return NULL;
431 }
432 return _ro (Py_None);
433 }
434
435 static PyMethodDef methods[] = {
436 { "machine_init", (PyCFunction) api_sieve_machine_init, METH_VARARGS,
437 "Create and initialize new Sieve MACHine." },
438
439 { "machine_destroy", (PyCFunction) api_sieve_machine_destroy, METH_VARARGS,
440 "Destroy Sieve MACHine." },
441
442 { "compile", (PyCFunction) api_sieve_compile, METH_VARARGS,
443 "Compile the sieve script from the file NAME." },
444
445 { "disass", (PyCFunction) api_sieve_disass, METH_VARARGS,
446 "Dump the disassembled code of the sieve machine MACH." },
447
448 { "mailbox", (PyCFunction) api_sieve_mailbox, METH_VARARGS,
449 "Execute the code from the given instance of sieve machine "
450 "MACH over each message in the mailbox MBOX." },
451
452 { "message", (PyCFunction) api_sieve_message, METH_VARARGS,
453 "Execute the code from the given instance of sieve machine "
454 "MACH over the MESSAGE. " },
455
456 { "set_debug", (PyCFunction) api_sieve_set_debug, METH_VARARGS,
457 "" },
458
459 { "set_error", (PyCFunction) api_sieve_set_error, METH_VARARGS,
460 "" },
461
462 { "set_parse_error", (PyCFunction) api_sieve_set_parse_error, METH_VARARGS,
463 "" },
464
465 { "set_logger", (PyCFunction) api_sieve_set_logger, METH_VARARGS,
466 "" },
467
468 { NULL, NULL, 0, NULL }
469 };
470
471 int
472 mu_py_init_sieve (void)
473 {
474 PySieveMachineType.tp_new = PyType_GenericNew;
475 return PyType_Ready (&PySieveMachineType);
476 }
477
478 void
479 _mu_py_attach_sieve (void)
480 {
481 PyObject *m;
482 if ((m = _mu_py_attach_module (PY_MODULE, methods)))
483 {
484 Py_INCREF (&PySieveMachineType);
485 PyModule_AddObject (m, PY_CSNAME, (PyObject *)&PySieveMachineType);
486 }
487 }
...@@ -37,6 +37,7 @@ MU_PY_PKGPYTHON_LIST=\ ...@@ -37,6 +37,7 @@ MU_PY_PKGPYTHON_LIST=\
37 nls.py \ 37 nls.py \
38 registrar.py \ 38 registrar.py \
39 secret.py \ 39 secret.py \
40 sieve.py \
40 stream.py \ 41 stream.py \
41 url.py \ 42 url.py \
42 util.py 43 util.py
......
...@@ -35,6 +35,7 @@ __all__ = [ ...@@ -35,6 +35,7 @@ __all__ = [
35 "nls", 35 "nls",
36 "registrar", 36 "registrar",
37 "secret", 37 "secret",
38 "sieve",
38 "stream", 39 "stream",
39 "url", 40 "url",
40 "util", 41 "util",
......
...@@ -43,6 +43,7 @@ class MailcapError (Error): pass ...@@ -43,6 +43,7 @@ class MailcapError (Error): pass
43 class MessageError (Error): pass 43 class MessageError (Error): pass
44 class MimeError (Error): pass 44 class MimeError (Error): pass
45 class SecretError (Error): pass 45 class SecretError (Error): pass
46 class SieveMachineError (Error): pass
46 class StreamError (Error): pass 47 class StreamError (Error): pass
47 class UrlError (Error): pass 48 class UrlError (Error): pass
48 49
......
1 #
2 # GNU Mailutils -- a suite of utilities for electronic mail
3 # Copyright (C) 2009 Free Software Foundation, Inc.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General
16 # Public License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301 USA
19 #
20
21 from mailutils.c_api import sieve
22 from mailutils.error import SieveMachineError
23
24 class Machine:
25 def __init__ (self):
26 self.mach = sieve.SieveMachineType ()
27 status = sieve.machine_init (self.mach)
28 if status:
29 raise SieveMachineError (status)
30
31 def __del__ (self):
32 sieve.machine_destroy (self.mach)
33 del self.mach
34
35 def set_debug (self, fnc):
36 status = sieve.set_debug (self.mach, fnc)
37 if status:
38 raise SieveMachineError (status)
39
40 def set_error (self, fnc):
41 status = sieve.set_error (self.mach, fnc)
42 if status:
43 raise SieveMachineError (status)
44
45 def set_parse_error (self, fnc):
46 status = sieve.set_parse_error (self.mach, fnc)
47 if status:
48 raise SieveMachineError (status)
49
50 def set_logger (self, fnc):
51 status = sieve.set_logger (self.mach, fnc)
52 if status:
53 raise SieveMachineError (status)
54
55 def compile (self, name):
56 """Compile the sieve script from the file NAME."""
57 status = sieve.compile (self.mach, name)
58 if status:
59 raise SieveMachineError (status)
60
61 def disass (self):
62 """Dump the disassembled code of the sieve machine."""
63 status = sieve.disass (self.mach)
64 if status:
65 raise SieveMachineError (status)
66
67 def mailbox (self, mbox):
68 """Execute the code from the given instance of sieve machine
69 over each message in the mailbox."""
70 status = sieve.mailbox (self.mach, mbox.mbox)
71 if status:
72 raise SieveMachineError (status)
73
74 def message (self, msg):
75 """Execute the code from the given instance of sieve machine
76 over the MSG."""
77 status = sieve.message (self.mach, msg.msg)
78 if status:
79 raise SieveMachineError (status)