forked from greghaynes/NagBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
80 lines (62 loc) · 2.18 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
from twisted.words.protocols import irc
from twisted.internet import ssl, reactor, protocol
from twisted.internet.error import DNSLookupError
from twisted.python import log
from nagger import Nagger
from controller import NagController
import sys
import ConfigParser
import re
import json
import random
nicks = open('names.txt')
class NagBotMessageHandler(object):
def __init__(self, bot):
self.bot = bot
self.bot_re = re.compile('^' + self.bot.nickname)
self.controller = NagController(bot)
self.nagger = Nagger(bot)
def handleMessage(self, sender, receiver, message):
if receiver == self.bot.nickname or self.bot_re.match(message):
"""Message to the bot"""
message = message[message.find(' ')+1:]
self.controller.handleMessageToBot(sender, receiver, message, receiver == self.bot.nickname)
else:
self.nagger.handlePublicMessage(sender, receiver, message)
class NagBot(irc.IRCClient):
"""A bot that sends notifications based on irc message regexes"""
nickname = "nagbot"
def __init__(self):
self.message_handler = NagBotMessageHandler(self)
def connectionMade(self):
irc.IRCClient.connectionMade(self)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
for channel in self.factory.channels:
self.join(channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self.say(channel, "\x034Hello!")
def irc_PRIVMSG(self, prefix, params):
self.message_handler.handleMessage(prefix, params[0], params[1])
class NagBotFactory(protocol.ClientFactory):
protocol = NagBot
channels = ('#cs305',)
def __init__(self, configpath):
self.config = ConfigParser.ConfigParser()
self.config.read(configpath)
def clientConnectionFailed(self, connector, reason):
print "Connection failed:", reason
reactor.stop()
if __name__ == '__main__':
# create factory protocol and application
try:
f = NagBotFactory('test.conf')
except IndexError:
print 'Supply the path to a valid config file as an argument'
else:
for i in range(1):
reactor.connectSSL("irc.cat.pdx.edu", 6697, f, ssl.ClientContextFactory())
reactor.run()