import os from ConfigParser import RawConfigParser class Whale: """A Whale represents a distribution list. On the filesystem, a Whale looks like a directory with three files in it: - config - to.addrs - from.addrs Kraken eat Whales for dinner. """ def __init__(self, path): """Given a path to a directory, initialize a Whale. """ conf_path = os.path.join(path, 'config') send_path = os.path.join(path, 'to.addrs') from_path = os.path.join(path, 'from.addrs') self.id = os.path.split(path)[1] self.path = path cp = RawConfigParser() cp.read(conf_path) self.global_ = dict(cp.items('global')) self.imap = dict(cp.items('imap')) self.smtp = dict(cp.items('smtp')) mode = self.global_.get('mode') if mode not in ('announcements', 'discussion'): raise SystemExit("No mode") if mode == 'announcements': # Only sends to to.addrs. Only from.addrs can post. self.mode = 'announcements' self.send_to = self.addrs(send_path) self.accept_from = self.addrs(from_path) elif mode == 'discussion': # All list members can post. from.addrs contains aliases. self.mode = 'discussion' self.send_to = self.addrs(send_path) accept_from = [] for addr in self.addrs(from_path): if addr not in self.send_to: accept_from.append(addr) self.accept_from = self.send_to + accept_from def addrs(self, filename): """Given a filename, return a list of email addresses. """ lines = file(filename).read().split(os.linesep) return [l.lower() for l in lines if l and not l.startswith('#')]