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