3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 13:09:23 +01:00

protocols: move S2S_BUFSIZE definition into a class variable

This commit is contained in:
James Lu 2017-07-07 20:13:52 -07:00
parent 1172ca7387
commit b7466327db
5 changed files with 19 additions and 23 deletions

View File

@ -1103,6 +1103,8 @@ class PyLinkNetworkCoreWithUtils(PyLinkNetworkCore):
_apply()
class IRCNetwork(PyLinkNetworkCoreWithUtils):
S2S_BUFSIZE = 510
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -1347,7 +1349,7 @@ class IRCNetwork(PyLinkNetworkCoreWithUtils):
# Safeguard against newlines in input!! Otherwise, each line gets
# treated as a separate command, which is particularly nasty.
data = data.replace('\n', ' ')
encoded_data = data.encode(self.encoding, 'replace')[:510] + b"\r\n"
encoded_data = data.encode(self.encoding, 'replace')[:self.S2S_BUFSIZE] + b"\r\n"
log.debug("(%s) -> %s", self.name, data)

View File

@ -15,8 +15,6 @@ from pylinkirc.classes import *
from pylinkirc.log import log
from pylinkirc.protocols.ircs2s_common import *
S2S_BUFSIZE = 510
class NgIRCdProtocol(IRCS2SProtocol):
def __init__(self, irc):
super().__init__(irc)
@ -189,7 +187,7 @@ class NgIRCdProtocol(IRCS2SProtocol):
if utils.isChannel(target):
msgprefix = ':%s MODE %s ' % (self._expandPUID(source), target)
bufsize = S2S_BUFSIZE - len(msgprefix)
bufsize = self.S2S_BUFSIZE - len(msgprefix)
# Expand PUIDs when sending outgoing prefix modes.
for idx, mode in enumerate(modes):
@ -239,7 +237,7 @@ class NgIRCdProtocol(IRCS2SProtocol):
self._expandPUID(userpair[1])) for userpair in users]
# Use 13 args max per line: this is equal to the max of 15 minus the command name and target channel.
for message in utils.wrapArguments(njoin_prefix, nicks_to_send, S2S_BUFSIZE, separator=',', max_args_per_line=13):
for message in utils.wrapArguments(njoin_prefix, nicks_to_send, self.S2S_BUFSIZE, separator=',', max_args_per_line=13):
self.send(message)
# Add the affected users to our state.

View File

