mirror of https://github.com/Shizmob/pydle.git
#2 removed `async` references in `ircv3/cap`
This commit is contained in:
parent
2e15894c35
commit
471fce4b24
|
@ -1,7 +1,6 @@
|
||||||
## cap.py
|
## cap.py
|
||||||
# Server <-> client optional extension indication support.
|
# Server <-> client optional extension indication support.
|
||||||
# See also: http://ircv3.atheme.org/specification/capability-negotiation-3.1
|
# See also: http://ircv3.atheme.org/specification/capability-negotiation-3.1
|
||||||
from pydle import async
|
|
||||||
import pydle.protocol
|
import pydle.protocol
|
||||||
from pydle.features import rfc1459
|
from pydle.features import rfc1459
|
||||||
|
|
||||||
|
@ -29,17 +28,16 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
self._capabilities_requested = set()
|
self._capabilities_requested = set()
|
||||||
self._capabilities_negotiating = set()
|
self._capabilities_negotiating = set()
|
||||||
|
|
||||||
@async.coroutine
|
async def _register(self):
|
||||||
def _register(self):
|
|
||||||
""" Hijack registration to send a CAP LS first. """
|
""" Hijack registration to send a CAP LS first. """
|
||||||
if self.registered:
|
if self.registered:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Ask server to list capabilities.
|
# Ask server to list capabilities.
|
||||||
yield from self.rawmsg('CAP', 'LS', '302')
|
await self.rawmsg('CAP', 'LS', '302')
|
||||||
|
|
||||||
# Register as usual.
|
# Register as usual.
|
||||||
yield from super()._register()
|
await super()._register()
|
||||||
|
|
||||||
def _capability_normalize(self, cap):
|
def _capability_normalize(self, cap):
|
||||||
cap = cap.lstrip(PREFIXES).lower()
|
cap = cap.lstrip(PREFIXES).lower()
|
||||||
|
@ -53,19 +51,17 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
|
|
||||||
## API.
|
## API.
|
||||||
|
|
||||||
@async.coroutine
|
async def _capability_negotiated(self, capab):
|
||||||
def _capability_negotiated(self, capab):
|
|
||||||
""" Mark capability as negotiated, and end negotiation if we're done. """
|
""" Mark capability as negotiated, and end negotiation if we're done. """
|
||||||
self._capabilities_negotiating.discard(capab)
|
self._capabilities_negotiating.discard(capab)
|
||||||
|
|
||||||
if not self._capabilities_requested and not self._capabilities_negotiating:
|
if not self._capabilities_requested and not self._capabilities_negotiating:
|
||||||
yield from self.rawmsg('CAP', 'END')
|
await self.rawmsg('CAP', 'END')
|
||||||
|
|
||||||
|
|
||||||
## Message handlers.
|
## Message handlers.
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap(self, message):
|
||||||
def on_raw_cap(self, message):
|
|
||||||
""" Handle CAP message. """
|
""" Handle CAP message. """
|
||||||
target, subcommand = message.params[:2]
|
target, subcommand = message.params[:2]
|
||||||
params = message.params[2:]
|
params = message.params[2:]
|
||||||
|
@ -73,12 +69,11 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
# Call handler.
|
# Call handler.
|
||||||
attr = 'on_raw_cap_' + pydle.protocol.identifierify(subcommand)
|
attr = 'on_raw_cap_' + pydle.protocol.identifierify(subcommand)
|
||||||
if hasattr(self, attr):
|
if hasattr(self, attr):
|
||||||
yield from getattr(self, attr)(params)
|
await getattr(self, attr)(params)
|
||||||
else:
|
else:
|
||||||
self.logger.warning('Unknown CAP subcommand sent from server: %s', subcommand)
|
self.logger.warning('Unknown CAP subcommand sent from server: %s', subcommand)
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap_ls(self, params):
|
||||||
def on_raw_cap_ls(self, params):
|
|
||||||
""" Update capability mapping. Request capabilities. """
|
""" Update capability mapping. Request capabilities. """
|
||||||
to_request = set()
|
to_request = set()
|
||||||
|
|
||||||
|
@ -91,7 +86,7 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
|
|
||||||
# Check if we support the capability.
|
# Check if we support the capability.
|
||||||
attr = 'on_capability_' + pydle.protocol.identifierify(capab) + '_available'
|
attr = 'on_capability_' + pydle.protocol.identifierify(capab) + '_available'
|
||||||
supported = (yield from getattr(self, attr)(value)) if hasattr(self, attr) else False
|
supported = (await getattr(self, attr)(value)) if hasattr(self, attr) else False
|
||||||
|
|
||||||
if supported:
|
if supported:
|
||||||
if isinstance(supported, str):
|
if isinstance(supported, str):
|
||||||
|
@ -104,13 +99,12 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
if to_request:
|
if to_request:
|
||||||
# Request some capabilities.
|
# Request some capabilities.
|
||||||
self._capabilities_requested.update(x.split(CAPABILITY_VALUE_DIVIDER, 1)[0] for x in to_request)
|
self._capabilities_requested.update(x.split(CAPABILITY_VALUE_DIVIDER, 1)[0] for x in to_request)
|
||||||
yield from self.rawmsg('CAP', 'REQ', ' '.join(to_request))
|
await self.rawmsg('CAP', 'REQ', ' '.join(to_request))
|
||||||
else:
|
else:
|
||||||
# No capabilities requested, end negotiation.
|
# No capabilities requested, end negotiation.
|
||||||
yield from self.rawmsg('CAP', 'END')
|
await self.rawmsg('CAP', 'END')
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap_list(self, params):
|
||||||
def on_raw_cap_list(self, params):
|
|
||||||
""" Update active capabilities. """
|
""" Update active capabilities. """
|
||||||
self._capabilities = { capab: False for capab in self._capabilities }
|
self._capabilities = { capab: False for capab in self._capabilities }
|
||||||
|
|
||||||
|
@ -118,8 +112,7 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
capab, value = self._capability_normalize(capab)
|
capab, value = self._capability_normalize(capab)
|
||||||
self._capabilities[capab] = value if value else True
|
self._capabilities[capab] = value if value else True
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap_ack(self, params):
|
||||||
def on_raw_cap_ack(self, params):
|
|
||||||
""" Update active capabilities: requested capability accepted. """
|
""" Update active capabilities: requested capability accepted. """
|
||||||
for capab in params[0].split():
|
for capab in params[0].split():
|
||||||
cp, value = self._capability_normalize(capab)
|
cp, value = self._capability_normalize(capab)
|
||||||
|
@ -139,11 +132,11 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
|
|
||||||
# Indicate we're gonna use this capability if needed.
|
# Indicate we're gonna use this capability if needed.
|
||||||
if capab.startswith(ACKNOWLEDGEMENT_REQUIRED_PREFIX):
|
if capab.startswith(ACKNOWLEDGEMENT_REQUIRED_PREFIX):
|
||||||
yield from self.rawmsg('CAP', 'ACK', cp)
|
await self.rawmsg('CAP', 'ACK', cp)
|
||||||
|
|
||||||
# Run callback.
|
# Run callback.
|
||||||
if hasattr(self, attr):
|
if hasattr(self, attr):
|
||||||
status = yield from getattr(self, attr)()
|
status = await getattr(self, attr)()
|
||||||
else:
|
else:
|
||||||
status = NEGOTIATED
|
status = NEGOTIATED
|
||||||
|
|
||||||
|
@ -154,15 +147,14 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
# Ruh-roh, negotiation failed. Disable the capability.
|
# Ruh-roh, negotiation failed. Disable the capability.
|
||||||
self.logger.warning('Capability negotiation for %s failed. Attempting to disable capability again.', cp)
|
self.logger.warning('Capability negotiation for %s failed. Attempting to disable capability again.', cp)
|
||||||
|
|
||||||
yield from self.rawmsg('CAP', 'REQ', '-' + cp)
|
await self.rawmsg('CAP', 'REQ', '-' + cp)
|
||||||
self._capabilities_requested.add(cp)
|
self._capabilities_requested.add(cp)
|
||||||
|
|
||||||
# If we have no capabilities left to process, end it.
|
# If we have no capabilities left to process, end it.
|
||||||
if not self._capabilities_requested and not self._capabilities_negotiating:
|
if not self._capabilities_requested and not self._capabilities_negotiating:
|
||||||
yield from self.rawmsg('CAP', 'END')
|
await self.rawmsg('CAP', 'END')
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap_nak(self, params):
|
||||||
def on_raw_cap_nak(self, params):
|
|
||||||
""" Update active capabilities: requested capability rejected. """
|
""" Update active capabilities: requested capability rejected. """
|
||||||
for capab in params[0].split():
|
for capab in params[0].split():
|
||||||
capab, _ = self._capability_normalize(capab)
|
capab, _ = self._capability_normalize(capab)
|
||||||
|
@ -171,39 +163,34 @@ class CapabilityNegotiationSupport(rfc1459.RFC1459Support):
|
||||||
|
|
||||||
# If we have no capabilities left to process, end it.
|
# If we have no capabilities left to process, end it.
|
||||||
if not self._capabilities_requested and not self._capabilities_negotiating:
|
if not self._capabilities_requested and not self._capabilities_negotiating:
|
||||||
yield from self.rawmsg('CAP', 'END')
|
await self.rawmsg('CAP', 'END')
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap_del(self, params):
|
||||||
def on_raw_cap_del(self, params):
|
|
||||||
for capab in params[0].split():
|
for capab in params[0].split():
|
||||||
attr = 'on_capability_{}_disabled'.format(pydle.protocol.identifierify(capab))
|
attr = 'on_capability_{}_disabled'.format(pydle.protocol.identifierify(capab))
|
||||||
if self._capabilities.get(capab, False) and hasattr(self, attr):
|
if self._capabilities.get(capab, False) and hasattr(self, attr):
|
||||||
yield from getattr(self, attr)()
|
await getattr(self, attr)()
|
||||||
yield from self.on_raw_cap_nak(params)
|
await self.on_raw_cap_nak(params)
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_cap_new(self, params):
|
||||||
def on_raw_cap_new(self, params):
|
await self.on_raw_cap_ls(params)
|
||||||
yield from self.on_raw_cap_ls(params)
|
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_410(self, message):
|
||||||
def on_raw_410(self, message):
|
|
||||||
""" Unknown CAP subcommand or CAP error. Force-end negotiations. """
|
""" Unknown CAP subcommand or CAP error. Force-end negotiations. """
|
||||||
self.logger.error('Server sent "Unknown CAP subcommand: %s". Aborting capability negotiation.', message.params[0])
|
self.logger.error('Server sent "Unknown CAP subcommand: %s". Aborting capability negotiation.', message.params[0])
|
||||||
|
|
||||||
self._capabilities_requested = set()
|
self._capabilities_requested = set()
|
||||||
self._capabilities_negotiating = set()
|
self._capabilities_negotiating = set()
|
||||||
yield from self.rawmsg('CAP', 'END')
|
await self.rawmsg('CAP', 'END')
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_421(self, message):
|
||||||
def on_raw_421(self, message):
|
|
||||||
""" Hijack to ignore the absence of a CAP command. """
|
""" Hijack to ignore the absence of a CAP command. """
|
||||||
if message.params[0] == 'CAP':
|
if message.params[0] == 'CAP':
|
||||||
return
|
return
|
||||||
yield from super().on_raw_421(message)
|
await super().on_raw_421(message)
|
||||||
|
|
||||||
@async.coroutine
|
async def on_raw_451(self, message):
|
||||||
def on_raw_451(self, message):
|
|
||||||
""" Hijack to ignore the absence of a CAP command. """
|
""" Hijack to ignore the absence of a CAP command. """
|
||||||
if message.params[0] == 'CAP':
|
if message.params[0] == 'CAP':
|
||||||
return
|
return
|
||||||
yield from super().on_raw_451(message)
|
await super().on_raw_451(message)
|
||||||
|
|
Loading…
Reference in New Issue