pep_geoloc.c
changeset 29 23fa36d480fb
child 31 e404cd1c7077
equal deleted inserted replaced
28:c035fbbab184 29:23fa36d480fb
       
     1 /*
       
     2  * pep_geoloc.c         -- Pep geographical location 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 <stdlib.h>                 // atoi
       
    25 #include <string.h>
       
    26 #include <time.h>
       
    27 
       
    28 #include <mcabber/logprint.h>
       
    29 #include <mcabber/utils.h>
       
    30 #include <mcabber/xmpp.h>
       
    31 #include <mcabber/xmpp_helper.h>
       
    32 #include <mcabber/hooks.h>
       
    33 #include <mcabber/settings.h>
       
    34 #include <mcabber/modules.h>
       
    35 
       
    36 #include "pep.h"
       
    37 #include "geoloc.h"
       
    38 
       
    39 #include "config.h"
       
    40 
       
    41 //
       
    42 //  module description
       
    43 //
       
    44 
       
    45 void pep_geoloc_init   (void);
       
    46 void pep_geoloc_uninit (void);
       
    47 
       
    48 #define DESCRIPTION ( \
       
    49 	"PEP geoloc event handling\n" \
       
    50 	"Recognizes option geoloc_interval" )
       
    51 
       
    52 static const gchar *deps[] = { "pep", NULL };
       
    53 
       
    54 static module_info_t info_geoloc_dev = {
       
    55 	.branch      = "dev",
       
    56 	.api         = 20,
       
    57 	.version     = PROJECT_VERSION,
       
    58 	.description = DESCRIPTION,
       
    59 	.requires    = deps,
       
    60 	.init        = pep_geoloc_init,
       
    61 	.uninit      = pep_geoloc_uninit,
       
    62 	.next        = NULL,
       
    63 };
       
    64 
       
    65 module_info_t info_pep_geoloc = {
       
    66 	.branch      = "0.10.1",
       
    67 	.api         = 1,
       
    68 	.version     = PROJECT_VERSION,
       
    69 	.description = DESCRIPTION,
       
    70 	.requires    = deps,
       
    71 	.init        = pep_geoloc_init,
       
    72 	.uninit      = pep_geoloc_uninit,
       
    73 	.next        = &info_geoloc_dev,
       
    74 };
       
    75 
       
    76 //
       
    77 //  globals
       
    78 //
       
    79 
       
    80 #define MAX_NO ( 21 )
       
    81 
       
    82 static geoloc_pair_t info[] = {
       
    83 	{ "accuracy",    NULL },
       
    84 	{ "alt",         NULL },
       
    85 	{ "area",        NULL },
       
    86 	{ "bearing",     NULL },
       
    87 	{ "building",    NULL },
       
    88 	{ "country",     NULL },
       
    89 	{ "countrycode", NULL },
       
    90 	{ "datum",       NULL },
       
    91 	{ "description", NULL },
       
    92 	{ "error",       NULL },
       
    93 	{ "floor",       NULL },
       
    94 	{ "lat",         NULL },
       
    95 	{ "locality",    NULL },
       
    96 	{ "lon",         NULL },
       
    97 	{ "postalcode",  NULL },
       
    98 	{ "region",      NULL },
       
    99 	{ "room",        NULL },
       
   100 	{ "speed",       NULL },
       
   101 	{ "street",      NULL },
       
   102 	{ "text",        NULL },
       
   103 	{ "timestamp",   NULL },
       
   104 	{ "uri",         NULL },
       
   105 	{ NULL,          NULL },
       
   106 };
       
   107 
       
   108 static GQuark            geoloc_gerror_quark    = 0;
       
   109 static gboolean          publish_delayed        = FALSE;
       
   110 static guint             geoloc_hid_connect     = 0;
       
   111 static guint             geoloc_hid_disconnect  = 0;
       
   112 static guint             geoloc_hid_geolocout   = 0;
       
   113 static guint             geoloc_interval        = 0;
       
   114 static time_t            geoloc_timestamp       = 0;
       
   115 static LmMessageHandler *geoloc_reply_handler   = NULL;
       
   116 static guint             geoloc_source          = 0;
       
   117 static gboolean          geoloc_guard_installed = FALSE;
       
   118 
       
   119 //
       
   120 //  predeclarations
       
   121 //
       
   122 
       
   123 static void geoloc_publish_info (void);
       
   124 
       
   125 //
       
   126 //  code
       
   127 //
       
   128 
       
   129 static LmHandlerResult geoloc_publish_reply_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer userdata)
       
   130 {
       
   131 	switch (lm_message_get_sub_type (message)) {
       
   132 	case LM_MESSAGE_SUB_TYPE_RESULT:
       
   133 		break;
       
   134 	
       
   135 	case LM_MESSAGE_SUB_TYPE_ERROR:
       
   136 		
       
   137 		{
       
   138 			LmMessageNode *node   = lm_message_get_node (message);
       
   139 			const gchar   *type;
       
   140 			const gchar   *reason;
       
   141 
       
   142 			node = lm_message_node_get_child (node, "error");
       
   143 			type = lm_message_node_get_attribute (node, "type");
       
   144 			if (node->children)
       
   145 				reason = node->children->name;
       
   146 			else
       
   147 				reason = "undefined";
       
   148 
       
   149 			scr_log_print (LPRINT_LOGNORM, "geoloc: Publish failed: %s - %s.", type, reason);
       
   150 		}
       
   151 
       
   152 		break;
       
   153 	
       
   154 	default:
       
   155 		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   156 		break;
       
   157 	}
       
   158 
       
   159 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
       
   160 }
       
   161 
       
   162 static gboolean geoloc_delayed_publish_cb (gpointer data)
       
   163 {
       
   164 	geoloc_source = 0;
       
   165 	geoloc_publish_info ();
       
   166 	return FALSE;
       
   167 }
       
   168 
       
   169 static void geoloc_publish_info (void)
       
   170 {
       
   171 	if (!xmpp_is_online ()) {
       
   172 		scr_log_print (LPRINT_DEBUG, "geoloc: Not online, delaying publish.");
       
   173 		publish_delayed = TRUE;
       
   174 		return;
       
   175 	}
       
   176 
       
   177 	// check for frequency of publishes
       
   178 	if (geoloc_interval) {
       
   179 		time_t now = time (NULL);
       
   180 
       
   181 		if (now - geoloc_timestamp < geoloc_interval) {
       
   182 
       
   183 			scr_log_print (LPRINT_DEBUG, "geoloc: Publish interval not passed, delaying.");
       
   184 			if (!geoloc_source)
       
   185 				geoloc_source = g_timeout_add_seconds ( geoloc_interval - ( now - geoloc_timestamp ), geoloc_delayed_publish_cb, NULL );
       
   186 			return;
       
   187 
       
   188 		} else
       
   189 			geoloc_timestamp = now;
       
   190 	}
       
   191 
       
   192 	LmMessage     *request = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET);
       
   193 	LmMessageNode *node    = lm_message_get_node (request);
       
   194 	lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
       
   195 
       
   196 	node = lm_message_node_add_child (node, "pubsub", NULL);
       
   197 	lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
       
   198 
       
   199 	node = lm_message_node_add_child (node, "publish", NULL);
       
   200 	lm_message_node_set_attribute (node, "node", NS_GEOLOC);
       
   201 
       
   202 	node = lm_message_node_add_child (node, "item", NULL);
       
   203 
       
   204 	node = lm_message_node_add_child (node, "geoloc", NULL);
       
   205 	lm_message_node_set_attribute (node, "xmlns", NS_GEOLOC);
       
   206 
       
   207 	{ // put data inside
       
   208 		int i;
       
   209 
       
   210 		for ( i = 0; i <= MAX_NO; ++ i )
       
   211 			if ( info[i].value )
       
   212 				lm_message_node_add_child ( node, info[i].name, info[i].value );
       
   213 	}
       
   214 
       
   215 	{ // send
       
   216 		GError *error = NULL;
       
   217 
       
   218 		lm_connection_send_with_reply (lconnection, request, geoloc_reply_handler, &error);
       
   219 
       
   220 		if (error) {
       
   221 			scr_log_print (LPRINT_DEBUG, "geoloc: Publishing error: %s.", error -> message);
       
   222 			g_error_free (error);
       
   223 		}
       
   224 	}
       
   225 
       
   226 	lm_message_unref (request);
       
   227 	//publish_delayed = FALSE; XXX
       
   228 }
       
   229 
       
   230 void geoloc_publish (const geoloc_pair_t *pairs)
       
   231 {
       
   232 	gboolean              publish    = FALSE;
       
   233 	const geoloc_pair_t  *tag;
       
   234 	static geoloc_pair_t  new_info[] = {
       
   235 		{ "accuracy",    NULL },
       
   236 		{ "alt",         NULL },
       
   237 		{ "area",        NULL },
       
   238 		{ "bearing",     NULL },
       
   239 		{ "building",    NULL },
       
   240 		{ "country",     NULL },
       
   241 		{ "countrycode", NULL },
       
   242 		{ "datum",       NULL },
       
   243 		{ "description", NULL },
       
   244 		{ "error",       NULL },
       
   245 		{ "floor",       NULL },
       
   246 		{ "lat",         NULL },
       
   247 		{ "locality",    NULL },
       
   248 		{ "lon",         NULL },
       
   249 		{ "postalcode",  NULL },
       
   250 		{ "region",      NULL },
       
   251 		{ "room",        NULL },
       
   252 		{ "speed",       NULL },
       
   253 		{ "street",      NULL },
       
   254 		{ "text",        NULL },
       
   255 		{ "timestamp",   NULL },
       
   256 		{ "uri",         NULL },
       
   257 		{ NULL,          NULL },
       
   258 	};
       
   259 
       
   260 	// populate new_info with new values
       
   261 	for (tag = pairs; tag->name; ++tag) {
       
   262 		int i;
       
   263 		for (i = 0; i <= MAX_NO; ++i)
       
   264 			if (!g_strcmp0 (tag->name, new_info[i].name))
       
   265 				new_info[i].value = tag->value;
       
   266 	}
       
   267 
       
   268 	{ // check, if it differ from info
       
   269 		int i;
       
   270 		for (i = 0; i <= MAX_NO; ++i)
       
   271 			if (g_strcmp0 (new_info[i].value, info[i].value)) {
       
   272 				publish = TRUE;
       
   273 				break;
       
   274 			}
       
   275 	}
       
   276 
       
   277 	if (publish) {
       
   278 
       
   279 		{ // copy new vaules to info
       
   280 			int i;
       
   281 			for (i = 0; i <= MAX_NO; ++i) {
       
   282 				if (info[i].value)
       
   283 					g_free (info[i].value);
       
   284 				info[i].value = g_strdup (new_info[i].value);
       
   285 			}
       
   286 		}
       
   287 
       
   288 		geoloc_publish_info ();
       
   289 	}
       
   290 }
       
   291 
       
   292 gboolean geoloc_request ( const gchar *to, GError **err )
       
   293 {
       
   294 
       
   295 	LmMessage     *request;
       
   296 	LmMessageNode *node;
       
   297 
       
   298 	if (!xmpp_is_online ()) {
       
   299 		g_set_error ( err, geoloc_gerror_quark, GEOLOC_ERROR_NOTCONNECTED, "You are not connected" );
       
   300 		return FALSE;
       
   301 	}
       
   302 
       
   303 	request = lm_message_new_with_sub_type (to, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET);
       
   304 	node = lm_message_get_node (request);
       
   305 	lm_message_node_set_attribute (node, "from", lm_connection_get_jid (lconnection));
       
   306 
       
   307 	node = lm_message_node_add_child (node, "pubsub", NULL);
       
   308 	lm_message_node_set_attribute (node, "xmlns", NS_PUBSUB);
       
   309 
       
   310 	node = lm_message_node_add_child (node, "items", NULL);
       
   311 	lm_message_node_set_attribute (node, "node", NS_GEOLOC);
       
   312 
       
   313 	{ // send, result will be handled by pep
       
   314 		GError *error = NULL;
       
   315 
       
   316 		lm_connection_send (lconnection, request, &error);
       
   317 
       
   318 		if (error) {
       
   319 			g_propagate_error ( err, error );
       
   320 			return FALSE;
       
   321 		}
       
   322 	}
       
   323 	
       
   324 	lm_message_unref (request);
       
   325 
       
   326 	return TRUE;
       
   327 }
       
   328 
       
   329 static void geoloc_handler (const gchar *from, const gchar *node, LmMessageNode *n, const gchar *id, gpointer ignore)
       
   330 {
       
   331 	LmMessageNode *tag;
       
   332 	hk_arg_t args[] = {
       
   333 		{ "accuracy",    NULL },
       
   334 		{ "alt",         NULL },
       
   335 		{ "area",        NULL },
       
   336 		{ "bearing",     NULL },
       
   337 		{ "building",    NULL },
       
   338 		{ "country",     NULL },
       
   339 		{ "countrycode", NULL },
       
   340 		{ "datum",       NULL },
       
   341 		{ "description", NULL },
       
   342 		{ "error",       NULL },
       
   343 		{ "floor",       NULL },
       
   344 		{ "lat",         NULL },
       
   345 		{ "locality",    NULL },
       
   346 		{ "lon",         NULL },
       
   347 		{ "postalcode",  NULL },
       
   348 		{ "region",      NULL },
       
   349 		{ "room",        NULL },
       
   350 		{ "speed",       NULL },
       
   351 		{ "street",      NULL },
       
   352 		{ "text",        NULL },
       
   353 		{ "timestamp",   NULL },
       
   354 		{ "uri",         NULL },
       
   355 		{ "from",        from },
       
   356 		{ NULL,          NULL },
       
   357 	};
       
   358 
       
   359 	for ( tag = n -> children; tag; tag = tag -> next ) {
       
   360 		const gchar *name = tag -> name;
       
   361 		if ( name ) {
       
   362 			int i;
       
   363 			for ( i = 0; i <= MAX_NO; ++ i )
       
   364 				if ( ! strcmp ( name, args[i].name ) ) {
       
   365 					const gchar *value = lm_message_node_get_value ( tag );
       
   366 					if ( value )
       
   367 						args[i].value = value;
       
   368 				}
       
   369 		}
       
   370 	}
       
   371 
       
   372 	hk_run_handlers ( HOOK_GEOLOC_IN, args );
       
   373 }
       
   374 
       
   375 static guint geoloc_hch (const gchar *hid, hk_arg_t *args, gpointer userdata)
       
   376 {
       
   377 	if (publish_delayed) {
       
   378 		scr_log_print (LPRINT_DEBUG, "geoloc: Publishing delayed data.");
       
   379 
       
   380 		publish_delayed = FALSE;
       
   381 		geoloc_publish_info ();
       
   382 	}
       
   383 
       
   384 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   385 }
       
   386 
       
   387 static guint geoloc_hdh (const gchar *hid, hk_arg_t *args, gpointer userdata)
       
   388 {
       
   389 #ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
       
   390 		if (lconnection)
       
   391 			lm_connection_unregister_reply_handler (lconnection, geoloc_reply_handler);
       
   392 #endif
       
   393 
       
   394 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   395 }
       
   396 
       
   397 static guint geoloc_hgoh ( const gchar *hid, hk_arg_t *args, gpointer userdata )
       
   398 {
       
   399 	geoloc_publish ( (const geoloc_pair_t *) args );
       
   400 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   401 }
       
   402 
       
   403 static gchar *geoloc_guard (const char *key, const char *new_value)
       
   404 {
       
   405 	if (new_value)
       
   406 		geoloc_interval = atoi (new_value);
       
   407 	else
       
   408 		geoloc_interval = 0;
       
   409 	
       
   410 	if (geoloc_source) {
       
   411 		g_source_remove (geoloc_source);
       
   412 		geoloc_source = 0;
       
   413 		// Will reinstall source, if necessary
       
   414 		geoloc_publish_info ();
       
   415 	}
       
   416 
       
   417 	return g_strdup (new_value);
       
   418 }
       
   419 
       
   420 void pep_geoloc_init (void)
       
   421 {
       
   422 	geoloc_gerror_quark = g_quark_from_string ( "pep-geoloc-gerror-quark" );
       
   423 
       
   424 	geoloc_interval = settings_opt_get_int ("geoloc_interval");
       
   425 
       
   426 	geoloc_guard_installed = settings_set_guard ("geoloc_interval", geoloc_guard);
       
   427 	if (!geoloc_guard_installed)
       
   428 		scr_log_print (LPRINT_LOGNORM, "geoloc: Warning: cannot install option guard for 'geoloc_interval'.");
       
   429 
       
   430 	pep_register_xmlns_handler (NS_GEOLOC, geoloc_handler, NULL, NULL);
       
   431 
       
   432 	geoloc_reply_handler = lm_message_handler_new (geoloc_publish_reply_handler, NULL, NULL);
       
   433 
       
   434 	geoloc_hid_connect    = hk_add_handler ( geoloc_hch,  HOOK_POST_CONNECT, G_PRIORITY_DEFAULT, NULL );
       
   435 	geoloc_hid_disconnect = hk_add_handler ( geoloc_hdh,  HOOK_PRE_DISCONNECT, G_PRIORITY_DEFAULT, NULL );
       
   436 	geoloc_hid_geolocout  = hk_add_handler ( geoloc_hgoh, HOOK_GEOLOC_OUT, G_PRIORITY_DEFAULT, NULL );
       
   437 
       
   438 	xmpp_add_feature ( NS_GEOLOC        );
       
   439 	xmpp_add_feature ( NS_GEOLOC_NOTIFY );
       
   440 }
       
   441 
       
   442 void pep_geoloc_uninit (void)
       
   443 {
       
   444 	xmpp_del_feature ( NS_GEOLOC        );
       
   445 	xmpp_del_feature ( NS_GEOLOC_NOTIFY );
       
   446 
       
   447 	hk_del_handler ( HOOK_POST_CONNECT,   geoloc_hid_connect    );
       
   448 	hk_del_handler ( HOOK_PRE_DISCONNECT, geoloc_hid_disconnect );
       
   449 	hk_del_handler ( HOOK_GEOLOC_OUT,     geoloc_hid_geolocout  );
       
   450 
       
   451 	if (geoloc_source)
       
   452 		g_source_remove (geoloc_source);
       
   453 
       
   454 	pep_unregister_xmlns_handler ( NS_GEOLOC );
       
   455 
       
   456 	lm_message_handler_invalidate (geoloc_reply_handler);
       
   457 	lm_message_handler_unref (geoloc_reply_handler);
       
   458 #ifdef HAVE_LM_CONNECTION_UNREGISTE_REPLY_HANDLER
       
   459 	if (lconnection)
       
   460 		lm_connection_unregister_reply_handler (lconnection, handler);
       
   461 #endif
       
   462 
       
   463 	{
       
   464 		int i;
       
   465 		for (i = 0; i <= MAX_NO; ++i)
       
   466 			if (info[i].value)
       
   467 				g_free (info[i].value);
       
   468 	}
       
   469 
       
   470 	if (geoloc_guard_installed)
       
   471 		settings_del_guard ("geoloc_interval");
       
   472 }
       
   473 
       
   474 /* vim: se ts=4 sw=4: */