2019-10-23 19:09:19 +00:00
|
|
|
import collections, datetime, sys, textwrap, time, typing
|
2019-02-11 10:00:41 +00:00
|
|
|
from src import EventManager, IRCBot, IRCChannel, IRCChannels, IRCLine
|
|
|
|
from src import IRCObject, IRCSocket, IRCUser, utils
|
2019-02-10 14:09:27 +00:00
|
|
|
|
2018-08-29 13:33:27 +00:00
|
|
|
READ_TIMEOUT_SECONDS = 120
|
|
|
|
PING_INTERVAL_SECONDS = 30
|
2019-02-10 14:09:27 +00:00
|
|
|
|
2018-10-01 12:48:55 +00:00
|
|
|
class Server(IRCObject.Object):
|
2018-10-30 14:58:48 +00:00
|
|
|
def __init__(self,
|
|
|
|
bot: "IRCBot.Bot",
|
2019-06-26 10:01:09 +00:00
|
|
|
events: EventManager.Events,
|
2018-11-05 18:23:02 +00:00
|
|
|
id: int,
|
2019-04-24 10:45:21 +00:00
|
|
|
alias: str,
|
2018-11-05 18:23:02 +00:00
|
|
|
connection_params: utils.irc.IRCConnectionParameters):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2016-03-29 11:56:58 +00:00
|
|
|
self.id = id
|
2018-11-05 18:30:14 +00:00
|
|
|
self.alias = alias
|
2018-11-05 18:23:02 +00:00
|
|
|
self.connection_params = connection_params
|
2018-10-31 15:12:46 +00:00
|
|
|
self.name = None # type: typing.Optional[str]
|
2019-05-15 16:12:21 +00:00
|
|
|
self.version = None # type: typing.Optional[str]
|
2018-09-03 21:17:01 +00:00
|
|
|
|
2019-06-17 13:20:11 +00:00
|
|
|
self.connected = False
|
|
|
|
self.reconnected = False
|
|
|
|
self.from_init = False
|
|
|
|
|
2018-11-27 12:16:36 +00:00
|
|
|
self.nickname = None # type: typing.Optional[str]
|
|
|
|
self.username = None # type: typing.Optional[str]
|
|
|
|
self.realname = None # type: typing.Optional[str]
|
|
|
|
self.hostname = None # type: typing.Optional[str]
|
|
|
|
|
2019-05-11 17:22:40 +00:00
|
|
|
self.capability_queue = {
|
|
|
|
} # type: typing.Dict[str, utils.irc.Capability]
|
2019-06-08 09:17:13 +00:00
|
|
|
self.capabilities_requested = {
|
|
|
|
} # type: typing.Dict[str, utils.irc.Capability]
|
|
|
|
|
2018-10-31 15:12:46 +00:00
|
|
|
self._capabilities_waiting = set([]) # type: typing.Set[str]
|
2019-02-16 09:12:43 +00:00
|
|
|
self.agreed_capabilities = set([]) # type: typing.Set[str]
|
2018-10-31 15:12:46 +00:00
|
|
|
self.server_capabilities = {} # type: typing.Dict[str, str]
|
2019-10-30 13:46:29 +00:00
|
|
|
self.batches = {} # type: typing.Dict[str, IRCLine.IRCBatch]
|
2018-08-29 07:38:43 +00:00
|
|
|
|
2018-10-31 15:12:46 +00:00
|
|
|
self.users = {} # type: typing.Dict[str, IRCUser.User]
|
|
|
|
self.new_users = set([]) #type: typing.Set[IRCUser.User]
|
2018-11-11 14:53:16 +00:00
|
|
|
self.channels = IRCChannels.Channels(self, self.bot, self.events)
|
2018-10-31 15:12:46 +00:00
|
|
|
self.own_modes = {} # type: typing.Dict[str, typing.Optional[str]]
|
2018-11-08 12:34:30 +00:00
|
|
|
|
|
|
|
self.isupport = {} # type: typing.Dict[str, typing.Optional[str]]
|
2019-02-09 18:52:59 +00:00
|
|
|
|
2018-10-03 15:44:00 +00:00
|
|
|
self.prefix_symbols = collections.OrderedDict(
|
|
|
|
(("@", "o"), ("+", "v")))
|
|
|
|
self.prefix_modes = collections.OrderedDict(
|
|
|
|
(("o", "@"), ("v", "+")))
|
2019-02-09 18:52:59 +00:00
|
|
|
|
|
|
|
self.channel_list_modes = ["b"] # type: typing.List[str]
|
2019-07-04 16:23:36 +00:00
|
|
|
self.channel_parametered_modes = ["k"] # type: typing.List[str]
|
2019-02-09 18:52:59 +00:00
|
|
|
self.channel_setting_modes = ["l"] # type: typing.List[str]
|
|
|
|
self.channel_modes = [
|
|
|
|
"n", "i", "m", "t", "p", "s"
|
|
|
|
] # type: typing.List[str]
|
|
|
|
|
2018-09-17 21:08:54 +00:00
|
|
|
self.channel_types = ["#"]
|
2018-09-11 07:52:12 +00:00
|
|
|
self.case_mapping = "rfc1459"
|
2019-06-08 11:10:14 +00:00
|
|
|
self.statusmsg = [] # type: typing.List[str]
|
2018-08-29 07:38:43 +00:00
|
|
|
|
2018-11-13 14:59:47 +00:00
|
|
|
self.motd_lines = [] # type: typing.List[str]
|
|
|
|
self.motd_done = False
|
|
|
|
|
2016-04-10 16:30:44 +00:00
|
|
|
self.ping_sent = False
|
2019-03-05 11:29:28 +00:00
|
|
|
self.send_enabled = True
|
2018-07-22 20:46:22 +00:00
|
|
|
|
2018-11-21 20:13:56 +00:00
|
|
|
def __repr__(self) -> str:
|
2018-08-28 15:05:43 +00:00
|
|
|
return "IRCServer.Server(%s)" % self.__str__()
|
2018-11-21 20:13:56 +00:00
|
|
|
def __str__(self) -> str:
|
2019-04-24 10:45:21 +00:00
|
|
|
return self.alias
|
2019-02-11 10:00:41 +00:00
|
|
|
|
2018-11-21 20:13:56 +00:00
|
|
|
def fileno(self) -> int:
|
2019-02-11 10:00:41 +00:00
|
|
|
return self.socket.fileno()
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2019-02-10 20:44:29 +00:00
|
|
|
def hostmask(self):
|
|
|
|
return "%s!%s@%s" % (self.nickname, self.username, self.hostname)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def connect(self):
|
2019-02-11 10:00:41 +00:00
|
|
|
self.socket = IRCSocket.Socket(
|
|
|
|
self.bot.log,
|
|
|
|
self.get_setting("encoding", "utf8"),
|
|
|
|
self.get_setting("fallback-encoding", "iso-8859-1"),
|
|
|
|
self.connection_params.hostname,
|
|
|
|
self.connection_params.port,
|
|
|
|
self.connection_params.bindhost,
|
|
|
|
self.connection_params.tls,
|
|
|
|
tls_verify=self.get_setting("ssl-verify", True),
|
|
|
|
cert=self.bot.config.get("tls-certificate", None),
|
|
|
|
key=self.bot.config.get("tls-key", None))
|
2019-07-28 17:07:23 +00:00
|
|
|
self.events.on("preprocess.connect").call(server=self)
|
2019-02-11 10:00:41 +00:00
|
|
|
self.socket.connect()
|
2017-07-12 09:00:27 +00:00
|
|
|
|
2018-11-05 18:30:14 +00:00
|
|
|
if self.connection_params.password:
|
2018-11-05 18:23:02 +00:00
|
|
|
self.send_pass(self.connection_params.password)
|
2018-07-15 22:56:06 +00:00
|
|
|
|
2019-02-07 17:41:10 +00:00
|
|
|
self.send_capibility_ls()
|
|
|
|
|
2018-11-05 20:18:22 +00:00
|
|
|
nickname = self.connection_params.nickname
|
2018-11-05 20:08:55 +00:00
|
|
|
username = self.connection_params.username or nickname
|
|
|
|
realname = self.connection_params.realname or nickname
|
|
|
|
|
|
|
|
self.send_user(username, realname)
|
|
|
|
self.send_nick(nickname)
|
2019-02-11 10:00:41 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def disconnect(self):
|
2019-02-11 10:00:41 +00:00
|
|
|
self.socket.disconnect()
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_setting(self, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot.database.server_settings.set(self.id, setting,
|
2016-03-29 11:56:58 +00:00
|
|
|
value)
|
2018-11-21 20:13:56 +00:00
|
|
|
def get_setting(self, setting: str, default: typing.Any=None
|
|
|
|
) -> typing.Any:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.server_settings.get(self.id,
|
2016-03-29 11:56:58 +00:00
|
|
|
setting, default)
|
2019-10-07 11:46:52 +00:00
|
|
|
def find_settings(self, pattern: str=None, prefix: str=None,
|
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
|
|
|
if not pattern == None:
|
|
|
|
return self.bot.database.server_settings.find(self.id, pattern,
|
|
|
|
default)
|
|
|
|
elif not prefix == None:
|
|
|
|
return self.bot.database.server_settings.find_prefix(self.id,
|
|
|
|
prefix, default)
|
|
|
|
else:
|
|
|
|
raise ValueError("Please provide 'pattern' or 'prefix'")
|
2018-10-30 14:58:48 +00:00
|
|
|
def del_setting(self, setting: str):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.bot.database.server_settings.delete(self.id, setting)
|
2018-10-14 13:30:19 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_user_setting(self, nickname: str, setting: str,
|
2018-11-21 20:13:56 +00:00
|
|
|
default: typing.Any=None) -> typing.Any:
|
2018-10-14 13:30:19 +00:00
|
|
|
user_id = self.get_user_id(nickname)
|
2018-10-18 16:16:23 +00:00
|
|
|
return self.bot.database.user_settings.get(user_id, setting, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_user_setting(self, nickname: str, setting: str, value: typing.Any):
|
2018-10-14 13:30:19 +00:00
|
|
|
user_id = self.get_user_id(nickname)
|
|
|
|
self.bot.database.user_settings.set(user_id, setting, value)
|
|
|
|
|
2018-11-21 20:13:56 +00:00
|
|
|
def get_all_user_settings(self, setting: str, default: typing.Any=[]
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.bot.database.user_settings.find_all_by_setting(
|
|
|
|
self.id, setting, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_all_user_channel_settings(self, setting: str,
|
2018-11-21 20:13:56 +00:00
|
|
|
default: typing.Any=[]) -> typing.List[typing.Any]:
|
2018-08-31 14:34:56 +00:00
|
|
|
return self.bot.database.user_channel_settings.find_all_by_setting(
|
|
|
|
self.id, setting, default)
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_own_nickname(self, nickname: str):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.nickname = nickname
|
2019-01-24 17:12:50 +00:00
|
|
|
self.nickname_lower = self.irc_lower(nickname)
|
2018-11-21 20:05:21 +00:00
|
|
|
def is_own_nickname(self, nickname: str) -> bool:
|
2018-12-02 09:56:57 +00:00
|
|
|
if self.nickname == None:
|
|
|
|
return False
|
2019-01-24 17:12:50 +00:00
|
|
|
return self.irc_equals(nickname, typing.cast(str, self.nickname))
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def add_own_mode(self, mode: str, arg: str=None):
|
2018-07-15 12:30:27 +00:00
|
|
|
self.own_modes[mode] = arg
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_own_mode(self, mode: str):
|
2019-06-22 21:23:05 +00:00
|
|
|
del self.own_modes[mode]
|
2018-10-30 14:58:48 +00:00
|
|
|
def change_own_mode(self, remove: bool, mode: str, arg: str=None):
|
2018-08-30 10:12:48 +00:00
|
|
|
if remove:
|
2018-08-30 10:17:00 +00:00
|
|
|
self.remove_own_mode(mode)
|
2018-08-30 10:12:48 +00:00
|
|
|
else:
|
|
|
|
self.add_own_mode(mode, arg)
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2018-11-21 20:05:21 +00:00
|
|
|
def has_user(self, nickname: str) -> bool:
|
2019-01-24 17:12:50 +00:00
|
|
|
return self.irc_lower(nickname) in self.users
|
2019-10-25 22:32:00 +00:00
|
|
|
def get_user(self, nickname: str, username: typing.Optional[str]=None, hostname: str=None,
|
2019-09-27 14:52:25 +00:00
|
|
|
create: bool=True) -> typing.Optional[IRCUser.User]:
|
|
|
|
new = False
|
2018-10-03 10:32:31 +00:00
|
|
|
if not self.has_user(nickname) and create:
|
2019-09-27 14:52:25 +00:00
|
|
|
new = True
|
2018-08-18 23:19:53 +00:00
|
|
|
user_id = self.get_user_id(nickname)
|
|
|
|
new_user = IRCUser.User(nickname, user_id, self, self.bot)
|
2018-08-04 11:10:10 +00:00
|
|
|
self.users[new_user.nickname_lower] = new_user
|
2016-03-29 11:56:58 +00:00
|
|
|
self.new_users.add(new_user)
|
2019-09-27 14:52:25 +00:00
|
|
|
|
|
|
|
user = self.users.get(self.irc_lower(nickname), None)
|
|
|
|
if user:
|
2019-10-25 22:32:00 +00:00
|
|
|
if username is not None:
|
2019-09-27 14:52:25 +00:00
|
|
|
user.username = username
|
2019-10-25 22:32:00 +00:00
|
|
|
if hostname is not None:
|
2019-09-27 14:52:25 +00:00
|
|
|
user.hostname = hostname
|
|
|
|
if new:
|
|
|
|
self.events.on("new.user").call(user=new_user, server=self)
|
|
|
|
return user
|
|
|
|
|
2018-11-21 20:05:21 +00:00
|
|
|
def get_user_id(self, nickname: str) -> int:
|
2019-02-06 11:25:20 +00:00
|
|
|
nickname_lower = self.irc_lower(nickname)
|
|
|
|
self.bot.database.users.add(self.id, nickname_lower)
|
|
|
|
return self.bot.database.users.get_id(self.id, nickname_lower)
|
2019-05-04 23:16:37 +00:00
|
|
|
def has_user_id(self, nickname: str) -> bool:
|
|
|
|
id = self.bot.database.users.get_id(self.id, self.irc_lower(nickname))
|
|
|
|
return not id == None
|
2018-10-30 14:58:48 +00:00
|
|
|
def remove_user(self, user: IRCUser.User):
|
2018-08-04 11:10:10 +00:00
|
|
|
del self.users[user.nickname_lower]
|
2016-03-29 11:56:58 +00:00
|
|
|
for channel in user.channels:
|
|
|
|
channel.remove_user(user)
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2019-09-20 13:52:41 +00:00
|
|
|
def is_channel(self, name: str) -> bool:
|
|
|
|
return name[0] in self.channel_types
|
|
|
|
|
2018-11-21 20:02:36 +00:00
|
|
|
def get_target(self, name: str
|
2018-11-21 20:13:56 +00:00
|
|
|
) -> typing.Optional[
|
|
|
|
typing.Union[IRCChannel.Channel, IRCUser.User]]:
|
2019-09-20 13:52:41 +00:00
|
|
|
if self.is_channel(name):
|
2018-11-21 20:02:36 +00:00
|
|
|
if name in self.channels:
|
|
|
|
return self.channels.get(name)
|
|
|
|
else:
|
|
|
|
return self.get_user(name)
|
|
|
|
return None
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def change_user_nickname(self, old_nickname: str, new_nickname: str):
|
2019-01-24 17:12:50 +00:00
|
|
|
user = self.users.pop(self.irc_lower(old_nickname))
|
2018-09-05 11:58:12 +00:00
|
|
|
user._id = self.get_user_id(new_nickname)
|
2019-01-24 17:12:50 +00:00
|
|
|
self.users[self.irc_lower(new_nickname)] = user
|
|
|
|
|
2019-01-24 17:15:02 +00:00
|
|
|
def irc_lower(self, s: str) -> str:
|
2019-01-24 17:12:50 +00:00
|
|
|
return utils.irc.lower(self.case_mapping, s)
|
2019-01-24 17:13:51 +00:00
|
|
|
def irc_equals(self, s1: str, s2: str) -> bool:
|
2019-01-24 17:12:50 +00:00
|
|
|
return utils.irc.equals(self.case_mapping, s1, s2)
|
2018-11-04 14:48:55 +00:00
|
|
|
|
2019-05-19 15:24:38 +00:00
|
|
|
def hostmask_match(self, hostmask: str, pattern: str) -> bool:
|
|
|
|
return utils.irc.hostmask_match(self.irc_lower(hostmask),
|
|
|
|
self.irc_lower(pattern))
|
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _post_read(self, lines: typing.List[str]):
|
|
|
|
for line in lines:
|
|
|
|
self.bot.log.debug("%s (raw recv) | %s", [str(self), line])
|
|
|
|
self.events.on("raw.received").call_unsafe(server=self,
|
2019-10-27 10:19:00 +00:00
|
|
|
line=IRCLine.parse_line(line))
|
2019-06-06 16:05:44 +00:00
|
|
|
self.check_users()
|
2016-03-29 11:56:58 +00:00
|
|
|
def check_users(self):
|
|
|
|
for user in self.new_users:
|
|
|
|
if not len(user.channels):
|
|
|
|
self.remove_user(user)
|
|
|
|
self.new_users.clear()
|
2018-08-29 13:33:27 +00:00
|
|
|
|
2018-11-21 20:13:56 +00:00
|
|
|
def until_next_ping(self) -> typing.Optional[float]:
|
2018-09-11 17:25:27 +00:00
|
|
|
if self.ping_sent:
|
|
|
|
return None
|
2019-02-11 20:49:53 +00:00
|
|
|
return max(0, (self.socket.last_read+PING_INTERVAL_SECONDS
|
2018-08-29 13:33:27 +00:00
|
|
|
)-time.monotonic())
|
2018-11-21 20:13:56 +00:00
|
|
|
def ping_due(self) -> bool:
|
2018-08-29 13:33:27 +00:00
|
|
|
return self.until_next_ping() == 0
|
|
|
|
|
2018-11-21 20:17:02 +00:00
|
|
|
def until_read_timeout(self) -> float:
|
2019-02-11 20:49:53 +00:00
|
|
|
return max(0, (self.socket.last_read+READ_TIMEOUT_SECONDS
|
2018-08-29 13:33:27 +00:00
|
|
|
)-time.monotonic())
|
2018-11-21 20:13:56 +00:00
|
|
|
def read_timed_out(self) -> bool:
|
2019-06-22 22:31:06 +00:00
|
|
|
return self.until_read_timeout() == 0
|
2018-08-29 13:33:27 +00:00
|
|
|
|
2019-02-11 10:00:41 +00:00
|
|
|
def read(self) -> typing.Optional[typing.List[str]]:
|
2019-02-11 21:23:52 +00:00
|
|
|
lines = self.socket.read()
|
|
|
|
if lines:
|
|
|
|
self.ping_sent = False
|
2019-02-12 11:14:36 +00:00
|
|
|
|
2019-02-11 21:23:52 +00:00
|
|
|
return lines
|
|
|
|
|
2019-06-06 18:15:34 +00:00
|
|
|
def _send(self) -> typing.List[IRCLine.SentLine]:
|
|
|
|
lines = self.socket._send()
|
|
|
|
for line in lines:
|
|
|
|
self.bot.log.debug("%s (raw send) | %s", [
|
|
|
|
str(self), line.parsed_line.format()])
|
|
|
|
return lines
|
|
|
|
def _post_send(self, lines: typing.List[IRCLine.SentLine]):
|
|
|
|
for line in lines:
|
2019-06-07 10:05:43 +00:00
|
|
|
line.events.on("send").call()
|
2019-06-06 18:15:34 +00:00
|
|
|
self.events.on("raw.send").call_unsafe(server=self,
|
|
|
|
line=line.parsed_line)
|
|
|
|
|
2019-06-10 09:11:31 +00:00
|
|
|
def send(self, line_parsed: IRCLine.ParsedLine, immediate: bool=False
|
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-03-05 11:29:28 +00:00
|
|
|
if not self.send_enabled:
|
|
|
|
return None
|
|
|
|
|
2019-06-04 16:51:20 +00:00
|
|
|
line_events = self.events.new_root()
|
|
|
|
|
2019-02-23 21:40:11 +00:00
|
|
|
self.events.on("preprocess.send").on(line_parsed.command
|
2019-06-04 16:51:20 +00:00
|
|
|
).call_unsafe(server=self, line=line_parsed, events=line_events)
|
2019-05-30 14:34:39 +00:00
|
|
|
self.events.on("preprocess.send").call_unsafe(server=self,
|
2019-06-04 16:51:20 +00:00
|
|
|
line=line_parsed, events=line_events)
|
2019-02-23 21:33:04 +00:00
|
|
|
|
2019-06-10 09:45:02 +00:00
|
|
|
if line_parsed.valid() or line_parsed.assured():
|
2019-06-10 09:11:31 +00:00
|
|
|
line = line_parsed.format()
|
|
|
|
line_obj = IRCLine.SentLine(line_events, datetime.datetime.utcnow(),
|
|
|
|
self.hostmask(), line_parsed)
|
|
|
|
self.socket.send(line_obj, immediate=immediate)
|
2019-06-06 16:58:35 +00:00
|
|
|
|
2019-06-10 09:11:31 +00:00
|
|
|
if immediate:
|
|
|
|
self.bot.trigger_write()
|
2019-06-06 16:58:35 +00:00
|
|
|
|
2019-06-10 09:11:31 +00:00
|
|
|
return line_obj
|
|
|
|
return None
|
2019-04-16 12:47:06 +00:00
|
|
|
def send_raw(self, line: str):
|
2019-10-27 10:19:00 +00:00
|
|
|
return self.send(IRCLine.parse_line(line))
|
2019-02-18 14:56:25 +00:00
|
|
|
|
2019-10-30 13:46:29 +00:00
|
|
|
def _line(self, command: str,
|
|
|
|
unfiltered_args: typing.Sequence[typing.Optional[str]], tags={}):
|
|
|
|
args: typing.List[str] = [a for a in unfiltered_args if not a is None]
|
|
|
|
return IRCLine.ParsedLine(command, args, tags=tags)
|
2019-10-28 10:56:33 +00:00
|
|
|
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_user(self, username: str, realname: str
|
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("USER",[username, "0", "*", realname]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_nick(self, nickname: str) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("NICK", [nickname]))
|
2017-07-12 09:00:27 +00:00
|
|
|
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_capibility_ls(self) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("CAP", ["LS", "302"]))
|
2018-09-03 10:14:27 +00:00
|
|
|
def send_capability_queue(self):
|
2019-10-23 19:09:19 +00:00
|
|
|
# textwrap works here because in ASCII, all chars are 1 bytes:
|
2019-10-25 10:52:11 +00:00
|
|
|
capabilities = list(self.capability_queue.keys())
|
|
|
|
capabilities_str = " ".join(capabilities)
|
|
|
|
capability_batches = textwrap.wrap(capabilities_str,
|
2019-10-23 19:09:19 +00:00
|
|
|
IRCLine.LINE_MAX-len("CAP REQ :"))
|
2019-05-11 17:22:40 +00:00
|
|
|
|
2019-10-25 10:52:11 +00:00
|
|
|
for cap_name in capabilities:
|
|
|
|
cap = self.capability_queue[cap_name]
|
|
|
|
del self.capability_queue[cap_name]
|
|
|
|
self.capabilities_requested[cap_name] = cap
|
|
|
|
|
2019-10-23 19:09:19 +00:00
|
|
|
for capability_batch in capability_batches:
|
|
|
|
self.send_capability_request(capability_batch)
|
2019-06-08 09:17:13 +00:00
|
|
|
|
2019-10-28 10:56:33 +00:00
|
|
|
def send_capability_request(self, capabilities: str
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("CAP", ["REQ", capabilities]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_capability_end(self) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("CAP", ["END"]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_authenticate(self, text: str) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("AUTHENTICATE", [text]))
|
2019-05-20 15:33:32 +00:00
|
|
|
def has_capability(self, capability: utils.irc.Capability) -> bool:
|
2019-05-30 14:34:39 +00:00
|
|
|
return bool(self.available_capability(capability))
|
2019-05-20 15:33:32 +00:00
|
|
|
def has_capability_str(self, capability: str) -> bool:
|
2019-03-03 22:38:34 +00:00
|
|
|
return capability in self.agreed_capabilities
|
2019-06-02 09:14:09 +00:00
|
|
|
def available_capability(self, capability: utils.irc.Capability
|
|
|
|
) -> typing.Optional[str]:
|
2019-05-30 14:34:39 +00:00
|
|
|
return capability.available(self.agreed_capabilities)
|
2017-07-12 09:00:27 +00:00
|
|
|
|
2018-11-21 20:13:56 +00:00
|
|
|
def waiting_for_capabilities(self) -> bool:
|
2018-09-03 10:18:20 +00:00
|
|
|
return bool(len(self._capabilities_waiting))
|
2018-10-30 14:58:48 +00:00
|
|
|
def wait_for_capability(self, capability: str):
|
2018-09-03 10:14:27 +00:00
|
|
|
self._capabilities_waiting.add(capability)
|
2018-10-30 14:58:48 +00:00
|
|
|
def capability_done(self, capability: str):
|
2019-06-16 14:33:20 +00:00
|
|
|
if capability in self._capabilities_waiting:
|
|
|
|
self._capabilities_waiting.discard(capability)
|
|
|
|
if not self._capabilities_waiting:
|
|
|
|
self.send_capability_end()
|
|
|
|
def clear_waiting_capabilities(self):
|
|
|
|
self._capabilities_waiting.clear()
|
2018-09-03 10:14:27 +00:00
|
|
|
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_pass(self, password: str) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("PASS", [password]))
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2019-10-28 10:56:33 +00:00
|
|
|
def send_ping(self, token: str="hello"
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("PING", [token]))
|
|
|
|
def send_pong(self, token: str) -> typing.Optional[IRCLine.SentLine]:
|
|
|
|
return self.send(self._line("PONG", [token]))
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2019-10-30 13:46:29 +00:00
|
|
|
def send_join(self, channel_name: str, keys: typing.List[str]=None
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-30 13:46:29 +00:00
|
|
|
return self.send(self._line("JOIN", [channel_name]+(keys or [])))
|
2019-02-20 17:08:30 +00:00
|
|
|
def send_joins(self, channel_names: typing.List[str],
|
|
|
|
keys: typing.List[str]=None):
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("JOIN",
|
|
|
|
[",".join(channel_names)]+(keys or [])))
|
2019-02-24 08:24:58 +00:00
|
|
|
def send_part(self, channel_name: str, reason: str=None
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("PART", [channel_name, reason]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_quit(self, reason: str="Leaving"
|
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("QUIT", [reason]))
|
2019-02-18 14:56:25 +00:00
|
|
|
|
|
|
|
def send_message(self, target: str, message: str, tags: dict={}
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("PRIVMSG", [target, message], tags=tags))
|
2019-02-18 14:56:25 +00:00
|
|
|
|
|
|
|
def send_notice(self, target: str, message: str, tags: dict={}
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("NOTICE", [target, message], tags=tags))
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2019-02-22 22:34:54 +00:00
|
|
|
def send_tagmsg(self, target: str, tags: dict):
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("TARGMSG", [], tags=tags))
|
2019-02-14 21:36:59 +00:00
|
|
|
|
2019-02-18 14:56:25 +00:00
|
|
|
def send_mode(self, target: str, mode: str=None, args: typing.List[str]=None
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-30 13:46:29 +00:00
|
|
|
line_args = [target, mode]
|
|
|
|
if args:
|
|
|
|
line_args.extend(args)
|
|
|
|
return self.send(self._line("MODE", line_args))
|
2018-07-15 12:30:27 +00:00
|
|
|
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_topic(self, channel_name: str, topic: str
|
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("TOPIC", [channel_name, topic]))
|
2019-02-10 14:21:14 +00:00
|
|
|
def send_kick(self, channel_name: str, target: str, reason: str=None
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("KICK", [channel_name, target, reason]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_names(self, channel_name: str) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("NAMES", [channel_name]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_list(self, search_for: str=None
|
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("LIST", [search_for]))
|
2019-09-12 10:24:25 +00:00
|
|
|
def send_invite(self, channel_name: str, target: str
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("INVITE", [target, channel_name]))
|
2019-02-10 14:21:14 +00:00
|
|
|
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_whois(self, target: str) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("WHOIS", [target]))
|
2019-02-10 14:21:14 +00:00
|
|
|
def send_whowas(self, target: str, amount: int=None, server: str=None
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
amount_str = str(amount) if not amount == None else None
|
|
|
|
return self.send(self._line("WHOWAS", [target, amount_str, server]))
|
2019-06-10 14:02:03 +00:00
|
|
|
def send_who(self, filter: str=None) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
return self.send(self._line("WHO", [filter]))
|
2019-02-10 14:21:14 +00:00
|
|
|
def send_whox(self, mask: str, filter: str, fields: str, label: str=None
|
2019-06-10 14:02:03 +00:00
|
|
|
) -> typing.Optional[IRCLine.SentLine]:
|
2019-10-28 10:56:33 +00:00
|
|
|
flags = "%s%%%s%s" % (filter, fields, ","+label if label else "")
|
|
|
|
return self.send(self._line("WHO", [mask, flags]))
|