]> git.0d.be Git - empathy.git/commitdiff
Move /me and /say support from EmpathyMessage to EmpathyChat.
authorXavier Claessens <xclaesse@gmail.com>
Tue, 27 Oct 2009 13:26:14 +0000 (14:26 +0100)
committerXavier Claessens <xclaesse@gmail.com>
Sun, 1 Nov 2009 14:36:46 +0000 (15:36 +0100)
Also make commands not case sensitive and use g_ascii_isspace to detect spaces.

libempathy-gtk/empathy-chat.c
libempathy/empathy-message.c
libempathy/empathy-message.h

index df76a02fc2962a7dba29a89fc4b12e7c4e61bd32..c2a88b2d5feb5d963234560bb6a6525af0478436 100644 (file)
@@ -508,6 +508,31 @@ chat_command_msg (EmpathyChat *chat,
        chat_command_msg_internal (chat, strv[1], strv[2]);
 }
 
+static void
+chat_command_me (EmpathyChat *chat,
+                 GStrv        strv)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyMessage *message;
+
+       message = empathy_message_new (strv[1]);
+       empathy_message_set_tptype (message, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION);
+       empathy_tp_chat_send (priv->tp_chat, message);
+       g_object_unref (message);
+}
+
+static void
+chat_command_say (EmpathyChat *chat,
+                 GStrv        strv)
+{
+       EmpathyChatPriv *priv = GET_PRIV (chat);
+       EmpathyMessage *message;
+
+       message = empathy_message_new (strv[1]);
+       empathy_tp_chat_send (priv->tp_chat, message);
+       g_object_unref (message);
+}
+
 static void chat_command_help (EmpathyChat *chat, GStrv strv);
 
 typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);
@@ -539,6 +564,14 @@ static ChatCommandItem commands[] = {
        {"msg", 3, 3, chat_command_msg,
         N_("/msg <contact id> <message>, open a private chat")},
 
+       {"me", 2, 2, chat_command_me,
+        N_("/me <message>, send an ACTION message to the current conversation")},
+
+       {"say", 2, 2, chat_command_say,
+        N_("/say <message>, send <message> to the current conversation. "
+           "This is used to send a message starting with a '/'. For example: "
+           "\"/say /join is used to join a new chatroom\"")},
+
        {"help", 1, 2, chat_command_help,
         N_("/help [<command>], show all supported commands. "
            "If <command> is defined, show its usage.")},
@@ -572,7 +605,7 @@ chat_command_help (EmpathyChat *chat,
        }
 
        for (i = 0; i < G_N_ELEMENTS (commands); i++) {
-               if (!tp_strdiff (strv[1], commands[i].prefix)) {
+               if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
                        chat_command_show_help (chat, &commands[i]);
                        return;
                }
@@ -592,12 +625,13 @@ chat_command_parse (const gchar *text, guint max_parts)
                const gchar *end;
 
                /* Skip white spaces */
-               while (*text == ' ') {
+               while (g_ascii_isspace (*text)) {
                        text++;
                }
 
-               end = strchr (text, ' ');
-               if (end == NULL) {
+               /* Search the end of this part, until first space. */
+               for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++);
+               if (*end == '\0') {
                        break;
                }
 
@@ -624,6 +658,13 @@ chat_command_parse (const gchar *text, guint max_parts)
        return (GStrv) g_ptr_array_free (array, FALSE);
 }
 
+static gboolean
+has_prefix_case (const gchar *s,
+                 const gchar *prefix)
+{
+       return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
+}
+
 static void
 chat_send (EmpathyChat  *chat,
           const gchar *msg)
@@ -641,11 +682,14 @@ chat_send (EmpathyChat  *chat,
        chat_sent_message_add (chat, msg);
 
        if (msg[0] == '/') {
+               gboolean second_slash = FALSE;
+               const gchar *iter = msg + 1;
+
                for (i = 0; i < G_N_ELEMENTS (commands); i++) {
                        GStrv strv;
                        guint strv_len;
 
-                       if (!g_str_has_prefix (msg + 1, commands[i].prefix)) {
+                       if (!has_prefix_case (msg + 1, commands[i].prefix)) {
                                continue;
                        }
 
@@ -654,7 +698,7 @@ chat_send (EmpathyChat  *chat,
                         * between args */
                        strv = chat_command_parse (msg + 1, commands[i].max_parts);
 
-                       if (tp_strdiff (strv[0], commands[i].prefix)) {
+                       if (g_ascii_strcasecmp (strv[0], commands[i].prefix) != 0) {
                                g_strfreev (strv);
                                continue;
                        }
@@ -671,17 +715,28 @@ chat_send (EmpathyChat  *chat,
                        g_strfreev (strv);
                        return;
                }
-       }
 
-       message = empathy_message_new_from_entry (msg);
+               /* 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. */
+               while (*iter != '\0' && !g_ascii_isspace (*iter)) {
+                       if (*iter == '/') {
+                               second_slash = TRUE;
+                               break;
+                       }
+                       iter++;
+               }
 
-       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);
+               if (!second_slash) {
+                       empathy_chat_view_append_event (chat->view,
+                               _("Unsupported command"));
+                       return;
+               }
        }
+
+       message = empathy_message_new (msg);
+       empathy_tp_chat_send (priv->tp_chat, message);
+       g_object_unref (message);
 }
 
 static void
index a403766e4b98149a7c3e8f61ca99b31d8da86228..705bd224bad973d2d3bcdfcd1ddcfbc392096200 100644 (file)
@@ -234,62 +234,6 @@ 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)
 {
index 7ca624031539b34f1835dd5c7be200b7042c2a2b..09175e625c631c9a64becfe16b58a22e9124749e 100644 (file)
@@ -52,7 +52,6 @@ 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,