pep_activity.c
changeset 29 23fa36d480fb
child 31 e404cd1c7077
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pep_activity.c	Sun May 20 22:15:51 2012 +0300
@@ -0,0 +1,347 @@
+/*
+ * pep_activity.c       -- Pep activity events
+ *
+ * Copyright (C) 2009-2012 Myhailo Danylenko <isbear@ukrpost.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ */
+
+#include <glib.h>
+#include <loudmouth/loudmouth.h>
+#include <string.h>
+
+#include <mcabber/utils.h>
+#include <mcabber/logprint.h>
+#include <mcabber/hooks.h>
+#include <mcabber/modules.h>
+#include <mcabber/xmpp.h>
+#include <mcabber/xmpp_helper.h>
+
+#include "pep.h"
+#include "activity.h"
+
+#include "config.h"
+
+//
+//  module description
+//
+
+void pep_activity_init   (void);
+void pep_activity_uninit (void);
+
+#define DESCRIPTION ( "PEP activity support" )
+static const gchar *deps[] = { "pep", NULL };
+
+static module_info_t info_activity_dev = {
+	.branch       = "dev",
+	.api          = 20,
+	.version      = PROJECT_VERSION,
+	.description  = DESCRIPTION,
+	.requires     = deps,
+	.init         = pep_activity_init,
+	.uninit       = pep_activity_uninit,
+	.next         = NULL,
+};
+
+static module_info_t info_activity_0_10_1 = {
+	.branch       = "0.10.1",
+	.api          = 1,
+	.version      = PROJECT_VERSION,
+	.description  = DESCRIPTION,
+	.requires     = deps,
+	.init         = pep_activity_init,
+	.uninit       = pep_activity_uninit,
+	.next         = &info_activity_dev,
+};
+
+module_info_t info_pep_activity = {
+	.branch       = "0.10.0",
+	.api          = 1,
+	.version      = PROJECT_VERSION,
+	.description  = DESCRIPTION,
+	.requires     = deps,
+	.init         = pep_activity_init,
+	.uninit       = pep_activity_uninit,
+	.next         = &info_activity_0_10_1,
+};
+
+//
+//  globals
+//
+
+static GQuark            activity_gerror_quark    = 0;
+static gboolean          activity_delayed         = FALSE;
+static guint             activity_hid_connect     = 0;
+static guint             activity_hid_disconnect  = 0;
+static guint             activity_hid_activityout = 0;
+static gchar            *activity_major           = NULL;
+static gchar            *activity_minor           = NULL;
+static gchar            *activity_text            = NULL;
+static LmMessageHandler *activity_reply_handler   = NULL;
+
+//
+//  code
+//
+
+static LmHandlerResult activity_publish_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata)
+{
+	switch (lm_message_get_sub_type (message)) {
+	case LM_MESSAGE_SUB_TYPE_RESULT:
+		break;
+	
+	case LM_MESSAGE_SUB_TYPE_ERROR:
+		
+		{
+			LmMessageNode *node   = lm_message_get_node (message);
+			const gchar   *type;
+			const gchar   *reason;
+
+			node = lm_message_node_get_child (node, "error");
+			type = lm_message_node_get_attribute (node, "type");
+			if (node->children)
+				reason = node->children->name;
+			else
+				reason = "undefined";
+
+			scr_log_print (LPRINT_LOGNORM, "activity: Publish failed: %s - %s", type, reason);
+		}
+
+		break;
+	
+	default:
+		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+		break;
+	}
+
+	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
+}
+
+void activity_publish (const gchar *major, const gchar *minor, const gchar *text)
+{
+	if (!xmpp_is_online ()) {
+		g_free (activity_major);
+		g_free (activity_minor);
+		g_free (activity_text);
+
+		activity_major   = g_strdup (major);
+		activity_minor   = g_strdup (minor);
+		activity_text    = g_strdup (text);
+		activity_delayed = TRUE;
+
+		return;
+	}
+
+	LmMessage     *request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
+	LmMessageNode *node    = lm_message_get_node (request);
+	lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
+
+	node = lm_message_node_add_child (node, "pubsub", NULL);
+	lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
+
+	node = lm_message_node_add_child (node, "publish", NULL);
+	lm_message_node_set_attribute (node, "node", NS_ACTIVITY);
+
+	node = lm_message_node_add_child (node, "item", NULL);
+
+	node = lm_message_node_add_child (node, "activity", NULL);
+	lm_message_node_set_attribute (node, "xmlns", NS_ACTIVITY);
+
+	if (major) {
+		LmMessageNode *mnode = lm_message_node_add_child (node, major, NULL);
+
+		if (minor)
+			lm_message_node_add_child (mnode, minor, NULL);
+	}
+
+	if (text)
+		lm_message_node_add_child (node, "text", text);
+
+	{ // send
+		GError *error = NULL;
+
+		lm_connection_send_with_reply (lconnection, request, activity_reply_handler, &error);
+
+		if (error) {
+			scr_log_print (LPRINT_DEBUG, "activity: Publish failed: %s.", error -> message);
+			g_error_free (error);
+		}
+	}
+
+	lm_message_unref (request);
+}
+
+gboolean activity_request ( const gchar *to, GError **err )
+{
+	LmMessage     *request;
+	LmMessageNode *node;
+
+	if ( ! xmpp_is_online () ) {
+		g_set_error ( err, activity_gerror_quark, ACTIVITY_ERROR_NOTCONNECTED, "You are not connected" );
+		return FALSE;
+	}
+
+	request = lm_message_new_with_sub_type (to, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
+	node = lm_message_get_node (request);
+	lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
+
+	node = lm_message_node_add_child (node, "pubsub", NULL);
+	lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
+
+	node = lm_message_node_add_child (node, "items", NULL);
+	lm_message_node_set_attribute (node, "node", NS_ACTIVITY);
+
+	{ // send, result will be handled by pep
+		GError *error = NULL;
+
+		lm_connection_send (lconnection, request, &error);
+
+		if (error) {
+			g_propagate_error ( err, error );
+			return FALSE;
+		}
+	}
+
+	lm_message_unref (request);
+
+	return TRUE;
+}
+
+static void activity_handler ( const gchar *from, const gchar *enode, LmMessageNode *n, const gchar *id, gpointer ignore )
+{
+	LmMessageNode *node;
+	hk_arg_t args[] = {
+		{ "major", NULL },
+		{ "minor", NULL },
+		{ "text",  NULL },
+		{ "from",  from },
+		{ NULL,    NULL },
+	};
+
+	for ( node = n -> children; node; node = node -> next ) {
+		const gchar *name = node -> name;
+
+		if ( ! strcmp ( name, "text" ) )
+			args[2].value = lm_message_node_get_value ( node );
+		else {
+			LmMessageNode *mnode = node -> children;
+
+			if ( mnode ) // XXX check for xmlns presence?
+				args[1].value = mnode -> name;
+
+			args[0].value = name;
+		}
+	}
+
+	hk_run_handlers ( HOOK_ACTIVITY_IN, args );
+}
+
+static guint activity_hch ( const gchar *hid, hk_arg_t *args, gpointer userdata )
+{
+	if (activity_delayed) {
+		gchar *tmp_major = activity_major;
+		gchar *tmp_minor = activity_minor;
+		gchar *tmp_text  = activity_text;
+
+		activity_delayed = FALSE;
+		activity_major = activity_minor = activity_text = NULL;
+
+		activity_publish (tmp_major, tmp_minor, tmp_text);
+
+		g_free (tmp_major);
+		g_free (tmp_minor);
+		g_free (tmp_text);
+	}
+
+	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+static guint activity_hdh ( const gchar *hid, hk_arg_t *args, gpointer userdata )
+{
+#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
+	if ( lconnection && activity_reply_handler )
+		lm_connection_unregister_reply_handler ( lconnection, activity_reply_handler );
+#endif
+
+	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+static guint activity_haoh ( const gchar *hid, hk_arg_t *args, gpointer userdata )
+{
+	const gchar *minor = NULL;
+	const gchar *major = NULL;
+	const gchar *text  = NULL;
+
+	{
+		hk_arg_t *arg;
+		for ( arg = args; arg -> name; arg ++ ) {
+			const gchar *value = arg -> value;
+			if ( value ) {
+				const gchar *name = arg -> name;
+				if ( ! strcmp ( name, "major" ) )
+					major = value;
+				else if ( ! strcmp ( name, "minor" ) )
+					minor = value;
+				else if ( !strcmp ( name, "text" ) )
+					text = value;
+			}
+		}
+	}
+
+	activity_publish ( major, minor, text );
+
+	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+void pep_activity_init (void)
+{
+	activity_gerror_quark = g_quark_from_string ( "pep-activity-gerror-quark" );
+
+	activity_reply_handler = lm_message_handler_new (activity_publish_reply_handler, NULL, NULL);
+
+	pep_register_xmlns_handler (NS_ACTIVITY, activity_handler, NULL, NULL);
+
+	activity_hid_connect     = hk_add_handler (activity_hch,  HOOK_POST_CONNECT, G_PRIORITY_DEFAULT, NULL);
+	activity_hid_disconnect  = hk_add_handler (activity_hdh,  HOOK_PRE_DISCONNECT, G_PRIORITY_DEFAULT, NULL);
+	activity_hid_activityout = hk_add_handler (activity_haoh, HOOK_ACTIVITY_OUT, G_PRIORITY_DEFAULT, NULL);
+
+	xmpp_add_feature ( NS_ACTIVITY        );
+	xmpp_add_feature ( NS_ACTIVITY_NOTIFY );
+}
+
+void pep_activity_uninit (void)
+{
+	xmpp_del_feature ( NS_ACTIVITY        );
+	xmpp_del_feature ( NS_ACTIVITY_NOTIFY );
+
+	hk_del_handler ( HOOK_POST_CONNECT,   activity_hid_connect );
+	hk_del_handler ( HOOK_PRE_DISCONNECT, activity_hid_disconnect );
+	hk_del_handler ( HOOK_ACTIVITY_OUT,   activity_hid_activityout );
+
+	pep_unregister_xmlns_handler ( NS_ACTIVITY );
+
+#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
+	if (lconnection)
+		lm_connection_unregister_reply_handler (lconnection, activity_reply_handler);
+#endif
+	lm_message_handler_invalidate (activity_reply_handler);
+	lm_message_handler_unref (activity_reply_handler);
+
+	g_free (activity_major);
+	g_free (activity_minor);
+	g_free (activity_text);
+}
+
+/* vim: se ts=4 sw=4: */