[Telepathy-commits] [telepathy-gabble/master] Move gabble_text_mixin_parse_incoming_message to message-util, renaming accordingly

Will Thompson will.thompson at collabora.co.uk
Tue Feb 3 06:34:48 PST 2009


---
 src/im-factory.c           |    4 +-
 src/message-util.c         |  202 ++++++++++++++++++++++++++++++++++++++++++++
 src/message-util.h         |    7 ++
 src/muc-factory.c          |    4 +-
 src/text-mixin.c           |  195 ------------------------------------------
 src/text-mixin.h           |    6 --
 tests/test-parse-message.c |   13 ++--
 7 files changed, 220 insertions(+), 211 deletions(-)

diff --git a/src/im-factory.c b/src/im-factory.c
index 2a96279..41950ba 100644
--- a/src/im-factory.c
+++ b/src/im-factory.c
@@ -37,7 +37,7 @@
 #include "debug.h"
 #include "disco.h"
 #include "im-channel.h"
-#include "text-mixin.h"
+#include "message-util.h"
 
 static void channel_manager_iface_init (gpointer, gpointer);
 
@@ -216,7 +216,7 @@ im_factory_message_cb (LmMessageHandler *handler,
   gint state;
   TpChannelTextSendError send_error;
 
-  if (!gabble_text_mixin_parse_incoming_message (message, &from, &stamp,
+  if (!gabble_message_util_parse_incoming_message (message, &from, &stamp,
         &msgtype, &body, &state, &send_error))
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 
diff --git a/src/message-util.c b/src/message-util.c
index b716cc8..e738139 100644
--- a/src/message-util.c
+++ b/src/message-util.c
@@ -22,8 +22,13 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#define _GNU_SOURCE /* Needed for strptime (_XOPEN_SOURCE can also be used). */
+
 #include "message-util.h"
 
+#include <string.h>
+#include <time.h>
+
 #include <loudmouth/loudmouth.h>
 #include <telepathy-glib/dbus.h>
 
@@ -210,3 +215,200 @@ gabble_message_util_send_chat_state (GObject *obj,
 
   return result;
 }
+
+
+gboolean
+gabble_message_util_parse_incoming_message (LmMessage *message,
+                                            const gchar **from,
+                                            time_t *stamp,
+                                            TpChannelTextMessageType *msgtype,
+                                            const gchar **body_ret,
+                                            gint *state,
+                                            TpChannelTextSendError *send_error)
+{
+  const gchar *type, *body;
+  LmMessageNode *node;
+
+  *send_error = GABBLE_TEXT_CHANNEL_SEND_NO_ERROR;
+
+  if (lm_message_get_sub_type (message) == LM_MESSAGE_SUB_TYPE_ERROR)
+    {
+      LmMessageNode *error_node;
+
+      error_node = lm_message_node_get_child (message->node, "error");
+      if (error_node)
+        {
+          GabbleXmppError err = gabble_xmpp_error_from_node (error_node);
+          DEBUG ("got xmpp error: %s: %s", gabble_xmpp_error_string (err),
+                 gabble_xmpp_error_description (err));
+
+          /* these are based on descriptions of errors, and some testing */
+          switch (err)
+            {
+              case XMPP_ERROR_SERVICE_UNAVAILABLE:
+              case XMPP_ERROR_RECIPIENT_UNAVAILABLE:
+                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
+                break;
+
+              case XMPP_ERROR_ITEM_NOT_FOUND:
+              case XMPP_ERROR_JID_MALFORMED:
+              case XMPP_ERROR_REMOTE_SERVER_TIMEOUT:
+                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
+                break;
+
+              case XMPP_ERROR_FORBIDDEN:
+                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
+                break;
+
+              case XMPP_ERROR_RESOURCE_CONSTRAINT:
+                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG;
+                break;
+
+              case XMPP_ERROR_FEATURE_NOT_IMPLEMENTED:
+                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
+                break;
+
+              default:
+                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
+            }
+        }
+      else
+        {
+          *send_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
+        }
+    }
+
+  *from = lm_message_node_get_attribute (message->node, "from");
+  if (*from == NULL)
+    {
+      NODE_DEBUG (message->node, "got a message without a from field");
+      return FALSE;
+    }
+
+  type = lm_message_node_get_attribute (message->node, "type");
+
+  /*
+   * Parse timestamp of delayed messages. For non-delayed, it's
+   * 0 and the channel code should set the current timestamp.
+   */
+  *stamp = 0;
+
+  node = lm_message_node_get_child_with_namespace (message->node, "x",
+      NS_X_DELAY);
+  if (node != NULL)
+    {
+      const gchar *stamp_str, *p;
+      struct tm stamp_tm = { 0, };
+
+      stamp_str = lm_message_node_get_attribute (node, "stamp");
+      if (stamp_str != NULL)
+        {
+          p = strptime (stamp_str, "%Y%m%dT%T", &stamp_tm);
+          if (p == NULL || *p != '\0')
+            {
+              g_warning ("%s: malformed date string '%s' for jabber:x:delay",
+                         G_STRFUNC, stamp_str);
+            }
+          else
+            {
+              *stamp = timegm (&stamp_tm);
+            }
+        }
+    }
+
+  /*
+   * Parse body if it exists.
+   */
+  node = lm_message_node_get_child (message->node, "body");
+
+  if (node)
+    {
+      body = lm_message_node_get_value (node);
+    }
+  else
+    {
+      body = NULL;
+    }
+
+  /* Messages starting with /me are ACTION messages, and the /me should be
+   * removed. type="chat" messages are NORMAL.  everything else is
+   * something that doesn't necessarily expect a reply or ongoing
+   * conversation ("normal") or has been auto-sent, so we make it NOTICE in
+   * all other cases. */
+
+  *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
+  *body_ret = body;
+
+  if (body != NULL)
+    {
+      if (type == NULL &&
+          lm_message_node_get_child_with_namespace (message->node,
+              "time", "google:timestamp") != NULL &&
+          lm_message_node_get_child_with_namespace (message->node,
+              "x", "jabber:x:delay") != NULL)
+        {
+          /* Google servers send offline messages without a type. Work around
+           * this. */
+          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
+        }
+      else if (0 == strncmp (body, "/me ", 4))
+        {
+          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
+          *body_ret = body + 4;
+        }
+      else if (type != NULL && (0 == strcmp (type, "chat") ||
+                                0 == strcmp (type, "groupchat")))
+        {
+          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
+          *body_ret = body;
+        }
+    }
+
+  /*
+   * Parse chat state if it exists.
+   */
+
+  node = lm_message_node_get_child_with_namespace (message->node, "active",
+      NS_CHAT_STATES);
+  if (node)
+    {
+      *state = TP_CHANNEL_CHAT_STATE_ACTIVE;
+      return TRUE;
+    }
+
+  node = lm_message_node_get_child_with_namespace  (message->node, "composing",
+      NS_CHAT_STATES);
+  if (node)
+    {
+      *state = TP_CHANNEL_CHAT_STATE_COMPOSING;
+      return TRUE;
+    }
+
+  node = lm_message_node_get_child_with_namespace  (message->node, "inactive",
+      NS_CHAT_STATES);
+  if (node)
+    {
+      *state = TP_CHANNEL_CHAT_STATE_INACTIVE;
+      return TRUE;
+    }
+
+  node = lm_message_node_get_child_with_namespace  (message->node, "paused",
+      NS_CHAT_STATES);
+  if (node)
+    {
+      *state = TP_CHANNEL_CHAT_STATE_PAUSED;
+      return TRUE;
+    }
+
+  node = lm_message_node_get_child_with_namespace  (message->node, "gone",
+      NS_CHAT_STATES);
+  if (node)
+    {
+      *state = TP_CHANNEL_CHAT_STATE_GONE;
+      return TRUE;
+    }
+
+  *state = -1;
+
+  return TRUE;
+}
diff --git a/src/message-util.h b/src/message-util.h
index b05acd3..eb68e96 100644
--- a/src/message-util.h
+++ b/src/message-util.h
@@ -37,6 +37,13 @@ gboolean gabble_message_util_send_chat_state (GObject *obj,
     GabbleConnection *conn, LmMessageSubType subtype, TpChannelChatState state,
     const char *recipient, GError **error);
 
+
+#define GABBLE_TEXT_CHANNEL_SEND_NO_ERROR ((TpChannelTextSendError)-1)
+
+gboolean gabble_message_util_parse_incoming_message (LmMessage *message,
+    const gchar **from, time_t *stamp, TpChannelTextMessageType *msgtype,
+    const gchar **body_ret, gint *state, TpChannelTextSendError *send_error);
+
 G_END_DECLS
 
 #endif /* #ifndef __GABBLE_MESSAGE_UTIL_H__ */
diff --git a/src/muc-factory.c b/src/muc-factory.c
index df8759e..a1fe06d 100644
--- a/src/muc-factory.c
+++ b/src/muc-factory.c
@@ -38,10 +38,10 @@
 #include "conn-olpc.h"
 #include "debug.h"
 #include "disco.h"
+#include "message-util.h"
 #include "muc-channel.h"
 #include "namespaces.h"
 #include "presence-cache.h"
-#include "text-mixin.h"
 #include "tubes-channel.h"
 #include "util.h"
 
@@ -735,7 +735,7 @@ muc_factory_message_cb (LmMessageHandler *handler,
   gchar *room;
   LmMessageNode *subj_node;
 
-  if (!gabble_text_mixin_parse_incoming_message (message, &from, &stamp,
+  if (!gabble_message_util_parse_incoming_message (message, &from, &stamp,
         &msgtype, &body, &state, &send_error))
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 
diff --git a/src/text-mixin.c b/src/text-mixin.c
index 07ce42c..5773aab 100644
--- a/src/text-mixin.c
+++ b/src/text-mixin.c
@@ -191,198 +191,3 @@ gabble_text_mixin_send (GObject *obj,
   return TRUE;
 }
 
-gboolean
-gabble_text_mixin_parse_incoming_message (LmMessage *message,
-                        const gchar **from,
-                        time_t *stamp,
-                        TpChannelTextMessageType *msgtype,
-                        const gchar **body_ret,
-                        gint *state,
-                        TpChannelTextSendError *send_error)
-{
-  const gchar *type, *body;
-  LmMessageNode *node;
-
-  *send_error = GABBLE_TEXT_CHANNEL_SEND_NO_ERROR;
-
-  if (lm_message_get_sub_type (message) == LM_MESSAGE_SUB_TYPE_ERROR)
-    {
-      LmMessageNode *error_node;
-
-      error_node = lm_message_node_get_child (message->node, "error");
-      if (error_node)
-        {
-          GabbleXmppError err = gabble_xmpp_error_from_node (error_node);
-          DEBUG ("got xmpp error: %s: %s", gabble_xmpp_error_string (err),
-                 gabble_xmpp_error_description (err));
-
-          /* these are based on descriptions of errors, and some testing */
-          switch (err)
-            {
-              case XMPP_ERROR_SERVICE_UNAVAILABLE:
-              case XMPP_ERROR_RECIPIENT_UNAVAILABLE:
-                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE;
-                break;
-
-              case XMPP_ERROR_ITEM_NOT_FOUND:
-              case XMPP_ERROR_JID_MALFORMED:
-              case XMPP_ERROR_REMOTE_SERVER_TIMEOUT:
-                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT;
-                break;
-
-              case XMPP_ERROR_FORBIDDEN:
-                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED;
-                break;
-
-              case XMPP_ERROR_RESOURCE_CONSTRAINT:
-                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG;
-                break;
-
-              case XMPP_ERROR_FEATURE_NOT_IMPLEMENTED:
-                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED;
-                break;
-
-              default:
-                *send_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
-            }
-        }
-      else
-        {
-          *send_error = TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN;
-        }
-    }
-
-  *from = lm_message_node_get_attribute (message->node, "from");
-  if (*from == NULL)
-    {
-      NODE_DEBUG (message->node, "got a message without a from field");
-      return FALSE;
-    }
-
-  type = lm_message_node_get_attribute (message->node, "type");
-
-  /*
-   * Parse timestamp of delayed messages. For non-delayed, it's
-   * 0 and the channel code should set the current timestamp.
-   */
-  *stamp = 0;
-
-  node = lm_message_node_get_child_with_namespace (message->node, "x",
-      NS_X_DELAY);
-  if (node != NULL)
-    {
-      const gchar *stamp_str, *p;
-      struct tm stamp_tm = { 0, };
-
-      stamp_str = lm_message_node_get_attribute (node, "stamp");
-      if (stamp_str != NULL)
-        {
-          p = strptime (stamp_str, "%Y%m%dT%T", &stamp_tm);
-          if (p == NULL || *p != '\0')
-            {
-              g_warning ("%s: malformed date string '%s' for jabber:x:delay",
-                         G_STRFUNC, stamp_str);
-            }
-          else
-            {
-              *stamp = timegm (&stamp_tm);
-            }
-        }
-    }
-
-  /*
-   * Parse body if it exists.
-   */
-  node = lm_message_node_get_child (message->node, "body");
-
-  if (node)
-    {
-      body = lm_message_node_get_value (node);
-    }
-  else
-    {
-      body = NULL;
-    }
-
-  /* Messages starting with /me are ACTION messages, and the /me should be
-   * removed. type="chat" messages are NORMAL.  everything else is
-   * something that doesn't necessarily expect a reply or ongoing
-   * conversation ("normal") or has been auto-sent, so we make it NOTICE in
-   * all other cases. */
-
-  *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
-  *body_ret = body;
-
-  if (body != NULL)
-    {
-      if (type == NULL &&
-          lm_message_node_get_child_with_namespace (message->node,
-              "time", "google:timestamp") != NULL &&
-          lm_message_node_get_child_with_namespace (message->node,
-              "x", "jabber:x:delay") != NULL)
-        {
-          /* Google servers send offline messages without a type. Work around
-           * this. */
-          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
-        }
-      else if (0 == strncmp (body, "/me ", 4))
-        {
-          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
-          *body_ret = body + 4;
-        }
-      else if (type != NULL && (0 == strcmp (type, "chat") ||
-                                0 == strcmp (type, "groupchat")))
-        {
-          *msgtype = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
-          *body_ret = body;
-        }
-    }
-
-  /*
-   * Parse chat state if it exists.
-   */
-
-  node = lm_message_node_get_child_with_namespace (message->node, "active",
-      NS_CHAT_STATES);
-  if (node)
-    {
-      *state = TP_CHANNEL_CHAT_STATE_ACTIVE;
-      return TRUE;
-    }
-
-  node = lm_message_node_get_child_with_namespace  (message->node, "composing",
-      NS_CHAT_STATES);
-  if (node)
-    {
-      *state = TP_CHANNEL_CHAT_STATE_COMPOSING;
-      return TRUE;
-    }
-
-  node = lm_message_node_get_child_with_namespace  (message->node, "inactive",
-      NS_CHAT_STATES);
-  if (node)
-    {
-      *state = TP_CHANNEL_CHAT_STATE_INACTIVE;
-      return TRUE;
-    }
-
-  node = lm_message_node_get_child_with_namespace  (message->node, "paused",
-      NS_CHAT_STATES);
-  if (node)
-    {
-      *state = TP_CHANNEL_CHAT_STATE_PAUSED;
-      return TRUE;
-    }
-
-  node = lm_message_node_get_child_with_namespace  (message->node, "gone",
-      NS_CHAT_STATES);
-  if (node)
-    {
-      *state = TP_CHANNEL_CHAT_STATE_GONE;
-      return TRUE;
-    }
-
-  *state = -1;
-
-  return TRUE;
-}
diff --git a/src/text-mixin.h b/src/text-mixin.h
index 1c8cf45..f658c8c 100644
--- a/src/text-mixin.h
+++ b/src/text-mixin.h
@@ -45,8 +45,6 @@ struct _GabbleTextMixin
 #define GABBLE_TEXT_MIXIN(o) \
   ((GabbleTextMixin *) tp_mixin_offset_cast (o, TP_TEXT_MIXIN_OFFSET (o)))
 
-#define GABBLE_TEXT_CHANNEL_SEND_NO_ERROR ((TpChannelTextSendError)-1)
-
 void gabble_text_mixin_init (GObject *obj, glong offset,
     TpHandleRepoIface *contacts_repo, gboolean send_nick);
 
@@ -54,10 +52,6 @@ gboolean gabble_text_mixin_send (GObject *obj, guint type, guint subtype,
     gint state, const char *recipient, const gchar *text,
     GabbleConnection *conn, gboolean emit_signal, GError **error);
 
-gboolean gabble_text_mixin_parse_incoming_message (LmMessage *message,
-    const gchar **from, time_t *stamp, TpChannelTextMessageType *msgtype,
-    const gchar **body_ret, gint *state, TpChannelTextSendError *send_error);
-
 G_END_DECLS
 
 #endif /* #ifndef __GABBLE_TEXT_MIXIN_H__ */
diff --git a/tests/test-parse-message.c b/tests/test-parse-message.c
index b33bf42..1c7b6f3 100644
--- a/tests/test-parse-message.c
+++ b/tests/test-parse-message.c
@@ -3,7 +3,8 @@
 
 #include <string.h>
 
-#include "src/text-mixin.h"
+#include "src/util.h"
+#include "src/message-util.h"
 
 /* Test the most basic <message> possible. */
 static gboolean
@@ -21,7 +22,7 @@ test1 (void)
   msg = lm_message_build (NULL, LM_MESSAGE_TYPE_MESSAGE,
         '@', "from", "foo at bar.com",
         NULL);
-  ret = gabble_text_mixin_parse_incoming_message (
+  ret = gabble_message_util_parse_incoming_message (
       msg, &from, &stamp, &type, &body, &state, &send_error);
   g_assert (ret == TRUE);
   g_assert (0 == strcmp (from, "foo at bar.com"));
@@ -52,7 +53,7 @@ test2 (void)
         '@', "from", "foo at bar.com",
         '(', "body", "hello", ')',
         NULL);
-  ret = gabble_text_mixin_parse_incoming_message (
+  ret = gabble_message_util_parse_incoming_message (
       msg, &from, &stamp, &type, &body, &state, &send_error);
   g_assert (ret == TRUE);
   g_assert (0 == strcmp (from, "foo at bar.com"));
@@ -83,7 +84,7 @@ test3 (void)
         '@', "type", "chat",
         '(', "body", "hello", ')',
         NULL);
-  ret = gabble_text_mixin_parse_incoming_message (
+  ret = gabble_message_util_parse_incoming_message (
       msg, &from, &stamp, &type, &body, &state, &send_error);
   g_assert (ret == TRUE);
   g_assert (0 == strcmp (from, "foo at bar.com"));
@@ -115,7 +116,7 @@ test_error (void)
       '@', "type", "error",
       '(', "error", "oops", ')',
       NULL);
-  ret = gabble_text_mixin_parse_incoming_message (
+  ret = gabble_message_util_parse_incoming_message (
       msg, &from, &stamp, &type, &body, &state, &send_error);
   g_assert (ret == TRUE);
   g_assert (0 == strcmp (from, "foo at bar.com"));
@@ -152,7 +153,7 @@ test_google_offline (void)
          '@', "ms", "1190899454656",
       ')',
       NULL);
-  ret = gabble_text_mixin_parse_incoming_message (
+  ret = gabble_message_util_parse_incoming_message (
       msg, &from, &stamp, &type, &body, &state, &send_error);
   g_assert (ret == TRUE);
   g_assert (0 == strcmp (from, "foo at bar.com"));
-- 
1.5.6.5




More information about the telepathy-commits mailing list