3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 21:19:31 +01:00

protocols: consolidate modeClient and modeServer into mode()

This commit is contained in:
James Lu 2016-01-16 17:08:17 -08:00
parent cb467bcfdb
commit 5ccf204c75
8 changed files with 37 additions and 70 deletions

View File

@ -127,7 +127,7 @@ def handle_mode(irc, source, command, args):
# client, revert any forced deoper attempts. # client, revert any forced deoper attempts.
if irc.isInternalClient(target) and not irc.isInternalClient(source): if irc.isInternalClient(target) and not irc.isInternalClient(source):
if ('-o', None) in modes and (target == irc.pseudoclient.uid or not utils.isManipulatableClient(irc, target)): if ('-o', None) in modes and (target == irc.pseudoclient.uid or not utils.isManipulatableClient(irc, target)):
irc.proto.modeServer(irc.sid, target, {('+o', None)}) irc.proto.mode(irc.sid, target, {('+o', None)})
utils.add_hook(handle_mode, 'MODE') utils.add_hook(handle_mode, 'MODE')
def handle_operup(irc, source, command, args): def handle_operup(irc, source, command, args):

View File

@ -60,9 +60,9 @@ internals](https://github.com/GLolol/PyLink/blob/0.4.0-dev/classes.py#L267-L272)
- **`message`**`(self, source, target, text)` - Sends a PRIVMSG from a PyLink client. - **`message`**`(self, source, target, text)` - Sends a PRIVMSG from a PyLink client.
- **`modeClient`**`(self, source, target, modes, ts=None)` - Sends modes from a PyLink client. `modes` takes a set of `([+/-]mode char, mode arg)` tuples. - **`mode`**`(self, source, target, modes, ts=None)` - Sends modes from a PyLink client. `modes` takes a set of `([+/-]mode char, mode arg)` tuples.
- **`modeServer`**`(self, source, target, modes, ts=None)` - Sends modes from a PyLink server. - **`mode`**`(self, source, target, modes, ts=None)` - Sends modes from a PyLink server.
- **`nick`**`(self, source, newnick)` - Changes the nick of a PyLink client. - **`nick`**`(self, source, newnick)` - Changes the nick of a PyLink client.

View File

@ -225,7 +225,7 @@ def mode(irc, source, args):
irc.reply("Error: No valid modes were given.") irc.reply("Error: No valid modes were given.")
return return
irc.proto.modeClient(irc.pseudoclient.uid, target, parsedmodes) irc.proto.mode(irc.pseudoclient.uid, target, parsedmodes)
# Call the appropriate hooks for plugins like relay. # Call the appropriate hooks for plugins like relay.
irc.callHooks([irc.pseudoclient.uid, 'OPERCMDS_MODEOVERRIDE', irc.callHooks([irc.pseudoclient.uid, 'OPERCMDS_MODEOVERRIDE',

View File

@ -618,10 +618,10 @@ def relayModes(irc, remoteirc, sender, channel, modes=None):
# Check if the sender is a user; remember servers are allowed to set modes too. # Check if the sender is a user; remember servers are allowed to set modes too.
u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False) u = getRemoteUser(irc, remoteirc, sender, spawnIfMissing=False)
if u: if u:
remoteirc.proto.modeClient(u, remotechan, supported_modes) remoteirc.proto.mode(u, remotechan, supported_modes)
else: else:
rsid = getRemoteSid(remoteirc, irc) rsid = getRemoteSid(remoteirc, irc)
remoteirc.proto.modeServer(rsid, remotechan, supported_modes) remoteirc.proto.mode(rsid, remotechan, supported_modes)
def relayWhoisHandler(irc, target): def relayWhoisHandler(irc, target):
user = irc.users[target] user = irc.users[target]
@ -904,7 +904,7 @@ def handle_mode(irc, numeric, command, args):
reversed_modes = utils.reverseModes(irc, target, modes, oldobj=oldchan) reversed_modes = utils.reverseModes(irc, target, modes, oldobj=oldchan)
log.debug('(%s) Reversing mode changes of %r with %r (CLAIM).', log.debug('(%s) Reversing mode changes of %r with %r (CLAIM).',
irc.name, modes, reversed_modes) irc.name, modes, reversed_modes)
irc.proto.modeClient(irc.pseudoclient.uid, target, reversed_modes) irc.proto.mode(irc.pseudoclient.uid, target, reversed_modes)
break break
else: else:
# Set hideoper on remote opers, to prevent inflating # Set hideoper on remote opers, to prevent inflating
@ -918,7 +918,7 @@ def handle_mode(irc, numeric, command, args):
modes.append(('-%s' % hideoper_mode, None)) modes.append(('-%s' % hideoper_mode, None))
remoteuser = getRemoteUser(irc, remoteirc, target, spawnIfMissing=False) remoteuser = getRemoteUser(irc, remoteirc, target, spawnIfMissing=False)
if remoteuser and modes: if remoteuser and modes:
remoteirc.proto.modeClient(remoteuser, remoteuser, modes) remoteirc.proto.mode(remoteuser, remoteuser, modes)
utils.add_hook(handle_mode, 'MODE') utils.add_hook(handle_mode, 'MODE')

View File

@ -154,10 +154,15 @@ class InspIRCdProtocol(TS6BaseProtocol):
userobj.opertype = otype userobj.opertype = otype
self._send(target, 'OPERTYPE %s' % otype.replace(" ", "_")) self._send(target, 'OPERTYPE %s' % otype.replace(" ", "_"))
def _sendModes(self, numeric, target, modes, ts=None): def mode(self, numeric, target, modes, ts=None):
"""Internal function to send mode changes from a PyLink client/server.""" """Sends mode changes from a PyLink client/server."""
# -> :9PYAAAAAA FMODE #pylink 1433653951 +os 9PYAAAAAA # -> :9PYAAAAAA FMODE #pylink 1433653951 +os 9PYAAAAAA
# -> :9PYAAAAAA MODE 9PYAAAAAA -i+w # -> :9PYAAAAAA MODE 9PYAAAAAA -i+w
if (not self.irc.isInternalClient(numeric)) and \
(not self.irc.isInternalServer(numeric)):
raise LookupError('No such PyLink client/server exists.')
log.debug('(%s) inspircd._sendModes: received %r for mode list', self.irc.name, modes) log.debug('(%s) inspircd._sendModes: received %r for mode list', self.irc.name, modes)
if ('+o', None) in modes and not utils.isChannel(target): if ('+o', None) in modes and not utils.isChannel(target):
# https://github.com/inspircd/inspircd/blob/master/src/modules/m_spanningtree/opertype.cpp#L26-L28 # https://github.com/inspircd/inspircd/blob/master/src/modules/m_spanningtree/opertype.cpp#L26-L28
@ -171,24 +176,6 @@ class InspIRCdProtocol(TS6BaseProtocol):
else: else:
self._send(numeric, 'MODE %s %s' % (target, joinedmodes)) self._send(numeric, 'MODE %s %s' % (target, joinedmodes))
def modeClient(self, numeric, target, modes, ts=None):
"""
Sends mode changes from a PyLink client. <modes> should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
if not self.irc.isInternalClient(numeric):
raise LookupError('No such PyLink client exists.')
self._sendModes(numeric, target, modes, ts=ts)
def modeServer(self, numeric, target, modes, ts=None):
"""
Sends mode changes from a PyLink server. <list of modes> should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
if not self.irc.isInternalServer(numeric):
raise LookupError('No such PyLink server exists.')
self._sendModes(numeric, target, modes, ts=ts)
def kill(self, numeric, target, reason): def kill(self, numeric, target, reason):
"""Sends a kill from a PyLink client/server.""" """Sends a kill from a PyLink client/server."""
if (not self.irc.isInternalClient(numeric)) and \ if (not self.irc.isInternalClient(numeric)) and \

View File

@ -132,10 +132,16 @@ class TS6Protocol(TS6BaseProtocol):
# Only save our prefix modes in the channel state if our TS is lower than or equal to theirs. # Only save our prefix modes in the channel state if our TS is lower than or equal to theirs.
utils.applyModes(self.irc, channel, changedmodes) utils.applyModes(self.irc, channel, changedmodes)
def _sendModes(self, numeric, target, modes, ts=None): def mode(self, numeric, target, modes, ts=None):
"""Internal function to send mode changes from a PyLink client/server.""" """Sends mode changes from a PyLink client/server."""
if (not self.irc.isInternalClient(numeric)) and \
(not self.irc.isInternalServer(numeric)):
raise LookupError('No such PyLink client/server exists.')
utils.applyModes(self.irc, target, modes) utils.applyModes(self.irc, target, modes)
modes = list(modes) modes = list(modes)
if utils.isChannel(target): if utils.isChannel(target):
ts = ts or self.irc.channels[utils.toLower(self.irc, target)].ts ts = ts or self.irc.channels[utils.toLower(self.irc, target)].ts
# TMODE: # TMODE:
@ -144,6 +150,8 @@ class TS6Protocol(TS6BaseProtocol):
# On output, at most ten cmode parameters should be sent; if there are more, # On output, at most ten cmode parameters should be sent; if there are more,
# multiple TMODE messages should be sent. # multiple TMODE messages should be sent.
while modes[:9]: while modes[:9]:
# Seriously, though. If you send more than 10 mode parameters in
# a line, charybdis will silently REJECT the entire command!
joinedmodes = utils.joinModes(modes = [m for m in modes[:9] if m[0] not in self.irc.cmodes['*A']]) joinedmodes = utils.joinModes(modes = [m for m in modes[:9] if m[0] not in self.irc.cmodes['*A']])
modes = modes[9:] modes = modes[9:]
self._send(numeric, 'TMODE %s %s %s' % (ts, target, joinedmodes)) self._send(numeric, 'TMODE %s %s %s' % (ts, target, joinedmodes))
@ -151,24 +159,6 @@ class TS6Protocol(TS6BaseProtocol):
joinedmodes = utils.joinModes(modes) joinedmodes = utils.joinModes(modes)
self._send(numeric, 'MODE %s %s' % (target, joinedmodes)) self._send(numeric, 'MODE %s %s' % (target, joinedmodes))
def modeClient(self, numeric, target, modes, ts=None):
"""
Sends mode changes from a PyLink client. <modes> should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
if not self.irc.isInternalClient(numeric):
raise LookupError('No such PyLink client exists.')
self._sendModes(numeric, target, modes, ts=ts)
def modeServer(self, numeric, target, modes, ts=None):
"""
Sends mode changes from a PyLink server. <list of modes> should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
if not self.irc.isInternalServer(numeric):
raise LookupError('No such PyLink server exists.')
self._sendModes(numeric, target, modes, ts=ts)
def kill(self, numeric, target, reason): def kill(self, numeric, target, reason):
"""Sends a kill from a PyLink client/server.""" """Sends a kill from a PyLink client/server."""

View File

@ -190,9 +190,17 @@ class UnrealProtocol(TS6BaseProtocol):
self._send(numeric, 'KILL %s :%s!PyLink (%s)' % (target, self.irc.serverdata['hostname'], reason)) self._send(numeric, 'KILL %s :%s!PyLink (%s)' % (target, self.irc.serverdata['hostname'], reason))
self.removeClient(target) self.removeClient(target)
def _sendModes(self, numeric, target, modes, ts=None): def mode(self, numeric, target, modes, ts=None):
"""Internal function to send mode changes from a PyLink client/server.""" """
Sends mode changes from a PyLink client/server. The mode list should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
# <- :unreal.midnight.vpn MODE #endlessvoid +ntCo GL 1444361345 # <- :unreal.midnight.vpn MODE #endlessvoid +ntCo GL 1444361345
if (not self.irc.isInternalClient(numeric)) and \
(not self.irc.isInternalServer(numeric)):
raise LookupError('No such PyLink client/server exists.')
utils.applyModes(self.irc, target, modes) utils.applyModes(self.irc, target, modes)
joinedmodes = utils.joinModes(modes) joinedmodes = utils.joinModes(modes)
if utils.isChannel(target): if utils.isChannel(target):
@ -208,24 +216,6 @@ class UnrealProtocol(TS6BaseProtocol):
raise ProtocolError('Cannot force mode change on external clients!') raise ProtocolError('Cannot force mode change on external clients!')
self._send(target, 'UMODE2 %s' % joinedmodes) self._send(target, 'UMODE2 %s' % joinedmodes)
def modeClient(self, numeric, target, modes, ts=None):
"""
Sends mode changes from a PyLink client. The mode list should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
if not self.irc.isInternalClient(numeric):
raise LookupError('No such PyLink client exists.')
self._sendModes(numeric, target, modes, ts=ts)
def modeServer(self, numeric, target, modes, ts=None):
"""
Sends mode changes from a PyLink server. The mode list should be
a list of (mode, arg) tuples, i.e. the format of utils.parseModes() output.
"""
if not self.irc.isInternalServer(numeric):
raise LookupError('No such PyLink server exists.')
self._sendModes(numeric, target, modes, ts=ts)
def topicServer(self, numeric, target, text): def topicServer(self, numeric, target, text):
"""Sends a TOPIC change from a PyLink server.""" """Sends a TOPIC change from a PyLink server."""
if not self.irc.isInternalServer(numeric): if not self.irc.isInternalServer(numeric):

View File

@ -43,12 +43,12 @@ class CommonProtoTestCase(PluginTestCase):
def testModeClient(self): def testModeClient(self):
testuser = self.proto.spawnClient('testcakes') testuser = self.proto.spawnClient('testcakes')
self.irc.takeMsgs() self.irc.takeMsgs()
self.proto.modeClient(self.u, testuser.uid, [('+i', None), ('+w', None)]) self.proto.mode(self.u, testuser.uid, [('+i', None), ('+w', None)])
self.assertEqual({('i', None), ('w', None)}, testuser.modes) self.assertEqual({('i', None), ('w', None)}, testuser.modes)
# Default channels start with +nt # Default channels start with +nt
self.assertEqual({('n', None), ('t', None)}, self.irc.channels['#pylink'].modes) self.assertEqual({('n', None), ('t', None)}, self.irc.channels['#pylink'].modes)
self.proto.modeClient(self.u, '#pylink', [('+s', None), ('+l', '30')]) self.proto.mode(self.u, '#pylink', [('+s', None), ('+l', '30')])
self.assertEqual({('s', None), ('l', '30'), ('n', None), ('t', None)}, self.irc.channels['#pylink'].modes) self.assertEqual({('s', None), ('l', '30'), ('n', None), ('t', None)}, self.irc.channels['#pylink'].modes)
cmds = self.irc.takeCommands(self.irc.takeMsgs()) cmds = self.irc.takeCommands(self.irc.takeMsgs())