2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2019-10-27 10:19:00 +00:00
|
|
|
from src import IRCLine, ModuleManager, utils
|
2016-05-17 15:14:23 +00:00
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-09-27 11:26:14 +00:00
|
|
|
@utils.hook("received.command.nick", min_args=1)
|
2016-05-17 15:14:23 +00:00
|
|
|
def change_nickname(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Change my nickname
|
|
|
|
:usage: <nickname>
|
|
|
|
:permission: changenickname
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-05-17 15:14:23 +00:00
|
|
|
nickname = event["args_split"][0]
|
|
|
|
event["server"].send_nick(nickname)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.raw", min_args=1)
|
2016-05-17 15:14:23 +00:00
|
|
|
def raw(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Send a line of raw IRC data
|
|
|
|
:usage: <raw line>
|
|
|
|
:permission: raw
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-11-27 17:38:17 +00:00
|
|
|
if IRCLine.is_human(event["args"]):
|
|
|
|
line = IRCLine.parse_human(event["args"])
|
|
|
|
else:
|
|
|
|
line = IRCLine.parse_line(event["args"])
|
|
|
|
line = event["server"].send(line)
|
|
|
|
|
2019-09-07 11:50:50 +00:00
|
|
|
if not line == None:
|
|
|
|
event["stdout"].write("Sent: %s" % line.parsed_line.format())
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Line was filtered")
|
2016-06-24 08:17:27 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.part")
|
2016-06-24 08:17:27 +00:00
|
|
|
def part(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Part from the current or given channel
|
|
|
|
:usage: [channel]
|
|
|
|
:permission: part
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
if event["args"]:
|
|
|
|
target = event["args_split"][0]
|
|
|
|
elif event["is_channel"]:
|
|
|
|
target = event["target"].name
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No channel provided")
|
|
|
|
event["server"].send_part(target)
|
2018-09-03 17:21:13 +00:00
|
|
|
|
2019-10-03 04:15:30 +00:00
|
|
|
def _id_from_alias(self, alias):
|
|
|
|
return self.bot.database.servers.get_by_alias(alias)
|
|
|
|
def _server_from_alias(self, alias):
|
2019-10-10 17:02:55 +00:00
|
|
|
id, server = self._both_from_alias(alias)
|
|
|
|
return server
|
|
|
|
def _both_from_alias(self, alias):
|
2019-10-03 04:15:30 +00:00
|
|
|
id = self._id_from_alias(alias)
|
2019-09-27 11:35:28 +00:00
|
|
|
if id == None:
|
|
|
|
raise utils.EventError("Unknown server alias")
|
2019-10-10 17:02:55 +00:00
|
|
|
return id, self.bot.get_server_by_id(id)
|
2019-09-27 11:35:28 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.reconnect")
|
2018-09-03 17:21:13 +00:00
|
|
|
def reconnect(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Reconnect to the current network
|
|
|
|
:permission: reconnect
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-09-27 11:35:28 +00:00
|
|
|
server = event["server"]
|
|
|
|
alias = str(event["server"])
|
|
|
|
if event["args"]:
|
|
|
|
alias = event["args_split"][0]
|
2019-10-03 04:15:30 +00:00
|
|
|
server = self._server_from_alias(alias)
|
2019-09-27 11:35:28 +00:00
|
|
|
|
|
|
|
if server:
|
|
|
|
line = server.send_quit("Reconnecting")
|
|
|
|
line.events.on("send").hook(lambda e: self.bot.reconnect(
|
|
|
|
server.id, server.connection_params))
|
|
|
|
if not server == event["server"]:
|
|
|
|
event["stdout"].write("Reconnecting to %s" % alias)
|
|
|
|
else:
|
|
|
|
event["stdout"].write("Not connected to %s" % alias)
|
2018-10-03 14:59:36 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.connect", min_args=1)
|
|
|
|
def connect(self, event):
|
|
|
|
"""
|
|
|
|
:help: Connect to a network
|
|
|
|
:usage: <server id>
|
2018-11-20 14:27:24 +00:00
|
|
|
:permission: connect
|
2018-10-03 14:59:36 +00:00
|
|
|
"""
|
2019-10-03 04:19:16 +00:00
|
|
|
alias = event["args_split"][0]
|
|
|
|
server = self._server_from_alias(alias)
|
2019-09-27 11:35:28 +00:00
|
|
|
if server:
|
2019-10-25 12:39:00 +00:00
|
|
|
raise utils.EventError("Already connected to %s" % str(server))
|
2018-10-16 14:09:58 +00:00
|
|
|
|
2019-10-03 04:15:30 +00:00
|
|
|
server = self.bot.add_server(self._id_from_alias(alias))
|
2018-10-03 14:59:36 +00:00
|
|
|
event["stdout"].write("Connecting to %s" % str(server))
|
2018-11-20 14:27:24 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.disconnect")
|
|
|
|
def disconnect(self, event):
|
|
|
|
"""
|
|
|
|
:help: Disconnect from a server
|
|
|
|
:usage: [server id]
|
|
|
|
:permission: disconnect
|
|
|
|
"""
|
2019-09-27 11:35:28 +00:00
|
|
|
server = event["server"]
|
2019-10-10 17:02:55 +00:00
|
|
|
id = -1
|
2019-09-27 11:35:28 +00:00
|
|
|
alias = str(event["server"])
|
2018-11-20 14:27:24 +00:00
|
|
|
if event["args"]:
|
2019-09-25 11:33:12 +00:00
|
|
|
alias = event["args_split"][0]
|
2019-10-10 17:02:55 +00:00
|
|
|
id, server = self._both_from_alias(alias)
|
2019-05-30 15:17:39 +00:00
|
|
|
|
2019-09-01 07:41:22 +00:00
|
|
|
if not server == None:
|
2019-09-25 11:33:12 +00:00
|
|
|
alias = str(server)
|
2019-09-01 07:41:22 +00:00
|
|
|
server.disconnect()
|
|
|
|
self.bot.disconnect(server)
|
2019-09-07 11:22:54 +00:00
|
|
|
elif id in self.bot.reconnections:
|
|
|
|
self.bot.reconnections[id].cancel()
|
|
|
|
del self.bot.reconnections[id]
|
2019-09-01 07:41:22 +00:00
|
|
|
else:
|
|
|
|
raise utils.EventError("Server not connected")
|
|
|
|
|
2019-09-27 11:35:28 +00:00
|
|
|
event["stdout"].write("Disconnected from %s" % alias)
|
2019-02-10 23:53:33 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.shutdown")
|
|
|
|
def shutdown(self, event):
|
|
|
|
"""
|
|
|
|
:help: Shutdown bot
|
|
|
|
:usage: [reason]
|
|
|
|
:permission: shutdown
|
|
|
|
"""
|
|
|
|
reason = event["args"] or ""
|
2019-02-10 23:54:31 +00:00
|
|
|
for server in self.bot.servers.values():
|
2019-02-10 23:53:33 +00:00
|
|
|
line = server.send_quit(reason)
|
2019-06-04 16:51:20 +00:00
|
|
|
line.events.on("send").hook(self._shutdown_hook(server))
|
2019-02-10 23:53:33 +00:00
|
|
|
def _shutdown_hook(self, server):
|
2019-06-04 16:51:20 +00:00
|
|
|
def shutdown(e):
|
2019-02-11 10:00:55 +00:00
|
|
|
server.disconnect()
|
|
|
|
self.bot.disconnect(server)
|
2019-02-11 10:09:39 +00:00
|
|
|
return shutdown
|
2019-05-21 09:16:20 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.addserver", min_args=3)
|
|
|
|
def add_server(self, event):
|
|
|
|
"""
|
|
|
|
:help: Add a new server
|
|
|
|
:usage: <alias> <hostname>:[+]<port> <nickname>!<username>[@<bindhost>]
|
|
|
|
:permission: addserver
|
|
|
|
"""
|
|
|
|
alias = event["args_split"][0]
|
|
|
|
hostname, sep, port = event["args_split"][1].partition(":")
|
|
|
|
tls = port.startswith("+")
|
2019-05-21 09:42:40 +00:00
|
|
|
port = port.lstrip("+")
|
2019-05-21 09:16:20 +00:00
|
|
|
|
|
|
|
if not hostname or not port or not port.isdigit():
|
|
|
|
raise utils.EventError("Please provide <hostname>:[+]<port>")
|
|
|
|
port = int(port)
|
|
|
|
|
2019-10-27 10:19:00 +00:00
|
|
|
hostmask = IRCLine.parse_hostmask(event["args_split"][2])
|
2019-05-21 09:16:20 +00:00
|
|
|
nickname = hostmask.nickname
|
2019-05-21 09:45:15 +00:00
|
|
|
username = hostmask.username or nickname
|
2019-05-21 09:16:20 +00:00
|
|
|
realname = nickname
|
|
|
|
bindhost = hostmask.hostname or None
|
|
|
|
|
|
|
|
try:
|
|
|
|
server_id = self.bot.database.servers.add(alias, hostname, port, "",
|
|
|
|
tls, bindhost, nickname, username, realname)
|
|
|
|
except Exception as e:
|
|
|
|
event["stderr"].write("Failed to add server")
|
|
|
|
self.log.error("failed to add server \"%s\"", [alias],
|
|
|
|
exc_info=True)
|
|
|
|
return
|
|
|
|
event["stdout"].write("Added server '%s'" % alias)
|
2019-07-04 12:15:51 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.editserver")
|
|
|
|
@utils.kwarg("min_args", 3)
|
|
|
|
@utils.kwarg("help", "Edit server details")
|
|
|
|
@utils.kwarg("usage", "<alias> <option> <value>")
|
|
|
|
@utils.kwarg("permission", "editserver")
|
|
|
|
def edit_server(self, event):
|
|
|
|
alias = event["args_split"][0]
|
2019-10-09 19:56:38 +00:00
|
|
|
server_id = self._id_from_alias(alias)
|
|
|
|
if server_id == None:
|
2019-07-04 12:15:51 +00:00
|
|
|
raise utils.EventError("Unknown server '%s'" % alias)
|
|
|
|
|
|
|
|
option = event["args_split"][1].lower()
|
|
|
|
value = " ".join(event["args_split"][2:])
|
|
|
|
value_parsed = None
|
|
|
|
|
|
|
|
if option == "hostname":
|
|
|
|
value_parsed = value
|
|
|
|
elif option == "port":
|
|
|
|
if not value.isdigit():
|
|
|
|
raise utils.EventError("Invalid port")
|
|
|
|
value_parsed = int(value.lstrip("0"))
|
|
|
|
elif option == "tls":
|
|
|
|
value_lower = value.lower()
|
|
|
|
if not value_lower in ["yes", "no"]:
|
|
|
|
raise utils.EventError("TLS should be either 'yes' or 'no'")
|
|
|
|
value_parsed = value_lower == "yes"
|
|
|
|
elif option == "password":
|
|
|
|
value_parsed = value
|
|
|
|
elif option == "bindhost":
|
|
|
|
value_parsed = value
|
2019-08-30 10:53:57 +00:00
|
|
|
elif option in ["nickname", "username", "realname"]:
|
2019-07-04 12:15:51 +00:00
|
|
|
value_parsed = value
|
|
|
|
else:
|
|
|
|
raise utils.EventError("Unknown option '%s'" % option)
|
|
|
|
|
2019-10-09 19:56:38 +00:00
|
|
|
self.bot.database.servers.edit(server_id, option, value_parsed)
|
2019-07-04 12:15:51 +00:00
|
|
|
event["stdout"].write("Set %s for %s" % (option, alias))
|