-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisobot.py
executable file
·136 lines (113 loc) · 4.11 KB
/
isobot.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
#!/usr/bin/python
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
# system imports
import time, sys
import re
import urllib
import tiny_settings
import tinyurl
class IsoBot(irc.IRCClient):
def __init__(self):
self.nickname = tiny_settings.nickname
reactor.callLater(30,self.ping_pong)
def ping_pong(self):
self.sendLine("PING :Ph33r!")
reactor.callLater(30,self.ping_pong)
def connectionMade(self):
irc.IRCClient.connectionMade(self)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
if tiny_settings.x_login:
self.msg("[email protected]", tiny_settings.x_login)
self.mode(self.nickname, '+', 'ix')
def join_channels(self):
for channel, key in tiny_settings.channels.items():
self.join(channel, key)
def irc_unknown(self, prefix, command, params):
# Ignore these commands
if command in [
"PONG",
"333", # RPL_TOPICWHOTIME
"RPL_NAMREPLY",
"RPL_ENDOFNAMES",
"ERR_NOMOTD",
]:
return
# Display these, they might be important
if command in ["ERR_BADCHANNELKEY"]:
print "WARNING:",command,params
return
# Our host is now hidden, we can join channels now
if command == "396": # RPL_HOSTHIDDEN
self.join_channels()
return
if command == "INVITE":
who,channel = params[:2]
print "Invited to",channel,"by",prefix
if channel in tiny_settings.channels:
self.join(channel,tiny_settings.channels[channel])
else:
self.msg(prefix,"I don't like that channel")
return
# NFI?
print "Unknown command:",command,params
def notice(self, user, channel, msg):
print user,"NOTICE",channel,msg
def invite(self, user, channel, msg):
pass
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
print user, "PRIVMSG", channel, msg
if "@" not in user and "." in user:
# Server message
return
if channel == 'AUTH':
# server message during connect
return
target = channel # where to send reply
reply = None
if target == self.nickname:
target = user
reply = 'sorry, I\'m just a bot'
else:
# see if this message should get a response
reply = tinyurl.tiny(user, channel, msg)
if reply is not None:
# get 1st line only, if we have multi-line response
while "\n" in reply or "\r" in reply:
reply=reply.split("\r",1)[0]
reply=reply.split("\n",1)[0]
if reply is not None:
print "PRIVMSG reply", target, reply
for line in reply.split("\n"):
# channel messages will already be set to single line (above),
# so we'll only send multiline responses to a user
self.msg(target, line)
class IsoBotFactory(protocol.ClientFactory):
# the class of the protocol to build when new connection is made
protocol = IsoBot
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
time.sleep(30)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
time.sleep(30)
connector.connect()
if __name__ == '__main__':
# initialize logging
# create factory protocol and application
f = IsoBotFactory()
# connect factory to this host and port
name, ip = tiny_settings.ircserver
print "connecting to %s:%d" % (name, ip)
reactor.connectTCP(name, ip, f)
# run bot
reactor.run()
# vi:et:ts=4:sw=4