Made it so handle and ident are passed to _max_msg_len

This commit is contained in:
Dpeta 2021-04-04 06:03:50 +02:00
parent 22e6f6bb99
commit 4ae5f7d25d
8 changed files with 41 additions and 93 deletions

View file

@ -1,11 +1,23 @@
# Changelog
(This document uses YYYY-MM-DD as per ISO 8601)
## [v2.1.0] - 2021-4-4
### Changed
- Made it so handle and ident are passed to ``_max_msg_len``, so, hopefully the text cutoff will be *slightly* less restrictive.
### Deprecated
- Removed splitMessage function.
## [v2.0.2] - 2021-4-2
### Fixed
- "Fixed" crash when closing certain windows on certain platforms.
## [v2.0.1] - 2021-4-1
- Fixed encoding-related crash
### Fixed
- Added a fallback for non-unicode characters, and for when decoding fails completely, so hopefully they won't cause a crash anymore.
## [v2.0] - 2021-3-25

View file

@ -8,4 +8,6 @@
- Exclude unnecessary imports for builds.
- Icon is crusty...?
- Mood doesn't show up next to handle
- "CHUMHANDLE" is cut off on pchum2.5 theme.
- "CHUMHANDLE" is cut off on pchum2.5 theme.
- Mask & target not being passed to ``_max_msg_len``.
- No error message when Pesterchum fails to join a channel. (For example, when the channel name length is over CHANNELLEN)

View file

@ -10,7 +10,7 @@ from PyQt5 import QtCore, QtGui, QtWidgets
from mood import Mood
from dataobjs import PesterProfile, PesterHistory
from generic import PesterIcon
from parsetools import convertTags, lexMessage, splitMessage, mecmd, colorBegin, colorEnd, \
from parsetools import convertTags, lexMessage, mecmd, colorBegin, colorEnd, \
img2smiley, smiledict
import parsetools

4
irc.py
View file

@ -317,6 +317,10 @@ class PesterIRC(QtCore.QThread):
def quit_dc(self):
helpers.quit(self.cli, _pcVersion + " <3")
#def getMask(self):
# This needs to be updated when our hostname is changed.
# Nevermind this entire thing, actually.
moodUpdated = QtCore.pyqtSignal('QString', Mood)
colorUpdated = QtCore.pyqtSignal('QString', QtGui.QColor)
messageReceived = QtCore.pyqtSignal('QString', 'QString')

View file

@ -226,7 +226,7 @@ class PesterQuirkList(QtWidgets.QTreeWidget):
from copy import copy
from convo import PesterInput, PesterText
from parsetools import convertTags, lexMessage, splitMessage, mecmd, colorBegin, colorEnd, img2smiley, smiledict
from parsetools import convertTags, lexMessage, mecmd, colorBegin, colorEnd, img2smiley, smiledict
import parsetools
from dataobjs import pesterQuirks, PesterHistory
class QuirkTesterWindow(QtWidgets.QDialog):

View file

