Commit 2122acb7 authored by Julian Sparber's avatar Julian Sparber

MessageList: Strip and escape markup in messages

By stripping and escaping messages we can controll what markup is
rendered in a gtkLabel. We only allow html tags for links.

Fixes: Librem5/chatty#116
Fixes: Librem5/chatty#150
parent 4ea3fb7c
......@@ -1372,6 +1372,7 @@ chatty_blist_chats_update_node (PurpleBuddy *buddy,
const gchar *alias;
const gchar *protocol_id;
const char *color;
g_autofree gchar *last_message_striped = NULL;
gboolean notify;
gboolean blur;
......@@ -1431,9 +1432,10 @@ chatty_blist_chats_update_node (PurpleBuddy *buddy,
}
// FIXME: Don't hard code the color it should read it from the theme
last_message_striped = purple_markup_strip_html (chatty_node->conv.last_message);
last_msg_text = g_markup_printf_escaped ("<span color='#3584e4'>%s</span><span alpha='55%%'>%s</span>",
tag,
chatty_node->conv.last_message);
last_message_striped);
last_msg_ts = chatty_utils_time_ago_in_words (chatty_node->conv.last_msg_ts_raw,
CHATTY_UTILS_TIME_AGO_SHOW_DATE);
......@@ -1502,6 +1504,7 @@ chatty_blist_chats_update_group_chat (PurpleBlistNode *node)
g_autofree gchar *last_msg_text = NULL;
g_autofree gchar *last_msg_ts = NULL;
g_autofree gchar *unread_messages = NULL;
g_autofree gchar *last_message_striped = NULL;
const gchar *chat_name;
gboolean notify;
......@@ -1534,9 +1537,10 @@ chatty_blist_chats_update_group_chat (PurpleBlistNode *node)
chatty_node->conv.last_message = "";
}
last_message_striped = purple_markup_strip_html (chatty_node->conv.last_message);
// FIXME: Don't hard code the color it should read it from the theme
last_msg_text = g_markup_printf_escaped ("<span color='#3584e4'>Group Chat: </span>%s",
chatty_node->conv.last_message);
last_message_striped);
g_strdup (chatty_node->conv.last_msg_timestamp);
......
......@@ -623,24 +623,6 @@ chatty_update_typing_status (ChattyConversation *chatty_conv)
g_free (text);
}
static gchar *
chatty_conv_check_for_links (const gchar *message)
{
gchar *msg_html;
gchar *msg_xhtml = NULL;
msg_html = purple_markup_strip_html (message);
msg_html = purple_markup_linkify (msg_html);
// convert all tags to lowercase for GtkLabel markup parser
purple_markup_html_to_xhtml (msg_html, &msg_xhtml, NULL);
g_free (msg_html);
return msg_xhtml;
}
static PurpleCmdRet
cb_chatty_cmd (PurpleConversation *conv,
const gchar *cmd,
......@@ -983,7 +965,6 @@ chatty_conv_get_im_messages_cb (const unsigned char* msg,
const unsigned char* uuid,
gpointer data,
int last_message){
gchar *msg_html;
guint msg_dir;
ChattyConversation *chatty_conv;
g_autofree gchar *iso_timestamp;
......@@ -1004,25 +985,20 @@ chatty_conv_get_im_messages_cb (const unsigned char* msg,
msg_dir = MSG_IS_SYSTEM; // TODO: LELAND: Do we have this case for IMs?
}
msg_html = chatty_conv_check_for_links ((const gchar*)msg);
strftime (iso_timestamp,
MAX_GMT_ISO_SIZE * sizeof(char),
"%b %d",
localtime(&time_stamp));
if (msg_html[0] != '\0') {
if (msg[0] != '\0') {
chatty_msg_list_add_message_at (chatty_conv->msg_list,
msg_dir,
msg_html,
(const gchar *) msg,
last_message ? iso_timestamp : NULL,
NULL,
ADD_MESSAGE_ON_TOP);
}
g_free (msg_html);
g_object_set_data (G_OBJECT (chatty_conv->input.entry),
"attach-start-time",
NULL);
......@@ -1039,7 +1015,6 @@ chatty_conv_get_chat_messages_cb (const unsigned char* msg,
const unsigned char *who,
const unsigned char *uuid,
gpointer data){
gchar *msg_html;
PurpleBuddy *buddy;
const char *color;
GdkPixbuf *avatar = NULL;
......@@ -1051,17 +1026,13 @@ chatty_conv_get_chat_messages_cb (const unsigned char* msg,
chatty_conv = (ChattyConversation *)data;
msg_html = chatty_conv_check_for_links ((const gchar*)msg);
// TODO: @LELAND: Chechk this memory management, don't like it
free(chatty_conv->oldest_message_displayed);
chatty_conv->oldest_message_displayed = g_strdup((const gchar *)uuid);
if (msg_html[0] != '\0') {
if (msg[0] != '\0') {
if (direction == 1) {
msg_html = chatty_conv_check_for_links ((const gchar*)msg);
account = purple_conversation_get_account (chatty_conv->conv);
buddy = purple_find_buddy (account, (const gchar*)room);
color = chatty_conv_muc_get_avatar_color ((const gchar*)room);
......@@ -1088,17 +1059,15 @@ chatty_conv_get_chat_messages_cb (const unsigned char* msg,
chatty_msg_list_add_message_at (chatty_conv->msg_list,
MSG_IS_INCOMING,
msg_html,
(const gchar *) msg,
NULL,
icon ? icon : NULL,
ADD_MESSAGE_ON_TOP);
} else if (direction == -1) {
msg_html = chatty_conv_check_for_links ((const gchar*)msg);
chatty_msg_list_add_message_at (chatty_conv->msg_list,
MSG_IS_OUTGOING,
msg_html,
(const gchar *) msg,
NULL,
NULL,
ADD_MESSAGE_ON_TOP);
......@@ -1115,12 +1084,9 @@ chatty_conv_get_chat_messages_cb (const unsigned char* msg,
}
g_free (msg_html);
g_object_set_data (G_OBJECT (chatty_conv->input.entry),
"attach-start-time",
NULL);
}
......@@ -1898,7 +1864,6 @@ chatty_conv_update_muc_info (PurpleConversation *conv)
gtk_label_set_text (GTK_LABEL(chatty->muc.label_num_user), user_count_str);
topic = purple_conv_chat_get_topic (PURPLE_CONV_CHAT(conv));
topic = chatty_conv_check_for_links (topic);
flags = purple_conv_chat_user_get_flags (chat, chat->nick);
......@@ -2158,7 +2123,6 @@ chatty_conv_write_conversation (PurpleConversation *conv,
PurpleBuddy *buddy;
PurpleBlistNode *node;
gchar *real_who = NULL;
gchar *msg_html;
const char *color;
gboolean group_chat;
GdkPixbuf *avatar = NULL;
......@@ -2247,11 +2211,9 @@ chatty_conv_write_conversation (PurpleConversation *conv,
who_no_resource = chatty_utils_jabber_id_strip(who);
if (flags & PURPLE_MESSAGE_RECV) {
msg_html = chatty_conv_check_for_links (message);
chatty_msg_list_add_message (chatty_conv->msg_list,
MSG_IS_INCOMING,
msg_html,
message,
group_chat ? who : timestamp,
icon ? icon : NULL);
if (type == PURPLE_CONV_TYPE_CHAT){
......@@ -2259,14 +2221,10 @@ chatty_conv_write_conversation (PurpleConversation *conv,
} else {
chatty_history_add_im_message (message, 1, account->username, who_no_resource, uuid, mtime);
}
g_free (msg_html);
} else if (flags & PURPLE_MESSAGE_SEND) {
msg_html = chatty_conv_check_for_links (message);
chatty_msg_list_add_message (chatty_conv->msg_list,
MSG_IS_OUTGOING,
msg_html,
message,
NULL,
NULL);
if (type == PURPLE_CONV_TYPE_CHAT){
......@@ -2275,7 +2233,6 @@ chatty_conv_write_conversation (PurpleConversation *conv,
chatty_history_add_im_message (message, -1, account->username, who_no_resource, uuid, mtime);
}
g_free (msg_html);
} else if (flags & PURPLE_MESSAGE_SYSTEM) {
if (purple_blist_node_get_bool (node, "chatty-status-msg")) {
chatty_msg_list_add_message (chatty_conv->msg_list,
......
......@@ -12,6 +12,7 @@
#include <string.h>
#include <math.h>
#include <cairo.h>
#include <purple.h>
#include "chatty-message-list.h"
#define INDICATOR_WIDTH 60
......@@ -561,10 +562,28 @@ chatty_msg_list_clear (ChattyMsgList *self)
}
/* This removes all markup exept for links */
static gchar *
chatty_msg_list_escape_message (const gchar *message)
{
g_autofree gchar *striped;
g_autofree gchar *escaped;
g_autofree gchar *linkified;
gchar *result;
striped = purple_markup_strip_html (message);
escaped = purple_markup_escape_text (striped, -1);
linkified = purple_markup_linkify (escaped);
// convert all tags to lowercase for GtkLabel markup parser
purple_markup_html_to_xhtml (linkified, &result, NULL);
return result;
}
GtkWidget *
chatty_msg_list_add_message_at (ChattyMsgList *self,
guint message_dir,
const gchar *message,
const gchar *html_message,
const gchar *footer,
GtkWidget *icon,
guint position)
......@@ -581,9 +600,12 @@ chatty_msg_list_add_message_at (ChattyMsgList *self,
gchar *style;
gchar *str;
gint width, height;
gchar *message;
ChattyMsgListPrivate *priv = chatty_msg_list_get_instance_private (self);
message = chatty_msg_list_escape_message (html_message);
row = GTK_LIST_BOX_ROW (gtk_list_box_row_new ());
g_object_set (G_OBJECT(row),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment