Improve chat/GUI handling of < and > characters

This commit is contained in:
Et0h 2016-12-27 13:50:10 +00:00
parent 665956b84c
commit ddae114694
3 changed files with 12 additions and 10 deletions

View File

@ -153,7 +153,8 @@ STYLE_READY_PUSHBUTTON = "QPushButton { text-align: left; padding: 10px 5px 10px
STYLE_AUTO_PLAY_PUSHBUTTON = "QPushButton { text-align: left; padding: 5px 5px 5px 5px; }" STYLE_AUTO_PLAY_PUSHBUTTON = "QPushButton { text-align: left; padding: 5px 5px 5px 5px; }"
STYLE_NOTIFICATIONBOX = "Username { color: #367AA9; font-weight:bold; }" STYLE_NOTIFICATIONBOX = "Username { color: #367AA9; font-weight:bold; }"
STYLE_CONTACT_INFO = u"<span style=\"color: grey\"><strong><small>{}</span><br /><br />" # Contact info message STYLE_CONTACT_INFO = u"<span style=\"color: grey\"><strong><small>{}</span><br /><br />" # Contact info message
STYLE_USERNAME = "color: #367AA9; font-weight:bold;" STYLE_USER_MESSAGE = u"<span style=\"{}\">&lt;{}&gt;</span> {}"
STYLE_USERNAME = u"color: #367AA9; font-weight:bold;"
STYLE_ERRORNOTIFICATION = "color: red;" STYLE_ERRORNOTIFICATION = "color: red;"
STYLE_DIFFERENTITEM_COLOR = 'red' STYLE_DIFFERENTITEM_COLOR = 'red'
STYLE_NOFILEITEM_COLOR = 'blue' STYLE_NOFILEITEM_COLOR = 'blue'
@ -180,6 +181,7 @@ UI_COMMAND_REGEX = r"^(?P<command>[^\ ]+)(?:\ (?P<parameter>.+))?"
UI_OFFSET_REGEX = r"^(?:o|offset)\ ?(?P<sign>[/+-])?(?P<time>\d{1,9}(?:[^\d\.](?:\d{1,9})){0,2}(?:\.(?:\d{1,3}))?)$" UI_OFFSET_REGEX = r"^(?:o|offset)\ ?(?P<sign>[/+-])?(?P<time>\d{1,9}(?:[^\d\.](?:\d{1,9})){0,2}(?:\.(?:\d{1,3}))?)$"
UI_SEEK_REGEX = r"^(?:s|seek)?\ ?(?P<sign>[+-])?(?P<time>\d{1,4}(?:[^\d\.](?:\d{1,6})){0,2}(?:\.(?:\d{1,3}))?)$" UI_SEEK_REGEX = r"^(?:s|seek)?\ ?(?P<sign>[+-])?(?P<time>\d{1,4}(?:[^\d\.](?:\d{1,6})){0,2}(?:\.(?:\d{1,3}))?)$"
PARSE_TIME_REGEX = r'(:?(?:(?P<hours>\d+?)[^\d\.])?(?:(?P<minutes>\d+?))?[^\d\.])?(?P<seconds>\d+?)(?:\.(?P<miliseconds>\d+?))?$' PARSE_TIME_REGEX = r'(:?(?:(?P<hours>\d+?)[^\d\.])?(?:(?P<minutes>\d+?))?[^\d\.])?(?P<seconds>\d+?)(?:\.(?P<miliseconds>\d+?))?$'
MESSAGE_WITH_USERNAME_REGEX = "^(<(?P<username>[^<>]+)>)(?P<message>.*)"
SERVER_MAX_TEMPLATE_LENGTH = 10000 SERVER_MAX_TEMPLATE_LENGTH = 10000
PRIVACY_SENDRAW_MODE = "SendRaw" PRIVACY_SENDRAW_MODE = "SendRaw"
PRIVACY_SENDHASHED_MODE = "SendHashed" PRIVACY_SENDHASHED_MODE = "SendHashed"

View File

@ -249,10 +249,9 @@ class SyncClientProtocol(JSONCommandProtocol):
} }
}) })
def handleChat(self,message): def handleChat(self,message):
userMessage = message['message'].replace(u"<",u"<<").replace(u">",u">>") userMessage = message['message']
messageString = u"<{}> {}".format(message['username'], userMessage) messageString = u"<{}> {}".format(message['username'], userMessage)
self._client.ui.showMessage(messageString) self._client.ui.showMessage(messageString)
#TODO
def setReady(self, isReady, manuallyInitiated=True): def setReady(self, isReady, manuallyInitiated=True):
self.sendSet({ self.sendSet({

View File

@ -337,13 +337,14 @@ class MainWindow(QtGui.QMainWindow):
def showMessage(self, message, noTimestamp=False): def showMessage(self, message, noTimestamp=False):
message = unicode(message) message = unicode(message)
message = message.replace(u"&", u"&amp;").replace(u'"', u"&quot;").replace(u"<", u"&lt;").replace(">", u"&gt;") username = None
message = message.replace(u"&lt;&lt;", u"<LT>") messageWithUsername = re.match(constants.MESSAGE_WITH_USERNAME_REGEX, message, re.UNICODE)
message = message.replace(u"&gt;&gt;", u"<GT>") if messageWithUsername:
message = message.replace(u"&lt;", u"<span style=\"{}\">&lt;".format(constants.STYLE_USERNAME)) username = messageWithUsername.group("username")
message = message.replace(u"&gt;", u"&gt;</span>") message = messageWithUsername.group("message")
message = message.replace(u"<LT>;", u"&lt;") message = message.replace(u"&", u"&amp;").replace(u'"', u"&quot;").replace(u"<", u"&lt;").replace(u">", u"&gt;")
message = message.replace(u"<GT>", u"&gt;") if username:
message = constants.STYLE_USER_MESSAGE.format(constants.STYLE_USERNAME, username, message)
message = message.replace(u"\n", u"<br />") message = message.replace(u"\n", u"<br />")
if noTimestamp: if noTimestamp:
self.newMessage(u"{}<br />".format(message)) self.newMessage(u"{}<br />".format(message))