-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.py
152 lines (122 loc) · 4.44 KB
/
irc.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
# system imports
import time, sys
# Local imports
from actions import config, bot, security
class client(irc.IRCClient):
"""
An IRC bot...
That does stuff...
"""
def __init__(self, nick, **k):
'''Call the other functions we\'ll need to do stuff'''
self.nick = nick
self.nickname = nick
self.bot = bot.worker(self.nick)
# self.logger = bot.logger
self.channels = bot.channelPool()
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."""
print ('/joined '+ channel)
self.bot.channelPool.joined(channel)
def left(self, channel):
"""This will get called when the bot joins the channel."""
print ('/left '+ channel)
self.bot.channelPool.parted(channel)
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
print("user: '%s' channel: '%s' message: '%s' " % (user, channel, msg))
def restart():
print('Trying to restart')
self.msg(suser, 'attempting reload bot...')
self.bot.wisper(suser, 'storeing session...')
self.bot.save()
reload(bot)
self.msg(suser, 'reload complete!')
self.bot = bot.worker(self.nick)
self.bot.wisper(suser, 'handeler reset...')
self.bot.wisper(suser, 'loading session...')
self.bot.load()
self.bot.wisper(suser, 'session loaded...')
if channel == self.nick:
suser = user.split('!',1)[0]
pvmsg = True
else:
suser = channel
pvmsg = False
if msg == "!backdoor" and pvmsg:
restart()
elif msg == '!restart':
if self.bot.auth.owner(user):
restart()
else:
self.msg(suser, 'you\'re not my mommy...')
else:
self.bot.msgin(user, channel, msg)
jobs = self.bot.queue()
for job in jobs:
do, target, params = job
action = getattr(self, do, None)
try:
if action is not None:
action(target, params)
except:
print(action)
print(target)
print(params)
print('!!!!!Not implemented: ' + do + ' !!!!!')
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
pass
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
pass
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 clientFactory(protocol.ClientFactory):
"""
A factory for irc Bots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, channel, nick):
self.channel = channel
self.nick = nick
def buildProtocol(self, addr):
p = client(self.nick)
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
print("connection failed:", reason)
reactor.stop()
def connect(server, port, channel, nick):
# initialize logging to stdout
log.startLogging(sys.stdout)
# create factory protocol and application
factory = clientFactory(channel, nick)
# connect factory to this host and port
## todo error checking for port
reactor.connectTCP(server, port, factory)
# run bot
## start running in ractorbase in base.py (good luck)
reactor.run()
def main():
connect(config.server, config.port, config.channel, config.nick)
if __name__ == '__main__':
main()