-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
138 lines (111 loc) · 4.48 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/env python
import ConfigParser
import datetime
import functions
import sys
import time
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol, ssl
from twisted.python import log
from twisted.protocols.policies import TrafficLoggingFactory
from functions import Seen
class CatBot(irc.IRCClient):
def __init__(self, nickname, password):
self.nickname = nickname
self.password = password
self.seen = Seen()
def dataReceived(self, bytes):
print "Got", repr(bytes)
# Make sure to up-call - otherwise all of the IRC logic is disabled!
return irc.IRCClient.dataReceived(self, bytes)
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."""
self.join(self.factory.channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self.setNick("CatBot")
self.seen.load_dict()
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
self.seen.update_dict(user, msg, str(datetime.datetime.utcnow()))
self.seen.store_dict()
user = user.split('!', 1)[0]
#Check to see if they're executing the user command
if msg.startswith("!"):
self.parseFunctions(user, channel, msg)
# Check to see if they're sending me a private message
elif channel == self.nickname:
msg = "Go away"
self.msg(user, msg)
# Otherwise check to see if it is a message directed at me
elif msg.startswith(self.nickname + ":"):
msg = "Meow {usr}".format(usr=user)
self.msg(channel, msg)
def parseFunctions(self, user, channel, msg):
"""Executes all of the function commands"""
if msg.startswith("!user"):
msg = functions.get_user(msg)
elif msg.startswith("!weather"):
msg = functions.weather(msg)
elif msg.startswith("!jerkcity"):
msg = functions.jerkcity()
elif msg.startswith("!seen"):
msg = self.seen.get_seen(msg)
else:
msg = "Invalid Request"
self.msg(channel, msg)
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
self.seen.update_dict(user, msg, str(datetime.datetime.utcnow()))
user = user.split('!', 1)[0]
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
def alterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
return nickname + '^'
class CatBotFactory(protocol.ClientFactory):
"""
A factory for CatBots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, config):
self.channel = config.get("irc", "channel")
self.nickname = config.get("irc", "nickname")
self.password = config.get("irc", "password")
def buildProtocol(self, addr):
p = CatBot(self.nickname, self.password)
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
print "Connection lost - goodbye!"
reactor.stop()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
if __name__ == '__main__':
# initialize logging
log.startLogging(sys.stdout)
try:
#open config file
config = ConfigParser.ConfigParser()
config.read(sys.argv[1])
# create factory protocol and application
f = CatBotFactory(config)
# connect factory to this host and port
reactor.connectSSL(config.get("irc", "host"), int(config.get("irc", "port")), f, ssl.ClientContextFactory())
# run bot
reactor.run()
except IndexError:
print "Correct usage is python bot.py <config_filename.ini>"