-
Notifications
You must be signed in to change notification settings - Fork 0
/
memebot.py
69 lines (59 loc) · 1.96 KB
/
memebot.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
from ircbot import SingleServerIRCBot
from irclib import nm_to_n
from optparse import OptionParser
import re
import sys
class BaseBot(SingleServerIRCBot):
def __init__(self, options):
SingleServerIRCBot.__init__(
self,
[(options.host, options.port, None)],
options.nick,
options.nick)
self._nick = options.nick
self._channels_to_join = [c.lower() for c in options.channels]
if options.signal:
self._signal = options.signal
else:
self._signal = self._nick + ':'
def quit(self):
self.connection.disconnect('bye')
def on_welcome(self, c, e):
for channel in self._channels_to_join:
c.join(channel)
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + '_')
def on_invite(self, c, e):
c.join(e.arguments()[0])
def _act(self, c, target, msg):
pass
def on_pubmsg(self, c, e):
message = e.arguments()[0].strip()
if not re.match(self._signal, message):
return
if self._signal == self._nick + ':':
message_without_signal = message[len(self._signal):].strip()
self._act(c, e.target(), message_without_signal)
else:
self._act(c, e.target(), message)
def startBot(bot_class, options):
bot = bot_class(options)
try:
bot.start()
except KeyboardInterrupt:
bot.quit()
def parseArgs(parser=OptionParser()):
def helpExit(msg):
print msg
sys.exit(1)
parser.add_option('--host', dest='host', help='host to connect to')
parser.add_option('--port', dest='port', type='int',
help='port to connect to')
parser.add_option('--nick', dest='nick', help='nick to join with')
parser.add_option('--signal', dest='signal', help='regexp to match messages to listen to, default ^nick:')
parser.add_option('--channel', dest='channels', action='append',
help='channels to join')
options, args = parser.parse_args()
if not options.channels:
helpExit('must specify at least one channel with --channel')
return options, args