Initial checkin
[people/stefanha/gpxebot.git] / cmds.py
1 import re
2 import time
3
4 import config
5 import utils
6 import errcode
7
8 logs = {}
9 commands = {}
10 op_commands = {}
11 op_members = [config.NICK]
12 aliases = {}
13
14 def readrc_command(target=None, replyto=None, words=None):
15     f = open(config.RCFILE).readlines()
16     cmd = []
17     for line in f:
18         response = do_command(config.NICK, config.NICK, config.NICK, line.split())
19         if isinstance(response, list):
20             cmd += response
21         elif response:
22             cmd.append(response)
23     return cmd
24 op_commands['readrc'] = readrc_command
25
26 def alias_command(_, replyto, words):
27     # alias stefanha stefanha@gmail.com
28     if len(words) == 2:
29         if words[1].lower() == 'list':
30             return [('PRIVMSG', (replyto, '%s    %s' % (user, email))) for (user, email) in aliases.items()]
31     if len(words) < 3:
32         return
33     user = words[1]
34     email = words[2]
35     aliases[user] = email
36 op_commands['alias'] = alias_command
37
38 def op_command(target, replyto, words):
39     # op stefanha
40     if len(words) < 2:
41         return
42     nick = words[1]
43     if not nick in op_members:
44         op_members.append(nick)
45 op_commands['op'] = op_command
46
47 def deop_command(target, replyto, words):
48     # deop stefanha
49     if len(words) < 2:
50         return
51     nick = words[1]
52     if nick in op_members:
53         op_members.remove(nick)
54 op_commands['deop'] = deop_command
55
56 ERRCODE_RE = re.compile(r'((?:0x)?[0-9a-fA-F]{8})')
57
58 def errcode_command(_, replyto, words):
59     # errcode 0x12345678
60     msg = ' '.join(words)
61     m = ERRCODE_RE.search(msg)
62     if m:
63         try:
64             return 'PRIVMSG', (replyto, str(errcode.Errcode(int(m.group(1), 16))))
65         except ValueError:
66             pass
67 commands['errcode'] = errcode_command
68 commands['error'] = errcode_command
69
70 def help_command(_, replyto, msg):
71     cmd = []
72     cmd.append(('PRIVMSG', (replyto, 'I look up gPXE error codes.  Message me like this:')))
73     cmd.append(('PRIVMSG', (replyto, 'errcode 0x12345678  OR  Error 0x12345678')))
74     return cmd
75 commands['help'] = help_command
76
77 def log_command(target, replyto, words):
78     if len(words) < 2:
79         return
80     channel = target
81     if len(words) >= 3:
82         channel = words[2].startswith('#') and words[2] or target
83     if words[1] == 'start':
84         if channel in logs:
85             return 'PRIVMSG', (replyto, 'Logging %s already started' % channel)
86         cmd = []
87         if channel.startswith('#') and channel != target:
88             cmd.append(('CMD', ('JOIN %s' % channel,)))
89         name = utils.make_log(channel)
90         f = open(name, 'w')
91         logs[channel] = (name, f)
92         cmd.append(('PRIVMSG', (replyto, 'Start logging %s...' % channel)))
93         return cmd
94
95     elif words[1] == 'stop':
96         if not channel in logs:
97             return 'PRIVMSG', (replyto, 'Logging %s not started yet' % channel)
98         (name, f) = logs.pop(channel)
99         f.close()
100
101         to_address = None
102         if len(words) >= 3 and words[-1] != channel:
103             to_address = words[-1]
104         if to_address:
105             unrecognized = utils.email_log(to_address, channel, name, aliases)
106             cmd = [('PRIVMSG', (replyto, 'Unrecognized user %s' % user)) for user in unrecognized]
107         return [('PRIVMSG', (replyto, 'Stop logging %s.  Saved log file (%s)' % (channel, name)))] + cmd
108
109     elif words[1] == 'list':
110         return 'PRIVMSG', (replyto, 'Logging: %s' % ','.join(logs.keys()))
111 op_commands['log'] = log_command
112
113 def join_command(target, replyto, words):
114     index = words.index('join') + 1
115     if index < len(words):
116         channel = words[index]
117         return 'CMD', ('JOIN %s' % (channel.startswith('#') and channel or '#' + channel),)
118 op_commands['join'] = join_command
119
120 def privmsg_command(target, replyto, words):
121     if len(words) < 3:
122         return
123     return ('PRIVMSG', (words[1], ' '.join(words[2:])))
124 op_commands['privmsg'] = privmsg_command
125
126 def restart_command(target, replyto, words):
127     return ('RESTART', ())
128 op_commands['restart'] = restart_command
129
130 def do_command(who, target, replyto, words):
131     if not words:
132         return
133     command = words[0].lower()
134
135     if command in commands:
136         return commands[command](target, replyto, words)
137     if utils.nick_from_mask(who) in op_members and command in op_commands:
138         return op_commands[command](target, replyto, words)