-
Notifications
You must be signed in to change notification settings - Fork 19
/
bot.py
99 lines (82 loc) · 2.84 KB
/
bot.py
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
import gevent
import json
import argparse
from irc import IRCBot, run_bot
from gevent import monkey
from services.lib.api import load_config, get_redis_client
monkey.patch_all()
parser = argparse.ArgumentParser(
description='ZenIRCBot, a new and different bot',
)
parser.add_argument('-c', '--config', default='./bot.json',
help='The config to use. (default: %(default)s)')
opts = parser.parse_args()
config = load_config(opts.config)
pub = get_redis_client(config['redis'])
class RelayBot(IRCBot):
def __init__(self, *args, **kwargs):
super(RelayBot, self).__init__(*args, **kwargs)
pub.set('zenircbot:nick', self.conn.nick)
gevent.spawn(self.do_sub)
def do_sub(self):
sub = get_redis_client(config['redis'])
self.pubsub = sub.pubsub()
self.pubsub.subscribe('out')
for msg in self.pubsub.listen():
if msg['type'] != 'message':
continue
message = json.loads(msg['data'])
print 'Got %s' % message
if message['version'] == 1:
if message['type'] == 'privmsg':
self.respond(message['data']['message'],
channel=message['data']['to'])
def do_privmsg(self, nick, message, channel):
if not channel:
channel = nick
to_publish = json.dumps({
'version': 1,
'type': 'privmsg',
'data': {
'sender': nick,
'channel': channel,
'message': message,
},
})
pub.publish('in', to_publish)
print 'Sending to in %s' % to_publish
def do_part(self, nick, command, channel):
to_publish = json.dumps({
'version': 1,
'type': 'part',
'data': {
'sender': nick,
'channel': channel,
}
})
pub.publish('in', to_publish)
print 'Sending to in %s' % to_publish
def do_quit(self, command, nick, channel):
to_publish = json.dumps({
'version': 1,
'type': 'quit',
'data': {
'sender': nick,
}
})
pub.publish('in', to_publish)
print 'Sending to in %s' % to_publish
def do_nick(self, old_nick, command, new_nick):
if pub.get('zenircbot:nick') == old_nick:
pub.set('zenircbot:nick', new_nick)
print 'nick change: %s -> %s' % (old_nick, new_nick)
def command_patterns(self):
return (
('/privmsg', self.do_privmsg),
('/part', self.do_part),
('/quit', self.do_quit),
('/nick', self.do_nick),
)
run_bot(RelayBot, config['servers'][0]['hostname'],
config['servers'][0]['port'], config['servers'][0]['nick'],
config['servers'][0]['channels'])