Skip to content
Snippets Groups Projects
Commit adf1bac5 authored by nightfox's avatar nightfox
Browse files

Version 1.17 beta 34: Updated the way it deals with rogue ANSI codes that seem...

Version 1.17 beta 34: Updated the way it deals with rogue ANSI codes that seem to be added to certain messages (including cursor movement characters) which probably weren't intended to be there, which can mess up the display of messages when using the scrolling interface.
parent e7e1bef0
Branches
Tags
No related merge requests found
...@@ -301,8 +301,8 @@ if (system.version_num < 31500) ...@@ -301,8 +301,8 @@ if (system.version_num < 31500)
} }
   
// Reader version information // Reader version information
var READER_VERSION = "1.17 Beta 33"; var READER_VERSION = "1.17 Beta 34";
var READER_DATE = "2017-02-20"; var READER_DATE = "2017-03-12";
   
// Keyboard key codes for displaying on the screen // Keyboard key codes for displaying on the screen
var UP_ARROW = ascii(24); var UP_ARROW = ascii(24);
...@@ -484,8 +484,15 @@ gNetMsgAttrStrs[MSG_TYPELOCAL] = "ForLocal"; ...@@ -484,8 +484,15 @@ gNetMsgAttrStrs[MSG_TYPELOCAL] = "ForLocal";
gNetMsgAttrStrs[MSG_TYPEECHO] = "ForEcho"; gNetMsgAttrStrs[MSG_TYPEECHO] = "ForEcho";
gNetMsgAttrStrs[MSG_TYPENET] = "ForNetmail"; gNetMsgAttrStrs[MSG_TYPENET] = "ForNetmail";
   
// A regular expression to check whether a string is an email address
var gEmailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; var gEmailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// An array of regular expressions for checking for ANSI codes (globally in a string & ignore case)
var gANSIRegexes = [ new RegExp(ascii(27) + "\[[0-9]+[mM]", "gi"),
new RegExp(ascii(27) + "\[[0-9]+(;[0-9]+)+[mM]", "gi"),
new RegExp(ascii(27) + "\[[0-9]+[aAbBcCdD]", "gi"),
new RegExp(ascii(27) + "\[[0-9]+;[0-9]+[hHfF]", "gi"),
new RegExp(ascii(27) + "\[[sSuUkK]", "gi"),
new RegExp(ascii(27) + "\[2[jJ]", "gi") ];
   
