Improve message sending to properly split on the end of lines
This commit is contained in:
parent
302b69c263
commit
9a71b34488
@ -105,7 +105,7 @@ class IRCContext:
|
|||||||
return self._who(self.client, self.name, data)
|
return self._who(self.client, self.name, data)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _send(data, client, send_type, name):
|
def _send(data, first, sep, client, send_type, name):
|
||||||
full_address = "{cli.nickname}!{cli.ident}@{cli.hostmask}".format(cli=client)
|
full_address = "{cli.nickname}!{cli.ident}@{cli.hostmask}".format(cli=client)
|
||||||
|
|
||||||
# Maximum length of sent data is 512 bytes. However, we have to
|
# Maximum length of sent data is 512 bytes. However, we have to
|
||||||
@ -123,21 +123,45 @@ class IRCContext:
|
|||||||
length -= len(name)
|
length -= len(name)
|
||||||
# Finally, we need to account for the send type's length
|
# Finally, we need to account for the send type's length
|
||||||
length -= len(send_type)
|
length -= len(send_type)
|
||||||
|
# The 'first' argument is sent along with every message, so deduce that too
|
||||||
|
if length - len(first) > 0: # make sure it's not negative (or worse, 0)
|
||||||
|
length -= len(first)
|
||||||
|
else:
|
||||||
|
first = ""
|
||||||
|
|
||||||
for line in data.splitlines():
|
messages = []
|
||||||
|
count = 0
|
||||||
|
for line in data:
|
||||||
|
if count and count + len(sep) + len(line) > length:
|
||||||
|
count = len(line)
|
||||||
|
cur_sep = "\n"
|
||||||
|
elif not messages:
|
||||||
|
count = len(line)
|
||||||
|
cur_sep = ""
|
||||||
|
else:
|
||||||
|
count += len(sep) + len(line)
|
||||||
|
cur_sep = sep
|
||||||
|
|
||||||
|
messages.append(cur_sep)
|
||||||
|
messages.append(line)
|
||||||
|
|
||||||
|
for line in "".join(messages).splitlines():
|
||||||
while line:
|
while line:
|
||||||
extra, line = line[:length], line[length:]
|
extra, line = line[:length], line[length:]
|
||||||
client.send("{0} {1} :{2}".format(send_type, name, extra))
|
client.send("{0} {1} :{2}{3}".format(send_type, name, first, extra))
|
||||||
|
|
||||||
def send(self, *data, notice=False, privmsg=False, prefix=None):
|
def send(self, *data, first=None, sep=None, notice=False, privmsg=False, prefix=None):
|
||||||
data = " ".join(data)
|
|
||||||
if self.is_fake:
|
if self.is_fake:
|
||||||
# Leave out 'fake' from the message; get_context_type() takes care of that
|
# Leave out 'fake' from the message; get_context_type() takes care of that
|
||||||
debuglog("Would message {0} {1}: {2!r}".format(self.get_context_type(), self.name, data))
|
debuglog("Would message {0} {1}: {2!r}".format(self.get_context_type(), self.name, " ".join(data)))
|
||||||
return
|
return
|
||||||
|
|
||||||
send_type = self.get_send_type(is_notice=notice, is_privmsg=privmsg)
|
send_type = self.get_send_type(is_notice=notice, is_privmsg=privmsg)
|
||||||
name = self.name
|
name = self.name
|
||||||
if prefix is not None:
|
if prefix is not None:
|
||||||
name = prefix + name
|
name = prefix + name
|
||||||
self._send(data, self.client, send_type, name)
|
if first is None:
|
||||||
|
first = ""
|
||||||
|
if sep is None:
|
||||||
|
sep = " "
|
||||||
|
self._send(data, first, sep, self.client, send_type, name)
|
||||||
|
12
src/users.py
12
src/users.py
@ -418,7 +418,7 @@ class User(IRCContext):
|
|||||||
max_targets = Features["TARGMAX"][send_type]
|
max_targets = Features["TARGMAX"][send_type]
|
||||||
while targets:
|
while targets:
|
||||||
using, targets = targets[:max_targets], targets[max_targets:]
|
using, targets = targets[:max_targets], targets[max_targets:]
|
||||||
cls._send(message, targets[0].client, send_type, ",".join([t.nick for t in using]))
|
cls._send([message], "", " ", targets[0].client, send_type, ",".join([t.nick for t in using]))
|
||||||
|
|
||||||
cls._messages.clear()
|
cls._messages.clear()
|
||||||
|
|
||||||
@ -535,13 +535,3 @@ class BotUser(User): # TODO: change all the 'if x is Bot' for 'if isinstance(x,
|
|||||||
if nick is None:
|
if nick is None:
|
||||||
nick = self.nick
|
nick = self.nick
|
||||||
self.client.send("NICK", nick)
|
self.client.send("NICK", nick)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user