0
|
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 "xmpp.h" |
|
28 |
#include "hooks.h" |
|
29 |
#include "pep.h" |
|
30 |
|
|
31 |
static GSList *pep_xmlns_handlers = NULL; |
|
32 |
|
|
33 |
static LmMessageHandler *pep_message_handler = NULL; |
|
34 |
|
|
35 |
static pep_handler_t *pep_find_handler (const gchar *xmlns) |
|
36 |
{ |
|
37 |
GSList *hel; |
|
38 |
|
|
39 |
for (hel = pep_xmlns_handlers; hel; hel = hel->next) { |
|
40 |
pep_handler_t *handler = (pep_handler_t *) hel->data; |
1
|
41 |
|
0
|
42 |
if (!strcmp (handler->xmlns, xmlns)) |
|
43 |
return handler; |
|
44 |
} |
|
45 |
|
|
46 |
return NULL; |
|
47 |
} |
|
48 |
|
|
49 |
static LmHandlerResult pep_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata) |
|
50 |
{ // it is MESSAGE! |
|
51 |
LmMessageNode *m = lm_message_get_node (message); |
|
52 |
|
1
|
53 |
LmMessageNode *node = lm_message_node_get_child (m, "event"); |
|
54 |
if (!node) |
0
|
55 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
56 |
|
1
|
57 |
{ |
|
58 |
const gchar *xmlns = lm_message_node_get_attribute (node, "xmlns"); |
|
59 |
if (strcmp (xmlns, NS_PUBSUB_EVENT)) |
|
60 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
61 |
} |
0
|
62 |
|
1
|
63 |
node = lm_message_node_get_child (node, "items"); |
|
64 |
if (!node) |
0
|
65 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; // XXX |
|
66 |
|
1
|
67 |
{ |
|
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"); |
|
71 |
|
|
72 |
for (item = node->children; item; item = item->next) { |
|
73 |
const gchar *id = lm_message_node_get_attribute (item, "id"); |
|
74 |
LmMessageNode *n; |
0
|
75 |
|
1
|
76 |
for (n = item->children; n; n = n->next) { |
|
77 |
const gchar *xmlns = lm_message_node_get_attribute (n, "xmlns"); |
|
78 |
|
|
79 |
if (xmlns) { |
|
80 |
pep_handler_t *handler = pep_find_handler (xmlns); |
|
81 |
|
|
82 |
if (handler) |
|
83 |
handler->handler (from, inode, n, id, handler->data); |
|
84 |
} |
0
|
85 |
} |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
90 |
} |
|
91 |
|
|
92 |
void pep_register_xmlns_handler (const gchar *xmlns, void (*handler) (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer udata), gpointer udata, void (*data_destroy) (pep_handler_t *handler)) |
|
93 |
{ |
|
94 |
pep_handler_t *h = g_new (pep_handler_t, 1); |
|
95 |
|
|
96 |
h->xmlns = g_strdup (xmlns); |
|
97 |
h->handler = handler; |
|
98 |
h->data = udata; |
|
99 |
h->data_destroy = data_destroy; |
|
100 |
|
|
101 |
pep_xmlns_handlers = g_slist_append (pep_xmlns_handlers, h); |
|
102 |
} |
|
103 |
|
|
104 |
void pep_unregister_xmlns_handler (const gchar *xmlns) |
|
105 |
{ |
|
106 |
pep_handler_t *handler = pep_find_handler (xmlns); |
|
107 |
if (handler) { |
|
108 |
pep_xmlns_handlers = g_slist_remove (pep_xmlns_handlers, handler); |
|
109 |
|
|
110 |
if (handler->data_destroy) |
|
111 |
handler->data_destroy (handler); |
|
112 |
g_free (handler->xmlns); |
|
113 |
g_free (handler); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
static void pep_register_handlers (void) |
|
118 |
{ |
|
119 |
if (!pep_message_handler) |
|
120 |
pep_message_handler = lm_message_handler_new (pep_handler, NULL, NULL); |
1
|
121 |
else if (lconnection) // XXX |
|
122 |
lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE); |
0
|
123 |
if (lconnection) // XXX |
|
124 |
lm_connection_register_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_FIRST); |
|
125 |
} |
|
126 |
|
|
127 |
static void pep_unregister_handlers (void) |
|
128 |
{ |
|
129 |
if (pep_message_handler) |
1
|
130 |
lm_connection_unregister_message_handler (lconnection, pep_message_handler, LM_MESSAGE_TYPE_MESSAGE); |
0
|
131 |
} |
|
132 |
|
|
133 |
// release handlers before reconnect |
|
134 |
static void pep_hh (guint32 hid, hk_arg_t *args, gpointer userdata) |
|
135 |
{ |
|
136 |
hk_arg_t *arg = args; |
|
137 |
|
|
138 |
for (arg = args; arg->name; arg++) { |
|
139 |
if (!strcmp (arg->name, "name")) { |
|
140 |
if (!strcmp (arg->value, "hook-pre-disconnect")) |
|
141 |
pep_unregister_handlers (); |
|
142 |
else if (!strcmp (arg->value, "hook-post-connect")) |
|
143 |
pep_register_handlers (); |
|
144 |
return; |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
const gchar *g_module_check_init(GModule *module) |
|
150 |
{ |
|
151 |
// register hook handler |
|
152 |
hk_add_handler (pep_hh, HOOK_INTERNAL, NULL); |
|
153 |
|
|
154 |
pep_register_handlers (); |
|
155 |
|
|
156 |
return NULL; |
|
157 |
} |
|
158 |
|
|
159 |
void g_module_unload(GModule *module) |
|
160 |
{ |
|
161 |
// release handlers |
|
162 |
pep_unregister_handlers (); |
|
163 |
if (pep_message_handler) |
|
164 |
lm_message_handler_unref (pep_message_handler); |
|
165 |
pep_message_handler = NULL; |
|
166 |
|
|
167 |
// remove hook |
|
168 |
hk_del_handler (pep_hh, NULL); |
|
169 |
} |
|
170 |
|
|
171 |
/* vim: se ts=4: */ |