mood.c
changeset 29 23fa36d480fb
child 31 e404cd1c7077
equal deleted inserted replaced
28:c035fbbab184 29:23fa36d480fb
       
     1 /*
       
     2  * mood.c               -- Pep mood 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/commands.h>
       
    26 #include <mcabber/compl.h>
       
    27 #include <mcabber/utils.h>
       
    28 #include <mcabber/screen.h>
       
    29 #include <mcabber/logprint.h>
       
    30 #include <mcabber/roster.h>
       
    31 #include <mcabber/hbuf.h>        // HBUF_PREFIX_*
       
    32 #include <mcabber/hooks.h>
       
    33 #include <mcabber/modules.h>
       
    34 
       
    35 #include "mood.h"
       
    36 
       
    37 #include "config.h"
       
    38 
       
    39 //
       
    40 //  module description
       
    41 //
       
    42 
       
    43 void mood_init   (void);
       
    44 void mood_uninit (void);
       
    45 
       
    46 #define DESCRIPTION ( "PEP mood events handling\nProvides command /mood" )
       
    47 
       
    48 static const gchar *deps[] = { "pep_mood", NULL };
       
    49 
       
    50 static module_info_t info_mood_dev = {
       
    51 	.branch      = "dev",
       
    52 	.api         = 20,
       
    53 	.version     = PROJECT_VERSION,
       
    54 	.description = DESCRIPTION,
       
    55 	.requires    = deps,
       
    56 	.init        = mood_init,
       
    57 	.uninit      = mood_uninit,
       
    58 	.next        = NULL,
       
    59 };
       
    60 
       
    61 module_info_t info_mood = {
       
    62 	.branch      = "0.10.1",
       
    63 	.api         = 1,
       
    64 	.version     = PROJECT_VERSION,
       
    65 	.description = DESCRIPTION,
       
    66 	.requires    = deps,
       
    67 	.init        = mood_init,
       
    68 	.uninit      = mood_uninit,
       
    69 	.next        = &info_mood_dev,
       
    70 };
       
    71 
       
    72 //
       
    73 //  globals
       
    74 //
       
    75 
       
    76 #ifdef MCABBER_API_HAVE_CMD_ID
       
    77 static gpointer mood_cmid     = NULL;
       
    78 static gboolean mood_set_safe = FALSE;
       
    79 #endif
       
    80 
       
    81 static guint mood_cid        = 0;
       
    82 static guint mood_hid_moodin = 0;
       
    83 
       
    84 //
       
    85 //  code
       
    86 //
       
    87 
       
    88 static void do_mood (char *arg)
       
    89 {
       
    90 	if (!*arg) { // request
       
    91 
       
    92 		GError *error = NULL;
       
    93 		
       
    94 		mood_request ( CURRENT_JID, &error );
       
    95 		if ( error ) {
       
    96 			scr_log_print ( LPRINT_NORMAL, "Error sending request: %s.", error -> message );
       
    97 			g_error_free ( error );
       
    98 		} else
       
    99 			scr_log_print ( LPRINT_NORMAL, "Request sent." );
       
   100 
       
   101 		return;
       
   102 
       
   103 	} else { // publish
       
   104 		
       
   105 		hk_arg_t hookargs[] = {
       
   106 			{ "mood", NULL },
       
   107 			{ "text", NULL },
       
   108 			{ NULL,   NULL },
       
   109 		};
       
   110 
       
   111 		if ( arg[0] != '-' || arg[1] != '\0' ) {
       
   112 			gchar **args = split_arg ( arg, 2, 1 );
       
   113 
       
   114 			hookargs[0].value = to_utf8 ( args[0] );
       
   115 
       
   116 			if ( args[1] )
       
   117 				hookargs[1].value = to_utf8 ( args[1] );
       
   118 
       
   119 			free_arg_lst ( args );
       
   120 		}
       
   121 
       
   122 		hk_run_handlers ( HOOK_MOOD_OUT, hookargs );
       
   123 
       
   124 		g_free ( (gchar *) hookargs[0].value );
       
   125 		g_free ( (gchar *) hookargs[1].value );
       
   126 	}
       
   127 }
       
   128 
       
   129 static guint mood_hmih (const gchar *hid, hk_arg_t *args, gpointer userdata)
       
   130 {
       
   131 	const gchar *from = NULL;
       
   132 	const gchar *mood = NULL;
       
   133 	const gchar *text = NULL;
       
   134 
       
   135 	{
       
   136 		hk_arg_t *arg;
       
   137 		for ( arg = args; arg -> name; arg ++ ) {
       
   138 			const gchar *value = arg -> value;
       
   139 			if ( value ) {
       
   140 				const gchar *name  = arg -> name;
       
   141 				if ( ! strcmp ( name, "from" ) )
       
   142 					from = value;
       
   143 				else if ( ! strcmp ( name, "mood" ) )
       
   144 					mood = value;
       
   145 				else if ( ! strcmp ( name, "text" ) )
       
   146 					text = value;
       
   147 			}
       
   148 		}
       
   149 	}
       
   150 
       
   151 	{ // print to buddy's buffer
       
   152 		gchar *jid  = jidtodisp (from);
       
   153 		gchar *mesg = NULL;
       
   154 
       
   155 		if (mood && text)
       
   156 			mesg = g_strdup_printf ("Mood: %s - %s.", mood, text);
       
   157 		else if (mood || text)
       
   158 			mesg = g_strdup_printf ("Mood: %s.", mood ? mood : text);
       
   159 
       
   160 		scr_write_incoming_message (jid, mesg ? mesg : "No specific mood.", 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0); // NO conversion from utf-8
       
   161 
       
   162 		g_free (mesg);
       
   163 		g_free (jid);
       
   164 	}
       
   165 
       
   166 	return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
       
   167 }
       
   168 
       
   169 static const gchar *defined_moods[] = {
       
   170 	"afraid",
       
   171 	"amazed",
       
   172 	"amorous",
       
   173 	"angry",
       
   174 	"annoyed",
       
   175 	"anxious",
       
   176 	"aroused",
       
   177 	"ashamed",
       
   178 	"bored",
       
   179 	"brave",
       
   180 	"calm",
       
   181 	"cautious",
       
   182 	"cold",
       
   183 	"confident",
       
   184 	"confused",
       
   185 	"contemplative",
       
   186 	"contented",
       
   187 	"cranky",
       
   188 	"crazy",
       
   189 	"creative",
       
   190 	"curious",
       
   191 	"dejected",
       
   192 	"depressed",
       
   193 	"disappointed",
       
   194 	"disgusted",
       
   195 	"dismayed",
       
   196 	"distracted",
       
   197 	"embarrassed",
       
   198 	"envious",
       
   199 	"excited",
       
   200 	"flirtatious",
       
   201 	"frustrated",
       
   202 	"grateful",
       
   203 	"grieving",
       
   204 	"grumpy",
       
   205 	"guilty",
       
   206 	"happy",
       
   207 	"hopeful",
       
   208 	"hot",
       
   209 	"humbled",
       
   210 	"humiliated",
       
   211 	"hungry",
       
   212 	"hurt",
       
   213 	"impressed",
       
   214 	"in_awe",
       
   215 	"in_love",
       
   216 	"indignant",
       
   217 	"interested",
       
   218 	"intoxicated",
       
   219 	"invincible",
       
   220 	"jealous",
       
   221 	"lonely",
       
   222 	"lost",
       
   223 	"lucky",
       
   224 	"mean",
       
   225 	"moody",
       
   226 	"nervous",
       
   227 	"neutral",
       
   228 	"offended",
       
   229 	"outraged",
       
   230 	"playful",
       
   231 	"proud",
       
   232 	"relaxed",
       
   233 	"relieved",
       
   234 	"remorseful",
       
   235 	"restless",
       
   236 	"sad",
       
   237 	"sarcastic",
       
   238 	"satisfied",
       
   239 	"serious",
       
   240 	"shocked",
       
   241 	"shy",
       
   242 	"sick",
       
   243 	"sleepy",
       
   244 	"spontaneous",
       
   245 	"stressed",
       
   246 	"strong",
       
   247 	"surprised",
       
   248 	"thankful",
       
   249 	"thirsty",
       
   250 	"tired",
       
   251 	"undefined",
       
   252 	"weak",
       
   253 	"worried",
       
   254 	NULL,
       
   255 };
       
   256 
       
   257 void mood_init (void)
       
   258 {
       
   259 	mood_cid = compl_new_category ();
       
   260 	if (mood_cid) {
       
   261 		const gchar **mood;
       
   262 
       
   263 		for (mood = defined_moods; *mood; ++mood)
       
   264 			compl_add_category_word (mood_cid, *mood);
       
   265 	}
       
   266 
       
   267 #ifndef MCABBER_API_HAVE_CMD_ID
       
   268 	cmd_add ( "mood", "", mood_cid, 0, do_mood, NULL );
       
   269 #else
       
   270 	mood_cmid     = cmd_add ( "mood", "", mood_cid, 0, do_mood, NULL );
       
   271 	mood_set_safe = cmd_set_safe ( "mood", TRUE );
       
   272 #endif
       
   273 
       
   274 	mood_hid_moodin = hk_add_handler ( mood_hmih, HOOK_MOOD_IN, G_PRIORITY_DEFAULT, NULL );
       
   275 }
       
   276 
       
   277 void mood_uninit (void)
       
   278 {
       
   279 	hk_del_handler ( HOOK_MOOD_IN, mood_hid_moodin );
       
   280 
       
   281 #ifndef MCABBER_API_HAVE_CMD_ID
       
   282 	cmd_del ( "mood" );
       
   283 #else
       
   284 	if ( mood_cmid )
       
   285 		cmd_del ( mood_cmid );
       
   286 	if ( mood_set_safe )
       
   287 		cmd_set_safe ( "mood", FALSE );
       
   288 #endif
       
   289 
       
   290 	if ( mood_cid )
       
   291 		compl_del_category ( mood_cid );
       
   292 }
       
   293 
       
   294 /* vim: se ts=4 sw=4: */