Merge pep modules & use events
* merge mood, tune, activity, geoloc, avatar
* split them into functionality/ui
* tune, geoloc: fix timeout bug
* geoloc: fix guard unregistering
* tune, mood: fix memleaking delayed info
* mood: fix non-publish of offline-delayed data
* activity: unregister reply handler on disconnect
* fix silly copyright typo
* update hgignore
* v0.0.3
/*
* pep_geoloc.c -- Pep geographical location 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 <stdlib.h> // atoi
#include <string.h>
#include <time.h>
#include <mcabber/logprint.h>
#include <mcabber/utils.h>
#include <mcabber/xmpp.h>
#include <mcabber/xmpp_helper.h>
#include <mcabber/hooks.h>
#include <mcabber/settings.h>
#include <mcabber/modules.h>
#include "pep.h"
#include "geoloc.h"
#include "config.h"
//
// module description
//
void pep_geoloc_init (void);
void pep_geoloc_uninit (void);
#define DESCRIPTION ( \
"PEP geoloc event handling\n" \
"Recognizes option geoloc_interval" )
static const gchar *deps[] = { "pep", NULL };
static module_info_t info_geoloc_dev = {
.branch = "dev",
.api = 20,
.version = PROJECT_VERSION,
.description = DESCRIPTION,
.requires = deps,
.init = pep_geoloc_init,
.uninit = pep_geoloc_uninit,
.next = NULL,
};
module_info_t info_pep_geoloc = {
.branch = "0.10.1",
.api = 1,
.version = PROJECT_VERSION,
.description = DESCRIPTION,
.requires = deps,
.init = pep_geoloc_init,
.uninit = pep_geoloc_uninit,
.next = &info_geoloc_dev,
};
//
// globals
//
#define MAX_NO ( 21 )
static geoloc_pair_t info[] = {
{ "accuracy", NULL },
{ "alt", NULL },
{ "area", NULL },
{ "bearing", NULL },
{ "building", NULL },
{ "country", NULL },
{ "countrycode", NULL },
{ "datum", NULL },
{ "description", NULL },
{ "error", NULL },
{ "floor", NULL },
{ "lat", NULL },
{ "locality", NULL },
{ "lon", NULL },
{ "postalcode", NULL },
{ "region", NULL },
{ "room", NULL },
{ "speed", NULL },
{ "street", NULL },
{ "text", NULL },
{ "timestamp", NULL },
{ "uri", NULL },
{ NULL, NULL },
};
static GQuark geoloc_gerror_quark = 0;
static gboolean publish_delayed = FALSE;
static guint geoloc_hid_connect = 0;
static guint geoloc_hid_disconnect = 0;
static guint geoloc_hid_geolocout = 0;
static guint geoloc_interval = 0;
static time_t geoloc_timestamp = 0;
static LmMessageHandler *geoloc_reply_handler = NULL;
static guint geoloc_source = 0;
static gboolean geoloc_guard_installed = FALSE;
//
// predeclarations
//
static void geoloc_publish_info (void);
//
// code
//
static LmHandlerResult geoloc_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, "geoloc: Publish failed: %s - %s.", type, reason);
}
break;
default:
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
break;
}
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
static gboolean geoloc_delayed_publish_cb (gpointer data)
{
geoloc_source = 0;
geoloc_publish_info ();
return FALSE;
}
static void geoloc_publish_info (void)
{
if (!xmpp_is_online ()) {
scr_log_print (LPRINT_DEBUG, "geoloc: Not online, delaying publish.");
publish_delayed = TRUE;
return;
}
// check for frequency of publishes
if (geoloc_interval) {
time_t now = time (NULL);
if (now - geoloc_timestamp < geoloc_interval) {
scr_log_print (LPRINT_DEBUG, "geoloc: Publish interval not passed, delaying.");
if (!geoloc_source)
geoloc_source = g_timeout_add_seconds ( geoloc_interval - ( now - geoloc_timestamp ), geoloc_delayed_publish_cb, NULL );
return;
} else
geoloc_timestamp = now;
}
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_GEOLOC);
node = lm_message_node_add_child (node, "item", NULL);
node = lm_message_node_add_child (node, "geoloc", NULL);
lm_message_node_set_attribute (node, "xmlns", NS_GEOLOC);
{ // put data inside
int i;
for ( i = 0; i <= MAX_NO; ++ i )
if ( info[i].value )
lm_message_node_add_child ( node, info[i].name, info[i].value );
}
{ // send
GError *error = NULL;
lm_connection_send_with_reply (lconnection, request, geoloc_reply_handler, &error);
if (error) {
scr_log_print (LPRINT_DEBUG, "geoloc: Publishing error: %s.", error -> message);
g_error_free (error);
}
}
lm_message_unref (request);
//publish_delayed = FALSE; XXX
}
void geoloc_publish (const geoloc_pair_t *pairs)
{
gboolean publish = FALSE;
const geoloc_pair_t *tag;
static geoloc_pair_t new_info[] = {
{ "accuracy", NULL },
{ "alt", NULL },
{ "area", NULL },
{ "bearing", NULL },
{ "building", NULL },
{ "country", NULL },
{ "countrycode", NULL },
{ "datum", NULL },
{ "description", NULL },
{ "error", NULL },
{ "floor", NULL },
{ "lat", NULL },
{ "locality", NULL },
{ "lon", NULL },
{ "postalcode", NULL },
{ "region", NULL },
{ "room", NULL },
{ "speed", NULL },
{ "street", NULL },
{ "text", NULL },
{ "timestamp", NULL },
{ "uri", NULL },
{ NULL, NULL },
};
// populate new_info with new values
for (tag = pairs; tag->name; ++tag) {
int i;
for (i = 0; i <= MAX_NO; ++i)
if (!g_strcmp0 (tag->name, new_info[i].name))
new_info[i].value = tag->value;
}
{ // check, if it differ from info
int i;
for (i = 0; i <= MAX_NO; ++i)
if (g_strcmp0 (new_info[i].value, info[i].value)) {
publish = TRUE;
break;
}
}
if (publish) {
{ // copy new vaules to info
int i;
for (i = 0; i <= MAX_NO; ++i) {
if (info[i].value)
g_free (info[i].value);
info[i].value = g_strdup (new_info[i].value);
}
}
geoloc_publish_info ();
}
}
gboolean geoloc_request ( const gchar *to, GError **err )
{
LmMessage *request;
LmMessageNode *node;
if (!xmpp_is_online ()) {
g_set_error ( err, geoloc_gerror_quark, GEOLOC_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_GEOLOC);
{ // 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 geoloc_handler (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer ignore)
{
LmMessageNode *tag;
hk_arg_t args[] = {
{ "accuracy", NULL },
{ "alt", NULL },
{ "area", NULL },
{ "bearing", NULL },
{ "building", NULL },
{ "country", NULL },
{ "countrycode", NULL },
{ "datum", NULL },
{ "description", NULL },
{ "error", NULL },
{ "floor", NULL },
{ "lat", NULL },
{ "locality", NULL },
{ "lon", NULL },
{ "postalcode", NULL },
{ "region", NULL },
{ "room", NULL },
{ "speed", NULL },
{ "street", NULL },
{ "text", NULL },
{ "timestamp", NULL },
{ "uri", NULL },
{ "from", from },
{ NULL, NULL },
};
for ( tag = n -> children; tag; tag = tag -> next ) {
const gchar *name = tag -> name;
if ( name ) {
int i;
for ( i = 0; i <= MAX_NO; ++ i )
if ( ! strcmp ( name, args[i].name ) ) {
const gchar *value = lm_message_node_get_value ( tag );
if ( value )
args[i].value = value;
}
}
}
hk_run_handlers ( HOOK_GEOLOC_IN, args );
}
static guint geoloc_hch (const gchar *hid, hk_arg_t *args, gpointer userdata)
{
if (publish_delayed) {
scr_log_print (LPRINT_DEBUG, "geoloc: Publishing delayed data.");
publish_delayed = FALSE;
geoloc_publish_info ();
}
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static guint geoloc_hdh (const gchar *hid, hk_arg_t *args, gpointer userdata)
{
#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
if (lconnection)
lm_connection_unregister_reply_handler (lconnection, geoloc_reply_handler);
#endif
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static guint geoloc_hgoh ( const gchar *hid, hk_arg_t *args, gpointer userdata )
{
geoloc_publish ( (const geoloc_pair_t *) args );
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static gchar *geoloc_guard (const char *key, const char *new_value)
{
if (new_value)
geoloc_interval = atoi (new_value);
else
geoloc_interval = 0;
if (geoloc_source) {
g_source_remove (geoloc_source);
geoloc_source = 0;
// Will reinstall source, if necessary
geoloc_publish_info ();
}
return g_strdup (new_value);
}
void pep_geoloc_init (void)
{
geoloc_gerror_quark = g_quark_from_string ( "pep-geoloc-gerror-quark" );
geoloc_interval = settings_opt_get_int ("geoloc_interval");
geoloc_guard_installed = settings_set_guard ("geoloc_interval", geoloc_guard);
if (!geoloc_guard_installed)
scr_log_print (LPRINT_LOGNORM, "geoloc: Warning: cannot install option guard for 'geoloc_interval'.");
pep_register_xmlns_handler (NS_GEOLOC, geoloc_handler, NULL, NULL);
geoloc_reply_handler = lm_message_handler_new (geoloc_publish_reply_handler, NULL, NULL);
geoloc_hid_connect = hk_add_handler ( geoloc_hch, HOOK_POST_CONNECT, G_PRIORITY_DEFAULT, NULL );
geoloc_hid_disconnect = hk_add_handler ( geoloc_hdh, HOOK_PRE_DISCONNECT, G_PRIORITY_DEFAULT, NULL );
geoloc_hid_geolocout = hk_add_handler ( geoloc_hgoh, HOOK_GEOLOC_OUT, G_PRIORITY_DEFAULT, NULL );
xmpp_add_feature ( NS_GEOLOC );
xmpp_add_feature ( NS_GEOLOC_NOTIFY );
}
void pep_geoloc_uninit (void)
{
xmpp_del_feature ( NS_GEOLOC );
xmpp_del_feature ( NS_GEOLOC_NOTIFY );
hk_del_handler ( HOOK_POST_CONNECT, geoloc_hid_connect );
hk_del_handler ( HOOK_PRE_DISCONNECT, geoloc_hid_disconnect );
hk_del_handler ( HOOK_GEOLOC_OUT, geoloc_hid_geolocout );
if (geoloc_source)
g_source_remove (geoloc_source);
pep_unregister_xmlns_handler ( NS_GEOLOC );
lm_message_handler_invalidate (geoloc_reply_handler);
lm_message_handler_unref (geoloc_reply_handler);
#ifdef HAVE_LM_CONNECTION_UNREGISTE_REPLY_HANDLER
if (lconnection)
lm_connection_unregister_reply_handler (lconnection, handler);
#endif
{
int i;
for (i = 0; i <= MAX_NO; ++i)
if (info[i].value)
g_free (info[i].value);
}
if (geoloc_guard_installed)
settings_del_guard ("geoloc_interval");
}
/* vim: se ts=4 sw=4: */