Use CAPAB IDENTIFY-MSG to validate bot operators
[people/stefanha/gpxebot.git] / utils.py
1 import smtplib
2 import tempfile
3 import time
4 import sys
5
6 import config
7
8 def dbg(msg):
9     if config.DEBUG:
10         sys.stderr.write(msg + '\n')
11
12 def email_log(to_address, channel, logfile, aliases):
13     email_addresses = []
14     unrecognized = []
15     for a in to_address.split(','):
16         if '@' in a:
17             email_addresses.append(a)
18         elif a in aliases:
19             email_addresses.append(aliases[a])
20         else:
21             unrecognized.append(a)
22     if email_addresses:
23         header = '''From: %(from_address)s
24 To: %(to_address)s
25 Subject: irc %(channel)s log
26
27 ''' % {'from_address': config.FROM_ADDRESS,
28             'to_address': ','.join(email_addresses),
29             'channel': channel,
30             }
31         msg = header + open(logfile).read()
32         s = smtplib.SMTP(config.MTA)
33         s.sendmail(config.FROM_ADDRESS, email_addresses, msg)
34     return unrecognized
35
36 def get_time():
37     return '%s-%02d-%02d %02d:%02d' % time.gmtime()[:5]
38
39 def make_log(channel):
40     (year, month, day, hour, minute) = time.gmtime()[:5]
41     return tempfile.mkstemp(
42             suffix='.log',
43             prefix='%s-%s%02d%02d.%02d%02d-' % (channel, year, month, day, hour, minute),
44             dir=config.LOG_DIR)[1]
45
46 def do_log(logs, target, nick, msg):
47     if target in logs:
48         (name, f) = logs[target]
49         f.write('%s <%s> %s\n' % (get_time(), nick, msg))