// Determine the script's startup directory. // Determine the script's startup directory.
// This code is a trick that was created by Deuce, suggested by Rob Swindell // This code is a trick that was created by Deuce, suggested by Rob Swindell
...@@ -12425,19 +12432,30 @@ function DigDistMsgReader_GetMsgInfoForEnhancedReader(pMsgHdr, pWordWrap, pDeter ...@@ -12425,19 +12432,30 @@ function DigDistMsgReader_GetMsgInfoForEnhancedReader(pMsgHdr, pWordWrap, pDeter
retObj.displayFrame = null; retObj.displayFrame = null;
retObj.displayFrameScrollbar = null; retObj.displayFrameScrollbar = null;
var msgHasANSICodes = (typeof(pMsgHasANSICodes) == "boolean" ? pMsgHasANSICodes : textHasANSICodes(retObj.msgText)); var msgHasANSICodes = (typeof(pMsgHasANSICodes) == "boolean" ? pMsgHasANSICodes : textHasANSICodes(retObj.msgText));
// If the message has ANSI codes, then check to see if the ANSI codees are // If the message has ANSI codes, then check to see if the amount of ANSI is very low.
// only on the first 2 lines (within 160 characters). If so, then convert // If so, then convert the ANSI color codes to Synchronet codes and eliminate unwanted
// the ANSI color codes to Synchronet codes. Some messages contain a couple // ANSI (such as cursor movement codes). It seems that some messages have ANSI inserted
// extra lines at the beginning with a "By: <name> to <name>" and a date, and // into them that shouldn't be there (i.e., the author didn't intend to post an ANSI
// some of those messages contain ANSI codes in those lines, which will mess // message), which is the reason for this check. If the message contains such rogue
// up the display of the message using the scrolling interface. // ANSI codes, they could mess up the display of the message.
if (msgHasANSICodes) if (msgHasANSICodes)
{ {
var lastANSIIdx = idxOfLastANSICode(msgTextAltered); // Count the number of ANSI codes in the message and find the percentage of
if (lastANSIIdx <= 160) // ANSI in the message.
var ANSICodeCount = countANSICodes(msgTextAltered, gANSIRegexes);
var percentANSICodes = (ANSICodeCount / msgTextAltered.length) * 100;
// Older: It seems that some messages contain a couple
// extra lines at the beginning with a "By: <name> to <name>" and a date, and
// some of those messages contain ANSI codes in those lines, which will mess
// up the display of the message using the scrolling interface.
//var lastANSIIdx = idxOfLastANSICode(msgTextAltered, gANSIRegexes);
//if (lastANSIIdx <= 160)
// If less than 6% of the message is ANSI codes, then consider those ANSI
// codes unwanted and convert them to Synchronet and remove unwanted ANSI.
if (percentANSICodes < 6)
{ {
msgTextAltered = cvtANSIToSyncAndRemoveUnwantedANSI(msgTextAltered); msgTextAltered = cvtANSIToSyncAndRemoveUnwantedANSI(msgTextAltered);
//msgTextAltered = removeANSIFromStr(msgTextAltered); //msgTextAltered = removeANSIFromStr(msgTextAltered, gANSIRegexes);
msgHasANSICodes = false; msgHasANSICodes = false;
} }
} }
...@@ -16879,50 +16897,56 @@ function textHasANSICodes(pText) ...@@ -16879,50 +16897,56 @@ function textHasANSICodes(pText)
// Returns the index of the last ANSI code in a string. // Returns the index of the last ANSI code in a string.
// //
// Parameters: // Parameters:
// pStr: The string to search for // pStr: The string to search in
// pANSIRegexes: An array of regular expressions to use for searching for ANSI codes
// //
// Return value: The index of the last ANSI code in the string, or -1 if not found // Return value: The index of the last ANSI code in the string, or -1 if not found
function idxOfLastANSICode(pStr) function idxOfLastANSICode(pStr, pANSIRegexes)
{ {
var ANSIRegexes = [ new RegExp(ascii(27) + "\[[0-9]+[mM]"),
new RegExp(ascii(27) + "\[[0-9]+(;[0-9]+)+[mM]"),
new RegExp(ascii(27) + "\[[0-9]+[aAbBcCdD]"),
new RegExp(ascii(27) + "\[[0-9]+;[0-9]+[hHfF]"),
new RegExp(ascii(27) + "\[[sSuUkK]"),
new RegExp(ascii(27) + "\[2[jJ]") ];
var lastANSIIdx = -1; var lastANSIIdx = -1;
for (var i = 0; i < ANSIRegexes.length; ++i) for (var i = 0; i < pANSIRegexes.length; ++i)
{ {
ANSIRegexes[i].ignoreCase = true; var lastANSIIdxTmp = regexLastIndexOf(pStr, pANSIRegexes[i]);
var lastANSIIdxTmp = regexLastIndexOf(pStr, ANSIRegexes[i]);
if (lastANSIIdxTmp > lastANSIIdx) if (lastANSIIdxTmp > lastANSIIdx)
lastANSIIdx = lastANSIIdxTmp; lastANSIIdx = lastANSIIdxTmp;
} }
return lastANSIIdx; return lastANSIIdx;
} }
   
// Returns the number of times an ANSI code is matched in a string.
//
// Parameters:
// pStr: The string to search in
// pANSIRegexes: An array of regular expressions to use for searching for ANSI codes
//
// Return value: The number of ANSI code matches in the string
function countANSICodes(pStr, pANSIRegexes)
{
var ANSICount = 0;
for (var i = 0; i < pANSIRegexes.length; ++i)
{
var matches = pStr.match(pANSIRegexes[i]);
if (matches != null)
ANSICount += matches.length;
}
return ANSICount;
}
// Removes ANSI codes from a string. // Removes ANSI codes from a string.
// //
// Parameters: // Parameters:
// pStr: The string to remove ANSI codes from // pStr: The string to remove ANSI codes from
// pANSIRegexes: An array of regular expressions to use for searching for ANSI codes
// //
// Return value: A version of the string without ANSI codes // Return value: A version of the string without ANSI codes
function removeANSIFromStr(pStr) function removeANSIFromStr(pStr, pANSIRegexes)
{ {
if (typeof(pStr) != "string") if (typeof(pStr) != "string")
return ""; return "";
   
var ANSIRegexes = [ new RegExp(ascii(27) + "\[[0-9]+[mM]", "gi"),
new RegExp(ascii(27) + "\[[0-9]+(;[0-9]+)+[mM]", "gi"),
new RegExp(ascii(27) + "\[[0-9]+[aAbBcCdD]", "gi"),
new RegExp(ascii(27) + "\[[0-9]+;[0-9]+[hHfF]", "gi"),
new RegExp(ascii(27) + "\[[sSuUkK]", "gi"),
new RegExp(ascii(27) + "\[2[jJ]", "gi") ];
var theStr = pStr; var theStr = pStr;
for (var i = 0; i < ANSIRegexes.length; ++i) for (var i = 0; i < pANSIRegexes.length; ++i)
theStr = theStr.replace(ANSIRegexes[i], ""); theStr = theStr.replace(pANSIRegexes[i], "");
return theStr; return theStr;
} }
   
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment