aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/sieve.bi
blob: 72cdfb45af25e053b58601ab9967344effe37bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2007, 2008, 2009, 2010 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <mailutils/mailutils.h>

#define MF_SIEVE_LOG                0x01
#define MF_SIEVE_DEBUG_TRACE        0x02
#define MF_SIEVE_DEBUG_INSTR        0x04
#define MF_SIEVE_DEBUG_MAILUTILS    0x08
#define MF_SIEVE_DEBUG_PROT         0x10

MF_STATE(eom)
MF_CAPTURE	
MF_DEFUN(sieve, NUMBER, STRING script, OPTIONAL, NUMBER dbg)
{
	mu_sieve_machine_t mach;
	mu_log_level_t debug_flags = 0; /* FIXME: Init */
	int sieve_debug_flags = 0;
	int sieve_log = 0;
	int rc = mu_sieve_machine_init(&mach);
	int retval = 0;
	int f = MF_OPTVAL(dbg);
	
	MF_ASSERT(rc == 0, mfe_failure,
		  _("failed to initialize sieve machine: %s"),
		  mu_strerror(rc));

	if (f) {
		if (f & MF_SIEVE_DEBUG_TRACE)
			sieve_debug_flags |= MU_SIEVE_DEBUG_TRACE;
		if (f & MF_SIEVE_DEBUG_INSTR)
			sieve_debug_flags |= MU_SIEVE_DEBUG_INSTR;
		if (f & MF_SIEVE_DEBUG_MAILUTILS)
			debug_flags |= MU_DEBUG_TRACE;
		if (f & MF_SIEVE_DEBUG_PROT)
			debug_flags |= MU_DEBUG_PROT;
		if (f & MF_SIEVE_LOG)
			sieve_log = 1;
	}

	if (debug_flags) {
		/* FIXME! */
	}
	
	mu_sieve_set_debug_level(mach, sieve_debug_flags);

	rc = mu_sieve_compile(mach, script);
	if (rc == 0) {
		mu_stream_t mstr = env_get_stream(env);
		mu_attribute_t attr;
		mu_message_t msg;

		rc = mu_stream_to_message(mstr, &msg);
		if (rc) {
			mu_sieve_machine_destroy(&mach);
			MF_THROW(mfe_failure,
				 _("cannot translate stream to message: %s"),
				   mu_strerror (rc));
		}
		
		mu_message_get_attribute(msg, &attr);
		mu_attribute_unset_deleted(attr);
		rc = mu_sieve_message(mach, msg);
		if (rc == 0)
			retval = !(mu_attribute_is_deleted(attr) == 0);
		/* FIXME: Cannot destroy msg because this will cause
		   destroying its stream (mstr).
		   Need a workaround. */
		/*mu_message_destroy(&msg, mu_message_get_owner(msg));*/
		if (rc) {
			mu_sieve_machine_destroy(&mach);
			MF_THROW(mfe_failure,
				 _("sieving failed: %s"),
				 mu_strerror(rc));
		}
	} else {
		MF_THROW(mfe_failure,
			 _("compilation of Sieve script %s failed"),
			 script);
	}
	
	mu_sieve_machine_destroy(&mach);

	MF_RETURN(retval);
}
END

MF_INIT

Return to:

Send suggestions and report system problems to the System administrator.