diff options
Diffstat (limited to 'whitelist.py')
-rw-r--r-- | whitelist.py | 80 |
1 files changed, 27 insertions, 53 deletions
diff --git a/whitelist.py b/whitelist.py index d76af85..cd0906c 100644 --- a/whitelist.py +++ b/whitelist.py @@ -12,10 +12,9 @@ Thanks to md for this useful formula. Beer is beer. whitelist manage a list of trusted user. """ +import logging import sys import time -import fcntl - import wlp @@ -24,48 +23,33 @@ class whitelist(object): Do you really want anyone can post? Ah ah ah. """ - wl = {} - logf = None # filedescriptor # constants DENY = 0 ACCEPT = 1 def __init__(self, wlfile='wl.pyg', 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) try: wlp.setfilebyname(wlfile) - except (Exception), (errno, message): - print 'Opening %s: %s (errno %d)' % (wlfile, message, errno) - sys.exit(0) + except Exception as ex: + self.logger.exception('Opening %s: %s', wlfile, ex) + sys.exit(1) # dict is a { ownername : {variable: value}} dictionary of dictionaries self.wl = wlp.mkdict() -# print 'owner: option = value' -# for owner in self.wl.keys(): -# for option in self.wl[owner].keys(): -# print '%s: %s = %s' % (owner,option,self.wl[owner][option]) - - try: - self.logf = open(logfile, 'a') - self.lock() - except (Exception), message: - print '%s\nAre you authorized to use this program? ' % message - sys.exit(1) - - def lock(self): - fcntl.flock(self.logf.fileno(), fcntl.LOCK_EX) - - # to unlock fd locked, usually fd are unlocked after process exit() - def unlock(self): - fcntl.flock(self.logf.fileno(), fcntl.LOCK_UN) - def checkfrom(self, fromhead): """have you permission to be here, sir?""" for owner in self.wl.keys(): -# if(self.wl[owner]['From'] == fromhead[:-1]): # remove '\n' # here colon after 'From' IS required, because binary module wl # expects it. # TODO: when switching to the python lexxing, remove this @@ -75,16 +59,8 @@ class whitelist(object): else: return None - def log(self, string): - """Captain Diary, Astral Date 962555394 from epoch. - it rawly write a line in logfile. Remeber to indent it, if you - like. - """ - self.logf.write(string + '\n') - def logmsg(self, heads, ok=DENY, owner=None): """who are walking through my gate? - log """ ltime = time.ctime(time.time()) @@ -95,33 +71,31 @@ class whitelist(object): tzone = time.tzname[0] if ok == self.ACCEPT: - self.logf.write('Permission Accorded ') + self.logger.info('Permission Accorded ') else: - self.logf.write('Permission Denied ') + self.logger.info('Permission Denied ') - self.logf.write('at %s (%s)\n' % (ltime, tzone)) + self.logger.info('at %s (%s)', ltime, tzone) if owner is not None: - self.logf.write('\tWLOwner: ' + owner + '\n') - self.logf.write('\tFrom: ' + heads.get('From', 'NOT PRESENT\n')) - self.logf.write('\tSubject: ' + heads.get('Subject', 'NOT PRESENT\n')) - self.logf.write('\tSender: ' + heads.get('Sender', 'NOT PRESENT\n')) - self.logf.write('\tDate: ' + heads.get('Date', 'NOT PRESENT\n')) + self.logger.info('\tWLOwner: ' + owner + '') + self.logger.info('\tFrom: ' + heads.get('From', 'NOT PRESENT')) + self.logger.info('\tSubject: ' + heads.get('Subject', 'NOT PRESENT')) + self.logger.info('\tSender: ' + heads.get('Sender', 'NOT PRESENT')) + self.logger.info('\tDate: ' + heads.get('Date', 'NOT PRESENT')) # some client create Message-Id other Message-ID. if 'Message-ID' in heads: - self.logf.write('\tMessage-ID: ' + heads.get('Message-ID')) + self.logger.info('\tMessage-ID: ' + heads.get('Message-ID')) else: - self.logf.write('\tMessage-Id: ' + heads.get('Message-Id', - 'NOT PRESENT\n')) + self.logger.info('\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.logf.write('\tTo: ' + heads.get('To', 'NOT PRESENT\n')) - self.logf.write('\tX-Newsgroups: ' + heads.get('X-Newsgroups', - 'NOT PRESENT\n')) + self.logger.info('\tTo: ' + heads.get('To', 'NOT PRESENT')) + self.logger.info('\tX-Newsgroups: ' + heads.get('X-Newsgroups', + 'NOT PRESENT')) else: - self.logf.write('\tNewsgroups: ' + - heads.get('Newsgroups', 'NOT PRESENT\n')) - - self.logf.write('\n') + self.logger.info('\tNewsgroups: ' + + heads.get('Newsgroups', 'NOT PRESENT')) |