[polari] util: Split out match_identify_message() method



commit 06dafa1973ebd37f3e8046110504b2ee54dbbc3c
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Sep 23 19:42:28 2016 +0200

    util: Split out match_identify_message() method
    
    Matching an identify command is currently done by the room to emit
    the ::identify-sent signal when the user entered the command, so
    we can offer to save the password. We now want to mask the sent
    password in the chat log, so split out the matching code into a
    utility method.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=771888

 src/lib/polari-room.c |   16 ++--------------
 src/lib/polari-util.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 src/lib/polari-util.h |    6 ++++++
 3 files changed, 51 insertions(+), 14 deletions(-)
---
diff --git a/src/lib/polari-room.c b/src/lib/polari-room.c
index 2310a38..8d400a0 100644
--- a/src/lib/polari-room.c
+++ b/src/lib/polari-room.c
@@ -90,8 +90,6 @@ static guint signals[LAST_SIGNAL];
 
 static GRegex *color_code_regex = NULL;
 
-static GRegex *identify_message_regex = NULL;
-
 G_DEFINE_TYPE_WITH_PRIVATE (PolariRoom, polari_room, G_TYPE_OBJECT)
 
 static void polari_room_set_channel (PolariRoom *room, TpChannel *channel);
@@ -468,24 +466,15 @@ on_message_sent (TpTextChannel      *channel,
 {
   PolariRoom *room = user_data;
   PolariRoomPrivate *priv = room->priv;
-  GMatchInfo *match;
-  char *text, *stripped_text;
+  char *username, *password, *text;
 
   if (priv->type != TP_HANDLE_TYPE_CONTACT)
     return;
 
   text = tp_message_to_text (TP_MESSAGE (message), NULL);
-  stripped_text = g_strstrip (text);
 
-  if (G_UNLIKELY (identify_message_regex == NULL))
-    identify_message_regex = g_regex_new ("^identify (?:(\\w+) )?(\\S+)$",
-                                          G_REGEX_OPTIMIZE | G_REGEX_CASELESS,
-                                          0, NULL);
-  if (g_regex_match (identify_message_regex, stripped_text, 0, &match))
+  if (polari_util_match_identify_message (text, &username, &password))
     {
-      char *username = g_match_info_fetch (match, 1);
-      char *password = g_match_info_fetch (match, 2);
-
       if (!priv->ignore_identify)
         g_signal_emit (room, signals[IDENTIFY_SENT], 0, username, password);
 
@@ -495,7 +484,6 @@ on_message_sent (TpTextChannel      *channel,
       g_free (password);
     }
 
-  g_match_info_free (match);
   g_free (text);
 }
 
diff --git a/src/lib/polari-util.c b/src/lib/polari-util.c
index 93f5141..bfc2c6a 100644
--- a/src/lib/polari-util.c
+++ b/src/lib/polari-util.c
@@ -41,3 +41,46 @@ polari_util_get_basenick (const char *nick)
   else
     return g_utf8_casefold (nick, -1);
 }
+
+/**
+ * polari_util_match_identify_message:
+ * @message: a text message
+ * @username: (optional) (out): the parsed name if the @message is an
+ *                              identify command
+ * @password: (optional) (out): the parsed password if the @message is an
+ *                              identify command
+ *
+ * Returns: %TRUE if @message is an identify command
+ */
+gboolean
+polari_util_match_identify_message (const char  *message,
+                                    char       **username,
+                                    char       **password)
+{
+  static GRegex *identify_message_regex = NULL;
+  GMatchInfo *match;
+  char *text, *stripped_text;
+  gboolean matched;
+
+  text = g_strdup (message);
+  stripped_text = g_strstrip (text);
+
+  if (G_UNLIKELY (identify_message_regex == NULL))
+    identify_message_regex = g_regex_new ("^identify (?:(\\w+) )?(\\S+)$",
+                                          G_REGEX_OPTIMIZE | G_REGEX_CASELESS,
+                                          0, NULL);
+
+  matched = g_regex_match (identify_message_regex, stripped_text, 0, &match);
+  if (matched)
+    {
+      if (username)
+        *username = g_match_info_fetch (match, 1);
+      if (password)
+        *password = g_match_info_fetch (match, 2);
+    }
+
+  g_match_info_free (match);
+  g_free (text);
+
+  return matched;
+}
diff --git a/src/lib/polari-util.h b/src/lib/polari-util.h
index 73888c5..db8b508 100644
--- a/src/lib/polari-util.h
+++ b/src/lib/polari-util.h
@@ -19,6 +19,12 @@
 #ifndef __POLARI_UTIL_H__
 #define __POLARI_UTIL_H__
 
+#include <glib.h>
+
 char *polari_util_get_basenick (const char *nick);
 
+gboolean polari_util_match_identify_message (const char  *message,
+                                             char       **username,
+                                             char       **password);
+
 #endif /* __POLARI_UTIL_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]