@ -300,7 +300,7 @@ def convertTags(lexed, format="html"):
return escaped
def _max_msg_len(mask=None, target=None):
def _max_msg_len(mask=None, target=None, nick=None, ident=None):
# karxi: Copied from another file of mine, and modified to work with
# Pesterchum.
# Note that this effectively assumes the worst when not provided the
@ -321,14 +321,14 @@ def _max_msg_len(mask=None, target=None):
# Since we should always be able to fetch this
# karxi: ... Which we can't, right now, unlike in the old script.
# TODO: Resolve this issue, give it the necessary information.
nick = None
# If we CAN'T, stick with a length of 30, since that seems to be
# the average maximum nowadays
limit -= len(nick) if nick is not None else 30
# '!', '@'
limit -= 2
# Maximum ident length
limit -= 10
# ident length
limit -= len(ident) if nick is not None else 10
# Maximum (?) host length
limit -= 63 # RFC 2812
# The target is the place this is getting sent to - a channel or a nick
@ -342,7 +342,7 @@ def _max_msg_len(mask=None, target=None):
return limit
def kxsplitMsg(lexed, fmt="pchum", maxlen=None, debug=False):
def kxsplitMsg(lexed, ctx, fmt="pchum", maxlen=None, debug=False):
"""Split messages so that they don't go over the length limit.
Returns a list of the messages, neatly split.
@ -375,11 +375,11 @@ def kxsplitMsg(lexed, fmt="pchum", maxlen=None, debug=False):
curlen = 0
# Maximum number of characters *to* use.
if not maxlen:
maxlen = _max_msg_len()
maxlen = __max_msg_len(None, None, ctx.mainwindow.profile().handle, ctx.mainwindow.irc.cli.real_name)
elif maxlen < 0:
# Subtract the (negative) length, giving us less leeway in this
# function.
maxlen = _max_msg_len() + maxlen
maxlen = _max_msg_len(None, None, ctx.mainwindow.profile().handle, ctx.mainwindow.irc.cli.real_name) + maxlen
# Defined here, but modified in the loop.
msglen = 0
@ -595,84 +595,6 @@ def kxsplitMsg(lexed, fmt="pchum", maxlen=None, debug=False):
# We're...done?
return output
def splitMessage(msg, format="ctag"):
"""Splits message if it is too long.
This is the older version of this function, kept for compatibility.
It will eventually be phased out."""
# split long text lines
buf = []
for o in msg:
if type(o) in [str, str] and len(o) > 200:
# Split with a step of 200. I.e., cut long segments into chunks of
# 200 characters.
# I'm...not sure why this is done. I'll probably factor it out
# later on.
for i in range(0, len(o), 200):
buf.append(o[i:i+200])
else:
# Add non-text tags or 'short' segments without processing.
buf.append(o)
# Copy the iterative variable.
msg = list(buf)
# This is the working segment.
working = []
# Keep a stack of open color tags.
cbegintags = []
# This is the final result.
output = []
print(repr(msg))
for o in msg:
oldctag = None
# Add to the working segment.
working.append(o)
if type(o) is colorBegin:
# Track the open tag.
cbegintags.append(o)
elif type(o) is colorEnd:
try:
# Remove the last open tag, since we've closed it.
oldctag = cbegintags.pop()
except IndexError:
pass
# THIS part is the part I don't get. I'll revise it later....
# It doesn't seem to catch ending ctags properly...or beginning ones.
# It's pretty much just broken, likely due to the line below.
# Maybe I can convert the tags, save the beginning tags, check their
# lengths and apply them after a split - or even iterate over each set,
# applying old tags before continuing...I don't know.
# yeah normally i'd do binary search but im lazy
# Get length of beginning tags, and the end tags that'd be applied.
msglen = len(convertTags(working, format)) + 4*(len(cbegintags))
# Previously this used 400.
if msglen > _max_msg_len():
working.pop()
if type(o) is colorBegin:
cbegintags.pop()
elif type(o) is colorEnd and oldctag is not None:
cbegintags.append(oldctag)
if len(working) == 0:
output.append([o])
else:
tmp = []
for color in cbegintags:
working.append(colorEnd("</c>"))
tmp.append(color)
output.append(working)
if type(o) is colorBegin:
cbegintags.append(o)
elif type(o) is colorEnd:
try:
cbegintags.pop()
except IndexError:
pass
tmp.append(o)
working = tmp
if len(working) > 0:
# Add any stragglers.
output.append(working)
return output
def _is_ooc(msg, strict=True):
"""Check if a line is OOC. Note that Pesterchum *is* kind enough to strip
trailing spaces for us, even in the older versions, but we don't do that in
@ -828,7 +750,9 @@ def kxhandleInput(ctx, text=None, flavor=None):
# We'll use those later.
# Split the messages so we don't go over the buffer and lose text.
maxlen = _max_msg_len()
maxlen = _max_msg_len(None, None, ctx.mainwindow.profile().handle, ctx.mainwindow.irc.cli.real_name)
# ctx.mainwindow.profile().handle ==> Get handle
# ctx.mainwindow.irc.cli.real_name ==> Get ident (Same as realname in this case.)
# Since we have to do some post-processing, we need to adjust the maximum
# length we can use.
if flavor == "convo":
@ -841,7 +765,7 @@ def kxhandleInput(ctx, text=None, flavor=None):
# Split the message. (Finally.)
# This is also set up to parse it into strings.
lexmsgs = kxsplitMsg(msg, "pchum", maxlen=maxlen)
lexmsgs = kxsplitMsg(msg, ctx, "pchum", maxlen=maxlen)
# Strip off the excess.
for i, m in enumerate(lexmsgs):
lexmsgs[i] = m.strip()

View file

@ -2946,6 +2946,11 @@ class PesterWindow(MovingWindow):
# Just in case.
sys.exit() # Actually, just gonna use this, 'cuz sockets are dumb :'3
def passIRC(self, irc):
# If this is a bad solution *PLEASE TELL ME A BETTER ONE*
# I just want to be able to access variables like our ident in this scope- hsjshdj
self.irc = irc
pcUpdate = QtCore.pyqtSignal('QString', 'QString')
closeToTraySignal = QtCore.pyqtSignal()
newConvoStarted = QtCore.pyqtSignal('QString', bool, name="newConvoStarted")
@ -3114,6 +3119,7 @@ class MainProgram(QtCore.QObject):
self.irc = PesterIRC(self.widget.config, self.widget, self.server)
self.connectWidgets(self.irc, self.widget)
self.widget.passIRC(self.irc) # Maybe this is absolutely terrible in practice, but screw it.
self.widget.gainAttention[QtWidgets.QWidget].connect(self.alertWindow)
#@QtCore.pyqtSlot()

View file

@ -1,2 +1,2 @@
_pcVersion = "Alt. v2.0.2"
buildVersion = "v2.0.2"
_pcVersion = "Alt. v2.1.0"
buildVersion = "v2.1.0"