activity.c
changeset 29 23fa36d480fb
child 31 e404cd1c7077
equal deleted inserted replaced
28:c035fbbab184 29:23fa36d480fb
       
     1 /*
       
     2  * 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 <string.h>
       
    24 
       
    25 #include <mcabber/utils.h>
       
    26 #include <mcabber/screen.h>
       
    27 #include <mcabber/logprint.h>
       
    28 #include <mcabber/hbuf.h>         // HBUF_PREFIX_*
       
    29 #include <mcabber/roster.h>
       
    30 #include <mcabber/hooks.h>
       
    31 #include <mcabber/commands.h>
       
    32 #include <mcabber/compl.h>
       
    33 #include <mcabber/modules.h>
       
    34 
       
    35 #include "activity.h"
       
    36 
       
    37 #include "config.h"
       
    38 
       
    39 //
       
    40 //  module description
       
    41 //
       
    42 
       
    43 void activity_init   (void);
       
    44 void activity_uninit (void);
       
    45 
       
    46 #define DESCRIPTION ( "PEP activity support\nProvides command /activity" )
       
    47 static const gchar *deps[] = { "pep_activity", NULL };
       
    48 
       
    49 static module_info_t info_activity_dev = {
       
    50 	.branch       = "dev",
       
    51 	.api          = 20,
       
    52 	.version      = PROJECT_VERSION,
       
    53 	.description  = DESCRIPTION,
       
    54 	.requires     = deps,
       
    55 	.init         = activity_init,
       
    56 	.uninit       = activity_uninit,
       
    57 	.next         = NULL,
       
    58 };
       
    59 
       
    60 static module_info_t info_activity_0_10_1 = {
       
    61 	.branch       = "0.10.1",
       
    62 	.api          = 1,
       
    63 	.version      = PROJECT_VERSION,
       
    64 	.description  = DESCRIPTION,
       
    65 	.requires     = deps,
       
    66 	.init         = activity_init,
       
    67 	.uninit       = activity_uninit,
       
    68 	.next         = &info_activity_dev,
       
    69 };
       
    70 
       
    71 module_info_t info_activity = {
       
    72 	.branch       = "0.10.0",
       
    73 	.api          = 1,
       
    74 	.version      = PROJECT_VERSION,
       
    75 	.description  = DESCRIPTION,
       
    76 	.requires     = deps,
       
    77 	.init         = activity_init,
       
    78 	.uninit       = activity_uninit,
       
    79 	.next         = &info_activity_0_10_1,
       
    80 };
       
    81 
       
    82 //
       
    83 //  globals
       
    84 //
       
    85 
       
    86 #ifdef MCABBER_API_HAVE_CMD_ID
       
    87 static gpointer activity_cmid     = NULL;
       
    88 static gboolean activity_set_safe = FALSE;
       
    89 #endif
       
    90 
       
    91 static guint activity_cid1           = 0;
       
    92 static guint activity_cid2           = 0;
       
    93 static guint activity_hid_activityin = 0;
       
    94 
       
    95 //
       
    96 //  code
       
    97 //
       
    98 
       
    99 static void do_activity (char *arg)
       
   100 {
       
   101 	if (!*arg) { // request
       
   102 
       
   103 		GError *error = NULL;
       
   104 
       
   105 		activity_request ( CURRENT_JID, &error );
       
   106 		if ( error ) {
       
   107 			scr_log_print ( LPRINT_NORMAL, "Error sending request: %s.", error -> message );
       
   108 			g_error_free ( error );
       
   109 		} else
       
   110 			scr_log_print ( LPRINT_NORMAL, "Request sent." );
       
   111 
       
   112 	} else { // publish
       
   113 
       
   114 		hk_arg_t hookargs[] = {
       
   115 			{ "major", NULL },
       
   116 			{ "minor", NULL },
       
   117 			{ "text",  NULL },
       
   118 			{ NULL,    NULL },
       
   119 		};
       
   120 
       
   121 		if ( arg[0] != '-' || arg[1] != '\0' ) {
       
   122 			gchar **args = split_arg ( arg, 3, 1 );
       
   123 			
       
   124 			hookargs[0].value = to_utf8 ( args[0] );
       
   125 
       
   126 			if ( args[1] ) {
       
   127 				if ( args[1][0] != '-' || args[1][1] != '\0' )
       
   128 					hookargs[1].value = to_utf8 ( args[1] );
       
   129 
       
   130 				if ( args[2] )
       
   131 					hookargs[2].value = to_utf8 ( args[2] );
       
   132 			}
       
   133 
       
   134 			free_arg_lst ( args );
       
   135 		}
       
   136 
       
   137 		hk_run_handlers ( HOOK_ACTIVITY_OUT, hookargs );
       
   138 
       
   139 		g_free ( (gchar *) hookargs[0].value );
       
   140 		g_free ( (gchar *) hookargs[1].value );
       
   141 		g_free ( (gchar *) hookargs[2].value );
       
   142 	}
       
   143 }
       
   144 
       
   145 static guint activity_haih ( const gchar *hid, hk_arg_t *args, gpointer userdata )
       
   146 {
       
   147 	const gchar *from  = NULL;
       
   148 	const gchar *major = NULL;
       
   149 	const gchar *minor = NULL;
       
   150 	const gchar *text  = NULL;
       
   151 
       
   152 	{
       
   153 		hk_arg_t *arg;
       
   154 		for ( arg = args; arg -> name; arg ++ ) {
       
   155 			const gchar *value = arg -> value;
       
   156 			if ( value ) {
       
   157 				const gchar *name = arg -> name;
       
   158 				if ( ! strcmp ( name, "from" ) )
       
   159 					from = value;
       
   160 				else if ( ! strcmp ( name, "major" ) )
       
   161 					major = value;
       
   162 				else if ( ! strcmp ( name, "minor" ) )
       
   163 					minor = value;
       
   164 				else if ( ! strcmp ( name, "text" ) )
       
   165 					text = value;
       
   166 			}
       
   167 		}
       
   168 	}
       
   169 
       
   170 	{ // print to buddy's buffer
       
   171 		gchar *jid  = jidtodisp (from);
       
   172 		gchar *mesg = NULL;
       
   173 
       
   174 		// this can be implemented easier with gstring...
       
   175 		if (major && text) {
       
   176 			if (minor)
       
   177 				mesg = g_strdup_printf ("Activity: %s (%s) - %s.", major, minor, text);
       
   178 			else
       
   179 				mesg = g_strdup_printf ("Activity: %s - %s.", major, text);
       
   180 		} else if (major) {
       
   181 			if (minor)
       
   182 				mesg = g_strdup_printf ("Activity: %s (%s).", major, minor);
       
   183 			else
       
   184 				mesg = g_strdup_printf ("Activity: %s.", major);
       
   185 		} else if (text)
       
   186 			mesg = g_strdup_printf ("Activity: %s.", text);
       
   187 
       
   188 		scr_write_incoming_message (jid, mesg ? mesg : "No specific activity.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
       
   189 
       
   190 		g_free (mesg);
       
   191 		g_free (jid);
       
   192 	}
       
   193 
       
   194 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   195 }
       
   196 
       
   197 static const gchar *defined_general_activities[] = {
       
   198 	"doing_chores",
       
   199 	"drinking",
       
   200 	"eating",
       
   201 	"exercising",
       
   202 	"grooming",
       
   203 	"having_appointment",
       
   204 	"inactive",
       
   205 	"relaxing",
       
   206 	"talking",
       
   207 	"traveling",
       
   208 	"working",
       
   209 	NULL,
       
   210 };
       
   211 
       
   212 static const gchar *defined_minor_activities[] = {
       
   213 	"buying_groceries",
       
   214 	"cleaning",
       
   215 	"cooking",
       
   216 	"doing_maintenance",
       
   217 	"doing_the_dishes",
       
   218 	"doing_the_laundry",
       
   219 	"gardening",
       
   220 	"running_an_errand",
       
   221 	"walking_the_dog",
       
   222 	"having_a_beer",
       
   223 	"having_coffee",
       
   224 	"having_tea",
       
   225 	"having_a_snack",
       
   226 	"having_breakfast",
       
   227 	"having_dinner",
       
   228 	"having_lunch",
       
   229 	"cycling",
       
   230 	"dancing",
       
   231 	"hiking",
       
   232 	"jogging",
       
   233 	"playing_sports",
       
   234 	"running",
       
   235 	"skiing",
       
   236 	"swimming",
       
   237 	"working_out",
       
   238 	"at_the_spa",
       
   239 	"brushing_teeth",
       
   240 	"getting_a_haircut",
       
   241 	"shaving",
       
   242 	"taking_a_bath",
       
   243 	"taking_a_shower",
       
   244 	"day_off",
       
   245 	"hanging_out",
       
   246 	"hiding",
       
   247 	"on_vacation",
       
   248 	"praying",
       
   249 	"scheduled_holiday",
       
   250 	"sleeping",
       
   251 	"thinking",
       
   252 	"fishing",
       
   253 	"gaming",
       
   254 	"going_out",
       
   255 	"partying",
       
   256 	"reading",
       
   257 	"rehearsing",
       
   258 	"shopping",
       
   259 	"smoking",
       
   260 	"socializing",
       
   261 	"sunbathing",
       
   262 	"watching_tv",
       
   263 	"watching_a_movie",
       
   264 	"in_real_life",
       
   265 	"on_the_phone",
       
   266 	"on_video_phone",
       
   267 	"commuting",
       
   268 	"cycling",
       
   269 	"driving",
       
   270 	"in_a_car",
       
   271 	"on_a_bus",
       
   272 	"on_a_plane",
       
   273 	"on_a_train",
       
   274 	"on_a_trip",
       
   275 	"walking",
       
   276 	"coding",
       
   277 	"in_a_meeting",
       
   278 	"studying",
       
   279 	"writing",
       
   280 	NULL,
       
   281 };
       
   282 
       
   283 void activity_init (void)
       
   284 {
       
   285 	activity_cid1 = compl_new_category ();
       
   286 	if (activity_cid1) {
       
   287 		const gchar **activity;
       
   288 
       
   289 		for (activity = defined_general_activities; *activity; ++activity)
       
   290 			compl_add_category_word (activity_cid1, *activity);
       
   291 	}
       
   292 
       
   293 	activity_cid2 = compl_new_category ();
       
   294 	if (activity_cid2) {
       
   295 		const gchar **activity;
       
   296 
       
   297 		for (activity = defined_minor_activities; *activity; ++activity)
       
   298 			compl_add_category_word (activity_cid2, *activity);
       
   299 	}
       
   300 
       
   301 #ifndef MCABBER_API_HAVE_CMD_ID
       
   302 	cmd_add ("activity", "", activity_cid1, activity_cid2, do_activity, NULL);
       
   303 #else
       
   304 	activity_cmid = cmd_add ("activity", "", activity_cid1, activity_cid2, do_activity, NULL);
       
   305 	activity_set_safe = cmd_set_safe ("activity", TRUE);
       
   306 #endif
       
   307 
       
   308 	activity_hid_activityin = hk_add_handler (activity_haih, HOOK_ACTIVITY_IN, G_PRIORITY_DEFAULT, NULL);
       
   309 }
       
   310 
       
   311 void activity_uninit (void)
       
   312 {
       
   313 	hk_del_handler (HOOK_ACTIVITY_IN, activity_hid_activityin);
       
   314 
       
   315 #ifndef MCABBER_API_HAVE_CMD_ID
       
   316 	cmd_del ("activity");
       
   317 #else
       
   318 	if (activity_cmid)
       
   319 		cmd_del (activity_cmid);
       
   320 	if (activity_set_safe)
       
   321 		cmd_set_safe ("activity", FALSE);
       
   322 #endif
       
   323 
       
   324 	if (activity_cid1)
       
   325 		compl_del_category (activity_cid1);
       
   326 	if (activity_cid2)
       
   327 		compl_del_category (activity_cid2);
       
   328 }
       
   329 
       
   330 /* vim: se ts=4 sw=4: */