Escape html entities while parsing the IRC message.

Now escapes html entities during the parsing of the message received
from IRC and added a unit test for this requirement.
Renamed format* methods to styleAs*, since this is more appropriate.
fix-message-formatting
Danny van Heumen 11 years ago
parent cc7072ab41
commit ed7b6454e4

@ -28,6 +28,9 @@ public class FormattedTextBuilder
/** /**
* Append a string of text. * Append a string of text.
* *
* Make sure that the text is safe for your purposes, as it is appended
* without further modifications.
*
* @param text string of text * @param text string of text
*/ */
public void append(final String text) public void append(final String text)
@ -38,6 +41,9 @@ public void append(final String text)
/** /**
* Append a character. * Append a character.
* *
* Make sure that the character is safe for your purposes, as it is appended
* without further modifications.
*
* @param c character * @param c character
*/ */
public void append(final char c) public void append(final char c)

@ -1259,7 +1259,8 @@ public void onError(final ErrorMessage msg)
public void onUserPrivMessage(final UserPrivMsg msg) public void onUserPrivMessage(final UserPrivMsg msg)
{ {
final String user = msg.getSource().getNick(); final String user = msg.getSource().getNick();
final String text = Utils.formatMessage(Utils.parseIrcMessage(msg.getText())); final String text =
Utils.styleAsMessage(Utils.parseIrcMessage(msg.getText()));
final MessageIrcImpl message = final MessageIrcImpl message =
new MessageIrcImpl(text, new MessageIrcImpl(text,
OperationSetBasicInstantMessaging.HTML_MIME_TYPE, OperationSetBasicInstantMessaging.HTML_MIME_TYPE,
@ -1302,7 +1303,7 @@ public void onUserNotice(final UserNotice msg)
// Jitsi set up. // Jitsi set up.
final String user = msg.getSource().getNick(); final String user = msg.getSource().getNick();
final String text = final String text =
Utils.formatNotice(Utils.parseIrcMessage(msg.getText()), user); Utils.styleAsNotice(Utils.parseIrcMessage(msg.getText()), user);
final MessageIrcImpl message = final MessageIrcImpl message =
new MessageIrcImpl(text, new MessageIrcImpl(text,
OperationSetBasicInstantMessaging.HTML_MIME_TYPE, OperationSetBasicInstantMessaging.HTML_MIME_TYPE,
@ -1326,7 +1327,7 @@ public void onUserAction(final UserActionMsg msg)
{ {
final String user = msg.getSource().getNick(); final String user = msg.getSource().getNick();
final String text = final String text =
Utils.formatAction(Utils.parseIrcMessage(msg.getText()), user); Utils.styleAsAction(Utils.parseIrcMessage(msg.getText()), user);
final MessageIrcImpl message = final MessageIrcImpl message =
new MessageIrcImpl(text, new MessageIrcImpl(text,
OperationSetBasicInstantMessaging.HTML_MIME_TYPE, OperationSetBasicInstantMessaging.HTML_MIME_TYPE,
@ -1707,7 +1708,8 @@ public void onChannelMessage(final ChannelPrivMsg msg)
return; return;
} }
String text = Utils.formatMessage(Utils.parseIrcMessage(msg.getText())); String text =
Utils.styleAsMessage(Utils.parseIrcMessage(msg.getText()));
MessageIrcImpl message = MessageIrcImpl message =
new MessageIrcImpl(text, "text/html", "UTF-8", null); new MessageIrcImpl(text, "text/html", "UTF-8", null);
ChatRoomMemberIrcImpl member = ChatRoomMemberIrcImpl member =
@ -1736,7 +1738,8 @@ public void onChannelAction(final ChannelActionMsg msg)
new ChatRoomMemberIrcImpl(IrcStack.this.provider, new ChatRoomMemberIrcImpl(IrcStack.this.provider,
this.chatroom, userNick, ChatRoomMemberRole.MEMBER); this.chatroom, userNick, ChatRoomMemberRole.MEMBER);
String text = String text =
Utils.formatAction(Utils.parseIrcMessage(msg.getText()), userNick); Utils.styleAsAction(Utils.parseIrcMessage(msg.getText()),
userNick);
MessageIrcImpl message = MessageIrcImpl message =
new MessageIrcImpl(text, "text/html", "UTF-8", null); new MessageIrcImpl(text, "text/html", "UTF-8", null);
this.chatroom.fireMessageReceivedEvent(message, member, new Date(), this.chatroom.fireMessageReceivedEvent(message, member, new Date(),
@ -1761,7 +1764,8 @@ public void onChannelNotice(final ChannelNotice msg)
new ChatRoomMemberIrcImpl(IrcStack.this.provider, new ChatRoomMemberIrcImpl(IrcStack.this.provider,
this.chatroom, userNick, ChatRoomMemberRole.MEMBER); this.chatroom, userNick, ChatRoomMemberRole.MEMBER);
final String text = final String text =
Utils.formatNotice(Utils.parseIrcMessage(msg.getText()), userNick); Utils.styleAsNotice(Utils.parseIrcMessage(msg.getText()),
userNick);
final MessageIrcImpl message = final MessageIrcImpl message =
new MessageIrcImpl(text, "text/html", "UTF-8", null); new MessageIrcImpl(text, "text/html", "UTF-8", null);
this.chatroom.fireMessageReceivedEvent(message, member, new Date(), this.chatroom.fireMessageReceivedEvent(message, member, new Date(),

@ -8,6 +8,8 @@
import net.java.sip.communicator.util.*; import net.java.sip.communicator.util.*;
import org.apache.commons.lang3.*;
/** /**
* Some IRC-related utility methods. * Some IRC-related utility methods.
* *
@ -114,8 +116,8 @@ public static String parseIrcMessage(final String text)
builder.cancelAll(); builder.cancelAll();
break; break;
default: default:
// value is a normal character, just append // value is a normal character, escape html entities and append
builder.append(val); builder.append(StringEscapeUtils.escapeHtml4("" + val));
break; break;
} }
} }
@ -192,9 +194,8 @@ private static Color parseForegroundColor(final String text)
* @param message original IRC message * @param message original IRC message
* @return returns HTML-formatted normal message * @return returns HTML-formatted normal message
*/ */
public static String formatMessage(final String message) public static String styleAsMessage(final String message)
{ {
// FIXME html entity encoding
return message; return message;
} }
@ -205,9 +206,8 @@ public static String formatMessage(final String message)
* @param user user nick name * @param user user nick name
* @return returns HTML-formatted notice * @return returns HTML-formatted notice
*/ */
public static String formatNotice(final String message, final String user) public static String styleAsNotice(final String message, final String user)
{ {
// FIXME html entity encoding
return "<i>" + user + "</i>: " + message; return "<i>" + user + "</i>: " + message;
} }
@ -218,9 +218,8 @@ public static String formatNotice(final String message, final String user)
* @param user user nick name * @param user user nick name
* @return returns HTML-formatted action * @return returns HTML-formatted action
*/ */
public static String formatAction(final String message, final String user) public static String styleAsAction(final String message, final String user)
{ {
// FIXME html entity encoding
return "<b>*" + user + "</b> " + message; return "<b>*" + user + "</b> " + message;
} }
} }

@ -21,4 +21,5 @@ Import-Package: org.osgi.framework,
com.ircclouds.irc.api.domain.messages, com.ircclouds.irc.api.domain.messages,
com.ircclouds.irc.api.domain.messages.interfaces, com.ircclouds.irc.api.domain.messages.interfaces,
com.ircclouds.irc.api.listeners, com.ircclouds.irc.api.listeners,
com.ircclouds.irc.api.state com.ircclouds.irc.api.state,
org.apache.commons.lang3

@ -14,16 +14,6 @@ public class UtilsTest
extends TestCase extends TestCase
{ {
protected void setUp() throws Exception
{
super.setUp();
}
protected void tearDown() throws Exception
{
super.tearDown();
}
public void testNullText() public void testNullText()
{ {
Assert.assertEquals(null, Utils.parseIrcMessage(null)); Assert.assertEquals(null, Utils.parseIrcMessage(null));
@ -183,26 +173,33 @@ public void testCancelColorFormat()
final String htmlMessage = "<font color=\"Navy\">With color</font> and without color."; final String htmlMessage = "<font color=\"Navy\">With color</font> and without color.";
Assert.assertEquals(htmlMessage, Utils.parseIrcMessage(ircMessage)); Assert.assertEquals(htmlMessage, Utils.parseIrcMessage(ircMessage));
} }
public void testMessageContainingHtmlEntities()
{
final String ircMessage = "\u0002This<b> is very</b> bad &&& text!!!<!-- \u0002<i>";
final String parsedMessage = "<b>This&lt;b&gt; is very&lt;/b&gt; bad &amp;&amp;&amp; text!!!&lt;!-- </b>&lt;i&gt;";
Assert.assertEquals(parsedMessage, Utils.parseIrcMessage(ircMessage));
}
public void testFormatMessage() public void testStyleAsMessage()
{ {
String message = "hello world"; String message = "hello world";
Assert.assertEquals(message, Utils.formatMessage(message)); Assert.assertEquals(message, Utils.styleAsMessage(message));
} }
public void testFormatNotice() public void testStyleAsNotice()
{ {
String message = "hello world"; String message = "hello world";
String nick = "MrNiceGuy"; String nick = "MrNiceGuy";
Assert.assertEquals("<i>MrNiceGuy</i>: hello world", Assert.assertEquals("<i>MrNiceGuy</i>: hello world",
Utils.formatNotice(message, nick)); Utils.styleAsNotice(message, nick));
} }
public void testFormatAction() public void testStyleAsAction()
{ {
String message = "is absolutely crazy!"; String message = "is absolutely crazy!";
String nick = "AbsoluteLunatic"; String nick = "AbsoluteLunatic";
Assert.assertEquals("<b>*AbsoluteLunatic</b> is absolutely crazy!", Assert.assertEquals("<b>*AbsoluteLunatic</b> is absolutely crazy!",
Utils.formatAction(message, nick)); Utils.styleAsAction(message, nick));
} }
} }

Loading…
Cancel
Save