aboutsummaryrefslogtreecommitdiffstats
path: root/src/whitelist.py
blob: 4e02f0e493ed216d8c8f66d20277100e29111f18 (plain) (blame)
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
"""News to mail gateway script. Copyright 2000 Cosimo Alfarano

Author: Cosimo Alfarano
Date: June 11 2000

whitelist.py - (C) 2000 by Cosimo Alfarano <Alfarano@Students.CS.UniBo.It>
You can use this software under the terms of the GPL. If we meet some day,
and you think this stuff is worth it, you can buy me a beer in return.

Thanks to md for this useful formula. Beer is beer.

whitelist manage a list of trusted user.
"""

from __future__ import absolute_import
import logging
# logging.basicConfig(level=logging.DEBUG)
import sys
import time

import wlp


class whitelist(object):
    """whitelist handling class

    Do you really want anyone can post? Ah ah ah.
    """
    wl = {}

    # constants
    DENY = 0
    ACCEPT = 1

    def __init__(self, wlfile, logfile='pyg.log'):
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.INFO)
        log_fh = logging.FileHandler(logfile)
        log_fmt = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        log_fh.setFormatter(log_fmt)
        self.logger.addHandler(log_fh)

        # dict is a { ownername : {variable: value}} dictionary of dictionaries
        with open(wlfile) as inf:
            self.wl = wlp.mkdict(inf)

    def checkfrom(self, fromhead):
        """have you permission to be here, sir?"""
        for owner in self.wl:
            # here colon after 'From' IS required, because binary module wl
            # expects it.
            # TODO: when switching to the python lexxing, remove this
            # limitation.
            if fromhead.find(self.wl[owner]['From:']) >= 0:
                return owner
            else:
                return None

    def logmsg(self, heads, ok=DENY, owner=None):
        """who are walking through my gate?
        """

        ltime = time.ctime(time.time())

        if time.daylight:
            tzone = time.tzname[1]
        else:
            tzone = time.tzname[0]

        if ok == self.ACCEPT:
            self.logger.info('Permission Accorded ')
        else:
            self.logger.info('Permission Denied ')

        self.logger.info('at %s (%s)', ltime, tzone)
        if owner is not None:
            self.logger.debug('\tWLOwner: ' + owner + '')
        self.logger.debug('\tFrom: ' + heads.get('From', 'NOT PRESENT'))
        self.logger.debug('\tSubject: ' + heads.get('Subject', 'NOT PRESENT'))
        self.logger.debug('\tSender: ' + heads.get('Sender', 'NOT PRESENT'))
        self.logger.debug('\tDate: ' + heads.get('Date', 'NOT PRESENT'))

        # some client create Message-Id other Message-ID.
        if 'Message-ID' in heads:
            self.logger.debug('\tMessage-ID: ' + heads.get('Message-ID'))
        else:
            self.logger.debug('\tMessage-Id: ' + heads.get('Message-Id',
                                                           'NOT PRESENT'))

        # X-Newsgroups: and To: are present if user is trusted, else
        # Newsgroup: exists since no changes on nntp headers are done.
        if 'X-Newsgroups' in heads:
            self.logger.debug('\tTo: ' + heads.get('To', 'NOT PRESENT'))
            self.logger.debug('\tX-Newsgroups: ' + heads.get('X-Newsgroups',
                                                             'NOT PRESENT'))
        else:
            self.logger.debug('\tNewsgroups: ' +
                              heads.get('Newsgroups', 'NOT PRESENT'))