Commit 4bbd9e2e 4bbd9e2e27cc4f1aeabed4d9be8e7f3cd5345233 by Sam Roberts

Implemented redirect action, and added option for sieve to specify

the mailer.
1 parent 6b93f609
...@@ -40,6 +40,7 @@ const char HELP[] = ...@@ -40,6 +40,7 @@ const char HELP[] =
40 " t - sieve trace\n" 40 " t - sieve trace\n"
41 " h - sieve header parseing\n" 41 " h - sieve header parseing\n"
42 " q - sieve message queries\n" 42 " q - sieve message queries\n"
43 " -m a mailer URL (default is \"sendmail:\")\n"
43 ; 44 ;
44 45
45 static void 46 static void
...@@ -94,6 +95,7 @@ main (int argc, char *argv[]) ...@@ -94,6 +95,7 @@ main (int argc, char *argv[])
94 wicket_t wicket = 0; 95 wicket_t wicket = 0;
95 ticket_t ticket = 0; 96 ticket_t ticket = 0;
96 mu_debug_t debug = 0; 97 mu_debug_t debug = 0;
98 mailer_t mailer = 0;
97 mailbox_t mbox = 0; 99 mailbox_t mbox = 0;
98 100
99 size_t count = 0; 101 size_t count = 0;
...@@ -107,6 +109,7 @@ main (int argc, char *argv[]) ...@@ -107,6 +109,7 @@ main (int argc, char *argv[])
107 char *opt_mbox = 0; 109 char *opt_mbox = 0;
108 char *opt_tickets = 0; 110 char *opt_tickets = 0;
109 int opt_debug_level = 0; 111 int opt_debug_level = 0;
112 char *opt_mailer = "sendmail:";
110 char *opt_script = 0; 113 char *opt_script = 0;
111 114
112 int opt; 115 int opt;
...@@ -238,6 +241,19 @@ main (int argc, char *argv[]) ...@@ -238,6 +241,19 @@ main (int argc, char *argv[])
238 } 241 }
239 } 242 }
240 243
244 /* Create a mailer. */
245 if ((rc = mailer_create(&mailer, opt_mailer)))
246 {
247 fprintf (stderr, "mailer create <%s> failed: %s\n",
248 opt_mailer, strerror (rc));
249 goto cleanup;
250 }
251 if (debug && (rc = mailer_set_debug (mailer, debug)))
252 {
253 fprintf (stderr, "mailer_set_debug failed: %s\n", strerror (rc));
254 goto cleanup;
255 }
256
241 /* Create, give a ticket to, and open the mailbox. */ 257 /* Create, give a ticket to, and open the mailbox. */
242 if ((rc = mailbox_create_default (&mbox, opt_mbox)) != 0) 258 if ((rc = mailbox_create_default (&mbox, opt_mbox)) != 0)
243 { 259 {
...@@ -319,7 +335,7 @@ main (int argc, char *argv[]) ...@@ -319,7 +335,7 @@ main (int argc, char *argv[])
319 goto cleanup; 335 goto cleanup;
320 } 336 }
321 337
322 rc = sv_script_execute (script, msg, ticket, debug, opt_no_actions); 338 rc = sv_script_execute (script, msg, ticket, debug, mailer, opt_no_actions);
323 339
324 if (rc) 340 if (rc)
325 { 341 {
...@@ -369,4 +385,8 @@ mutil_register_all_mbox_formats (void) ...@@ -369,4 +385,8 @@ mutil_register_all_mbox_formats (void)
369 list_append (bookie, mbox_record); 385 list_append (bookie, mbox_record);
370 list_append (bookie, pop_record); 386 list_append (bookie, pop_record);
371 list_append (bookie, imap_record); 387 list_append (bookie, imap_record);
388 list_append (bookie, mh_record);
389 list_append (bookie, sendmail_record);
390 list_append (bookie, smtp_record);
372 } 391 }
392
......
...@@ -32,8 +32,9 @@ Extended methods: ...@@ -32,8 +32,9 @@ Extended methods:
32 32
33 #include <errno.h> 33 #include <errno.h>
34 34
35 #include <mailutils/message.h>
36 #include <mailutils/debug.h> 35 #include <mailutils/debug.h>
36 #include <mailutils/mailer.h>
37 #include <mailutils/message.h>
37 38
38 /* Sieve specific intepretations of error numbers. */ 39 /* Sieve specific intepretations of error numbers. */
39 #define SV_EPARSE ENOEXEC 40 #define SV_EPARSE ENOEXEC
...@@ -43,9 +44,15 @@ extern const char* sv_strerror(int e); ...@@ -43,9 +44,15 @@ extern const char* sv_strerror(int e);
43 typedef struct sv_interp_ctx_t* sv_interp_t; 44 typedef struct sv_interp_ctx_t* sv_interp_t;
44 typedef struct sv_script_ctx_t* sv_script_t; 45 typedef struct sv_script_ctx_t* sv_script_t;
45 46
46 typedef void (*sv_parse_error_t)(const char* script, int lineno, const char* errmsg); 47 typedef void (*sv_parse_error_t) (const char *script, int lineno,
47 typedef void (*sv_execute_error_t)(const char* script, message_t msg, int rc, const char* errmsg); 48 const char *errmsg);
48 typedef void (*sv_action_log_t)(const char* script, message_t msg, const char* action, const char* fmt, va_list ap); 49
50 typedef void (*sv_execute_error_t) (const char *script, message_t msg, int rc,
51 const char *errmsg);
52
53 typedef void (*sv_action_log_t) (const char *script, message_t msg,
54 const char *action, const char *fmt,
55 va_list ap);
49 56
50 extern int sv_interp_alloc(sv_interp_t *i, 57 extern int sv_interp_alloc(sv_interp_t *i,
51 sv_parse_error_t pe, 58 sv_parse_error_t pe,
...@@ -54,7 +61,9 @@ extern int sv_interp_alloc(sv_interp_t *i, ...@@ -54,7 +61,9 @@ extern int sv_interp_alloc(sv_interp_t *i,
54 61
55 extern void sv_interp_free(sv_interp_t *i); 62 extern void sv_interp_free(sv_interp_t *i);
56 63
57 extern int sv_script_parse(sv_script_t *s, sv_interp_t i, const char *script); 64 extern int sv_script_parse (sv_script_t * s, sv_interp_t i,
65 const char *script);
66
58 extern void sv_script_free(sv_script_t *s); 67 extern void sv_script_free(sv_script_t *s);
59 68
60 #define SV_FLAG_NO_ACTIONS 0x01 69 #define SV_FLAG_NO_ACTIONS 0x01
...@@ -64,5 +73,7 @@ extern void sv_script_free(sv_script_t *s); ...@@ -64,5 +73,7 @@ extern void sv_script_free(sv_script_t *s);
64 #define SV_DEBUG_HDR_FILL 0x200 73 #define SV_DEBUG_HDR_FILL 0x200
65 #define SV_DEBUG_MSG_QUERY 0x400 74 #define SV_DEBUG_MSG_QUERY 0x400
66 75
67 extern int sv_script_execute(sv_script_t s, message_t m, ticket_t t, mu_debug_t d, int svflags); 76 extern int sv_script_execute (sv_script_t script, message_t message,
77 ticket_t ticket, mu_debug_t debug,
78 mailer_t mailer, int svflags);
68 79
......
...@@ -75,8 +75,12 @@ typedef struct sv_msg_ctx_t ...@@ -75,8 +75,12 @@ typedef struct sv_msg_ctx_t
75 /* Ticket for use by mailbox URLs for implicit authentication. */ 75 /* Ticket for use by mailbox URLs for implicit authentication. */
76 ticket_t ticket; 76 ticket_t ticket;
77 77
78 /* Debug used for debug output. */
78 mu_debug_t debug; 79 mu_debug_t debug;
79 80
81 /* Mailer for redirecting messages. */
82 mailer_t mailer;
83
80 /* Flags controlling execution of script. */ 84 /* Flags controlling execution of script. */
81 int svflags; 85 int svflags;
82 86
...@@ -115,7 +119,5 @@ extern int sv_mu_mark_deleted (message_t msg, int deleted); ...@@ -115,7 +119,5 @@ extern int sv_mu_mark_deleted (message_t msg, int deleted);
115 119
116 extern int sv_mu_copy_debug_level (const mailbox_t from, mailbox_t to); 120 extern int sv_mu_copy_debug_level (const mailbox_t from, mailbox_t to);
117 121
118 extern int sv_mu_save_to (const char *toname, message_t msg, ticket_t ticket, mu_debug_t debug);
119
120 #endif 122 #endif
121 123
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
4 #include <stdlib.h> 4 #include <stdlib.h>
5 #include <string.h> 5 #include <string.h>
6 6
7 #include <mailutils/envelope.h>
8
7 #include "sv.h" 9 #include "sv.h"
8 10
9 /** mailutils errno to sieve error code translation. */ 11 /** mailutils errno to sieve error code translation. */
...@@ -229,13 +231,67 @@ int ...@@ -229,13 +231,67 @@ int
229 sv_redirect (void *ac, void *ic, void *sc, void *mc, const char **errmsg) 231 sv_redirect (void *ac, void *ic, void *sc, void *mc, const char **errmsg)
230 { 232 {
231 sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc; 233 sv_msg_ctx_t *m = (sv_msg_ctx_t *) mc;
234 sieve_redirect_context_t *a = (sieve_redirect_context_t *) ac;
235 char* fromaddr = 0;
236 address_t to = 0;
237 address_t from = 0;
238
239 action_log (mc, "REDIRECT", "to %s", a->addr);
232 240
241 if (!m->mailer)
242 {
233 *errmsg = "redirect not supported"; 243 *errmsg = "redirect not supported";
234 m->rc = ENOTSUP; 244 m->rc = ENOTSUP;
245 return SIEVE_FAIL;
246 }
235 247
236 action_log (mc, "REDIRECT", ""); 248 *errmsg = "redirect, parsing address";
237 249
238 return SIEVE_FAIL; 250 if((m->rc = address_create(&to, a->addr)))
251 goto end;
252
253 *errmsg = "redirect, getting envelope sender";
254
255 {
256 envelope_t envelope = 0;
257 size_t sz = 0;
258 if((m->rc = message_get_envelope(m->msg, &envelope)))
259 goto end;
260
261 if((m->rc = envelope_sender(envelope, NULL, 0, &sz)))
262 goto end;
263
264 if(!(fromaddr = malloc(sz + 1)))
265 {
266 m->rc = ENOMEM;
267 goto end;
268 }
269 if((m->rc = envelope_sender(envelope, fromaddr, sz + 1, NULL)))
270 goto end;
271 }
272
273 if((m->rc = address_create(&from, fromaddr)))
274 goto end;
275
276 *errmsg = "redirect, opening mailer";
277
278 if((m->rc = mailer_open(m->mailer, 0)))
279 goto end;
280
281 *errmsg = "redirect, sending message";
282
283 if((m->rc = mailer_send_message(m->mailer, m->msg, from, to)))
284 goto end;
285
286 end:
287
288 if(fromaddr)
289 free(fromaddr);
290 mailer_close(m->mailer);
291 address_destroy(&to);
292 address_destroy(&from);
293
294 return sieve_err(m->rc);
239 } 295 }
240 296
241 int 297 int
......
...@@ -140,30 +140,32 @@ sv_script_free (sv_script_t * sp) ...@@ -140,30 +140,32 @@ sv_script_free (sv_script_t * sp)
140 *sp = 0; 140 *sp = 0;
141 } 141 }
142 142
143 int sv_script_execute (sv_script_t s, message_t m, ticket_t t, mu_debug_t d, 143 int
144 int svflags) 144 sv_script_execute (sv_script_t script, message_t msg, ticket_t ticket,
145 mu_debug_t debug, mailer_t mailer, int svflags)
145 { 146 {
146 sv_msg_ctx_t mc = { 0, }; 147 sv_msg_ctx_t mc = { 0, };
147 int rc; 148 int rc;
148 int isdeleted; 149 int isdeleted;
149 attribute_t attr = 0; 150 attribute_t attr = 0;
150 151
151 rc = message_get_attribute (m, &attr); 152 rc = message_get_attribute (msg, &attr);
152 153
153 if(rc) 154 if(rc)
154 return rc; 155 return rc;
155 156
156 isdeleted = attribute_is_deleted(attr); 157 isdeleted = attribute_is_deleted(attr);
157 158
158 mc.sc = s; 159 mc.sc = script;
159 mc.msg = m; 160 mc.msg = msg;
160 mc.ticket = t; 161 mc.ticket = ticket;
161 mc.debug = d; 162 mc.debug = debug;
163 mc.mailer = mailer;
162 mc.svflags = svflags; 164 mc.svflags = svflags;
163 165
164 message_get_uid (m, &mc.uid); 166 message_get_uid (msg, &mc.uid);
165 167
166 rc = sieve_execute_script (s->script, &mc); 168 rc = sieve_execute_script (script->script, &mc);
167 169
168 sv_field_cache_release (&mc.cache); 170 sv_field_cache_release (&mc.cache);
169 171
...@@ -172,7 +174,7 @@ int sv_script_execute (sv_script_t s, message_t m, ticket_t t, mu_debug_t d, ...@@ -172,7 +174,7 @@ int sv_script_execute (sv_script_t s, message_t m, ticket_t t, mu_debug_t d,
172 174
173 /* Reset the deleted flag if the script failed. */ 175 /* Reset the deleted flag if the script failed. */
174 if(rc) 176 if(rc)
175 sv_mu_mark_deleted (m, isdeleted); 177 sv_mu_mark_deleted (msg, isdeleted);
176 178
177 return rc; 179 return rc;
178 } 180 }
......