First public revision
[imapami.git] / config-samples / extension / imapami-ext.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright 2015, Olivier MATZ <zer0@droids-corp.org>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 #       notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above copyright
12 #       notice, this list of conditions and the following disclaimer in the
13 #       documentation and/or other materials provided with the distribution.
14 #     * Neither the name of the University of California, Berkeley nor the
15 #       names of its contributors may be used to endorse or promote products
16 #       derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29
30 #
31 # This is an example of how imapami can be extended in python. The
32 # following code creates a new condition class that is registered. The
33 # same can be done for actions.
34 #
35 # After that, the script calls imapami.run() which behaves like imapami
36 # with the new condition registered.
37 #
38
39 import imapami
40 import imapami.conditions
41
42 class ImapamiCondList(imapami.conditions.ImapamiCond):
43     """
44     Match if the sender is present in the file.
45
46     The file is a list of mails, one per line.
47
48     Arguments:
49       file: <string> [mandatory]
50         The path to the balcklist file.
51
52     Example:
53       list: {filename: /home/user/list.txt}
54     """
55     name = "list"
56
57     def __init__(self, filename):
58         ImapamiCond.__init__(self, fetch='headers')
59         self.filename = filename
60
61     def check(self, imapami_handler, msg_id, hdrs, body_part1, body_all):
62         From = self.evaluate("{From}", imapami_handler, hdrs)
63         try:
64             f = open(self.filename)
65             while True:
66                 l = f.readline()
67                 if l == '':
68                     break
69                 if From.lower() == l[:-1].lower():
70                     return True
71         except IOError:
72             imapami_handler.logger.warning("error opening list file <%s>")
73         return False
74 imapami.conditions.register(ImapamiCondList)
75
76 if __name__ == '__main__':
77     imapami.main()