-
Notifications
You must be signed in to change notification settings - Fork 0
/
robocop.py
executable file
·134 lines (113 loc) · 5.09 KB
/
robocop.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
#! /usr/bin/env python3
"""
Robocop!
"""
import logging
import irc.bot
import irc.client
import config
import handler
import commands
import templates
class Robocop(irc.bot.SingleServerIRCBot):
""" Robocop irc bot to aid in administration of a channel """
def __init__(self):
logging.info("Connecting to server...")
server = irc.bot.ServerSpec(config.server, port=config.port, password=config.password)
irc.bot.SingleServerIRCBot.__init__(self, [server], config.nickname, "Robocop")
logging.info("Registering commands..."),
self.modhandler = handler.Handler()
self.modhandler.register_default(commands.modchannel_default)
for command in commands.modchannel:
self.modhandler.register(command[0], command[1])
logging.debug("50%% done...")
self.ophandler = handler.Handler()
self.ophandler.register_default(commands.opchannel_default)
for command in commands.opchannel:
self.ophandler.register(command[0], command[1])
self.adminhandler = handler.Handler()
self.adminhandler.register_default(commands.admin_default)
for command in commands.admin:
self.adminhandler.register(command[0], command[1])
self.ratelimit = []
self.rl_cooldown = 0
logging.debug("Loading templates")
self.templates = templates.TemplateDB()
logging.info("Setup complete")
def reload_commands(self):
import imp
imp.reload(commands)
logging.info("Reloading commands")
self.modhandler = handler.Handler()
self.modhandler.register_default(commands.modchannel_default)
for command in commands.modchannel:
self.modhandler.register(command[0], command[1])
self.ophandler = handler.Handler()
self.ophandler.register_default(commands.opchannel_default)
for command in commands.opchannel:
self.ophandler.register(command[0], command[1])
self.adminhandler = handler.Handler()
self.adminhandler.register_default(commands.admin_default)
for command in commands.admin:
self.adminhandler.register(command[0], command[1])
commands.robocop = self
def on_welcome(self, connection, event):
logging.debug("Connecting to channels")
connection.privmsg("NickServ", "identify %s %s" % (config.password, config.nickname))
logging.debug("Identifiying...")
connection.privmsg("ChanServ", "invite %s" % (config.opchannel))
connection.join(config.modchannel)
connection.privmsg("ChanServ", "op %s" % (config.modchannel))
logging.debug("Connected to channels")
connection.join(config.opchannel)
logging.debug("Identified and opped on mod channel")
def on_privmsg(self, connection, event):
logging.debug(', '.join([event.type, event.source.nick, event.source.userhost, event.source.host, event.source.user, event.target]))
if event.source.nick in config.admins:
logging.debug("Handling admin command")
# This should be handled here
if event.arguments[0][0:7] == ".reload":
self.reload_commands()
else:
self.adminhandler.handle(connection, event)
else:
commands.privmsg(connection, event)
def on_pubmsg(self, connection, event):
logging.debug(', '.join([event.type, event.source.nick, event.source.userhost, event.source.host, event.source.user, event.target]))
# now = time.time()
# if self.rl_cooldown < now - 30:
# self.ratelimit.append((event.source.nick, now))
#
# while len(self.ratelimit) > 0 and self.ratelimit[0][1] < now - 10:
# self.ratelimit.pop(0)
# count = len([nick for nick in self.ratelimit if nick[0] == event.source.nick])
# if count >= 4:
# connection.privmsg(config.opchannel, "%s tripped flood guard." % (event.source.nick))
# self.rl_cooldown = now
# fakeevent = irc.client.Event("fake", config.nickname, config.opchannel, arguments=[".mute %s %d Automatic flood control" %(event.source.nick, 5)])
# print(fakeevent.arguments)
# logging.debug("Generated fake event")
# self.ophandler.handle(connection, fakeevent)
# logging.debug("Muted offender")
# elif len(self.ratelimit) > 10:
# connection.privmsg(config.opchannel, "General flood warning!")
# self.rl_cooldown = now
if event.target == config.modchannel:
self.modhandler.handle(connection, event)
elif event.target == config.opchannel:
self.ophandler.handle(connection, event)
def get_version(self):
return "Robocop 0.l.2"
def main():
logging.basicConfig(filename="robocop.log", level=logging.DEBUG)
#logging.basicConfig(level=logging.DEBUG)
global robocop
try:
robocop = Robocop()
except Exception as e:
print(e)
else:
commands.robocop = robocop
robocop.start()
if __name__ == "__main__":
main()