@ -12,8 +12,6 @@ from pylinkirc.classes import *
from pylinkirc.log import log
from pylinkirc.protocols.ircs2s_common import *
S2S_BUFSIZE = 510
class P10UIDGenerator(utils.IncrementalUIDGenerator):
"""Implements an incremental P10 UID Generator."""
@ -416,7 +414,7 @@ class P10Protocol(IRCS2SProtocol):
# Wrap modes: start with max bufsize and subtract the lengths of the source, target,
# mode command, and whitespace.
bufsize = S2S_BUFSIZE - len(numeric) - 4 - len(target) - len(str(ts))
bufsize = self.S2S_BUFSIZE - len(numeric) - 4 - len(target) - len(str(ts))
real_target = target
else:
@ -577,7 +575,7 @@ class P10Protocol(IRCS2SProtocol):
# Wrap all users and send them to prevent cutoff. Subtract 4 off the maximum
# buf size to account for user prefix data that may be re-added (e.g. ":ohv")
for linenum, wrapped_msg in \
enumerate(utils.wrapArguments(msgprefix, namelist, S2S_BUFSIZE-1-len(self.prefixmodes),
enumerate(utils.wrapArguments(msgprefix, namelist, self.S2S_BUFSIZE-1-len(self.prefixmodes),
separator=',')):
if linenum: # Implies "if linenum > 0"
# XXX: Ugh, this postprocessing sucks, but we have to make sure that mode prefixes are accounted
@ -612,12 +610,12 @@ class P10Protocol(IRCS2SProtocol):
if bans or exempts:
msgprefix += ':%' # Ban string starts with a % if there is anything
if bans:
for wrapped_msg in utils.wrapArguments(msgprefix, bans, S2S_BUFSIZE):
for wrapped_msg in utils.wrapArguments(msgprefix, bans, self.S2S_BUFSIZE):
self.send(wrapped_msg)
if exempts:
# Now add exempts, which are separated from the ban list by a single argument "~".
msgprefix += ' ~ '
for wrapped_msg in utils.wrapArguments(msgprefix, exempts, S2S_BUFSIZE):
for wrapped_msg in utils.wrapArguments(msgprefix, exempts, self.S2S_BUFSIZE):
self.send(wrapped_msg)
self.updateTS(server, channel, ts, changedmodes)

View File

@ -10,8 +10,6 @@ from pylinkirc.classes import *
from pylinkirc.log import log
from pylinkirc.protocols.ts6_common import *
S2S_BUFSIZE = 510
class TS6Protocol(TS6BaseProtocol):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -162,7 +160,7 @@ class TS6Protocol(TS6BaseProtocol):
msgprefix = ':{sid} BMASK {ts} {channel} {bmode} :'.format(sid=server, ts=ts,
channel=channel, bmode=bmode)
# Actually, we cut off at 17 arguments/line, since the prefix and command name don't count.
for msg in utils.wrapArguments(msgprefix, bans, S2S_BUFSIZE, max_args_per_line=17):
for msg in utils.wrapArguments(msgprefix, bans, self.S2S_BUFSIZE, max_args_per_line=17):
self.send(msg)
self.updateTS(server, channel, ts, changedmodes)
@ -187,7 +185,7 @@ class TS6Protocol(TS6BaseProtocol):
# On output, at most ten cmode parameters should be sent; if there are more,
# multiple TMODE messages should be sent.
msgprefix = ':%s TMODE %s %s ' % (numeric, ts, target)
bufsize = S2S_BUFSIZE - len(msgprefix)
bufsize = self.S2S_BUFSIZE - len(msgprefix)
for modestr in self.wrap_modes(modes, bufsize, max_modes_per_msg=10):
self.send(msgprefix + modestr)

View File

@ -14,13 +14,13 @@ from pylinkirc.protocols.ts6_common import *
SJOIN_PREFIXES = {'q': '*', 'a': '~', 'o': '@', 'h': '%', 'v': '+', 'b': '&', 'e': '"', 'I': "'"}
# I'm not sure what the real limit is, but the text posted at
# https://github.com/GLolol/PyLink/issues/378 suggests 427 characters.
# https://github.com/unrealircd/unrealircd/blob/4cad9cb/src/modules/m_server.c#L1260 may
# also help. (but why BUFSIZE-*80*?) -GL
S2S_BUFSIZE = 427
class UnrealProtocol(TS6BaseProtocol):
# I'm not sure what the real limit is, but the text posted at
# https://github.com/GLolol/PyLink/issues/378 suggests 427 characters.
# https://github.com/unrealircd/unrealircd/blob/4cad9cb/src/modules/m_server.c#L1260 may
# also help. (but why BUFSIZE-*80*?) -GL
S2S_BUFSIZE = 427
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.protocol_caps |= {'slash-in-nicks', 'underscore-in-hosts'}
@ -181,7 +181,7 @@ class UnrealProtocol(TS6BaseProtocol):
sjoin_prefix += " :"
# Wrap arguments to the max supported S2S line length to prevent cutoff
# (https://github.com/GLolol/PyLink/issues/378)
for line in utils.wrapArguments(sjoin_prefix, itemlist, S2S_BUFSIZE):
for line in utils.wrapArguments(sjoin_prefix, itemlist, self.S2S_BUFSIZE):
self.send(line)
self.channels[channel].users.update(uids)
@ -223,7 +223,7 @@ class UnrealProtocol(TS6BaseProtocol):
# 7 characters for "MODE", the space between MODE and the target, the space between the
# target and mode list, and the space between the mode list and TS.
bufsize = S2S_BUFSIZE - 7
bufsize = self.S2S_BUFSIZE - 7
# Subtract the length of the TS and channel arguments
bufsize -= len(str(ts))