/*
* pep_mood.c -- Pep mood events
*
* Copyright (C) 2009-2012 Myhailo Danylenko <isbear@ukrpost.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <glib.h>
#include <loudmouth/loudmouth.h>
#include <string.h>
#include <mcabber/utils.h>
#include <mcabber/xmpp.h>
#include <mcabber/xmpp_helper.h>
#include <mcabber/logprint.h>
#include <mcabber/hooks.h>
#include <mcabber/modules.h>
#include "pep.h"
#include "pep_mood.h"
#include "config.h"
//
// module description
//
void pep_mood_init (void);
void pep_mood_uninit (void);
#define DESCRIPTION ( PEP_MOOD_DESCRIPTION )
static const gchar *deps[] = { "pep", NULL };
module_info_t info_pep_mood = {
.branch = MCABBER_BRANCH,
.api = MCABBER_API_VERSION,
.version = PROJECT_VERSION,
.description = DESCRIPTION,
.requires = deps,
.init = pep_mood_init,
.uninit = pep_mood_uninit,
.next = NULL,
};
//
// globals
//
static GQuark mood_gerror_quark = 0;
static guint mood_hid_connect = 0;
static guint mood_hid_disconnect = 0;
static guint mood_hid_moodout = 0;
static LmMessageHandler *mood_reply_handler = NULL;
static gboolean publish_delayed = FALSE;
static gchar *delayed_mood = NULL;
static gchar *delayed_text = NULL;
//
// code
//
static LmHandlerResult mood_publish_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata)
{
switch (lm_message_get_sub_type (message)) {
case LM_MESSAGE_SUB_TYPE_RESULT:
break;
case LM_MESSAGE_SUB_TYPE_ERROR:
{
LmMessageNode *node = lm_message_get_node (message);
const gchar *type;
const gchar *reason;
node = lm_message_node_get_child (node, "error");
type = lm_message_node_get_attribute (node, "type");
if (node->children)
reason = node->children->name;
else
reason = "undefined";
scr_log_print (LPRINT_LOGNORM, "mood: Publish failed: %s - %s", type, reason);
}
break;
default:
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
break;
}
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
static void mood_publish (const gchar *mood, const gchar *text)
{
if (!xmpp_is_online ()) {
scr_log_print (LPRINT_DEBUG, "mood: Delaying publish until online.");
g_free (delayed_mood);
g_free (delayed_text);
delayed_mood = g_strdup (mood);
delayed_text = g_strdup (text);
publish_delayed = TRUE;
return;
}
LmMessage *request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
LmMessageNode *node = lm_message_get_node (request);
lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
node = lm_message_node_add_child (node, "pubsub", NULL);
lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
node = lm_message_node_add_child (node, "publish", NULL);
lm_message_node_set_attribute (node, "node", NS_MOOD);
node = lm_message_node_add_child (node, "item", NULL);
node = lm_message_node_add_child (node, "mood", NULL);
lm_message_node_set_attribute (node, "xmlns", NS_MOOD);
if (mood)
lm_message_node_add_child (node, mood, NULL);
if (text)
lm_message_node_add_child (node, "text", text);
{ // send
GError *error = NULL;
lm_connection_send_with_reply (lconnection, request, mood_reply_handler, &error);
if (error) {
scr_log_print (LPRINT_DEBUG, "mood: Publish sending error: %s.", error -> message);
g_error_free (error);
}
}
lm_message_unref (request);
}
gboolean pep_mood_request ( const gchar *to, GError **err )
{
LmMessage *request;
LmMessageNode *node;
if (!xmpp_is_online ()) {
g_set_error ( err, mood_gerror_quark, MOOD_ERROR_NOTCONNECTED, "You are not connected" );
return FALSE;
}
request = lm_message_new_with_sub_type (to, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
node = lm_message_get_node (request);
lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
node = lm_message_node_add_child (node, "pubsub", NULL);
lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
node = lm_message_node_add_child (node, "items", NULL);
lm_message_node_set_attribute (node, "node", NS_MOOD);
{ // send, result will be handled by pep
GError *error = NULL;
lm_connection_send (lconnection, request, &error);
if (error) {
g_propagate_error ( err, error );
return FALSE;
}
}
lm_message_unref (request);
return TRUE;
}
static void mood_handler ( const gchar *from, const gchar *enode, LmMessageNode *n, const gchar *id, gpointer ignore )
{
LmMessageNode *node;
hk_arg_t args[] = {
{ "mood", NULL },
{ "text", NULL },
{ "from", from },
{ NULL, NULL },
};
for ( node = n -> children; node; node = node -> next ) {
const gchar *name = node -> name;
if ( ! strcmp ( name, "text" ) )
args[1].value = lm_message_node_get_value ( node );
else
args[0].value = name;
}
hk_run_handlers ( HOOK_MOOD_IN, args );
}
static guint mood_hch (const gchar *hid, hk_arg_t *args, gpointer userdata)
{
if (publish_delayed) {
char *tmp_mood = delayed_mood;
char *tmp_text = delayed_text;
scr_log_print (LPRINT_DEBUG, "mood: Publishing delayed data.");
delayed_mood = delayed_text = NULL;
publish_delayed = FALSE;
mood_publish (tmp_mood, tmp_text);
g_free (tmp_mood);
g_free (tmp_text);
}
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static guint mood_hdh (const gchar *hid, hk_arg_t *args, gpointer userdata)
{
#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
if (lconnection && mood_reply_handler)
lm_connection_unregister_reply_handler (lconnection, mood_reply_handler);
#endif
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static guint mood_hmoh ( const gchar *hid, hk_arg_t *args, gpointer userdata )
{
const gchar *mood = NULL;
const gchar *text = NULL;
{
hk_arg_t *arg;
for ( arg = args; arg -> name; arg ++ ) {
const gchar *value = arg -> value;
if ( value ) {
const gchar *name = arg -> name;
if ( ! strcmp ( name, "mood" ) )
mood = value;
else if ( ! strcmp ( name, "text" ) )
text = value;
}
}
}
mood_publish ( mood, text );
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
void pep_mood_init (void)
{
mood_gerror_quark = g_quark_from_string ( "pep-mood-gerror-quark" );
mood_reply_handler = lm_message_handler_new (mood_publish_reply_handler, NULL, NULL);
pep_register_xmlns_handler (NS_MOOD, mood_handler, NULL, NULL);
mood_hid_connect = hk_add_handler ( mood_hch, HOOK_POST_CONNECT, G_PRIORITY_DEFAULT, NULL );
mood_hid_disconnect = hk_add_handler ( mood_hdh, HOOK_PRE_DISCONNECT, G_PRIORITY_DEFAULT, NULL );
mood_hid_moodout = hk_add_handler ( mood_hmoh, HOOK_MOOD_OUT, G_PRIORITY_DEFAULT, NULL );
xmpp_add_feature ( NS_MOOD );
xmpp_add_feature ( NS_MOOD_NOTIFY );
}
void pep_mood_uninit (void)
{
xmpp_del_feature ( NS_MOOD );
xmpp_del_feature ( NS_MOOD_NOTIFY );
hk_del_handler ( HOOK_POST_CONNECT, mood_hid_connect );
hk_del_handler ( HOOK_PRE_DISCONNECT, mood_hid_disconnect );
hk_del_handler ( HOOK_MOOD_OUT, mood_hid_moodout );
pep_unregister_xmlns_handler (NS_MOOD);
#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
if (lconnection && mood_reply_handler)
lm_connection_unregister_reply_handler (lconnection, mood_reply_handler);
#endif
lm_message_handler_invalidate (mood_reply_handler);
lm_message_handler_unref (mood_reply_handler);
g_free ( delayed_mood );
g_free ( delayed_text );
}
/* vim: se ts=4 sw=4: */