mood.c
changeset 29 23fa36d480fb
child 31 e404cd1c7077
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mood.c	Sun May 20 22:15:51 2012 +0300
@@ -0,0 +1,294 @@
+/*
+ * 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 <string.h>
+
+#include <mcabber/commands.h>
+#include <mcabber/compl.h>
+#include <mcabber/utils.h>
+#include <mcabber/screen.h>
+#include <mcabber/logprint.h>
+#include <mcabber/roster.h>
+#include <mcabber/hbuf.h>        // HBUF_PREFIX_*
+#include <mcabber/hooks.h>
+#include <mcabber/modules.h>
+
+#include "mood.h"
+
+#include "config.h"
+
+//
+//  module description
+//
+
+void mood_init   (void);
+void mood_uninit (void);
+
+#define DESCRIPTION ( "PEP mood events handling\nProvides command /mood" )
+
+static const gchar *deps[] = { "pep_mood", NULL };
+
+static module_info_t info_mood_dev = {
+	.branch      = "dev",
+	.api         = 20,
+	.version     = PROJECT_VERSION,
+	.description = DESCRIPTION,
+	.requires    = deps,
+	.init        = mood_init,
+	.uninit      = mood_uninit,
+	.next        = NULL,
+};
+
+module_info_t info_mood = {
+	.branch      = "0.10.1",
+	.api         = 1,
+	.version     = PROJECT_VERSION,
+	.description = DESCRIPTION,
+	.requires    = deps,
+	.init        = mood_init,
+	.uninit      = mood_uninit,
+	.next        = &info_mood_dev,
+};
+
+//
+//  globals
+//
+
+#ifdef MCABBER_API_HAVE_CMD_ID
+static gpointer mood_cmid     = NULL;
+static gboolean mood_set_safe = FALSE;
+#endif
+
+static guint mood_cid        = 0;
+static guint mood_hid_moodin = 0;
+
+//
+//  code
+//
+
+static void do_mood (char *arg)
+{
+	if (!*arg) { // request
+
+		GError *error = NULL;
+		
+		mood_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." );
+
+		return;
+
+	} else { // publish
+		
+		hk_arg_t hookargs[] = {
+			{ "mood", NULL },
+			{ "text", NULL },
+			{ NULL,   NULL },
+		};
+
+		if ( arg[0] != '-' || arg[1] != '\0' ) {
+			gchar **args = split_arg ( arg, 2, 1 );
+
+			hookargs[0].value = to_utf8 ( args[0] );
+
+			if ( args[1] )
+				hookargs[1].value = to_utf8 ( args[1] );
+
+			free_arg_lst ( args );
+		}
+
+		hk_run_handlers ( HOOK_MOOD_OUT, hookargs );
+
+		g_free ( (gchar *) hookargs[0].value );
+		g_free ( (gchar *) hookargs[1].value );
+	}
+}
+
+static guint mood_hmih (const gchar *hid, hk_arg_t *args, gpointer userdata)
+{
+	const gchar *from = NULL;
+	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, "from" ) )
+					from = value;
+				else if ( ! strcmp ( name, "mood" ) )
+					mood = value;
+				else if ( ! strcmp ( name, "text" ) )
+					text = value;
+			}
+		}
+	}
+
+	{ // print to buddy's buffer
+		gchar *jid  = jidtodisp (from);
+		gchar *mesg = NULL;
+
+		if (mood && text)
+			mesg = g_strdup_printf ("Mood: %s - %s.", mood, text);
+		else if (mood || text)
+			mesg = g_strdup_printf ("Mood: %s.", mood ? mood : text);
+
+		scr_write_incoming_message (jid, mesg ? mesg : "No specific mood.", 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_moods[] = {
+	"afraid",
+	"amazed",
+	"amorous",
+	"angry",
+	"annoyed",
+	"anxious",
+	"aroused",
+	"ashamed",
+	"bored",
+	"brave",
+	"calm",
+	"cautious",
+	"cold",
+	"confident",
+	"confused",
+	"contemplative",
+	"contented",
+	"cranky",
+	"crazy",
+	"creative",
+	"curious",
+	"dejected",
+	"depressed",
+	"disappointed",
+	"disgusted",
+	"dismayed",
+	"distracted",
+	"embarrassed",
+	"envious",
+	"excited",
+	"flirtatious",
+	"frustrated",
+	"grateful",
+	"grieving",
+	"grumpy",
+	"guilty",
+	"happy",
+	"hopeful",
+	"hot",
+	"humbled",
+	"humiliated",
+	"hungry",
+	"hurt",
+	"impressed",
+	"in_awe",
+	"in_love",
+	"indignant",
+	"interested",
+	"intoxicated",
+	"invincible",
+	"jealous",
+	"lonely",
+	"lost",
+	"lucky",
+	"mean",
+	"moody",
+	"nervous",
+	"neutral",
+	"offended",
+	"outraged",
+	"playful",
+	"proud",
+	"relaxed",
+	"relieved",
+	"remorseful",
+	"restless",
+	"sad",
+	"sarcastic",
+	"satisfied",
+	"serious",
+	"shocked",
+	"shy",
+	"sick",
+	"sleepy",
+	"spontaneous",
+	"stressed",
+	"strong",
+	"surprised",
+	"thankful",
+	"thirsty",
+	"tired",
+	"undefined",
+	"weak",
+	"worried",
+	NULL,
+};
+
+void mood_init (void)
+{
+	mood_cid = compl_new_category ();
+	if (mood_cid) {
+		const gchar **mood;
+
+		for (mood = defined_moods; *mood; ++mood)
+			compl_add_category_word (mood_cid, *mood);
+	}
+
+#ifndef MCABBER_API_HAVE_CMD_ID
+	cmd_add ( "mood", "", mood_cid, 0, do_mood, NULL );
+#else
+	mood_cmid     = cmd_add ( "mood", "", mood_cid, 0, do_mood, NULL );
+	mood_set_safe = cmd_set_safe ( "mood", TRUE );
+#endif
+
+	mood_hid_moodin = hk_add_handler ( mood_hmih, HOOK_MOOD_IN, G_PRIORITY_DEFAULT, NULL );
+}
+
+void mood_uninit (void)
+{
+	hk_del_handler ( HOOK_MOOD_IN, mood_hid_moodin );
+
+#ifndef MCABBER_API_HAVE_CMD_ID
+	cmd_del ( "mood" );
+#else
+	if ( mood_cmid )
+		cmd_del ( mood_cmid );
+	if ( mood_set_safe )
+		cmd_set_safe ( "mood", FALSE );
+#endif
+
+	if ( mood_cid )
+		compl_del_category ( mood_cid );
+}
+
+/* vim: se ts=4 sw=4: */