disco.c
changeset 0 1f40c28a4cc8
child 1 434f4b4ff37b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/disco.c	Sat Oct 31 01:43:26 2009 +0200
@@ -0,0 +1,203 @@
+/*
+ * disco.c         -- Service discovery requests
+ *
+ * Copyrigth (C) 2009      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 <gmodule.h>
+#include <loudmouth/loudmouth.h>
+#include <strings.h>
+
+#include "commands.h"
+#include "logprint.h"
+#include "utils.h"
+#include "xmpp.h"
+#include "compl.h"
+
+#define DISCO_INFO_XMLNS  ( "http:/" "/jabber.org/protocol/disco#info"  )
+#define DISCO_ITEMS_XMLNS ( "http:/" "/jabber.org/protocol/disco#items" )
+
+// TODO merge
+static LmHandlerResult disco_info_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer ignore)
+{
+	switch (lm_message_get_sub_type (message)) {
+	case LM_MESSAGE_SUB_TYPE_RESULT:
+
+		{
+			LmMessageNode *node = lm_message_get_node (message);
+			const gchar *from = lm_message_node_get_attribute (node, "from");
+			const gchar *rnode;
+			node = lm_message_node_get_child (node, "query");
+			// TODO check xmlns
+			if (!node)
+				break;
+			rnode = lm_message_node_get_attribute (node, "node");
+			// TODO print to buddy buffer
+			scr_LogPrint (LPRINT_NORMAL, "Service discovery info results for %s (%s):", from, rnode ? rnode : "main");
+			for (node = node->children; node; node = node->next) {
+				if (!strcasecmp (node->name, "identity")) {
+					const gchar *category = lm_message_node_get_attribute (node, "category");
+					const gchar *type     = lm_message_node_get_attribute (node, "type");
+					const gchar *name     = lm_message_node_get_attribute (node, "name");
+					scr_LogPrint (LPRINT_NORMAL, "Identity: [%s (%s)] %s", category ? category : "none", type ? type : "none", name ? name : "");
+				} else if (!strcasecmp (node->name, "feature")) {
+					const gchar *var = lm_message_node_get_attribute (node, "var");
+					scr_LogPrint (LPRINT_NORMAL, "Feature: [%s]", var ? var : "unset");
+				}
+			}
+		}
+
+		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_LogPrint (LPRINT_LOGNORM, "Service info discovery failed: %s - %s", type, reason);
+		}
+
+		break;
+
+	default:
+		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+		break;
+	}
+
+	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
+}
+
+static LmHandlerResult disco_items_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer ignore)
+{
+	switch (lm_message_get_sub_type (message)) {
+	case LM_MESSAGE_SUB_TYPE_RESULT:
+
+		{
+			LmMessageNode *node = lm_message_get_node (message);
+			const gchar *from = lm_message_node_get_attribute (node, "from");
+			const gchar *rnode;
+			node = lm_message_node_get_child (node, "query");
+			// TODO check xmlns
+			if (!node)
+				break;
+			rnode = lm_message_node_get_attribute (node, "node");
+			// TODO print to buddy buffer
+			scr_LogPrint (LPRINT_NORMAL, "Service discovery items results for %s (%s):", from, rnode ? rnode : "main");
+			for (node = node->children; node; node = node->next) {
+				const gchar *name = lm_message_node_get_attribute (node, "name");
+				const gchar *jid  = lm_message_node_get_attribute (node, "jid");
+				scr_LogPrint (LPRINT_NORMAL, "  [%s] %s", jid ? jid : "none", name ? name : "");
+			}
+		}
+
+		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_LogPrint (LPRINT_LOGNORM, "Service items discovery failed: %s - %s", type, reason);
+		}
+
+		break;
+
+	default:
+		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+		break;
+	}
+
+	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
+}
+
+static void do_disco (char *arg)
+{
+	char **args = split_arg (arg, 3, 0);
+	int    info = -1;
+
+	if (!args[0])
+		scr_LogPrint (LPRINT_NORMAL, "Subcommand not specified!");
+	else if (!strcmp (args[0], "info"))
+		info = 1;
+	else if (!strcmp (args[0], "items"))
+		info = 0;
+	else
+		scr_LogPrint (LPRINT_NORMAL, "Unknown subcomand.");
+
+	if (info != -1) {
+		LmMessageHandler *handler;
+		LmMessage        *request;
+		LmMessageNode    *node;
+		const char       *to      = NULL;
+		char             *dnode   = NULL;
+		
+		if (args[1]) {
+			if (strcmp (args[1], "."))
+				to = args[1];
+
+			if (args[2])
+				dnode = args[2];
+		}
+
+		if (!to)
+			to = CURRENT_JID; // TODO: send to all resources?
+
+		handler = lm_message_handler_new (info ? disco_info_handler : disco_items_handler, NULL, NULL);
+
+		request = lm_message_new_with_sub_type (to, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
+		node    = lm_message_get_node (request);
+		node    = lm_message_node_add_child (node, "query", NULL);
+		lm_message_node_set_attribute (node, "xmlns", info ? DISCO_INFO_XMLNS : DISCO_ITEMS_XMLNS);
+		if (dnode)
+			lm_message_node_set_attribute (node, "node", dnode);
+
+		lm_connection_send_with_reply (lconnection, request, handler, NULL);
+	}
+}
+
+const gchar *g_module_check_init(GModule *module)
+{
+	// TODO: completion
+	cmd_add ("disco", "", 0, COMPL_JID, do_disco, NULL);
+	return NULL;
+}
+
+void g_module_unload(GModule *module)
+{
+	cmd_del ("disco");
+}
+