2018-08-31 11:55:52 +00:00
|
|
|
import os, select, sys, threading, time, traceback, uuid
|
2018-09-30 18:43:20 +00:00
|
|
|
from . import EventManager, Exports, IRCServer, Logging, ModuleManager
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Bot(object):
|
2018-09-29 11:53:39 +00:00
|
|
|
def __init__(self, directory, args, cache, config, database, events,
|
2018-09-30 18:43:20 +00:00
|
|
|
exports, log, modules, timers):
|
2018-09-29 08:24:26 +00:00
|
|
|
self.directory = directory
|
2018-09-28 15:51:36 +00:00
|
|
|
self.args = args
|
2018-09-29 11:53:39 +00:00
|
|
|
self.cache = cache
|
2018-09-28 15:51:36 +00:00
|
|
|
self.config = config
|
|
|
|
self.database = database
|
|
|
|
self._events = events
|
|
|
|
self._exports = exports
|
|
|
|
self.log = log
|
|
|
|
self.modules = modules
|
|
|
|
self.timers = timers
|
|
|
|
|
|
|
|
events.on("timer.reconnect").hook(self.reconnect)
|
2018-09-11 17:24:34 +00:00
|
|
|
self.start_time = time.time()
|
2016-03-29 11:56:58 +00:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
self.servers = {}
|
|
|
|
self.running = True
|
|
|
|
self.poll = select.epoll()
|
|
|
|
|
2018-09-21 10:34:38 +00:00
|
|
|
def add_server(self, server_id, connect=True):
|
|
|
|
(_, alias, hostname, port, password, ipv4, tls, nickname,
|
|
|
|
username, realname) = self.database.servers.get(server_id)
|
|
|
|
|
|
|
|
new_server = IRCServer.Server(self, self._events, server_id, alias,
|
2018-09-19 00:13:54 +00:00
|
|
|
hostname, port, password, ipv4, tls, nickname, username,
|
|
|
|
realname)
|
2016-04-27 10:56:37 +00:00
|
|
|
if not new_server.get_setting("connect", True):
|
|
|
|
return
|
2018-09-19 12:25:12 +00:00
|
|
|
self._events.on("new.server").call(server=new_server)
|
2016-04-27 10:56:37 +00:00
|
|
|
if connect and new_server.get_setting("connect", True):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.connect(new_server)
|
2016-07-05 11:18:13 +00:00
|
|
|
return new_server
|
2018-09-30 12:28:26 +00:00
|
|
|
|
|
|
|
def get_server(self, id):
|
|
|
|
for server in self.servers.values():
|
|
|
|
if server.id == id:
|
|
|
|
return server
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def connect(self, server):
|
|
|
|
try:
|
|
|
|
server.connect()
|
|
|
|
except:
|
|
|
|
sys.stderr.write("Failed to connect to %s\n" % str(server))
|
|
|
|
traceback.print_exc()
|
|
|
|
return False
|
2018-07-02 11:08:26 +00:00
|
|
|
self.servers[server.fileno()] = server
|
2016-03-30 18:32:14 +00:00
|
|
|
self.poll.register(server.fileno(), select.EPOLLOUT)
|
2016-03-29 11:56:58 +00:00
|
|
|
return True
|
2017-01-27 21:41:47 +00:00
|
|
|
|
2018-08-28 14:32:50 +00:00
|
|
|
def next_send(self):
|
2018-08-28 13:36:16 +00:00
|
|
|
next = None
|
|
|
|
for server in self.servers.values():
|
2018-08-28 14:32:50 +00:00
|
|
|
timeout = server.send_throttle_timeout()
|
2018-08-29 13:33:27 +00:00
|
|
|
if server.waiting_send() and (next == None or timeout < next):
|
2018-08-28 13:36:16 +00:00
|
|
|
next = timeout
|
|
|
|
return next
|
|
|
|
|
2018-08-29 13:33:27 +00:00
|
|
|
def next_ping(self):
|
|
|
|
timeouts = []
|
|
|
|
for server in self.servers.values():
|
2018-09-11 17:25:27 +00:00
|
|
|
timeout = server.until_next_ping()
|
|
|
|
if not timeout == None:
|
|
|
|
timeouts.append(timeout)
|
2018-08-30 16:20:55 +00:00
|
|
|
if not timeouts:
|
|
|
|
return None
|
2018-08-29 13:33:27 +00:00
|
|
|
return min(timeouts)
|
|
|
|
def next_read_timeout(self):
|
|
|
|
timeouts = []
|
|
|
|
for server in self.servers.values():
|
|
|
|
timeouts.append(server.until_read_timeout())
|
2018-08-30 16:20:55 +00:00
|
|
|
if not timeouts:
|
|
|
|
return None
|
2018-08-29 13:33:27 +00:00
|
|
|
return min(timeouts)
|
|
|
|
|
2018-08-28 13:36:16 +00:00
|
|
|
def get_poll_timeout(self):
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts = []
|
2018-09-28 15:51:36 +00:00
|
|
|
timeouts.append(self.timers.next())
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts.append(self.next_send())
|
|
|
|
timeouts.append(self.next_ping())
|
|
|
|
timeouts.append(self.next_read_timeout())
|
2018-09-29 11:53:39 +00:00
|
|
|
timeouts.append(self.cache.next_expiration())
|
2018-08-29 13:33:27 +00:00
|
|
|
return min([timeout for timeout in timeouts if not timeout == None])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def register_read(self, server):
|
|
|
|
self.poll.modify(server.fileno(), select.EPOLLIN)
|
|
|
|
def register_write(self, server):
|
|
|
|
self.poll.modify(server.fileno(), select.EPOLLOUT)
|
|
|
|
def register_both(self, server):
|
|
|
|
self.poll.modify(server.fileno(),
|
|
|
|
select.EPOLLIN|select.EPOLLOUT)
|
|
|
|
|
2016-03-30 18:32:14 +00:00
|
|
|
def disconnect(self, server):
|
2016-07-14 08:17:41 +00:00
|
|
|
try:
|
|
|
|
self.poll.unregister(server.fileno())
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2016-03-30 18:32:14 +00:00
|
|
|
del self.servers[server.fileno()]
|
|
|
|
|
2016-04-19 11:21:27 +00:00
|
|
|
def reconnect(self, event):
|
2018-09-27 09:22:57 +00:00
|
|
|
server = self.add_server(event["server_id"], False)
|
2016-03-30 18:32:14 +00:00
|
|
|
if self.connect(server):
|
|
|
|
self.servers[server.fileno()] = server
|
2016-04-03 12:20:05 +00:00
|
|
|
else:
|
2016-04-19 11:21:27 +00:00
|
|
|
event["timer"].redo()
|
2016-04-14 15:48:17 +00:00
|
|
|
|
|
|
|
def set_setting(self, setting, value):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.bot_settings.set(setting, value)
|
2016-04-14 15:48:17 +00:00
|
|
|
def get_setting(self, setting, default=None):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.get(setting, default)
|
2016-04-14 15:48:17 +00:00
|
|
|
def find_settings(self, pattern, default=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.find(pattern, default)
|
2018-08-03 12:43:45 +00:00
|
|
|
def find_settings_prefix(self, prefix, default=[]):
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.find_prefix(
|
2018-08-03 12:43:45 +00:00
|
|
|
prefix, default)
|
2016-04-14 15:48:17 +00:00
|
|
|
def del_setting(self, setting):
|
2018-08-06 13:10:14 +00:00
|
|
|
self.database.bot_settings.delete(setting)
|
2016-04-14 15:48:17 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def run(self):
|
|
|
|
while self.running:
|
|
|
|
self.lock.acquire()
|
2018-08-28 13:36:16 +00:00
|
|
|
events = self.poll.poll(self.get_poll_timeout())
|
2018-09-28 15:51:36 +00:00
|
|
|
self.timers.call()
|
2018-09-29 11:53:39 +00:00
|
|
|
self.cache.expire()
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
for fd, event in events:
|
|
|
|
if fd in self.servers:
|
|
|
|
server = self.servers[fd]
|
|
|
|
if event & select.EPOLLIN:
|
|
|
|
lines = server.read()
|
|
|
|
for line in lines:
|
2018-10-01 12:48:22 +00:00
|
|
|
self.log.debug("%s (raw) | %s", [str(server), line])
|
2016-03-29 11:56:58 +00:00
|
|
|
server.parse_line(line)
|
|
|
|
elif event & select.EPOLLOUT:
|
|
|
|
server._send()
|
|
|
|
self.register_read(server)
|
|
|
|
elif event & select.EPULLHUP:
|
|
|
|
print("hangup")
|
2016-04-10 16:29:03 +00:00
|
|
|
server.disconnect()
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
for server in list(self.servers.values()):
|
2018-08-29 13:33:27 +00:00
|
|
|
if server.read_timed_out():
|
|
|
|
print("pingout from %s" % str(server))
|
|
|
|
server.disconnect()
|
|
|
|
elif server.ping_due() and not server.ping_sent:
|
2018-09-11 17:25:01 +00:00
|
|
|
server.send_ping()
|
|
|
|
server.ping_sent = True
|
2016-03-29 11:56:58 +00:00
|
|
|
if not server.connected:
|
2016-03-30 18:32:14 +00:00
|
|
|
self.disconnect(server)
|
|
|
|
|
|
|
|
reconnect_delay = self.config.get("reconnect-delay", 10)
|
2018-09-28 15:51:36 +00:00
|
|
|
self.timers.add("reconnect", reconnect_delay,
|
2016-06-27 23:09:05 +00:00
|
|
|
server_id=server.id)
|
2016-03-30 18:32:14 +00:00
|
|
|
|
|
|
|
print("disconnected from %s, reconnecting in %d seconds" % (
|
|
|
|
str(server), reconnect_delay))
|
2018-08-28 14:32:50 +00:00
|
|
|
elif server.waiting_send() and server.throttle_done():
|
2016-03-29 11:56:58 +00:00
|
|
|
self.register_both(server)
|
|
|
|
self.lock.release()
|