From cd9d7d1ef951ab85bf87e7ff56b1180169d053c2 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Wed, 26 Aug 2009 19:46:20 +0100 Subject: /clearly shouldn't clear the window. --- libempathy-gtk/empathy-chat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libempathy-gtk/empathy-chat.c b/libempathy-gtk/empathy-chat.c index 9096beeae..118baff70 100644 --- a/libempathy-gtk/empathy-chat.c +++ b/libempathy-gtk/empathy-chat.c @@ -391,7 +391,7 @@ chat_send (EmpathyChat *chat, chat_sent_message_add (chat, msg); - if (g_str_has_prefix (msg, "/clear")) { + if (strcmp (msg, "/clear") == 0) { empathy_chat_view_clear (chat->view); return; } -- cgit v1.2.3 From dec9d4dcf8c7611862221be9eadb53ef52a5bc3b Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Wed, 26 Aug 2009 19:46:39 +0100 Subject: Add empathy_message_new_from_entry() This will be the sole place that user input is parsed for special commands like /me; the parsing logic will be removed from EmpathyMessage and chat_send(). --- libempathy/empathy-message.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ libempathy/empathy-message.h | 1 + 2 files changed, 57 insertions(+) diff --git a/libempathy/empathy-message.c b/libempathy/empathy-message.c index a8fe6084c..0da55112d 100644 --- a/libempathy/empathy-message.c +++ b/libempathy/empathy-message.c @@ -218,6 +218,62 @@ message_set_property (GObject *object, }; } +static gboolean +has_prefix_case (const gchar *s, + const gchar *prefix) +{ + return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0; +} + +/* + * Constructs an EmpathyMessage based on user input, which may include "/me" + * and friends. + * + * Returns: an #EmpathyMessage if @message could be parsed, or %NULL if + * @message was an unknown command. + */ +EmpathyMessage * +empathy_message_new_from_entry (const gchar *message) +{ + TpChannelTextMessageType t = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL; + + g_return_val_if_fail (message != NULL, NULL); + + if (message[0] == '/') { + if (g_ascii_strcasecmp (message, "/me") == 0) { + message = ""; + t = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION; + } else if (has_prefix_case (message, "/me ")) { + message += strlen ("/me "); + t = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION; + } else if (has_prefix_case (message, "/say ")) { + message += strlen ("/say "); + } else { + /* Also allow messages with two slashes before the + * first space, so it is possible to send a /unix/path. + * This heuristic is kind of crap. + */ + gboolean second_slash = FALSE; + const gchar *m = message + 1; + + while (!second_slash && *m != '\0' && *m != ' ') { + if (*m == '/') + second_slash = TRUE; + + m++; + } + + if (!second_slash) + return NULL; + } + } + + return g_object_new (EMPATHY_TYPE_MESSAGE, + "type", t, + "body", message, + NULL); +} + EmpathyMessage * empathy_message_new (const gchar *body) { diff --git a/libempathy/empathy-message.h b/libempathy/empathy-message.h index 00064df57..f9a488703 100644 --- a/libempathy/empathy-message.h +++ b/libempathy/empathy-message.h @@ -52,6 +52,7 @@ struct _EmpathyMessageClass { }; GType empathy_message_get_type (void) G_GNUC_CONST; +EmpathyMessage * empathy_message_new_from_entry (const gchar *message); EmpathyMessage * empathy_message_new (const gchar *body); TpChannelTextMessageType empathy_message_get_tptype (EmpathyMessage *message); void empathy_message_set_tptype (EmpathyMessage *message, -- cgit v1.2.3 From c74685b02308c82d355fc59e1aa8491c84a2f114 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Wed, 26 Aug 2009 19:47:49 +0100 Subject: Don't parse message in EmpathyMessage:body setter This fixes a bug where the message (Type_Action, "/me wooo yay") is printed as " ! wjt woo yay" rather than as " ! wjt /me woo yay". This also fixes Gnome bug #593101 (Empathy exits sending empty /me message), which was caused by this deleted code walking off the end of the string "/me". --- libempathy/empathy-message.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/libempathy/empathy-message.c b/libempathy/empathy-message.c index 0da55112d..34a466752 100644 --- a/libempathy/empathy-message.c +++ b/libempathy/empathy-message.c @@ -393,28 +393,15 @@ empathy_message_set_body (EmpathyMessage *message, const gchar *body) { EmpathyMessagePriv *priv = GET_PRIV (message); - TpChannelTextMessageType type; g_return_if_fail (EMPATHY_IS_MESSAGE (message)); g_free (priv->body); - priv->body = NULL; - - type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL; - if (g_str_has_prefix (body, "/me")) { - type = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION; - body += 4; - } - else if (g_str_has_prefix (body, "/say")) { - body += 5; - } if (body) { priv->body = g_strdup (body); - } - - if (type != priv->type) { - empathy_message_set_tptype (message, type); + } else { + priv->body = NULL; } g_object_notify (G_OBJECT (message), "body"); -- cgit v1.2.3 From c2cbd1de858c0a8a994358fc717054ce32c8b895 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Wed, 26 Aug 2009 19:51:49 +0100 Subject: Remove validation from chat_send. Instead, call empathy_message_new_from_entry (), which parses the entered text for us. --- libempathy-gtk/empathy-chat.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/libempathy-gtk/empathy-chat.c b/libempathy-gtk/empathy-chat.c index 118baff70..a979c9e76 100644 --- a/libempathy-gtk/empathy-chat.c +++ b/libempathy-gtk/empathy-chat.c @@ -396,29 +396,15 @@ chat_send (EmpathyChat *chat, return; } - /* Blacklist messages begining by '/', except for "/me" and "/say" - * because they are handled in EmpathyMessage */ - if (msg[0] == '/' && - !g_str_has_prefix (msg, "/me") && - !g_str_has_prefix (msg, "/say")) { - /* Also allow messages with two slashes before the first space, - * so it is possible to send an /unix/path */ - int slash_count = 0, i; - for (i = 0; msg[i] && msg[i] != ' ' && slash_count < 2; i++) { - if (msg[i] == '/') - slash_count++; - } - if (slash_count == 1) { - empathy_chat_view_append_event (chat->view, - _("Unsupported command")); - return; - } - } + message = empathy_message_new_from_entry (msg); - /* We can send the message */ - message = empathy_message_new (msg); - empathy_tp_chat_send (priv->tp_chat, message); - g_object_unref (message); + if (message == NULL) { + empathy_chat_view_append_event (chat->view, + _("Unsupported command")); + } else { + empathy_tp_chat_send (priv->tp_chat, message); + g_object_unref (message); + } } static void -- cgit v1.2.3