-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbot.py
executable file
·188 lines (153 loc) · 6.21 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import os
import sys
import struct
import time
import lib.commands
from imp import load_source
from ConfigParser import ConfigParser
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_uh, is_channel
from lib.modules.logger import Logger
from lib.modules.daemonize import Daemonize
from lib.modules.edbot import Edbot
reload(sys)
sys.setdefaultencoding('utf-8')
class Commands(object):
commands = {}
def __init__(self, parent, cmd, event):
self.parent = parent
self.cmd = cmd.strip('!')
self.event = event
for command in os.listdir(lib.commands.__file__.rsplit("/", 1)[0]):
if command.endswith('.py') and command not in ['__init__.py', 'Base_Command.py']:
name = command.rstrip('.py')
self.commands[name] = load_source(name, os.path.join(lib.commands.__file__.rsplit("/", 1)[0], command))
def run(self):
if self.cmd in self.commands:
obj = getattr(self.commands[self.cmd], self.cmd.capitalize())(self.parent, self.event)
obj.run()
class Bot(SingleServerIRCBot):
def __init__(self, channel, nickname, password, server, port=6667):
self.dcc_received = {}
self.nickname = nickname
self.password = password
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname, reconnection_interval=60)
self.channel = channel
self.conn = self.connection
self.messages = []
self.log = Logger()
self.start()
self.connected_checker()
def connected_checker(self):
if not self.conn.is_connected():
self.start()
self.conn.execute_delayed(self.reconnection_interval, self.connected_checker, ())
def on_join(self, c, e):
nick = nm_to_n(e.source())
host = nm_to_uh(e.source())
self.log('%s [%s] has joined %s' % (nick, host, str(self.channel)))
def on_quit(self, c, e):
nick = nm_to_n(e.source())
host = nm_to_uh(e.source())
self.log('%s [%s] has quit %s' % (nick, host, str(self.channel)))
def on_part(self, c, e):
nick = nm_to_n(e.source())
host = nm_to_uh(e.source())
self.log('%s [%s] has left %s' % (nick, host, str(self.channel)))
def on_kick(self, c, e):
nick = nm_to_n(e.source())
kicked = e.arguments()[0]
self.log('%s was kicked off %s by %s' % (kicked, str(self.channel), nick))
def on_welcome(self, c, e):
c.privmsg('NickServ', 'IDENTIFY %s' % self.password)
for channel in self.channel:
c.join(channel)
c.privmsg('chanserv', 'set flood_protection off')
def on_privmsg(self, c, e):
self.do_command(c, e)
def on_pubmsg(self, c, e):
self.do_command(c, e)
def on_ctcp(self, c, e):
"""Default handler for ctcp events.
Replies to VERSION and PING requests and relays DCC requests
to the on_dccchat method.
ncode: Adding dccreceive support
"""
if e.arguments()[0] == "VERSION":
c.ctcp_reply(nm_to_n(e.source()), "VERSION " + self.get_version())
elif e.arguments()[0] == "PING":
if len(e.arguments()) > 1:
c.ctcp_reply(nm_to_n(e.source()), "PING " + e.arguments()[1])
elif e.arguments()[0] == "DCC" and e.arguments()[1].split(" ", 1)[0] == "CHAT":
self.on_dccchat(c, e)
elif e.arguments()[0] == "DCC" and e.arguments()[1].split(" ", 1)[0] == "SEND":
self.filename = "/tmp/%s" % os.path.basename(args[1])
if os.path.exists(self.filename):
print "A file named", self.filename,
print "already exists. Refusing to save it."
self.file = open(self.filename, "w")
peeraddress = irclib.ip_numstr_to_quad(args[2])
peerport = int(args[3])
self.dcc = self.dcc_connect(peeraddress, peerport, "raw")
def on_dccmsg(self, connection, event):
data = event.arguments()[0]
self.file.write(data)
self.received_bytes = self.received_bytes + len(data)
self.dcc.privmsg(struct.pack("!I", self.received_bytes))
def on_dcc_disconnect(self, connection, event):
self.file.close()
print "Received file %s (%d bytes)." % (self.filename, self.received_bytes)
def anti_flood(self, nick, cmd):
curtime = time.mktime(time.gmtime())
timestamp = curtime - 60
count = 0
self.messages.append("%f %s %s" % (curtime, nick, cmd))
if len(self.messages) > 200:
del self.messages[0]
for line in self.messages:
t, n, c = line.split()
if nick in line and cmd in line and float(t) > timestamp:
count += 1
return count
def do_command(self, c, e):
message = e.arguments()[0].strip()
if message:
cmd = message.strip().split()[0]
nick = nm_to_n(e.source())
if cmd.startswith('!'):
if self.anti_flood(nick, cmd) >= 4:
pass
else:
command = Commands(self, cmd, e)
command.run()
else:
if cmd.strip(':') == self.nickname:
edbot = Edbot()
result = edbot.answer(message)
if result:
self.conn.privmsg(e.target(), '%s %s' % (nick, result))
if is_channel(e.target()):
self.log('%s: %s - %s' % (self.channel, nick, message))
else:
self.log('pvt: %s - %s' % (nick, message))
def main():
config = ConfigParser()
section = 'lokky'
try:
config.read("conf/lokky.cfg")
except Exception, e:
self.log("Error while reading the config file: %s" % e)
return False
channel = config.get(section, 'channel').split(',')
nick = config.get(section, 'nickname')
passwd = config.get(section, 'password')
network = config.get(section, 'network')
port = config.getint(section, 'port')
bot = Bot(channel, nick, passwd, network, port)
if __name__ == "__main__":
daemon = Daemonize("log/startup.log")
daemon.start()
main()