2017-08-11 21:27:58 +02:00
|
|
|
"""
|
|
|
|
clientbot.py: Clientbot (regular IRC bot) protocol module for PyLink.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Here be dragons. There are lots of hacks and stubs in this module to recreate the same sort of state
|
2017-08-21 01:52:23 +02:00
|
|
|
# that a regular server would have (e.g. spawning virtual users for things like Relay). Somehow it
|
|
|
|
# works on most networks though!
|
2017-08-11 21:27:58 +02:00
|
|
|
|
2016-12-17 23:50:29 +01:00
|
|
|
import base64
|
2019-07-15 00:12:29 +02:00
|
|
|
import threading
|
|
|
|
import time
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2019-07-15 00:12:29 +02:00
|
|
|
from pylinkirc import conf, utils
|
|
|
|
from pylinkirc.classes import *
|
2016-07-17 07:42:17 +02:00
|
|
|
from pylinkirc.log import log
|
2017-07-03 06:59:29 +02:00
|
|
|
from pylinkirc.protocols.ircs2s_common import *
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-10-08 06:08:55 +02:00
|
|
|
FALLBACK_REALNAME = 'PyLink Relay Mirror Client'
|
2017-08-20 07:04:24 +02:00
|
|
|
|
|
|
|
# IRCv3 capabilities to request when available
|
2017-08-21 01:52:23 +02:00
|
|
|
IRCV3_CAPABILITIES = {'multi-prefix', 'sasl', 'away-notify', 'userhost-in-names', 'chghost', 'account-notify',
|
2017-08-24 04:28:10 +02:00
|
|
|
'account-tag', 'extended-join'}
|
2016-08-11 21:46:05 +02:00
|
|
|
|
2019-02-10 21:40:02 +01:00
|
|
|
class ClientbotBaseProtocol(PyLinkNetworkCoreWithUtils):
|
2017-06-25 10:12:22 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2019-06-24 04:46:23 +02:00
|
|
|
self.protocol_caps = {'visible-state-only', 'slash-in-nicks', 'slash-in-hosts', 'underscore-in-hosts',
|
|
|
|
'freeform-nicks'}
|
2016-08-04 20:10:51 +02:00
|
|
|
|
2016-07-29 06:34:00 +02:00
|
|
|
# Remove conf key checks for those not needed for Clientbot.
|
2016-07-29 07:22:47 +02:00
|
|
|
self.conf_keys -= {'recvpass', 'sendpass', 'sid', 'sidrange', 'hostname'}
|
2016-07-29 06:34:00 +02:00
|
|
|
|
2019-08-24 06:22:28 +02:00
|
|
|
def _get_UID(self, nick, ident=None, host=None, spawn_new=False):
|
2019-02-10 21:40:02 +01:00
|
|
|
"""
|
2019-06-24 04:46:23 +02:00
|
|
|
Fetches the UID for the given nick, creating one if it does not already exist and spawn_new is True.
|
2019-02-10 21:40:02 +01:00
|
|
|
|
2019-06-24 04:46:23 +02:00
|
|
|
To prevent message spoofing, this will only return an external (non-PyLink) client or the PyLink bot itself.
|
2019-02-10 21:40:02 +01:00
|
|
|
"""
|
2019-06-24 04:46:23 +02:00
|
|
|
#log.debug('(%s) _get_UID: searching for nick %s', self.name, nick, stack_info=True)
|
|
|
|
idsource = self.nick_to_uid(nick, filterfunc=lambda uid: uid == self.pseudoclient.uid or not self.is_internal_client(uid))
|
2019-02-10 21:40:02 +01:00
|
|
|
|
2019-06-24 04:46:23 +02:00
|
|
|
if idsource is None and spawn_new:
|
2019-02-10 21:40:02 +01:00
|
|
|
# If this sender doesn't already exist, spawn a new client.
|
|
|
|
idsource = self.spawn_client(nick, ident or 'unknown', host or 'unknown',
|
2019-06-24 04:46:23 +02:00
|
|
|
server=self.uplink, realname=FALLBACK_REALNAME).uid
|
2019-08-26 21:17:07 +02:00
|
|
|
return idsource or nick # Return input if missing per upstream spec
|
2019-02-10 21:40:02 +01:00
|
|
|
|
|
|
|
def away(self, source, text):
|
|
|
|
"""STUB: sets away messages for internal clients."""
|
|
|
|
log.debug('(%s) away: target is %s, internal client? %s', self.name, source, self.is_internal_client(source))
|
|
|
|
|
|
|
|
if self.users[source].away != text:
|
|
|
|
if not self.is_internal_client(source):
|
|
|
|
log.debug('(%s) away: sending AWAY hook from %s with text %r', self.name, source, text)
|
|
|
|
self.call_hooks([source, 'AWAY', {'text': text}])
|
|
|
|
|
|
|
|
self.users[source].away = text
|
|
|
|
|
|
|
|
def join(self, client, channel):
|
|
|
|
"""STUB: sends a virtual join (CLIENTBOT_JOIN) from the client to channel."""
|
|
|
|
self._channels[channel].users.add(client)
|
|
|
|
self.users[client].channels.add(channel)
|
|
|
|
|
|
|
|
if self.pseudoclient and client != self.pseudoclient:
|
|
|
|
log.debug('(%s) join: faking JOIN of client %s/%s to %s', self.name, client,
|
|
|
|
self.get_friendly_name(client), channel)
|
|
|
|
self.call_hooks([client, 'CLIENTBOT_JOIN', {'channel': channel}])
|
|
|
|
|
|
|
|
def kick(self, source, channel, target, reason=''):
|
|
|
|
"""STUB: rejoins users on kick attempts, for server transports where kicking users from channels is not implemented."""
|
2019-02-10 22:01:31 +01:00
|
|
|
if not self.is_internal_client(target):
|
2019-02-10 21:40:02 +01:00
|
|
|
log.info("(%s) Rejoining user %s to %s since kicks are not supported here.", self.name, target, channel)
|
|
|
|
self.join(target, channel)
|
2019-02-10 22:01:31 +01:00
|
|
|
self.call_hooks([None, 'JOIN', {'channel': channel, 'users': [target], 'modes': []}])
|
2019-02-10 21:40:02 +01:00
|
|
|
elif channel in self.channels:
|
|
|
|
self.channels[channel].remove_user(target)
|
|
|
|
self.users[target].channels.discard(channel)
|
|
|
|
self.call_hooks([source, 'CLIENTBOT_KICK', {'channel': channel, 'target': target, 'text': reason}])
|
|
|
|
else:
|
|
|
|
log.warning('(%s) Possible desync? Tried to kick() on non-existent channel %s', self.name, channel)
|
|
|
|
|
|
|
|
def message(self, source, target, text, notice=False):
|
|
|
|
"""STUB: Sends messages to the target."""
|
|
|
|
if self.pseudoclient and self.pseudoclient.uid != source:
|
|
|
|
self.call_hooks([source, 'CLIENTBOT_MESSAGE', {'target': target, 'is_notice': notice, 'text': text}])
|
|
|
|
|
|
|
|
def nick(self, source, newnick):
|
|
|
|
"""STUB: sends a virtual nick change (CLIENTBOT_NICK)."""
|
|
|
|
assert source, "No source given?"
|
|
|
|
self.call_hooks([source, 'CLIENTBOT_NICK', {'newnick': newnick}])
|
|
|
|
self.users[source].nick = newnick
|
|
|
|
|
|
|
|
def notice(self, source, target, text):
|
|
|
|
"""Sends notices to the target."""
|
|
|
|
# Wrap around message(), which does all the text formatting for us.
|
|
|
|
self.message(source, target, text, notice=True)
|
|
|
|
|
|
|
|
def sjoin(self, server, channel, users, ts=None, modes=set()):
|
|
|
|
"""STUB: bursts joins from a server."""
|
|
|
|
# This stub only updates the state internally with the users given. modes and TS are currently ignored.
|
|
|
|
puids = {u[-1] for u in users}
|
|
|
|
for user in puids:
|
|
|
|
self.users[user].channels.add(channel)
|
|
|
|
|
|
|
|
self._channels[channel].users |= puids
|
|
|
|
nicks = {self.get_friendly_name(u) for u in puids}
|
|
|
|
self.call_hooks([server, 'CLIENTBOT_SJOIN', {'channel': channel, 'nicks': nicks}])
|
|
|
|
|
|
|
|
# Note: clientbot clients are initialized with umode +i by default
|
|
|
|
def spawn_client(self, nick, ident='unknown', host='unknown.host', realhost=None, modes={('i', None)},
|
|
|
|
server=None, ip='0.0.0.0', realname='', ts=None, opertype=None,
|
|
|
|
manipulatable=False):
|
|
|
|
"""
|
|
|
|
STUB: Pretends to spawn a new client with a subset of the given options.
|
|
|
|
"""
|
|
|
|
|
|
|
|
server = server or self.sid
|
|
|
|
uid = self.uidgen.next_uid(prefix=nick)
|
|
|
|
|
|
|
|
ts = ts or int(time.time())
|
|
|
|
|
|
|
|
log.debug('(%s) spawn_client stub called, saving nick %s as PUID %s', self.name, nick, uid)
|
|
|
|
u = self.users[uid] = User(self, nick, ts, uid, server, ident=ident, host=host, realname=realname,
|
|
|
|
manipulatable=manipulatable, realhost=realhost, ip=ip)
|
|
|
|
self.servers[server].users.add(uid)
|
|
|
|
|
|
|
|
self.apply_modes(uid, modes)
|
|
|
|
|
|
|
|
return u
|
|
|
|
|
|
|
|
def spawn_server(self, name, sid=None, uplink=None, desc=None, internal=True):
|
|
|
|
"""
|
|
|
|
STUB: Pretends to spawn a new server with a subset of the given options.
|
|
|
|
"""
|
|
|
|
if internal:
|
|
|
|
# Use a custom pseudo-SID format for internal servers to prevent any server name clashes
|
|
|
|
sid = self.sidgen.next_sid(prefix=name)
|
|
|
|
else:
|
|
|
|
# For others servers, just use the server name as the SID.
|
|
|
|
sid = name
|
|
|
|
|
|
|
|
self.servers[sid] = Server(self, uplink, name, internal=internal)
|
|
|
|
return sid
|
|
|
|
|
|
|
|
def squit(self, source, target, text):
|
|
|
|
"""STUB: SQUITs a server."""
|
|
|
|
# What this actually does is just handle the SQUIT internally: i.e.
|
|
|
|
# Removing pseudoclients and pseudoservers.
|
|
|
|
squit_data = self._squit(source, 'CLIENTBOT_VIRTUAL_SQUIT', [target, text])
|
|
|
|
|
|
|
|
if squit_data and squit_data.get('nicks'):
|
|
|
|
self.call_hooks([source, 'CLIENTBOT_SQUIT', squit_data])
|
|
|
|
|
|
|
|
def part(self, source, channel, reason=''):
|
|
|
|
"""STUB: Parts a user from a channel."""
|
2019-06-16 20:23:32 +02:00
|
|
|
if self.pseudoclient and source == self.pseudoclient.uid:
|
|
|
|
raise NotImplementedError("Explicitly leaving channels is not supported here.")
|
2019-02-10 21:40:02 +01:00
|
|
|
self._channels[channel].remove_user(source)
|
|
|
|
self.users[source].channels.discard(channel)
|
|
|
|
self.call_hooks([source, 'CLIENTBOT_PART', {'channel': channel, 'text': reason}])
|
|
|
|
|
|
|
|
def quit(self, source, reason):
|
|
|
|
"""STUB: Quits a client."""
|
2019-07-01 22:36:53 +02:00
|
|
|
userdata = self._remove_client(source)
|
2019-02-10 21:40:02 +01:00
|
|
|
self.call_hooks([source, 'CLIENTBOT_QUIT', {'text': reason, 'userdata': userdata}])
|
|
|
|
|
|
|
|
def _stub(self, *args):
|
|
|
|
"""Stub outgoing command function (does nothing)."""
|
|
|
|
return
|
|
|
|
# Note: invite() and mode() are implemented in ClientbotWrapperProtocol below
|
|
|
|
invite = mode = topic = topic_burst = _stub # XXX: incomplete
|
|
|
|
|
|
|
|
def _stub_raise(self, *args):
|
|
|
|
"""Stub outgoing command function (raises an error)."""
|
|
|
|
raise NotImplementedError("Not supported on Clientbot")
|
|
|
|
kill = knock = numeric = _stub_raise
|
|
|
|
|
|
|
|
def update_client(self, target, field, text):
|
|
|
|
"""Updates the known ident, host, or realname of a client."""
|
2019-06-24 04:46:23 +02:00
|
|
|
# Note: unlike other protocol modules, this function is also called as a helper to
|
|
|
|
# update data for external clients.
|
|
|
|
# Following this, we only want to send hook payloads if the target is an external client.
|
2019-02-10 21:40:02 +01:00
|
|
|
if target not in self.users:
|
|
|
|
log.warning("(%s) Unknown target %s for update_client()", self.name, target)
|
|
|
|
return
|
|
|
|
|
|
|
|
u = self.users[target]
|
|
|
|
|
|
|
|
if field == 'IDENT' and u.ident != text:
|
|
|
|
u.ident = text
|
|
|
|
if not self.is_internal_client(target):
|
|
|
|
self.call_hooks([self.sid, 'CHGIDENT',
|
|
|
|
{'target': target, 'newident': text}])
|
|
|
|
elif field == 'HOST' and u.host != text:
|
|
|
|
u.host = text
|
|
|
|
if not self.is_internal_client(target):
|
|
|
|
self.call_hooks([self.sid, 'CHGHOST',
|
|
|
|
{'target': target, 'newhost': text}])
|
|
|
|
elif field in ('REALNAME', 'GECOS') and u.realname != text:
|
|
|
|
u.realname = text
|
|
|
|
if not self.is_internal_client(target):
|
|
|
|
self.call_hooks([self.sid, 'CHGNAME',
|
|
|
|
{'target': target, 'newgecos': text}])
|
|
|
|
else:
|
|
|
|
return # Nothing changed
|
|
|
|
|
|
|
|
class ClientbotWrapperProtocol(ClientbotBaseProtocol, IRCCommonProtocol):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2019-02-17 01:42:46 +01:00
|
|
|
self.protocol_caps |= {'ssl-should-verify'}
|
2019-02-10 21:40:02 +01:00
|
|
|
|
|
|
|
self.has_eob = False
|
|
|
|
|
2016-07-21 08:48:18 +02:00
|
|
|
# This is just a fallback. Actual casemapping is fetched by handle_005()
|
2016-07-17 07:42:17 +02:00
|
|
|
self.casemapping = 'ascii'
|
|
|
|
|
2017-07-03 21:42:51 +02:00
|
|
|
self._caps = {}
|
2016-12-17 07:28:40 +01:00
|
|
|
self.ircv3_caps = set()
|
2016-12-17 23:50:29 +01:00
|
|
|
self.ircv3_caps_available = {}
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-22 09:22:56 +02:00
|
|
|
# Tracks the users sent in a list of /who replies, so that users can be bursted all at once
|
|
|
|
# when ENDOFWHO is received.
|
|
|
|
self.who_received = set()
|
|
|
|
|
2016-07-23 07:36:34 +02:00
|
|
|
# This stores channel->Timer object mappings for users that we're waiting for a kick
|
|
|
|
# acknowledgement for. The timer is set to send a NAMES request to the uplink to prevent
|
|
|
|
# things like failed KICK attempts from desyncing plugins like relay.
|
|
|
|
self.kick_queue = {}
|
|
|
|
|
2016-09-01 07:46:19 +02:00
|
|
|
# Aliases: 463 (ERR_NOPERMFORHOST), 464 (ERR_PASSWDMISMATCH), and 465 (ERR_YOUREBANNEDCREEP)
|
|
|
|
# are essentially all fatal errors for connections.
|
|
|
|
self.handle_463 = self.handle_464 = self.handle_465 = self.handle_error
|
|
|
|
|
2017-07-03 21:42:51 +02:00
|
|
|
self._use_builtin_005_handling = True
|
2017-08-22 08:05:27 +02:00
|
|
|
self._nick_fails = 0
|
2017-07-03 21:42:51 +02:00
|
|
|
|
2017-08-21 01:12:13 +02:00
|
|
|
self.hook_map = {'ACCOUNT': 'CLIENT_SERVICES_LOGIN'}
|
|
|
|
|
2017-06-25 10:12:22 +02:00
|
|
|
def post_connect(self):
|
2016-07-17 07:42:17 +02:00
|
|
|
"""Initializes a connection to a server."""
|
2017-05-13 04:57:24 +02:00
|
|
|
# (Re)initialize counter-based pseudo UID generators
|
2017-07-03 21:42:51 +02:00
|
|
|
super().post_connect()
|
2017-08-29 04:42:10 +02:00
|
|
|
self.uidgen = PUIDGenerator('PUID')
|
|
|
|
self.sidgen = PUIDGenerator('ClientbotInternalSID')
|
2017-05-13 04:57:24 +02:00
|
|
|
|
2016-08-04 20:10:51 +02:00
|
|
|
self.has_eob = False
|
2017-06-25 11:03:12 +02:00
|
|
|
ts = self.start_ts
|
|
|
|
f = lambda text: self.send(text, queue=False)
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-29 06:49:16 +02:00
|
|
|
# Enumerate our own server
|
2017-06-25 11:03:12 +02:00
|
|
|
self.sid = self.sidgen.next_sid()
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-24 20:15:33 +02:00
|
|
|
# Clear states from last connect
|
|
|
|
self.who_received.clear()
|
|
|
|
self.kick_queue.clear()
|
2017-07-03 21:42:51 +02:00
|
|
|
self._caps.clear()
|
2016-12-17 07:28:40 +01:00
|
|
|
self.ircv3_caps.clear()
|
2016-12-17 23:50:29 +01:00
|
|
|
self.ircv3_caps_available.clear()
|
2016-07-24 20:15:33 +02:00
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
sendpass = self.serverdata.get("sendpass")
|
2016-07-17 07:42:17 +02:00
|
|
|
if sendpass:
|
|
|
|
f('PASS %s' % sendpass)
|
|
|
|
|
2017-03-04 00:39:28 +01:00
|
|
|
f('CAP LS 302')
|
|
|
|
|
2017-03-05 08:54:16 +01:00
|
|
|
# Start a timer to call CAP END if registration freezes (e.g. if AUTHENTICATE for SASL is
|
|
|
|
# never replied to).
|
2017-06-28 07:27:38 +02:00
|
|
|
def _do_cap_end_wrapper():
|
2017-03-05 08:54:16 +01:00
|
|
|
log.info('(%s) Skipping SASL due to timeout; are the IRCd and services configured '
|
2017-06-25 11:03:12 +02:00
|
|
|
'properly?', self.name)
|
2017-06-28 07:27:38 +02:00
|
|
|
self._do_cap_end()
|
|
|
|
self._cap_timer = threading.Timer(self.serverdata.get('sasl_timeout') or 15, _do_cap_end_wrapper)
|
2017-03-05 08:54:16 +01:00
|
|
|
self._cap_timer.start()
|
|
|
|
|
2017-08-22 08:05:27 +02:00
|
|
|
# Log in to IRC and set our irc.pseudoclient object.
|
|
|
|
sbot = world.services['pylink']
|
|
|
|
self._nick_fails = 0
|
|
|
|
|
|
|
|
nick = sbot.get_nick(self)
|
|
|
|
ident = sbot.get_ident(self)
|
|
|
|
realname = sbot.get_realname(self)
|
|
|
|
|
|
|
|
f('NICK %s' % nick)
|
|
|
|
f('USER %s 8 * :%s' % (ident, realname))
|
2018-05-27 01:22:36 +02:00
|
|
|
self.pseudoclient = User(self, nick, int(time.time()),
|
|
|
|
self.uidgen.next_uid(prefix='@ClientbotInternal'), self.sid,
|
2017-08-24 10:12:45 +02:00
|
|
|
ident=ident, realname=realname, host=self.hostname())
|
2017-08-22 08:05:27 +02:00
|
|
|
self.users[self.pseudoclient.uid] = self.pseudoclient
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-22 09:22:56 +02:00
|
|
|
|
2016-07-22 03:14:23 +02:00
|
|
|
def invite(self, client, target, channel):
|
|
|
|
"""Invites a user to a channel."""
|
2017-06-30 07:56:14 +02:00
|
|
|
self.send('INVITE %s %s' % (self.get_friendly_name(target), channel))
|
2016-07-22 03:14:23 +02:00
|
|
|
|
2016-07-18 00:20:57 +02:00
|
|
|
def join(self, client, channel):
|
|
|
|
"""STUB: Joins a user to a channel."""
|
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
# Only joins for the main PyLink client are actually forwarded. Others are ignored.
|
2016-08-11 20:23:41 +02:00
|
|
|
# Note: we do not automatically add our main client to the channel state, as we
|
|
|
|
# rely on the /NAMES reply to sync it up properly.
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.pseudoclient and client == self.pseudoclient.uid:
|
|
|
|
self.send('JOIN %s' % channel)
|
2016-07-18 00:20:57 +02:00
|
|
|
else:
|
2019-02-10 21:40:02 +01:00
|
|
|
# Pass on a virtual JOIN as a hook
|
|
|
|
super().join(client, channel)
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-18 07:34:26 +02:00
|
|
|
def kick(self, source, channel, target, reason=''):
|
|
|
|
"""Sends channel kicks."""
|
2016-07-23 21:36:13 +02:00
|
|
|
|
2016-07-24 20:54:14 +02:00
|
|
|
log.debug('(%s) kick: checking if target %s (nick: %s) is an internal client? %s',
|
2017-06-30 07:56:14 +02:00
|
|
|
self.name, target, self.get_friendly_name(target),
|
|
|
|
self.is_internal_client(target))
|
2018-08-25 06:52:34 +02:00
|
|
|
if self.is_internal_client(target) and (self.pseudoclient and source != self.pseudoclient.uid):
|
2016-07-23 21:36:13 +02:00
|
|
|
# Target was one of our virtual clients. Just remove them from the state.
|
|
|
|
self.handle_part(target, 'KICK', [channel, reason])
|
2016-07-24 20:54:14 +02:00
|
|
|
|
|
|
|
# Send a KICK hook for message formatting.
|
2017-06-30 07:56:14 +02:00
|
|
|
self.call_hooks([source, 'CLIENTBOT_KICK', {'channel': channel, 'target': target, 'text': reason}])
|
2016-07-23 21:36:13 +02:00
|
|
|
return
|
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('KICK %s %s :<%s> %s' % (channel, self._expandPUID(target),
|
2019-02-10 21:40:02 +01:00
|
|
|
self.get_friendly_name(source), reason))
|
2016-07-22 03:03:32 +02:00
|
|
|
|
|
|
|
# Don't update our state here: wait for the IRCd to send an acknowledgement instead.
|
2016-07-23 07:36:34 +02:00
|
|
|
# There is essentially a 3 second wait to do this, as we send NAMES with a delay
|
|
|
|
# to resync any users lost due to kicks being blocked, etc.
|
|
|
|
if (channel not in self.kick_queue) or (not self.kick_queue[channel][1].is_alive()):
|
|
|
|
# However, only do this if there isn't a NAMES request scheduled already.
|
2017-06-25 11:03:12 +02:00
|
|
|
t = threading.Timer(3, lambda: self.send('NAMES %s' % channel))
|
|
|
|
log.debug('(%s) kick: setting NAMES timer for %s on %s', self.name, target, channel)
|
2016-07-23 07:36:34 +02:00
|
|
|
|
|
|
|
# Store the channel, target UID, and timer object in the internal kick queue.
|
|
|
|
self.kick_queue[channel] = ({target}, t)
|
|
|
|
t.start()
|
|
|
|
else:
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) kick: adding %s to kick queue for channel %s', self.name, target, channel)
|
2016-07-23 07:36:34 +02:00
|
|
|
self.kick_queue[channel][0].add(target)
|
2016-07-18 07:34:26 +02:00
|
|
|
|
2016-07-18 05:59:42 +02:00
|
|
|
def message(self, source, target, text, notice=False):
|
|
|
|
"""Sends messages to the target."""
|
|
|
|
command = 'NOTICE' if notice else 'PRIVMSG'
|
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.pseudoclient and self.pseudoclient.uid == source:
|
|
|
|
self.send('%s %s :%s' % (command, self._expandPUID(target), text))
|
2016-08-31 23:07:32 +02:00
|
|
|
else:
|
2019-02-10 21:40:02 +01:00
|
|
|
# Pass the message on as a hook
|
|
|
|
super().message(source, target, text, notice=notice)
|
2016-07-18 07:34:26 +02:00
|
|
|
|
2016-09-26 04:28:15 +02:00
|
|
|
def mode(self, source, channel, modes, ts=None):
|
|
|
|
"""Sends channel MODE changes."""
|
2017-08-29 05:07:12 +02:00
|
|
|
if self.is_channel(channel):
|
2016-09-26 04:28:15 +02:00
|
|
|
extmodes = []
|
2016-10-08 05:38:45 +02:00
|
|
|
# Re-parse all channel modes locally to eliminate anything invalid, such as unbanning
|
|
|
|
# things that were never banned. This prevents the bot from getting caught in a loop
|
|
|
|
# with IRCd MODE acknowledgements.
|
|
|
|
# FIXME: More related safety checks should be added for this.
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) mode: re-parsing modes %s', self.name, modes)
|
2017-06-30 07:56:14 +02:00
|
|
|
joined_modes = self.join_modes(modes)
|
|
|
|
for modepair in self.parse_modes(channel, joined_modes):
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) mode: checking if %s a prefix mode: %s', self.name, modepair, self.prefixmodes)
|
|
|
|
if modepair[0][-1] in self.prefixmodes:
|
2017-06-30 07:56:14 +02:00
|
|
|
if self.is_internal_client(modepair[1]):
|
2016-09-26 05:06:08 +02:00
|
|
|
# Ignore prefix modes for virtual internal clients.
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) mode: skipping virtual client prefixmode change %s', self.name, modepair)
|
2016-09-26 05:06:08 +02:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# For other clients, change the mode argument to nick instead of PUID.
|
2017-06-30 07:56:14 +02:00
|
|
|
nick = self.get_friendly_name(modepair[1])
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) mode: coersing mode %s argument to %s', self.name, modepair, nick)
|
2016-09-26 05:06:08 +02:00
|
|
|
modepair = (modepair[0], nick)
|
2016-09-26 04:28:15 +02:00
|
|
|
extmodes.append(modepair)
|
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) mode: filtered modes for %s: %s', self.name, channel, extmodes)
|
2016-09-26 05:21:01 +02:00
|
|
|
if extmodes:
|
2017-09-24 06:54:02 +02:00
|
|
|
bufsize = self.S2S_BUFSIZE - len(':%s MODE %s ' % (self.get_hostmask(self.pseudoclient.uid), channel))
|
2019-06-24 05:13:04 +02:00
|
|
|
for msg in self.wrap_modes(extmodes, bufsize, max_modes_per_msg=int(self._caps.get('MODES') or 0)):
|
2017-09-24 06:54:02 +02:00
|
|
|
self.send('MODE %s %s' % (channel, msg))
|
|
|
|
# Don't update the state here: the IRCd sill respond with a MODE reply if successful.
|
2016-09-26 04:28:15 +02:00
|
|
|
|
2016-07-18 07:34:26 +02:00
|
|
|
def nick(self, source, newnick):
|
|
|
|
"""STUB: Sends NICK changes."""
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.pseudoclient and source == self.pseudoclient.uid:
|
|
|
|
self.send('NICK :%s' % newnick)
|
2016-08-25 07:54:53 +02:00
|
|
|
# No state update here: the IRCd will respond with a NICK acknowledgement if the change succeeds.
|
2016-07-23 09:55:06 +02:00
|
|
|
else:
|
2019-02-10 21:40:02 +01:00
|
|
|
super().nick(source, newnick)
|
2016-07-18 05:59:42 +02:00
|
|
|
|
2017-07-05 07:09:50 +02:00
|
|
|
def _ping_uplink(self):
|
2016-07-18 05:59:42 +02:00
|
|
|
"""
|
2017-07-05 07:09:50 +02:00
|
|
|
Sends a PING to the uplink.
|
2016-07-18 05:59:42 +02:00
|
|
|
"""
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.uplink:
|
2017-06-30 07:56:14 +02:00
|
|
|
self.send('PING %s' % self.get_friendly_name(self.uplink))
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-23 09:06:11 +02:00
|
|
|
# Poll WHO periodically to figure out any ident/host/away status changes.
|
2017-06-25 11:03:12 +02:00
|
|
|
for channel in self.pseudoclient.channels:
|
2017-08-21 03:49:07 +02:00
|
|
|
self._send_who(channel)
|
2016-07-23 09:06:11 +02:00
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
def part(self, source, channel, reason=''):
|
|
|
|
"""STUB: Parts a user from a channel."""
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].remove_user(source)
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[source].channels.discard(channel)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
|
|
|
# Only parts for the main PyLink client are actually forwarded. Others are ignored.
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.pseudoclient and source == self.pseudoclient.uid:
|
2018-07-19 04:10:19 +02:00
|
|
|
self._channels[channel]._clientbot_part_requested = True
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('PART %s :%s' % (channel, reason))
|
2016-07-23 09:55:06 +02:00
|
|
|
else:
|
2019-02-10 21:40:02 +01:00
|
|
|
super().part(source, channel, reason=reason)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2016-07-18 07:34:26 +02:00
|
|
|
def sjoin(self, server, channel, users, ts=None, modes=set()):
|
|
|
|
"""STUB: bursts joins from a server."""
|
2016-07-21 07:06:09 +02:00
|
|
|
# This stub only updates the state internally with the users
|
|
|
|
# given. modes and TS are currently ignored.
|
2016-07-18 07:34:26 +02:00
|
|
|
puids = {u[-1] for u in users}
|
|
|
|
for user in puids:
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.pseudoclient and self.pseudoclient.uid == user:
|
2016-08-11 20:24:23 +02:00
|
|
|
# If the SJOIN affects our main client, forward it as a regular JOIN.
|
|
|
|
self.join(user, channel)
|
|
|
|
else:
|
|
|
|
# Otherwise, track the state for our virtual clients.
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[user].channels.add(channel)
|
2016-07-18 07:34:26 +02:00
|
|
|
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].users |= puids
|
2017-06-30 07:56:14 +02:00
|
|
|
nicks = {self.get_friendly_name(u) for u in puids}
|
|
|
|
self.call_hooks([server, 'CLIENTBOT_SJOIN', {'channel': channel, 'nicks': nicks}])
|
2016-07-18 07:34:26 +02:00
|
|
|
|
2017-08-24 04:16:20 +02:00
|
|
|
def _set_account_name(self, uid, account):
|
|
|
|
"""
|
|
|
|
Updates the user's account metadata.
|
|
|
|
"""
|
|
|
|
if account is None: # Ignore when account=None
|
|
|
|
return
|
2017-10-26 01:00:24 +02:00
|
|
|
elif account in ('*', '0'): # No account
|
2017-08-24 04:16:20 +02:00
|
|
|
account = ''
|
|
|
|
|
|
|
|
if account != self.users[uid].services_account:
|
|
|
|
self.call_hooks([uid, 'CLIENT_SERVICES_LOGIN', {'text': account}])
|
|
|
|
|
2016-07-17 07:42:17 +02:00
|
|
|
def handle_events(self, data):
|
2016-07-18 07:34:26 +02:00
|
|
|
"""Event handler for the RFC1459/2812 (clientbot) protocol."""
|
2016-07-17 07:42:17 +02:00
|
|
|
data = data.split(" ")
|
2016-12-17 07:27:41 +01:00
|
|
|
|
2017-06-28 07:27:38 +02:00
|
|
|
tags = self.parse_message_tags(data)
|
2016-12-17 07:27:41 +01:00
|
|
|
if tags:
|
|
|
|
# If we have tags, split off the first argument.
|
|
|
|
data = data[1:]
|
|
|
|
|
2016-07-18 00:20:57 +02:00
|
|
|
try:
|
2017-06-28 02:15:27 +02:00
|
|
|
args = self.parse_prefixed_args(data)
|
2016-07-18 00:20:57 +02:00
|
|
|
sender = args[0]
|
|
|
|
command = args[1]
|
2016-07-18 05:59:31 +02:00
|
|
|
args = args[2:]
|
2016-07-18 00:20:57 +02:00
|
|
|
|
|
|
|
except IndexError:
|
|
|
|
# Raw command without an explicit sender; assume it's being sent by our uplink.
|
2017-06-28 02:15:27 +02:00
|
|
|
args = self.parse_args(data)
|
2017-06-25 11:03:12 +02:00
|
|
|
idsource = sender = self.uplink
|
2016-07-18 00:20:57 +02:00
|
|
|
command = args[0]
|
|
|
|
args = args[1:]
|
2016-07-17 07:42:17 +02:00
|
|
|
else:
|
2016-12-17 07:27:41 +01:00
|
|
|
# PyLink as a services framework expects UIDs and SIDs for everything. Since we connect
|
2016-07-18 00:20:57 +02:00
|
|
|
# as a bot here, there's no explicit user introduction, so we're going to generate
|
|
|
|
# pseudo-uids and pseudo-sids as we see prefixes.
|
2017-01-10 06:02:56 +01:00
|
|
|
if ('!' not in sender) and '.' in sender:
|
|
|
|
# Sender is a server name. XXX: make this check more foolproof
|
2017-07-03 07:13:43 +02:00
|
|
|
assert '@' not in sender, "Incoming server name %r clashes with a PUID!" % sender
|
2017-07-03 07:09:22 +02:00
|
|
|
if sender not in self.servers:
|
|
|
|
self.spawn_server(sender, internal=False)
|
|
|
|
idsource = sender
|
2016-07-18 00:20:57 +02:00
|
|
|
else:
|
2017-01-10 06:02:56 +01:00
|
|
|
# Sender is a either a nick or a nick!user@host prefix. Split it into its relevant parts.
|
|
|
|
try:
|
2018-03-03 05:06:23 +01:00
|
|
|
nick, ident, host = utils.split_hostmask(sender)
|
2017-01-10 06:02:56 +01:00
|
|
|
except ValueError:
|
|
|
|
ident = host = None # Set ident and host as null for now.
|
|
|
|
nick = sender # Treat the sender prefix we received as a nick.
|
2019-08-24 06:22:28 +02:00
|
|
|
idsource = self._get_UID(nick, ident, host, spawn_new=True)
|
2016-07-18 00:20:57 +02:00
|
|
|
|
2017-08-21 01:52:23 +02:00
|
|
|
if idsource in self.users:
|
|
|
|
# Handle IRCv3.2 account-tag.
|
2017-08-24 04:16:20 +02:00
|
|
|
self._set_account_name(idsource, tags.get('account'))
|
2017-08-21 01:52:23 +02:00
|
|
|
|
2016-07-17 07:42:17 +02:00
|
|
|
try:
|
|
|
|
func = getattr(self, 'handle_'+command.lower())
|
|
|
|
except AttributeError: # unhandled command
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
parsed_args = func(idsource, command, args)
|
|
|
|
if parsed_args is not None:
|
2016-12-17 07:28:40 +01:00
|
|
|
parsed_args['tags'] = tags # Add message tags to this dict.
|
2016-07-17 07:42:17 +02:00
|
|
|
return [idsource, command, parsed_args]
|
|
|
|
|
2017-06-28 07:27:38 +02:00
|
|
|
def _do_cap_end(self):
|
2017-03-05 08:54:16 +01:00
|
|
|
"""
|
|
|
|
Abort SASL login by sending CAP END.
|
|
|
|
"""
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('CAP END')
|
|
|
|
log.debug("(%s) Stopping CAP END timer.", self.name)
|
2017-03-05 08:54:16 +01:00
|
|
|
self._cap_timer.cancel()
|
|
|
|
|
2017-06-28 07:27:38 +02:00
|
|
|
def _try_sasl_auth(self):
|
2016-12-17 23:50:29 +01:00
|
|
|
"""
|
|
|
|
Starts an authentication attempt via SASL. This returns True if SASL
|
|
|
|
is enabled and correctly configured, and False otherwise.
|
|
|
|
"""
|
|
|
|
if 'sasl' not in self.ircv3_caps:
|
2017-06-25 11:03:12 +02:00
|
|
|
log.info("(%s) Skipping SASL auth since the IRCd doesn't support it.", self.name)
|
2016-12-17 23:50:29 +01:00
|
|
|
return
|
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
sasl_mech = self.serverdata.get('sasl_mechanism')
|
2016-12-18 00:47:15 +01:00
|
|
|
if sasl_mech:
|
2016-12-18 01:06:29 +01:00
|
|
|
sasl_mech = sasl_mech.upper()
|
2017-06-25 11:03:12 +02:00
|
|
|
sasl_user = self.serverdata.get('sasl_username')
|
|
|
|
sasl_pass = self.serverdata.get('sasl_password')
|
|
|
|
ssl_cert = self.serverdata.get('ssl_certfile')
|
|
|
|
ssl_key = self.serverdata.get('ssl_keyfile')
|
|
|
|
ssl = self.serverdata.get('ssl')
|
2016-12-17 23:50:29 +01:00
|
|
|
|
2016-12-18 01:06:29 +01:00
|
|
|
if sasl_mech == 'PLAIN':
|
|
|
|
if not (sasl_user and sasl_pass):
|
|
|
|
log.warning("(%s) Not attempting PLAIN authentication; sasl_username and/or "
|
2017-06-25 11:03:12 +02:00
|
|
|
"sasl_password aren't correctly set.", self.name)
|
2016-12-18 01:06:29 +01:00
|
|
|
return False
|
|
|
|
elif sasl_mech == 'EXTERNAL':
|
|
|
|
if not ssl:
|
|
|
|
log.warning("(%s) Not attempting EXTERNAL authentication; SASL external requires "
|
2017-06-25 11:03:12 +02:00
|
|
|
"SSL, but it isn't enabled.", self.name)
|
2016-12-18 01:06:29 +01:00
|
|
|
return False
|
|
|
|
elif not (ssl_cert and ssl_key):
|
|
|
|
log.warning("(%s) Not attempting EXTERNAL authentication; ssl_certfile and/or "
|
2017-06-25 11:03:12 +02:00
|
|
|
"ssl_keyfile aren't correctly set.", self.name)
|
2016-12-18 01:06:29 +01:00
|
|
|
return False
|
|
|
|
else:
|
2017-06-25 11:03:12 +02:00
|
|
|
log.warning('(%s) Unsupported SASL mechanism %s; aborting SASL.', self.name, sasl_mech)
|
2016-12-18 00:47:15 +01:00
|
|
|
return False
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('AUTHENTICATE %s' % sasl_mech, queue=False)
|
2016-12-17 23:50:29 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-06-28 07:27:38 +02:00
|
|
|
def _send_auth_chunk(self, data):
|
2016-12-17 23:50:29 +01:00
|
|
|
"""Send Base64 encoded SASL authentication chunks."""
|
|
|
|
enc_data = base64.b64encode(data).decode()
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('AUTHENTICATE %s' % enc_data, queue=False)
|
2016-12-17 23:50:29 +01:00
|
|
|
|
|
|
|
def handle_authenticate(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles AUTHENTICATE, or SASL authentication requests from the server.
|
|
|
|
"""
|
|
|
|
# Client: AUTHENTICATE PLAIN
|
|
|
|
# Server: AUTHENTICATE +
|
|
|
|
# Client: AUTHENTICATE ...
|
|
|
|
if not args:
|
|
|
|
return
|
|
|
|
if args[0] == '+':
|
2017-06-25 11:03:12 +02:00
|
|
|
sasl_mech = self.serverdata['sasl_mechanism'].upper()
|
2016-12-17 23:50:29 +01:00
|
|
|
if sasl_mech == 'PLAIN':
|
2017-06-25 11:03:12 +02:00
|
|
|
sasl_user = self.serverdata['sasl_username']
|
|
|
|
sasl_pass = self.serverdata['sasl_password']
|
2016-12-28 03:16:15 +01:00
|
|
|
authstring = '%s\0%s\0%s' % (sasl_user, sasl_user, sasl_pass)
|
2017-06-28 07:27:38 +02:00
|
|
|
self._send_auth_chunk(authstring.encode('utf-8'))
|
2016-12-17 23:50:29 +01:00
|
|
|
elif sasl_mech == 'EXTERNAL':
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('AUTHENTICATE +')
|
2016-12-17 23:50:29 +01:00
|
|
|
|
2017-08-24 04:19:53 +02:00
|
|
|
def handle_900(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles SASL RPL_LOGGEDIN numerics.
|
|
|
|
"""
|
|
|
|
# <- :charybdis.midnight.vpn 900 ice ice!pylink@localhost ice :You are now logged in as ice
|
|
|
|
# <- :server 900 <nick> <nick>!<ident>@<host> <account> :You are now logged in as <user>
|
|
|
|
|
|
|
|
self.pseudoclient.services_account = args[2]
|
|
|
|
log.info('(%s) SASL authentication successful: now logged in as %s', self.name, args[2])
|
|
|
|
|
|
|
|
# Note: we don't send a hook here yet; is doing so for the bot this early in login
|
|
|
|
# really necessary?
|
|
|
|
|
2016-12-17 23:50:29 +01:00
|
|
|
def handle_904(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles SASL authentication status reports.
|
|
|
|
"""
|
2017-08-24 04:19:53 +02:00
|
|
|
# <- :charybdis.midnight.vpn 903 ice :SASL authentication successful
|
|
|
|
logfunc = log.debug if command == '903' else log.warning
|
2017-06-25 11:03:12 +02:00
|
|
|
logfunc('(%s) %s', self.name, args[-1])
|
2016-12-19 09:52:28 +01:00
|
|
|
if not self.has_eob:
|
2017-08-24 04:19:53 +02:00
|
|
|
# End the pre-login CAP stuff regardless of whether SASL was successful.
|
2017-06-28 07:27:38 +02:00
|
|
|
self._do_cap_end()
|
2016-12-18 00:41:34 +01:00
|
|
|
handle_903 = handle_902 = handle_905 = handle_906 = handle_907 = handle_904
|
2016-12-17 23:50:29 +01:00
|
|
|
|
2017-06-28 07:27:38 +02:00
|
|
|
def _request_ircv3_caps(self):
|
2016-12-19 09:40:19 +01:00
|
|
|
# Filter the capabilities we want by the ones actually supported by the server.
|
|
|
|
available_caps = {cap for cap in IRCV3_CAPABILITIES if cap in self.ircv3_caps_available}
|
|
|
|
# And by the ones we don't already have.
|
|
|
|
caps_wanted = available_caps - self.ircv3_caps
|
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Requesting IRCv3 capabilities %s (available: %s)', self.name, caps_wanted, available_caps)
|
2016-12-19 09:40:19 +01:00
|
|
|
if caps_wanted:
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('CAP REQ :%s' % ' '.join(caps_wanted), queue=False)
|
2016-12-19 09:40:19 +01:00
|
|
|
|
2016-12-17 07:28:40 +01:00
|
|
|
def handle_cap(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles IRCv3 capabilities transmission.
|
|
|
|
"""
|
|
|
|
subcmd = args[1]
|
|
|
|
|
|
|
|
if subcmd == 'LS':
|
|
|
|
# Server: CAP * LS * :multi-prefix extended-join account-notify batch invite-notify tls
|
|
|
|
# Server: CAP * LS * :cap-notify server-time example.org/dummy-cap=dummyvalue example.org/second-dummy-cap
|
|
|
|
# Server: CAP * LS :userhost-in-names sasl=EXTERNAL,DH-AES,DH-BLOWFISH,ECDSA-NIST256P-CHALLENGE,PLAIN
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Got new capabilities %s', self.name, args[-1])
|
2017-06-28 02:15:27 +02:00
|
|
|
self.ircv3_caps_available.update(self.parse_isupport(args[-1], None))
|
2016-12-17 23:50:29 +01:00
|
|
|
if args[2] != '*':
|
2017-06-28 07:27:38 +02:00
|
|
|
self._request_ircv3_caps()
|
2016-12-19 09:40:19 +01:00
|
|
|
|
2016-12-17 07:28:40 +01:00
|
|
|
elif subcmd == 'ACK':
|
|
|
|
# Server: CAP * ACK :multi-prefix sasl
|
|
|
|
newcaps = set(args[-1].split())
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Received ACK for IRCv3 capabilities %s', self.name, newcaps)
|
2016-12-17 07:28:40 +01:00
|
|
|
self.ircv3_caps |= newcaps
|
|
|
|
|
2016-12-17 23:50:29 +01:00
|
|
|
# Only send CAP END immediately if SASL is disabled. Otherwise, wait for the 90x responses
|
|
|
|
# to do so.
|
2017-06-28 07:27:38 +02:00
|
|
|
if not self._try_sasl_auth():
|
2016-12-19 09:52:28 +01:00
|
|
|
if not self.has_eob:
|
2017-06-28 07:27:38 +02:00
|
|
|
self._do_cap_end()
|
2016-12-17 23:50:29 +01:00
|
|
|
elif subcmd == 'NAK':
|
2016-12-18 00:57:01 +01:00
|
|
|
log.warning('(%s) Got NAK for IRCv3 capabilities %s, even though they were supposedly available',
|
2017-06-25 11:03:12 +02:00
|
|
|
self.name, args[-1])
|
2016-12-19 09:52:28 +01:00
|
|
|
if not self.has_eob:
|
2017-06-28 07:27:38 +02:00
|
|
|
self._do_cap_end()
|
2016-12-19 09:40:19 +01:00
|
|
|
elif subcmd == 'NEW':
|
|
|
|
# :irc.example.com CAP modernclient NEW :batch
|
|
|
|
# :irc.example.com CAP tester NEW :away-notify extended-join
|
|
|
|
# Note: CAP NEW allows capabilities with values (e.g. sasl=mech1,mech2), while CAP DEL
|
|
|
|
# does not.
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Got new capabilities %s', self.name, args[-1])
|
2017-06-28 02:15:27 +02:00
|
|
|
newcaps = self.parse_isupport(args[-1], None)
|
2016-12-19 09:54:20 +01:00
|
|
|
self.ircv3_caps_available.update(newcaps)
|
2017-06-28 07:27:38 +02:00
|
|
|
self._request_ircv3_caps()
|
2016-12-19 09:40:19 +01:00
|
|
|
|
2016-12-19 10:06:49 +01:00
|
|
|
# Attempt SASL auth routines when sasl is added/removed, if doing so is enabled.
|
2017-06-25 11:03:12 +02:00
|
|
|
if 'sasl' in newcaps and self.serverdata.get('sasl_reauth'):
|
|
|
|
log.debug('(%s) Attempting SASL reauth due to CAP NEW', self.name)
|
2017-06-28 07:27:38 +02:00
|
|
|
self._try_sasl_auth()
|
2016-12-19 09:54:20 +01:00
|
|
|
|
2016-12-19 09:40:19 +01:00
|
|
|
elif subcmd == 'DEL':
|
|
|
|
# :irc.example.com CAP modernclient DEL :userhost-in-names multi-prefix away-notify
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Removing capabilities %s', self.name, args[-1])
|
2016-12-19 09:40:19 +01:00
|
|
|
for cap in args[-1].split():
|
|
|
|
# Remove the capabilities from the list available, and return None (ignore) if any fail
|
|
|
|
self.ircv3_caps_available.pop(cap, None)
|
|
|
|
self.ircv3_caps.discard(cap)
|
2016-12-17 23:50:29 +01:00
|
|
|
|
2016-07-18 00:20:57 +02:00
|
|
|
def handle_001(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles 001 / RPL_WELCOME.
|
|
|
|
"""
|
|
|
|
# enumerate our uplink
|
2017-06-25 11:03:12 +02:00
|
|
|
self.uplink = source
|
2016-07-18 00:20:57 +02:00
|
|
|
|
2016-08-04 20:10:51 +02:00
|
|
|
def handle_376(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles end of MOTD numerics, used to start things like autoperform.
|
|
|
|
"""
|
2016-07-18 08:10:10 +02:00
|
|
|
|
2016-08-04 20:10:51 +02:00
|
|
|
# Run autoperform commands.
|
2017-06-25 11:03:12 +02:00
|
|
|
for line in self.serverdata.get("autoperform", []):
|
2018-02-21 08:19:19 +01:00
|
|
|
# Expand substitutions like $nick, $ident, $host
|
|
|
|
tmpl = string.Template(line)
|
2018-05-26 11:12:38 +02:00
|
|
|
|
|
|
|
args = self.pseudoclient.get_fields()
|
|
|
|
|
|
|
|
log.debug('(%s) handle_376: bot user fields are %s', self.name, args)
|
|
|
|
line = tmpl.safe_substitute(**args)
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send(line)
|
2016-07-18 08:10:10 +02:00
|
|
|
|
2016-08-04 20:10:51 +02:00
|
|
|
# Virtual endburst hook.
|
2017-08-31 04:39:57 +02:00
|
|
|
self.connected.set() # Note, this should always be set before the actual ENDBURST hook
|
2018-02-01 04:32:39 +01:00
|
|
|
self.servers[source].has_eob = True
|
|
|
|
|
2017-08-31 04:39:57 +02:00
|
|
|
return {'parse_as': 'ENDBURST'}
|
2017-07-03 21:42:51 +02:00
|
|
|
|
2016-08-04 20:10:51 +02:00
|
|
|
handle_422 = handle_376
|
2016-07-17 07:42:17 +02:00
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
def handle_353(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles 353 / RPL_NAMREPLY.
|
|
|
|
"""
|
|
|
|
# <- :charybdis.midnight.vpn 353 ice = #test :ice @GL
|
|
|
|
|
|
|
|
# Mark "@"-type channels as secret automatically, per RFC2812.
|
2017-08-08 06:54:33 +02:00
|
|
|
channel = args[2]
|
2016-07-18 06:52:06 +02:00
|
|
|
if args[1] == '@':
|
2017-06-30 07:56:14 +02:00
|
|
|
self.apply_modes(channel, [('+s', None)])
|
2016-07-18 06:52:06 +02:00
|
|
|
|
|
|
|
names = set()
|
2016-07-22 03:37:50 +02:00
|
|
|
modes = set()
|
2017-06-25 11:03:12 +02:00
|
|
|
prefix_to_mode = {v:k for k, v in self.prefixmodes.items()}
|
|
|
|
prefixes = ''.join(self.prefixmodes.values())
|
2016-07-22 03:37:50 +02:00
|
|
|
|
2019-04-09 07:33:55 +02:00
|
|
|
# N.B. only split on spaces because of color in hosts nonsense...
|
|
|
|
# str.split() by default treats \x1f as whitespace
|
2019-04-29 21:04:11 +02:00
|
|
|
for name in args[-1].strip().split(' '):
|
2016-07-22 03:37:50 +02:00
|
|
|
nick = name.lstrip(prefixes)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2017-08-20 08:23:19 +02:00
|
|
|
# Handle userhost-in-names where available.
|
2019-04-09 07:33:55 +02:00
|
|
|
ident = host = None
|
2017-08-20 08:23:19 +02:00
|
|
|
if 'userhost-in-names' in self.ircv3_caps:
|
2019-04-09 07:33:55 +02:00
|
|
|
try:
|
|
|
|
nick, ident, host = utils.split_hostmask(nick)
|
|
|
|
except ValueError:
|
2019-04-29 20:58:18 +02:00
|
|
|
log.exception('(%s) Failed to split hostmask %r from /names reply on %s; args=%s', self.name, nick, channel, args)
|
2019-04-09 07:33:55 +02:00
|
|
|
# If error, leave nick unsplit
|
2017-08-20 08:23:19 +02:00
|
|
|
|
2019-04-29 21:04:11 +02:00
|
|
|
if not nick:
|
|
|
|
continue
|
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
# Get the PUID for the given nick. If one doesn't exist, spawn
|
2017-08-20 07:28:53 +02:00
|
|
|
# a new virtual user.
|
2019-08-24 06:22:28 +02:00
|
|
|
idsource = self._get_UID(nick, ident=ident, host=host, spawn_new=True)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2016-07-23 07:36:34 +02:00
|
|
|
# Queue these virtual users to be joined if they're not already in the channel,
|
|
|
|
# or we're waiting for a kick acknowledgment for them.
|
2017-08-25 11:11:48 +02:00
|
|
|
if (idsource not in self._channels[channel].users) or (idsource in \
|
2016-07-23 07:36:34 +02:00
|
|
|
self.kick_queue.get(channel, ([],))[0]):
|
2016-07-18 06:52:06 +02:00
|
|
|
names.add(idsource)
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[idsource].channels.add(channel)
|
2017-08-20 08:23:19 +02:00
|
|
|
if host:
|
|
|
|
self.users[idsource]._clientbot_identhost_received = True
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2016-07-22 03:37:50 +02:00
|
|
|
# Process prefix modes
|
|
|
|
for char in name:
|
2017-06-25 11:03:12 +02:00
|
|
|
if char in self.prefixmodes.values():
|
2016-07-22 03:37:50 +02:00
|
|
|
modes.add(('+' + prefix_to_mode[char], idsource))
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
# Statekeeping: make sure the channel's user list is updated!
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].users |= names
|
2017-06-30 07:56:14 +02:00
|
|
|
self.apply_modes(channel, modes)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) handle_353: adding users %s to %s', self.name, names, channel)
|
|
|
|
log.debug('(%s) handle_353: adding modes %s to %s', self.name, modes, channel)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2017-08-20 07:28:53 +02:00
|
|
|
# Send JOIN hook payloads only for users that we know the ident@host of already.
|
2018-04-24 21:15:25 +02:00
|
|
|
# This is mostly used to resync kicked Clientbot users that can't actually be kicked
|
|
|
|
# after a delay.
|
|
|
|
if names and hasattr(self.irc.channels[channel], '_clientbot_initial_who_received'):
|
|
|
|
log.debug('(%s) handle_353: sending JOIN hook because /WHO was already received for %s',
|
|
|
|
self.irc.name, channel)
|
|
|
|
return {'channel': channel, 'users': names, 'modes': self._channels[channel].modes,
|
2016-07-23 07:36:34 +02:00
|
|
|
'parse_as': "JOIN"}
|
2016-07-22 09:22:56 +02:00
|
|
|
|
2017-08-21 03:49:07 +02:00
|
|
|
def _send_who(self, channel):
|
|
|
|
"""Sends /WHO to a channel, with WHOX args if that is supported."""
|
|
|
|
# Note: %% = escaped %
|
|
|
|
# %cuhsnfdr is the default; adding 'a' to it sends the account name.
|
|
|
|
# 'd' is omitted because we don't really care about hop count.
|
|
|
|
if 'WHOX' in self._caps:
|
|
|
|
self.send('WHO %s %%cuhsnfar' % channel)
|
|
|
|
else:
|
|
|
|
self.send('WHO %s' % channel)
|
|
|
|
|
2016-07-22 09:22:56 +02:00
|
|
|
def handle_352(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles 352 / RPL_WHOREPLY.
|
|
|
|
"""
|
2017-08-21 03:49:07 +02:00
|
|
|
# parameter count: 0 1 2 3 4 5 6 7(-1)
|
2016-07-22 09:22:56 +02:00
|
|
|
# <- :charybdis.midnight.vpn 352 ice #test ~pylink 127.0.0.1 charybdis.midnight.vpn ice H+ :0 PyLink
|
|
|
|
# <- :charybdis.midnight.vpn 352 ice #test ~gl 127.0.0.1 charybdis.midnight.vpn GL H*@ :0 realname
|
2017-08-21 03:49:07 +02:00
|
|
|
# with WHO %cuhsnfar (WHOX) - note, hopcount and realname are separate!
|
|
|
|
# 0 1 2 3 4 5 6 7 8(-1)
|
|
|
|
# <- :charybdis.midnight.vpn 354 ice #test ~gl localhost charybdis.midnight.vpn GL H*@ GL :realname
|
2018-04-24 21:15:25 +02:00
|
|
|
channel = args[1]
|
2016-07-22 09:22:56 +02:00
|
|
|
ident = args[2]
|
|
|
|
host = args[3]
|
|
|
|
nick = args[5]
|
|
|
|
status = args[6]
|
2017-08-21 03:49:07 +02:00
|
|
|
|
|
|
|
# Hopcount and realname field are together in regular WHO. We only care about the latter.
|
|
|
|
realname = args[-1]
|
|
|
|
if command == '352':
|
|
|
|
realname = realname.split(' ', 1)[-1]
|
2017-04-10 00:12:52 +02:00
|
|
|
|
2019-06-24 04:46:23 +02:00
|
|
|
uid = self._get_UID(nick, spawn_new=False)
|
2016-07-22 09:22:56 +02:00
|
|
|
|
2016-09-09 03:45:34 +02:00
|
|
|
if uid is None:
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug("(%s) Ignoring extraneous /WHO info for %s", self.name, nick)
|
2016-09-09 03:45:34 +02:00
|
|
|
return
|
|
|
|
|
2017-07-01 06:29:38 +02:00
|
|
|
self.update_client(uid, 'IDENT', ident)
|
|
|
|
self.update_client(uid, 'HOST', host)
|
|
|
|
self.update_client(uid, 'GECOS', realname)
|
2017-08-20 07:28:53 +02:00
|
|
|
self.users[uid]._clientbot_identhost_received = True
|
2016-07-22 09:22:56 +02:00
|
|
|
|
|
|
|
# The status given uses the following letters: <H|G>[*][@|+]
|
|
|
|
# H means here (not marked /away)
|
|
|
|
# G means away is set (we'll have to fake a message because it's not given)
|
|
|
|
# * means IRCop.
|
|
|
|
# The rest are prefix modes. Multiple can be given by the IRCd if multiple are set
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) handle_352: status string on user %s: %s', self.name, nick, status)
|
2016-07-22 09:22:56 +02:00
|
|
|
if status[0] == 'G':
|
2018-05-10 07:23:14 +02:00
|
|
|
if not self.users[uid].away:
|
|
|
|
# Only set away status if not previously set
|
|
|
|
log.debug('(%s) handle_352: calling away() with argument', self.name)
|
|
|
|
self.away(uid, 'Away')
|
2016-07-22 09:22:56 +02:00
|
|
|
elif status[0] == 'H':
|
2018-05-10 07:23:14 +02:00
|
|
|
if self.users[uid].away:
|
|
|
|
log.debug('(%s) handle_352: calling away() without argument', self.name)
|
|
|
|
self.away(uid, '') # Unmark away status, but only if /away was previously set
|
2016-07-22 09:45:37 +02:00
|
|
|
else:
|
2017-06-25 11:03:12 +02:00
|
|
|
log.warning('(%s) handle_352: got wrong string %s for away status', self.name, status[0])
|
2016-07-22 09:22:56 +02:00
|
|
|
|
2017-08-21 03:49:07 +02:00
|
|
|
if command == '354' and len(args) >= 9: # WHOX account
|
2017-08-22 08:27:47 +02:00
|
|
|
account = args[7]
|
2017-08-21 03:49:07 +02:00
|
|
|
log.debug('(%s) handle_354: got account %r for %s', self.name, account, uid)
|
|
|
|
|
2017-08-24 04:16:20 +02:00
|
|
|
self._set_account_name(uid, account)
|
2017-08-21 03:49:07 +02:00
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.serverdata.get('track_oper_statuses'):
|
2016-10-10 04:03:46 +02:00
|
|
|
if '*' in status: # Track IRCop status
|
2018-06-12 08:56:44 +02:00
|
|
|
if not self.is_oper(uid):
|
2016-10-10 04:03:46 +02:00
|
|
|
# Don't send duplicate oper ups if the target is already oper.
|
2017-06-30 07:56:14 +02:00
|
|
|
self.apply_modes(uid, [('+o', None)])
|
|
|
|
self.call_hooks([uid, 'MODE', {'target': uid, 'modes': {('+o', None)}}])
|
|
|
|
self.call_hooks([uid, 'CLIENT_OPERED', {'text': 'IRC Operator'}])
|
2018-06-12 08:56:44 +02:00
|
|
|
elif self.is_oper(uid) and not self.is_internal_client(uid):
|
2016-10-10 04:03:46 +02:00
|
|
|
# Track deopers
|
2017-06-30 07:56:14 +02:00
|
|
|
self.apply_modes(uid, [('-o', None)])
|
|
|
|
self.call_hooks([uid, 'MODE', {'target': uid, 'modes': {('-o', None)}}])
|
2016-07-22 09:22:56 +02:00
|
|
|
|
|
|
|
self.who_received.add(uid)
|
2017-08-21 03:49:07 +02:00
|
|
|
handle_354 = handle_352 # 354 = RPL_WHOSPCRPL, used by WHOX
|
2016-07-22 09:22:56 +02:00
|
|
|
|
|
|
|
def handle_315(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles 315 / RPL_ENDOFWHO.
|
|
|
|
"""
|
|
|
|
# <- :charybdis.midnight.vpn 315 ice #test :End of /WHO list.
|
|
|
|
# Join all the users in which the last batch of /who requests were received.
|
|
|
|
users = self.who_received.copy()
|
|
|
|
self.who_received.clear()
|
|
|
|
|
2017-08-08 06:54:33 +02:00
|
|
|
channel = args[1]
|
2017-08-25 11:11:48 +02:00
|
|
|
c = self._channels[channel]
|
2016-09-26 04:28:15 +02:00
|
|
|
|
|
|
|
modes = set(c.modes)
|
2018-05-10 07:31:19 +02:00
|
|
|
bursted_before = hasattr(c, '_clientbot_initial_who_received')
|
|
|
|
|
|
|
|
queued_users = []
|
2017-08-20 08:23:19 +02:00
|
|
|
for user in users.copy():
|
2016-09-26 04:28:15 +02:00
|
|
|
# Fill in prefix modes of everyone when doing mock SJOIN.
|
2016-12-28 07:15:57 +01:00
|
|
|
try:
|
2017-07-01 06:40:05 +02:00
|
|
|
for mode in c.get_prefix_modes(user):
|
2017-06-25 11:03:12 +02:00
|
|
|
modechar = self.cmodes.get(mode)
|
|
|
|
log.debug('(%s) handle_315: adding mode %s +%s %s', self.name, mode, modechar, user)
|
2016-12-28 07:15:57 +01:00
|
|
|
if modechar:
|
|
|
|
modes.add((modechar, user))
|
|
|
|
except KeyError as e:
|
|
|
|
log.debug("(%s) Ignoring KeyError (%s) from WHO response; it's probably someone we "
|
2017-06-25 11:03:12 +02:00
|
|
|
"don't share any channels with", self.name, e)
|
2016-09-26 04:28:15 +02:00
|
|
|
|
2018-05-10 07:31:19 +02:00
|
|
|
if bursted_before and user in c.users:
|
|
|
|
log.debug("(%s) Skipping join of %s/%s to %r", self.name, user,
|
|
|
|
self.get_friendly_name(user), channel)
|
|
|
|
continue
|
|
|
|
queued_users.append(user)
|
|
|
|
|
|
|
|
c._clientbot_initial_who_received = True
|
|
|
|
if queued_users:
|
2017-08-20 08:23:19 +02:00
|
|
|
return {'channel': channel, 'users': users, 'modes': modes,
|
|
|
|
'parse_as': "JOIN"}
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2016-07-23 21:48:26 +02:00
|
|
|
def handle_433(self, source, command, args):
|
|
|
|
# <- :millennium.overdrivenetworks.com 433 * ice :Nickname is already in use.
|
2017-08-22 08:05:27 +02:00
|
|
|
|
|
|
|
self._nick_fails += 1
|
|
|
|
newnick = self.pseudoclient.nick = world.services['pylink'].get_nick(self, fails=self._nick_fails)
|
|
|
|
log.debug('(%s) _nick_fails = %s, trying new nick %r', self.name, self._nick_fails, newnick)
|
|
|
|
|
|
|
|
self.send('NICK %s' % newnick)
|
2016-07-23 21:48:26 +02:00
|
|
|
handle_432 = handle_437 = handle_433
|
|
|
|
|
2017-08-21 01:12:13 +02:00
|
|
|
def handle_account(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles IRCv3 account-notify messages.
|
|
|
|
"""
|
|
|
|
# <- :nick!user@host ACCOUNT accountname
|
|
|
|
# <- :nick!user@host ACCOUNT *
|
|
|
|
|
2017-08-24 04:16:20 +02:00
|
|
|
self._set_account_name(source, args[0])
|
2017-08-21 01:12:13 +02:00
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
def handle_join(self, source, command, args):
|
|
|
|
"""
|
2017-08-21 04:24:46 +02:00
|
|
|
Handles incoming JOINs, as well as JOIN acknowledgements for us.
|
2016-07-18 06:52:06 +02:00
|
|
|
"""
|
2017-08-24 04:28:10 +02:00
|
|
|
# Classic format:
|
2016-07-18 06:52:06 +02:00
|
|
|
# <- :GL|!~GL@127.0.0.1 JOIN #whatever
|
2017-08-24 04:28:10 +02:00
|
|
|
# With extended-join:
|
|
|
|
# <- :GL|!~GL@127.0.0.1 JOIN #whatever accountname :realname
|
2017-08-08 06:54:33 +02:00
|
|
|
channel = args[0]
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].users.add(source)
|
2017-08-24 04:11:15 +02:00
|
|
|
self.users[source].channels.add(channel)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2017-08-24 04:28:10 +02:00
|
|
|
if len(args) >= 3:
|
|
|
|
self._set_account_name(source, args[1])
|
|
|
|
# Hmm, because _get_UID only looks at the sender prefix, it doesn't
|
|
|
|
# have a way to process the realname info from JOIN yet...
|
|
|
|
self.update_client(source, 'GECOS', args[2])
|
|
|
|
|
2017-08-21 04:24:46 +02:00
|
|
|
# Only fetch modes, TS, and user hosts once we're actually in the channel.
|
|
|
|
# The IRCd will send us a JOIN with our nick!user@host if our JOIN succeeded.
|
|
|
|
if self.pseudoclient and source == self.pseudoclient.uid:
|
|
|
|
self.send('MODE %s' % channel)
|
|
|
|
self._send_who(channel)
|
2017-09-24 06:25:48 +02:00
|
|
|
|
|
|
|
if self.serverdata.get('fetch_ban_lists', False):
|
|
|
|
self.send('MODE %s +b' % channel)
|
|
|
|
|
|
|
|
for m in ('banexception', 'invex'):
|
|
|
|
if m in self.cmodes:
|
|
|
|
self.send('MODE %s +%s' % (channel, self.cmodes[m]))
|
2017-08-24 04:11:15 +02:00
|
|
|
else:
|
|
|
|
self.call_hooks([source, 'CLIENTBOT_JOIN', {'channel': channel}])
|
2017-08-25 11:11:48 +02:00
|
|
|
return {'channel': channel, 'users': [source], 'modes': self._channels[channel].modes}
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2016-07-18 07:34:26 +02:00
|
|
|
def handle_kick(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles incoming KICKs.
|
|
|
|
"""
|
|
|
|
# <- :GL!~gl@127.0.0.1 KICK #whatever GL| :xd
|
2017-08-08 06:54:33 +02:00
|
|
|
channel = args[0]
|
2019-06-24 04:46:23 +02:00
|
|
|
target = self._get_UID(args[1], spawn_new=False)
|
2016-07-18 07:34:26 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
reason = args[2]
|
|
|
|
except IndexError:
|
|
|
|
reason = ''
|
|
|
|
|
2016-07-23 07:36:34 +02:00
|
|
|
if channel in self.kick_queue:
|
|
|
|
# Remove this client from the kick queue if present there.
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) kick: removing %s from kick queue for channel %s', self.name, target, channel)
|
2016-07-23 07:36:34 +02:00
|
|
|
self.kick_queue[channel][0].discard(target)
|
|
|
|
|
|
|
|
if not self.kick_queue[channel][0]:
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) kick: cancelling kick timer for channel %s (all kicks accounted for)', self.name, channel)
|
2016-07-23 07:36:34 +02:00
|
|
|
# There aren't any kicks that failed to be acknowledged. We can remove the timer now
|
|
|
|
self.kick_queue[channel][1].cancel()
|
|
|
|
del self.kick_queue[channel]
|
|
|
|
|
2016-12-17 05:45:47 +01:00
|
|
|
# Statekeeping: remove the target from the channel they were previously in.
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].remove_user(target)
|
2016-12-17 05:45:47 +01:00
|
|
|
try:
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[target].channels.remove(channel)
|
2016-12-17 05:45:47 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2018-08-25 06:52:34 +02:00
|
|
|
# Don't repeat hooks if we're the kicker, unless we're also the target.
|
|
|
|
if self.is_internal_client(source) or self.is_internal_server(source):
|
|
|
|
if self.pseudoclient and target != self.pseudoclient.uid:
|
|
|
|
return
|
|
|
|
return {'channel': channel, 'target': target, 'text': reason}
|
2016-07-18 07:34:26 +02:00
|
|
|
|
2016-07-22 04:12:05 +02:00
|
|
|
def handle_mode(self, source, command, args):
|
|
|
|
"""Handles MODE changes."""
|
|
|
|
# <- :GL!~gl@127.0.0.1 MODE #dev +v ice
|
|
|
|
# <- :ice MODE ice :+Zi
|
|
|
|
target = args[0]
|
2017-08-29 05:07:12 +02:00
|
|
|
if self.is_channel(target):
|
2017-08-25 11:11:48 +02:00
|
|
|
oldobj = self._channels[target].deepcopy()
|
2016-07-22 04:12:05 +02:00
|
|
|
else:
|
2019-06-24 04:46:23 +02:00
|
|
|
target = self._get_UID(target, spawn_new=False)
|
2016-07-22 04:12:05 +02:00
|
|
|
oldobj = None
|
|
|
|
modes = args[1:]
|
2017-06-30 07:56:14 +02:00
|
|
|
changedmodes = self.parse_modes(target, modes)
|
|
|
|
self.apply_modes(target, changedmodes)
|
2016-07-22 04:12:05 +02:00
|
|
|
|
2017-06-30 07:56:14 +02:00
|
|
|
if self.is_internal_client(target):
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Suppressing MODE change hook for internal client %s', self.name, target)
|
2016-08-11 04:38:46 +02:00
|
|
|
return
|
2016-10-08 05:38:45 +02:00
|
|
|
if changedmodes:
|
2016-10-08 05:54:15 +02:00
|
|
|
# Prevent infinite loops: don't send MODE hooks if the sender is US.
|
|
|
|
# Note: this is not the only check in Clientbot to prevent mode loops: if our nick
|
|
|
|
# somehow gets desynced, this may not catch everything it's supposed to.
|
2017-06-25 11:03:12 +02:00
|
|
|
if (self.pseudoclient and source != self.pseudoclient.uid) or not self.pseudoclient:
|
2016-10-08 05:54:15 +02:00
|
|
|
return {'target': target, 'modes': changedmodes, 'channeldata': oldobj}
|
2016-07-22 04:12:05 +02:00
|
|
|
|
2017-04-09 23:49:19 +02:00
|
|
|
def handle_324(self, source, command, args):
|
|
|
|
"""Handles MODE announcements via RPL_CHANNELMODEIS (i.e. the response to /mode #channel)"""
|
|
|
|
# -> MODE #test
|
|
|
|
# <- :midnight.vpn 324 GL #test +nt
|
|
|
|
# <- :midnight.vpn 329 GL #test 1491773459
|
2017-08-08 06:54:33 +02:00
|
|
|
channel = args[1]
|
2017-04-09 23:49:19 +02:00
|
|
|
modes = args[2:]
|
2017-06-25 11:03:12 +02:00
|
|
|
log.debug('(%s) Got RPL_CHANNELMODEIS (324) modes %s for %s', self.name, modes, channel)
|
2018-06-09 03:45:20 +02:00
|
|
|
|
|
|
|
# Sometimes IRCds suppress arguments to +lk, so ignore missing args
|
|
|
|
changedmodes = self.parse_modes(channel, modes, ignore_missing_args=True)
|
|
|
|
|
2017-06-30 07:56:14 +02:00
|
|
|
self.apply_modes(channel, changedmodes)
|
2017-04-09 23:49:19 +02:00
|
|
|
|
|
|
|
def handle_329(self, source, command, args):
|
|
|
|
"""Handles TS announcements via RPL_CREATIONTIME."""
|
2017-08-08 06:54:33 +02:00
|
|
|
channel = args[1]
|
2017-04-09 23:49:19 +02:00
|
|
|
ts = int(args[2])
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].ts = ts
|
2017-04-09 23:49:19 +02:00
|
|
|
|
2017-08-20 08:55:32 +02:00
|
|
|
def handle_chghost(self, source, command, args):
|
|
|
|
"""Handles the IRCv3 CHGHOST command."""
|
|
|
|
# <- :nick!user@host CHGHOST ident new.host
|
|
|
|
|
|
|
|
ident = self.users[source].ident
|
|
|
|
host = self.users[source].host
|
|
|
|
|
|
|
|
self.users[source].ident = newident = args[0]
|
|
|
|
self.users[source].host = newhost = args[1]
|
|
|
|
|
|
|
|
if ident != newident:
|
|
|
|
self.call_hooks([source, 'CHGIDENT',
|
|
|
|
{'target': source, 'newident': newident}])
|
|
|
|
|
|
|
|
if host != newhost:
|
|
|
|
self.call_hooks([source, 'CHGHOST',
|
|
|
|
{'target': source, 'newhost': newhost}])
|
|
|
|
|
2016-07-18 07:34:26 +02:00
|
|
|
def handle_nick(self, source, command, args):
|
2016-07-22 04:12:05 +02:00
|
|
|
"""Handles NICK changes."""
|
2016-07-18 07:34:26 +02:00
|
|
|
# <- :GL|!~GL@127.0.0.1 NICK :GL_
|
2017-10-08 05:06:48 +02:00
|
|
|
newnick = args[0]
|
2016-09-01 08:05:36 +02:00
|
|
|
|
2017-08-22 08:05:27 +02:00
|
|
|
if not self.connected.is_set():
|
2016-09-01 08:05:36 +02:00
|
|
|
# We haven't properly logged on yet, so any initial NICK should be treated as a forced
|
2017-08-22 08:05:27 +02:00
|
|
|
# nick change for us. For example, this clause is used to handle forced nick changes
|
2016-09-01 08:05:36 +02:00
|
|
|
# sent by ZNC, when the login nick and the actual IRC nick of the bouncer differ.
|
2017-10-08 05:06:48 +02:00
|
|
|
self.pseudoclient.nick = newnick
|
2017-08-22 08:05:27 +02:00
|
|
|
log.debug('(%s) Pre-auth FNC: Changing our nick to %s', self.name, args[0])
|
2016-09-01 21:56:34 +02:00
|
|
|
return
|
2017-08-22 08:05:27 +02:00
|
|
|
elif source == self.pseudoclient.uid:
|
|
|
|
self._nick_fails = 0 # Our last nick change succeeded.
|
2016-09-01 08:05:36 +02:00
|
|
|
|
2017-06-25 11:03:12 +02:00
|
|
|
oldnick = self.users[source].nick
|
2016-09-01 08:12:45 +02:00
|
|
|
|
2017-10-08 05:06:48 +02:00
|
|
|
self.users[source].nick = newnick
|
|
|
|
|
|
|
|
return {'newnick': newnick, 'oldnick': oldnick}
|
2016-07-18 07:34:26 +02:00
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
def handle_part(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles incoming PARTs.
|
|
|
|
"""
|
|
|
|
# <- :GL|!~GL@127.0.0.1 PART #whatever
|
2018-02-01 04:35:13 +01:00
|
|
|
channels = args[0].split(',')
|
2016-07-18 06:52:06 +02:00
|
|
|
try:
|
|
|
|
reason = args[1]
|
|
|
|
except IndexError:
|
|
|
|
reason = ''
|
|
|
|
|
|
|
|
for channel in channels:
|
2017-08-25 11:11:48 +02:00
|
|
|
self._channels[channel].remove_user(source)
|
2017-06-25 11:03:12 +02:00
|
|
|
self.users[source].channels -= set(channels)
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2018-07-19 04:10:19 +02:00
|
|
|
# Only send the PART hook for parts not initiated by us - this is for consistency with other
|
|
|
|
# protocols
|
|
|
|
notify_channels = []
|
|
|
|
for channel in channels:
|
|
|
|
is_part_requested = getattr(self._channels[channel], '_clientbot_part_requested', False)
|
|
|
|
if is_part_requested:
|
|
|
|
log.debug('(%s) clientbot.handle_part: not forwarding part hook for %s since we requested it', self.name, channel)
|
|
|
|
self._channels[channel]._clientbot_part_requested = False
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
notify_channels.append(channel)
|
|
|
|
|
|
|
|
if notify_channels:
|
|
|
|
log.debug('(%s) clientbot.handle_part: returning part hook for %s (original: %s)', self.name, notify_channels, channels)
|
|
|
|
return {'channels': notify_channels, 'text': reason}
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2016-07-18 05:20:49 +02:00
|
|
|
def handle_ping(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles incoming PING requests.
|
|
|
|
"""
|
2017-06-25 11:03:12 +02:00
|
|
|
self.send('PONG :%s' % args[0], queue=False)
|
2016-07-18 05:20:49 +02:00
|
|
|
|
2016-07-18 05:59:42 +02:00
|
|
|
def handle_privmsg(self, source, command, args):
|
|
|
|
"""Handles incoming PRIVMSG/NOTICE."""
|
|
|
|
# <- :sender PRIVMSG #dev :afasfsa
|
|
|
|
# <- :sender NOTICE somenick :afasfsa
|
|
|
|
target = args[0]
|
|
|
|
|
2017-06-30 07:56:14 +02:00
|
|
|
if self.is_internal_client(source) or self.is_internal_server(source):
|
2017-06-25 11:03:12 +02:00
|
|
|
log.warning('(%s) Received %s to %s being routed the wrong way!', self.name, command, target)
|
2017-02-18 07:18:42 +01:00
|
|
|
return
|
|
|
|
|
2018-02-19 07:11:46 +01:00
|
|
|
real_target = target.lstrip(''.join(self.prefixmodes.values()))
|
|
|
|
if not self.is_channel(real_target):
|
2019-06-24 04:46:23 +02:00
|
|
|
target = self._get_UID(target, spawn_new=False)
|
2017-08-08 06:54:33 +02:00
|
|
|
|
2016-10-15 07:29:13 +02:00
|
|
|
if target:
|
|
|
|
return {'target': target, 'text': args[1]}
|
2017-02-18 07:18:42 +01:00
|
|
|
handle_notice = handle_privmsg
|
2016-07-18 05:59:42 +02:00
|
|
|
|
2016-07-18 06:52:06 +02:00
|
|
|
def handle_quit(self, source, command, args):
|
|
|
|
"""Handles incoming QUITs."""
|
2017-06-25 11:03:12 +02:00
|
|
|
if self.pseudoclient and source == self.pseudoclient.uid:
|
2017-02-18 07:26:35 +01:00
|
|
|
# Someone faked a quit from us? We should abort.
|
|
|
|
raise ProtocolError("Received QUIT from uplink (%s)" % args[0])
|
2019-07-01 22:36:53 +02:00
|
|
|
elif source not in self.users:
|
|
|
|
log.debug('(%s) Ignoring QUIT on non-existent user %s', self.name, source)
|
|
|
|
return
|
2017-02-18 07:26:35 +01:00
|
|
|
|
2019-07-01 22:36:53 +02:00
|
|
|
userdata = self.users[source]
|
2016-07-18 06:52:06 +02:00
|
|
|
self.quit(source, args[0])
|
2019-07-01 22:36:53 +02:00
|
|
|
return {'text': args[0], 'userdata': userdata}
|
2016-07-18 06:52:06 +02:00
|
|
|
|
2017-09-03 06:46:09 +02:00
|
|
|
def handle_404(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles ERR_CANNOTSENDTOCHAN and other similar numerics.
|
|
|
|
"""
|
|
|
|
# <- :some.server 404 james #test :Cannot send to channel
|
|
|
|
if len(args) >= 2 and self.is_channel(args[1]):
|
|
|
|
channel = args[1]
|
|
|
|
f = log.warning
|
|
|
|
|
2017-11-05 10:17:59 +01:00
|
|
|
if channel not in self.channels:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Don't send the warning multiple times to prevent flood if the target
|
2017-09-03 06:46:09 +02:00
|
|
|
# is a log chan.
|
|
|
|
if hasattr(self.channels[channel], '_clientbot_cannot_send_warned'):
|
|
|
|
f = log.debug
|
|
|
|
f('(%s) Failed to send message to %s: %s', self.name, channel, args[-1])
|
|
|
|
|
|
|
|
self.channels[channel]._clientbot_cannot_send_warned = True
|
|
|
|
|
|
|
|
# 408: ERR_NOCOLORSONCHAN on Bahamut, ERR_NOCTRLSONCHAN on Hybrid
|
|
|
|
handle_408 = handle_404
|
|
|
|
# 492: ERR_NOCTCP on Hybrid
|
|
|
|
handle_492 = handle_404
|
2016-07-18 05:20:49 +02:00
|
|
|
|
2017-09-24 06:25:48 +02:00
|
|
|
def handle_367(self, source, command, args, banmode='b'):
|
|
|
|
"""
|
|
|
|
Handles RPL_BANLIST, used to enumerate bans.
|
|
|
|
"""
|
|
|
|
# <- :irc3.lose-the-game.nat 367 james #test *!*@1.2.3.4 james 1504560159
|
|
|
|
channel = args[1]
|
|
|
|
target = args[2]
|
|
|
|
if channel not in self.channels:
|
|
|
|
log.warning('(%s) got ban mode +%s %s on unknown channel %s?', self.name, banmode, target)
|
|
|
|
else:
|
|
|
|
# Just apply the mode; we send out a mode hook only when the corresponding ban list has finished sending.
|
|
|
|
self.apply_modes(channel, [('+%s' % banmode, target)])
|
|
|
|
|
|
|
|
def handle_368(self, source, command, args, banmode='b'):
|
|
|
|
"""
|
|
|
|
Handles RPL_ENDOFBANLIST, used to end off ban lists.
|
|
|
|
"""
|
|
|
|
# <- :irc3.lose-the-game.nat 368 james #test :End of Channel Ban List
|
|
|
|
channel = args[1]
|
2018-06-15 11:18:20 +02:00
|
|
|
if channel not in self.channels:
|
|
|
|
return
|
|
|
|
|
|
|
|
modes = [('+%s' % banmode, m[1]) for m in self.channels[channel].modes if m[0] == banmode]
|
|
|
|
|
2017-09-24 06:25:48 +02:00
|
|
|
# Send out the hook. We don't worry about repeats since these modes don't need to be
|
|
|
|
# enumerated more than once per JOIN anyways.
|
2017-10-08 06:51:38 +02:00
|
|
|
if modes:
|
|
|
|
return {'target': channel, 'parse_as': 'MODE',
|
|
|
|
'modes': modes}
|
2017-09-24 06:25:48 +02:00
|
|
|
|
|
|
|
def handle_346(self, *args):
|
|
|
|
"""
|
|
|
|
Handles RPL_INVITELIST, used to enumerate invite exceptions.
|
|
|
|
"""
|
|
|
|
return self.handle_367(*args, banmode=self.cmodes.get('invex', 'I'))
|
|
|
|
|
|
|
|
def handle_347(self, *args):
|
|
|
|
"""
|
|
|
|
Handles RPL_ENDOFINVITELIST, used to end off invite exception lists.
|
|
|
|
"""
|
|
|
|
if 'invex' not in self.cmodes:
|
|
|
|
log.warning('(%s) Got 347 / RPL_ENDOFINVITELIST even though invex is not a registered mode?', self.name)
|
|
|
|
return self.handle_368(*args, banmode=self.cmodes.get('invex', 'I'))
|
|
|
|
|
|
|
|
def handle_348(self, *args):
|
|
|
|
"""
|
|
|
|
Handles RPL_EXCEPTLIST, used to enumerate ban exceptions.
|
|
|
|
"""
|
|
|
|
return self.handle_367(*args, banmode=self.cmodes.get('banexception', 'e'))
|
|
|
|
|
|
|
|
def handle_349(self, *args):
|
|
|
|
"""
|
|
|
|
Handles RPL_ENDOFEXCEPTLIST, used to end off ban exception lists.
|
|
|
|
"""
|
|
|
|
if 'banexception' not in self.cmodes:
|
|
|
|
log.warning('(%s) Got 349 / RPL_ENDOFEXCEPTLIST even though banexception is not a registered mode?', self.name)
|
|
|
|
return self.handle_368(*args, banmode=self.cmodes.get('banexception', 'e'))
|
|
|
|
|
2017-12-22 21:19:07 +01:00
|
|
|
def handle_471(self, source, command, args):
|
|
|
|
"""
|
|
|
|
Handles numerics commonly sent when a client fails to join a channel:
|
|
|
|
|
|
|
|
* ERR_TOOMANYCHANNELS (405)
|
|
|
|
* ERR_CHANNELISFULL (471)
|
|
|
|
* ERR_INVITEONLYCHAN (473)
|
|
|
|
* ERR_BANNEDFROMCHAN (474)
|
|
|
|
* ERR_BADCHANNELKEY (475)
|
|
|
|
* ERR_BADCHANMASK (476)
|
|
|
|
* ERR_NEEDREGGEDNICK (477)
|
|
|
|
* ERR_BADCHANNAME (479)
|
|
|
|
* ERR_SECUREONLYCHAN / ERR_SSLONLYCHAN (489)
|
|
|
|
* ERR_DELAYREJOIN (495)
|
|
|
|
* ERR_OPERONLY (520)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if len(args) >= 3 and self.is_channel(args[1]):
|
|
|
|
log.warning('(%s) Failed to join channel %s: %s', self.name, args[1], args[-1])
|
|
|
|
|
|
|
|
handle_405 = handle_473 = handle_474 = handle_475 = handle_476 = handle_477 = \
|
|
|
|
handle_479 = handle_489 = handle_495 = handle_520 = handle_471
|
|
|
|
|
2016-07-17 07:42:17 +02:00
|
|
|
Class = ClientbotWrapperProtocol
|