|
1 /* |
|
2 * pep_activity.c -- Pep activity events |
|
3 * |
|
4 * Copyright (C) 2009-2012 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 <loudmouth/loudmouth.h> |
|
24 #include <string.h> |
|
25 |
|
26 #include <mcabber/utils.h> |
|
27 #include <mcabber/logprint.h> |
|
28 #include <mcabber/hooks.h> |
|
29 #include <mcabber/modules.h> |
|
30 #include <mcabber/xmpp.h> |
|
31 #include <mcabber/xmpp_helper.h> |
|
32 |
|
33 #include "pep.h" |
|
34 #include "activity.h" |
|
35 |
|
36 #include "config.h" |
|
37 |
|
38 // |
|
39 // module description |
|
40 // |
|
41 |
|
42 void pep_activity_init (void); |
|
43 void pep_activity_uninit (void); |
|
44 |
|
45 #define DESCRIPTION ( "PEP activity support" ) |
|
46 static const gchar *deps[] = { "pep", NULL }; |
|
47 |
|
48 static module_info_t info_activity_dev = { |
|
49 .branch = "dev", |
|
50 .api = 20, |
|
51 .version = PROJECT_VERSION, |
|
52 .description = DESCRIPTION, |
|
53 .requires = deps, |
|
54 .init = pep_activity_init, |
|
55 .uninit = pep_activity_uninit, |
|
56 .next = NULL, |
|
57 }; |
|
58 |
|
59 static module_info_t info_activity_0_10_1 = { |
|
60 .branch = "0.10.1", |
|
61 .api = 1, |
|
62 .version = PROJECT_VERSION, |
|
63 .description = DESCRIPTION, |
|
64 .requires = deps, |
|
65 .init = pep_activity_init, |
|
66 .uninit = pep_activity_uninit, |
|
67 .next = &info_activity_dev, |
|
68 }; |
|
69 |
|
70 module_info_t info_pep_activity = { |
|
71 .branch = "0.10.0", |
|
72 .api = 1, |
|
73 .version = PROJECT_VERSION, |
|
74 .description = DESCRIPTION, |
|
75 .requires = deps, |
|
76 .init = pep_activity_init, |
|
77 .uninit = pep_activity_uninit, |
|
78 .next = &info_activity_0_10_1, |
|
79 }; |
|
80 |
|
81 // |
|
82 // globals |
|
83 // |
|
84 |
|
85 static GQuark activity_gerror_quark = 0; |
|
86 static gboolean activity_delayed = FALSE; |
|
87 static guint activity_hid_connect = 0; |
|
88 static guint activity_hid_disconnect = 0; |
|
89 static guint activity_hid_activityout = 0; |
|
90 static gchar *activity_major = NULL; |
|
91 static gchar *activity_minor = NULL; |
|
92 static gchar *activity_text = NULL; |
|
93 static LmMessageHandler *activity_reply_handler = NULL; |
|
94 |
|
95 // |
|
96 // code |
|
97 // |
|
98 |
|
99 static LmHandlerResult activity_publish_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata) |
|
100 { |
|
101 switch (lm_message_get_sub_type (message)) { |
|
102 case LM_MESSAGE_SUB_TYPE_RESULT: |
|
103 break; |
|
104 |
|
105 case LM_MESSAGE_SUB_TYPE_ERROR: |
|
106 |
|
107 { |
|
108 LmMessageNode *node = lm_message_get_node (message); |
|
109 const gchar *type; |
|
110 const gchar *reason; |
|
111 |
|
112 node = lm_message_node_get_child (node, "error"); |
|
113 type = lm_message_node_get_attribute (node, "type"); |
|
114 if (node->children) |
|
115 reason = node->children->name; |
|
116 else |
|
117 reason = "undefined"; |
|
118 |
|
119 scr_log_print (LPRINT_LOGNORM, "activity: Publish failed: %s - %s", type, reason); |
|
120 } |
|
121 |
|
122 break; |
|
123 |
|
124 default: |
|
125 return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
126 break; |
|
127 } |
|
128 |
|
129 return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
130 } |
|
131 |
|
132 void activity_publish (const gchar *major, const gchar *minor, const gchar *text) |
|
133 { |
|
134 if (!xmpp_is_online ()) { |
|
135 g_free (activity_major); |
|
136 g_free (activity_minor); |
|
137 g_free (activity_text); |
|
138 |
|
139 activity_major = g_strdup (major); |
|
140 activity_minor = g_strdup (minor); |
|
141 activity_text = g_strdup (text); |
|
142 activity_delayed = TRUE; |
|
143 |
|
144 return; |
|
145 } |
|
146 |
|
147 LmMessage *request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET); |
|
148 LmMessageNode *node = lm_message_get_node (request); |
|
149 lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection)); |
|
150 |
|
151 node = lm_message_node_add_child (node, "pubsub", NULL); |
|
152 lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB); |
|
153 |
|
154 node = lm_message_node_add_child (node, "publish", NULL); |
|
155 lm_message_node_set_attribute (node, "node", NS_ACTIVITY); |
|
156 |
|
157 node = lm_message_node_add_child (node, "item", NULL); |
|
158 |
|
159 node = lm_message_node_add_child (node, "activity", NULL); |
|
160 lm_message_node_set_attribute (node, "xmlns", NS_ACTIVITY); |
|
161 |
|
162 if (major) { |
|
163 LmMessageNode *mnode = lm_message_node_add_child (node, major, NULL); |
|
164 |
|
165 if (minor) |
|
166 lm_message_node_add_child (mnode, minor, NULL); |
|
167 } |
|
168 |
|
169 if (text) |
|
170 lm_message_node_add_child (node, "text", text); |
|
171 |
|
172 { // send |
|
173 GError *error = NULL; |
|
174 |
|
175 lm_connection_send_with_reply (lconnection, request, activity_reply_handler, &error); |
|
176 |
|
177 if (error) { |
|
178 scr_log_print (LPRINT_DEBUG, "activity: Publish failed: %s.", error -> message); |
|
179 g_error_free (error); |
|
180 } |
|
181 } |
|
182 |
|
183 lm_message_unref (request); |
|
184 } |
|
185 |
|
186 gboolean activity_request ( const gchar *to, GError **err ) |
|
187 { |
|
188 LmMessage *request; |
|
189 LmMessageNode *node; |
|
190 |
|
191 if ( ! xmpp_is_online () ) { |
|
192 g_set_error ( err, activity_gerror_quark, ACTIVITY_ERROR_NOTCONNECTED, "You are not connected" ); |
|
193 return FALSE; |
|
194 } |
|
195 |
|
196 request = lm_message_new_with_sub_type (to, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET); |
|
197 node = lm_message_get_node (request); |
|
198 lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection)); |
|
199 |
|
200 node = lm_message_node_add_child (node, "pubsub", NULL); |
|
201 lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB); |
|
202 |
|
203 node = lm_message_node_add_child (node, "items", NULL); |
|
204 lm_message_node_set_attribute (node, "node", NS_ACTIVITY); |
|
205 |
|
206 { // send, result will be handled by pep |
|
207 GError *error = NULL; |
|
208 |
|
209 lm_connection_send (lconnection, request, &error); |
|
210 |
|
211 if (error) { |
|
212 g_propagate_error ( err, error ); |
|
213 return FALSE; |
|
214 } |
|
215 } |
|
216 |
|
217 lm_message_unref (request); |
|
218 |
|
219 return TRUE; |
|
220 } |
|
221 |
|
222 static void activity_handler ( const gchar *from, const gchar *enode, LmMessageNode *n, const gchar *id, gpointer ignore ) |
|
223 { |
|
224 LmMessageNode *node; |
|
225 hk_arg_t args[] = { |
|
226 { "major", NULL }, |
|
227 { "minor", NULL }, |
|
228 { "text", NULL }, |
|
229 { "from", from }, |
|
230 { NULL, NULL }, |
|
231 }; |
|
232 |
|
233 for ( node = n -> children; node; node = node -> next ) { |
|
234 const gchar *name = node -> name; |
|
235 |
|
236 if ( ! strcmp ( name, "text" ) ) |
|
237 args[2].value = lm_message_node_get_value ( node ); |
|
238 else { |
|
239 LmMessageNode *mnode = node -> children; |
|
240 |
|
241 if ( mnode ) // XXX check for xmlns presence? |
|
242 args[1].value = mnode -> name; |
|
243 |
|
244 args[0].value = name; |
|
245 } |
|
246 } |
|
247 |
|
248 hk_run_handlers ( HOOK_ACTIVITY_IN, args ); |
|
249 } |
|
250 |
|
251 static guint activity_hch ( const gchar *hid, hk_arg_t *args, gpointer userdata ) |
|
252 { |
|
253 if (activity_delayed) { |
|
254 gchar *tmp_major = activity_major; |
|
255 gchar *tmp_minor = activity_minor; |
|
256 gchar *tmp_text = activity_text; |
|
257 |
|
258 activity_delayed = FALSE; |
|
259 activity_major = activity_minor = activity_text = NULL; |
|
260 |
|
261 activity_publish (tmp_major, tmp_minor, tmp_text); |
|
262 |
|
263 g_free (tmp_major); |
|
264 g_free (tmp_minor); |
|
265 g_free (tmp_text); |
|
266 } |
|
267 |
|
268 return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
269 } |
|
270 |
|
271 static guint activity_hdh ( const gchar *hid, hk_arg_t *args, gpointer userdata ) |
|
272 { |
|
273 #ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER |
|
274 if ( lconnection && activity_reply_handler ) |
|
275 lm_connection_unregister_reply_handler ( lconnection, activity_reply_handler ); |
|
276 #endif |
|
277 |
|
278 return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
279 } |
|
280 |
|
281 static guint activity_haoh ( const gchar *hid, hk_arg_t *args, gpointer userdata ) |
|
282 { |
|
283 const gchar *minor = NULL; |
|
284 const gchar *major = NULL; |
|
285 const gchar *text = NULL; |
|
286 |
|
287 { |
|
288 hk_arg_t *arg; |
|
289 for ( arg = args; arg -> name; arg ++ ) { |
|
290 const gchar *value = arg -> value; |
|
291 if ( value ) { |
|
292 const gchar *name = arg -> name; |
|
293 if ( ! strcmp ( name, "major" ) ) |
|
294 major = value; |
|
295 else if ( ! strcmp ( name, "minor" ) ) |
|
296 minor = value; |
|
297 else if ( !strcmp ( name, "text" ) ) |
|
298 text = value; |
|
299 } |
|
300 } |
|
301 } |
|
302 |
|
303 activity_publish ( major, minor, text ); |
|
304 |
|
305 return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
306 } |
|
307 |
|
308 void pep_activity_init (void) |
|
309 { |
|
310 activity_gerror_quark = g_quark_from_string ( "pep-activity-gerror-quark" ); |
|
311 |
|
312 activity_reply_handler = lm_message_handler_new (activity_publish_reply_handler, NULL, NULL); |
|
313 |
|
314 pep_register_xmlns_handler (NS_ACTIVITY, activity_handler, NULL, NULL); |
|
315 |
|
316 activity_hid_connect = hk_add_handler (activity_hch, HOOK_POST_CONNECT, G_PRIORITY_DEFAULT, NULL); |
|
317 activity_hid_disconnect = hk_add_handler (activity_hdh, HOOK_PRE_DISCONNECT, G_PRIORITY_DEFAULT, NULL); |
|
318 activity_hid_activityout = hk_add_handler (activity_haoh, HOOK_ACTIVITY_OUT, G_PRIORITY_DEFAULT, NULL); |
|
319 |
|
320 xmpp_add_feature ( NS_ACTIVITY ); |
|
321 xmpp_add_feature ( NS_ACTIVITY_NOTIFY ); |
|
322 } |
|
323 |
|
324 void pep_activity_uninit (void) |
|
325 { |
|
326 xmpp_del_feature ( NS_ACTIVITY ); |
|
327 xmpp_del_feature ( NS_ACTIVITY_NOTIFY ); |
|
328 |
|
329 hk_del_handler ( HOOK_POST_CONNECT, activity_hid_connect ); |
|
330 hk_del_handler ( HOOK_PRE_DISCONNECT, activity_hid_disconnect ); |
|
331 hk_del_handler ( HOOK_ACTIVITY_OUT, activity_hid_activityout ); |
|
332 |
|
333 pep_unregister_xmlns_handler ( NS_ACTIVITY ); |
|
334 |
|
335 #ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER |
|
336 if (lconnection) |
|
337 lm_connection_unregister_reply_handler (lconnection, activity_reply_handler); |
|
338 #endif |
|
339 lm_message_handler_invalidate (activity_reply_handler); |
|
340 lm_message_handler_unref (activity_reply_handler); |
|
341 |
|
342 g_free (activity_major); |
|
343 g_free (activity_minor); |
|
344 g_free (activity_text); |
|
345 } |
|
346 |
|
347 /* vim: se ts=4 sw=4: */ |