disco.c
changeset 0 1f40c28a4cc8
child 1 434f4b4ff37b
equal deleted inserted replaced
-1:000000000000 0:1f40c28a4cc8
       
     1 /*
       
     2  * disco.c         -- Service discovery requests
       
     3  *
       
     4  * Copyrigth (C) 2009      Myhailo Danylenko <isbear@ukrpost.net>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; either version 2 of the License, or (at
       
     9  * your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but
       
    12  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this program; if not, write to the Free Software
       
    18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
       
    19  * USA
       
    20  */
       
    21 
       
    22 #include <glib.h>
       
    23 #include <gmodule.h>
       
    24 #include <loudmouth/loudmouth.h>
       
    25 #include <strings.h>
       
    26 
       
    27 #include "commands.h"
       
    28 #include "logprint.h"
       
    29 #include "utils.h"
       
    30 #include "xmpp.h"
       
    31 #include "compl.h"
       
    32 
       
    33 #define DISCO_INFO_XMLNS  ( "http:/" "/jabber.org/protocol/disco#info"  )
       
    34 #define DISCO_ITEMS_XMLNS ( "http:/" "/jabber.org/protocol/disco#items" )
       
    35 
       
    36 // TODO merge
       
    37 static LmHandlerResult disco_info_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer ignore)
       
    38 {
       
    39 	switch (lm_message_get_sub_type (message)) {
       
    40 	case LM_MESSAGE_SUB_TYPE_RESULT:
       
    41 
       
    42 		{
       
    43 			LmMessageNode *node = lm_message_get_node (message);
       
    44 			const gchar *from = lm_message_node_get_attribute (node, "from");
       
    45 			const gchar *rnode;
       
    46 			node = lm_message_node_get_child (node, "query");
       
    47 			// TODO check xmlns
       
    48 			if (!node)
       
    49 				break;
       
    50 			rnode = lm_message_node_get_attribute (node, "node");
       
    51 			// TODO print to buddy buffer
       
    52 			scr_LogPrint (LPRINT_NORMAL, "Service discovery info results for %s (%s):", from, rnode ? rnode : "main");
       
    53 			for (node = node->children; node; node = node->next) {
       
    54 				if (!strcasecmp (node->name, "identity")) {
       
    55 					const gchar *category = lm_message_node_get_attribute (node, "category");
       
    56 					const gchar *type     = lm_message_node_get_attribute (node, "type");
       
    57 					const gchar *name     = lm_message_node_get_attribute (node, "name");
       
    58 					scr_LogPrint (LPRINT_NORMAL, "Identity: [%s (%s)] %s", category ? category : "none", type ? type : "none", name ? name : "");
       
    59 				} else if (!strcasecmp (node->name, "feature")) {
       
    60 					const gchar *var = lm_message_node_get_attribute (node, "var");
       
    61 					scr_LogPrint (LPRINT_NORMAL, "Feature: [%s]", var ? var : "unset");
       
    62 				}
       
    63 			}
       
    64 		}
       
    65 
       
    66 		break;
       
    67 
       
    68 	case LM_MESSAGE_SUB_TYPE_ERROR:
       
    69 
       
    70 		{
       
    71 			LmMessageNode   *node   = lm_message_get_node (message);
       
    72 			const gchar     *type;
       
    73 			const gchar     *reason;
       
    74 
       
    75 			node = lm_message_node_get_child (node, "error");
       
    76 			type = lm_message_node_get_attribute (node, "type");
       
    77 			if (node->children)
       
    78 				reason = node->children->name;
       
    79 			else
       
    80 				reason = "undefined";
       
    81 
       
    82 			scr_LogPrint (LPRINT_LOGNORM, "Service info discovery failed: %s - %s", type, reason);
       
    83 		}
       
    84 
       
    85 		break;
       
    86 
       
    87 	default:
       
    88 		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    89 		break;
       
    90 	}
       
    91 
       
    92 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
       
    93 }
       
    94 
       
    95 static LmHandlerResult disco_items_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer ignore)
       
    96 {
       
    97 	switch (lm_message_get_sub_type (message)) {
       
    98 	case LM_MESSAGE_SUB_TYPE_RESULT:
       
    99 
       
   100 		{
       
   101 			LmMessageNode *node = lm_message_get_node (message);
       
   102 			const gchar *from = lm_message_node_get_attribute (node, "from");
       
   103 			const gchar *rnode;
       
   104 			node = lm_message_node_get_child (node, "query");
       
   105 			// TODO check xmlns
       
   106 			if (!node)
       
   107 				break;
       
   108 			rnode = lm_message_node_get_attribute (node, "node");
       
   109 			// TODO print to buddy buffer
       
   110 			scr_LogPrint (LPRINT_NORMAL, "Service discovery items results for %s (%s):", from, rnode ? rnode : "main");
       
   111 			for (node = node->children; node; node = node->next) {
       
   112 				const gchar *name = lm_message_node_get_attribute (node, "name");
       
   113 				const gchar *jid  = lm_message_node_get_attribute (node, "jid");
       
   114 				scr_LogPrint (LPRINT_NORMAL, "  [%s] %s", jid ? jid : "none", name ? name : "");
       
   115 			}
       
   116 		}
       
   117 
       
   118 		break;
       
   119 
       
   120 	case LM_MESSAGE_SUB_TYPE_ERROR:
       
   121 
       
   122 		{
       
   123 			LmMessageNode   *node   = lm_message_get_node (message);
       
   124 			const gchar     *type;
       
   125 			const gchar     *reason;
       
   126 
       
   127 			node = lm_message_node_get_child (node, "error");
       
   128 			type = lm_message_node_get_attribute (node, "type");
       
   129 			if (node->children)
       
   130 				reason = node->children->name;
       
   131 			else
       
   132 				reason = "undefined";
       
   133 
       
   134 			scr_LogPrint (LPRINT_LOGNORM, "Service items discovery failed: %s - %s", type, reason);
       
   135 		}
       
   136 
       
   137 		break;
       
   138 
       
   139 	default:
       
   140 		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   141 		break;
       
   142 	}
       
   143 
       
   144 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
       
   145 }
       
   146 
       
   147 static void do_disco (char *arg)
       
   148 {
       
   149 	char **args = split_arg (arg, 3, 0);
       
   150 	int    info = -1;
       
   151 
       
   152 	if (!args[0])
       
   153 		scr_LogPrint (LPRINT_NORMAL, "Subcommand not specified!");
       
   154 	else if (!strcmp (args[0], "info"))
       
   155 		info = 1;
       
   156 	else if (!strcmp (args[0], "items"))
       
   157 		info = 0;
       
   158 	else
       
   159 		scr_LogPrint (LPRINT_NORMAL, "Unknown subcomand.");
       
   160 
       
   161 	if (info != -1) {
       
   162 		LmMessageHandler *handler;
       
   163 		LmMessage        *request;
       
   164 		LmMessageNode    *node;
       
   165 		const char       *to      = NULL;
       
   166 		char             *dnode   = NULL;
       
   167 		
       
   168 		if (args[1]) {
       
   169 			if (strcmp (args[1], "."))
       
   170 				to = args[1];
       
   171 
       
   172 			if (args[2])
       
   173 				dnode = args[2];
       
   174 		}
       
   175 
       
   176 		if (!to)
       
   177 			to = CURRENT_JID; // TODO: send to all resources?
       
   178 
       
   179 		handler = lm_message_handler_new (info ? disco_info_handler : disco_items_handler, NULL, NULL);
       
   180 
       
   181 		request = lm_message_new_with_sub_type (to, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
       
   182 		node    = lm_message_get_node (request);
       
   183 		node    = lm_message_node_add_child (node, "query", NULL);
       
   184 		lm_message_node_set_attribute (node, "xmlns", info ? DISCO_INFO_XMLNS : DISCO_ITEMS_XMLNS);
       
   185 		if (dnode)
       
   186 			lm_message_node_set_attribute (node, "node", dnode);
       
   187 
       
   188 		lm_connection_send_with_reply (lconnection, request, handler, NULL);
       
   189 	}
       
   190 }
       
   191 
       
   192 const gchar *g_module_check_init(GModule *module)
       
   193 {
       
   194 	// TODO: completion
       
   195 	cmd_add ("disco", "", 0, COMPL_JID, do_disco, NULL);
       
   196 	return NULL;
       
   197 }
       
   198 
       
   199 void g_module_unload(GModule *module)
       
   200 {
       
   201 	cmd_del ("disco");
       
   202 }
       
   203