/*
* activity.c -- Pep activity 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 <string.h>
#include <mcabber/utils.h>
#include <mcabber/screen.h>
#include <mcabber/logprint.h>
#include <mcabber/hbuf.h> // HBUF_PREFIX_*
#include <mcabber/roster.h>
#include <mcabber/hooks.h>
#include <mcabber/commands.h>
#include <mcabber/compl.h>
#include <mcabber/modules.h>
#include "pep_activity.h"
#include "config.h"
//
// module description
//
void activity_init (void);
void activity_uninit (void);
#define DESCRIPTION ( ACTIVITY_DESCRIPTION )
static const gchar *deps[] = { "pep_activity", NULL };
module_info_t info_activity = {
.branch = MCABBER_BRANCH,
.api = MCABBER_API_VERSION,
.version = PROJECT_VERSION,
.description = DESCRIPTION,
.requires = deps,
.init = activity_init,
.uninit = activity_uninit,
.next = NULL,
};
//
// globals
//
#ifdef MCABBER_API_HAVE_CMD_ID
static gpointer activity_cmid = NULL;
static gboolean activity_set_safe = FALSE;
#endif
static guint activity_cid1 = 0;
static guint activity_cid2 = 0;
static guint activity_hid_activityin = 0;
//
// code
//
static void do_activity (char *arg)
{
if (!*arg) { // request
GError *error = NULL;
pep_activity_request ( CURRENT_JID, &error );
if ( error ) {
scr_log_print ( LPRINT_NORMAL, "Error sending request: %s.", error -> message );
g_error_free ( error );
} else
scr_log_print ( LPRINT_NORMAL, "Request sent." );
} else { // publish
hk_arg_t hookargs[] = {
{ "major", NULL },
{ "minor", NULL },
{ "text", NULL },
{ NULL, NULL },
};
if ( arg[0] != '-' || arg[1] != '\0' ) {
gchar **args = split_arg ( arg, 3, 1 );
hookargs[0].value = to_utf8 ( args[0] );
if ( args[1] ) {
if ( args[1][0] != '-' || args[1][1] != '\0' )
hookargs[1].value = to_utf8 ( args[1] );
if ( args[2] )
hookargs[2].value = to_utf8 ( args[2] );
}
free_arg_lst ( args );
}
hk_run_handlers ( HOOK_ACTIVITY_OUT, hookargs );
g_free ( (gchar *) hookargs[0].value );
g_free ( (gchar *) hookargs[1].value );
g_free ( (gchar *) hookargs[2].value );
}
}
static guint activity_haih ( const gchar *hid, hk_arg_t *args, gpointer userdata )
{
const gchar *from = NULL;
const gchar *major = NULL;
const gchar *minor = 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, "from" ) )
from = value;
else if ( ! strcmp ( name, "major" ) )
major = value;
else if ( ! strcmp ( name, "minor" ) )
minor = value;
else if ( ! strcmp ( name, "text" ) )
text = value;
}
}
}
{ // print to buddy's buffer
gchar *jid = jidtodisp (from);
gchar *mesg = NULL;
// this can be implemented easier with gstring...
if (major && text) {
if (minor)
mesg = g_strdup_printf ("Activity: %s (%s) - %s.", major, minor, text);
else
mesg = g_strdup_printf ("Activity: %s - %s.", major, text);
} else if (major) {
if (minor)
mesg = g_strdup_printf ("Activity: %s (%s).", major, minor);
else
mesg = g_strdup_printf ("Activity: %s.", major);
} else if (text)
mesg = g_strdup_printf ("Activity: %s.", text);
scr_write_incoming_message (jid, mesg ? mesg : "No specific activity.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
g_free (mesg);
g_free (jid);
}
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
static const gchar *defined_general_activities[] = {
"doing_chores",
"drinking",
"eating",
"exercising",
"grooming",
"having_appointment",
"inactive",
"relaxing",
"talking",
"traveling",
"working",
NULL,
};
static const gchar *defined_minor_activities[] = {
"buying_groceries",
"cleaning",
"cooking",
"doing_maintenance",
"doing_the_dishes",
"doing_the_laundry",
"gardening",
"running_an_errand",
"walking_the_dog",
"having_a_beer",
"having_coffee",
"having_tea",
"having_a_snack",
"having_breakfast",
"having_dinner",
"having_lunch",
"cycling",
"dancing",
"hiking",
"jogging",
"playing_sports",
"running",
"skiing",
"swimming",
"working_out",
"at_the_spa",
"brushing_teeth",
"getting_a_haircut",
"shaving",
"taking_a_bath",
"taking_a_shower",
"day_off",
"hanging_out",
"hiding",
"on_vacation",
"praying",
"scheduled_holiday",
"sleeping",
"thinking",
"fishing",
"gaming",
"going_out",
"partying",
"reading",
"rehearsing",
"shopping",
"smoking",
"socializing",
"sunbathing",
"watching_tv",
"watching_a_movie",
"in_real_life",
"on_the_phone",
"on_video_phone",
"commuting",
"cycling",
"driving",
"in_a_car",
"on_a_bus",
"on_a_plane",
"on_a_train",
"on_a_trip",
"walking",
"coding",
"in_a_meeting",
"studying",
"writing",
NULL,
};
void activity_init (void)
{
#ifdef HAVE_MCABBER_COMPLETION_FLAGS
activity_cid1 = compl_new_category (COMPL_FLAGS_SORT);
activity_cid2 = compl_new_category (COMPL_FLAGS_SORT);
#else
activity_cid1 = compl_new_category ();
activity_cid2 = compl_new_category ();
#endif
if (activity_cid1) {
const gchar **activity;
for (activity = defined_general_activities; *activity; ++activity)
compl_add_category_word (activity_cid1, *activity);
}
if (activity_cid2) {
const gchar **activity;
for (activity = defined_minor_activities; *activity; ++activity)
compl_add_category_word (activity_cid2, *activity);
}
#ifndef MCABBER_API_HAVE_CMD_ID
cmd_add ("activity", "", activity_cid1, activity_cid2, do_activity, NULL);
#else
activity_cmid = cmd_add ("activity", "", activity_cid1, activity_cid2, do_activity, NULL);
activity_set_safe = cmd_set_safe ("activity", TRUE);
#endif
activity_hid_activityin = hk_add_handler (activity_haih, HOOK_ACTIVITY_IN, G_PRIORITY_DEFAULT, NULL);
}
void activity_uninit (void)
{
hk_del_handler (HOOK_ACTIVITY_IN, activity_hid_activityin);
#ifndef MCABBER_API_HAVE_CMD_ID
cmd_del ("activity");
#else
if (activity_cmid)
cmd_del (activity_cmid);
if (activity_set_safe)
cmd_set_safe ("activity", FALSE);
#endif
if (activity_cid1)
compl_del_category (activity_cid1);
if (activity_cid2)
compl_del_category (activity_cid2);
}
/* vim: se ts=4 sw=4: */