pep.c
changeset 1 906c2966a1c1
parent 0 1d7a0bc3e38b
child 2 d55fe04d7f35
equal deleted inserted replaced
0:1d7a0bc3e38b 1:906c2966a1c1
    23 #include <gmodule.h>
    23 #include <gmodule.h>
    24 #include <loudmouth/loudmouth.h>
    24 #include <loudmouth/loudmouth.h>
    25 #include <strings.h>
    25 #include <strings.h>
    26 
    26 
    27 #include "xmpp.h"
    27 #include "xmpp.h"
    28 #include "xmpp_defines.h"
       
    29 #include "hooks.h"
    28 #include "hooks.h"
    30 #include "pep.h"
    29 #include "pep.h"
    31 
    30 
    32 static GSList *pep_xmlns_handlers = NULL;
    31 static GSList *pep_xmlns_handlers = NULL;
    33 
    32 
    34 static LmMessageHandler *pep_message_handler = NULL;
    33 static LmMessageHandler *pep_message_handler = NULL;
    35 
    34 
    36 static GSList *pep_reply_handlers = NULL;
       
    37 
       
    38 static pep_handler_t *pep_find_handler (const gchar *xmlns)
    35 static pep_handler_t *pep_find_handler (const gchar *xmlns)
    39 {
    36 {
    40 	GSList *hel;
    37 	GSList *hel;
    41 
    38 
    42 	for (hel = pep_xmlns_handlers; hel; hel = hel->next) {
    39 	for (hel = pep_xmlns_handlers; hel; hel = hel->next) {
    43 		pep_handler_t *handler = (pep_handler_t *) hel->data;
    40 		pep_handler_t *handler = (pep_handler_t *) hel->data;
       
    41 
    44 		if (!strcmp (handler->xmlns, xmlns))
    42 		if (!strcmp (handler->xmlns, xmlns))
    45 			return handler;
    43 			return handler;
    46 	}
    44 	}
    47 
    45 
    48 	return NULL;
    46 	return NULL;
    50 
    48 
    51 static LmHandlerResult pep_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
    49 static LmHandlerResult pep_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata)
    52 { // it is MESSAGE!
    50 { // it is MESSAGE!
    53 	LmMessageNode *m = lm_message_get_node (message);
    51 	LmMessageNode *m = lm_message_get_node (message);
    54 
    52 
    55 	LmMessageNode *event = lm_message_node_get_child (m, "event");
    53 	LmMessageNode *node = lm_message_node_get_child (m, "event");
    56 	if (!event)
    54 	if (!node)
    57 		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
    55 		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
    58 	
    56 	
    59 	const gchar *xmlns = lm_message_node_get_attribute (event, "xmlns");
    57 	{
    60 	if (strcmp (xmlns, NS_PUBSUB_EVENT))
    58 		const gchar *xmlns = lm_message_node_get_attribute (node, "xmlns");
    61 		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
    59 		if (strcmp (xmlns, NS_PUBSUB_EVENT))
       
    60 			return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
    61 	}
    62 	
    62 	
    63 	LmMessageNode *items = lm_message_node_get_child (event, "items");
    63 	node = lm_message_node_get_child (node, "items");
    64 	if (!items)
    64 	if (!node)
    65 		return LM_HANDLER_RESULT_REMOVE_MESSAGE; // XXX
    65 		return LM_HANDLER_RESULT_REMOVE_MESSAGE; // XXX
    66 
    66 
    67 	const gchar *from = lm_message_node_get_attribute (m, "from");
    67 	{
    68 	const gchar *node = lm_message_node_get_attribute (items, "node");
    68 		LmMessageNode *item;
       
    69 		const gchar   *from  = lm_message_node_get_attribute (m, "from");
       
    70 		const gchar   *inode = lm_message_node_get_attribute (node, "node");
    69 
    71 
    70 	LmMessageNode *item;
    72 		for (item = node->children; item; item = item->next) {
    71 	for (item = items->children; item; item = item->next) {
    73 			const gchar   *id = lm_message_node_get_attribute (item, "id");
    72 		const gchar *id = lm_message_node_get_attribute (item, "id");
    74 			LmMessageNode *n;
    73 		LmMessageNode *n;
    75 
    74 		for (n = item->children; n; n = n->next) {
    76 			for (n = item->children; n; n = n->next) {
    75 			const gchar *xmlns = lm_message_node_get_attribute (n, "xmlns");
    77 				const gchar *xmlns = lm_message_node_get_attribute (n, "xmlns");
    76 			if (xmlns) {
    78 
    77 				pep_handler_t *handler = pep_find_handler (xmlns);
    79 				if (xmlns) {
    78 				if (handler)
    80 					pep_handler_t *handler = pep_find_handler (xmlns);
    79 					handler->handler (from, node, n, id, handler->data);
    81 
       
    82 					if (handler)
       
    83 						handler->handler (from, inode, n, id, handler->data);
       
    84 				}
    80 			}
    85 			}
    81 		}
    86 		}
    82 	}
    87 	}
    83 
    88 
    84 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
    89 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
   111 
   116 
   112 static void pep_register_handlers (void)
   117 static void pep_register_handlers (void)
   113 {
   118 {
   114 	if (!pep_message_handler)
   119 	if (!pep_message_handler)
   115 		pep_message_handler = lm_message_handler_new (pep_handler, NULL, NULL);
   120 		pep_message_handler = lm_message_handler_new (pep_handler, NULL, NULL);
       
   121 	else if (lconnection) // XXX
       
   122 		lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE);
   116 	if (lconnection) // XXX
   123 	if (lconnection) // XXX
   117 		lm_connection_register_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST);
   124 		lm_connection_register_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST);
   118 }
   125 }
   119 
   126 
   120 static void pep_unregister_handlers (void)
   127 static void pep_unregister_handlers (void)
   121 {
   128 {
   122 	GSList *hel;
       
   123 
       
   124 	if (lconnection) { // XXX more checks?
       
   125 		// let's hope, that after invalidation, lm will remove and free unreffed by us handler
       
   126 		for (hel = pep_reply_handlers; hel; hel = hel->next) {
       
   127 			LmMessageHandler *handler = (LmMessageHandler *) hel->data;
       
   128 			lm_message_handler_invalidate (handler);
       
   129 		}
       
   130 	}
       
   131 
       
   132 	g_slist_free (pep_reply_handlers);
       
   133 	pep_reply_handlers = NULL;
       
   134 
       
   135 	if (pep_message_handler)
   129 	if (pep_message_handler)
   136 		lm_connection_unregister_handler (lconnection, pep_message_handler);
   130 		lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE);
   137 }
   131 }
   138 
   132 
   139 // release handlers before reconnect
   133 // release handlers before reconnect
   140 static void pep_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
   134 static void pep_hh (guint32 hid, hk_arg_t *args, gpointer userdata)
   141 {
   135 {
   152 	}
   146 	}
   153 }
   147 }
   154 
   148 
   155 const gchar *g_module_check_init(GModule *module)
   149 const gchar *g_module_check_init(GModule *module)
   156 {
   150 {
   157 	pep_reply_handlers = NULL;
       
   158 
       
   159 	// register hook handler
   151 	// register hook handler
   160 	hk_add_handler (pep_hh, HOOK_INTERNAL, NULL);
   152 	hk_add_handler (pep_hh, HOOK_INTERNAL, NULL);
   161 
   153 
   162 	pep_register_handlers ();
   154 	pep_register_handlers ();
   163 
   155