changeset 10:4bd8c89b4749

now it plays sound when sender of message is in the userlist.
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Thu, 01 May 2008 13:37:49 +0900
parents c6b80f47d4df
children 7ea2a717af42
files pidgin-twitter.c
diffstat 1 files changed, 41 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Thu May 01 05:50:48 2008 +0900
+++ b/pidgin-twitter.c	Thu May 01 13:37:49 2008 +0900
@@ -30,12 +30,15 @@
 #include "version.h"
 #include "sound.h"
 
+#define RECIPIENT   0
+#define SENDER      1
 
 #define PIDGINTWITTER_PLUGIN_ID	"pidgin_twitter"
 #define OPT_PIDGINTWITTER 		"/plugins/pidgin_twitter"
 #define OPT_TRANSLATE_RECIPIENT OPT_PIDGINTWITTER "/translate_recipient"
 #define OPT_TRANSLATE_SENDER    OPT_PIDGINTWITTER "/translate_sender"
-#define OPT_PLAYSOUND           OPT_PIDGINTWITTER "/playsound"
+#define OPT_PLAYSOUND_RECIPIENT OPT_PIDGINTWITTER "/playsound_recipient"
+#define OPT_PLAYSOUND_SENDER    OPT_PIDGINTWITTER "/playsound_sender"
 #define OPT_SOUNDID             OPT_PIDGINTWITTER "/soundid"
 #define OPT_USERLIST            OPT_PIDGINTWITTER "/userlist"
 #define RECIPIENT_FORMAT        "@<a href='http://twitter.com/%s'>%s</a>"
@@ -45,19 +48,19 @@
 #define twitter_debug(fmt, ...)	purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \
                                              fmt, ## __VA_ARGS__);
 #define twitter_error(fmt, ...)	purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \
-                                             fmt, ## __VA_ARGS__);
 
 /* globals */
-static GRegex *regrcpt;
-static GRegex *regsndr;
+static GRegex *regp[2];
 
 static gboolean
-eval_sender(const GMatchInfo * match_info, GString * result, gpointer user_data)
+eval(const GMatchInfo * match_info, GString * result, gpointer user_data)
 {
     gchar sub[128];
     gchar *match = g_match_info_fetch(match_info, 1);
 
-    snprintf(sub, 128, SENDER_FORMAT, match, match);
+    int which = *(int *)user_data;
+
+    snprintf(sub, 128, which ? SENDER_FORMAT : RECIPIENT_FORMAT, match, match);
     twitter_debug("sub = %s\n", sub);
     g_string_append(result, sub);
     g_free(match);
@@ -66,55 +69,19 @@
 }
 
 static void
-translate_sender(gchar **str)
+translate(gchar **str, int which)
 {
     gchar *newstr;
 
     twitter_debug("*str = %s\n", *str);
 
-    newstr = g_regex_replace_eval(regsndr, // compiled regex
+    newstr = g_regex_replace_eval(regp[which], // compiled regex
                                   *str, // subject string
                                   -1,   // length of the subject string
                                   0,    // start position
                                   0,    // match options
-                                  eval_sender, // function to call for each match
-                                  NULL, // user data (not used)
-                                  NULL);    // error handler
-
-    twitter_debug("newstr = %s\n", newstr);
-
-    g_free(*str);
-    *str = newstr;
-}
-
-static gboolean
-eval_recipient(const GMatchInfo * match_info, GString * result, gpointer user_data)
-{
-    gchar sub[128];
-    gchar *match = g_match_info_fetch(match_info, 1);
-
-    snprintf(sub, 128, RECIPIENT_FORMAT, match, match);
-    twitter_debug("sub = %s\n", sub);
-    g_string_append(result, sub);
-    g_free(match);
-
-    return FALSE;
-}
-
-static void
-translate_recipient(gchar **str)
-{
-    gchar *newstr;
-
-    twitter_debug("*str = %s\n", *str);
-
-    newstr = g_regex_replace_eval(regrcpt, // compiled regex
-                                  *str, // subject string
-                                  -1,   // length of the subject string
-                                  0,    // start position
-                                  0,    // match options
-                                  eval_recipient, // function to call for each match
-                                  NULL, // user data (not used)
+                                  eval, // function to call for each match
+                                  &which, // user data
                                   NULL);    // error handler
 
     twitter_debug("newstr = %s\n", newstr);
@@ -124,7 +91,7 @@
 }
 
 static void
-playsound(gchar **str)
+playsound(gchar **str, int which)
 {
     GMatchInfo *match_info;
     const gchar *list;
@@ -139,7 +106,7 @@
     if(!candidates)
         return;
 
-    g_regex_match(regrcpt, *str, 0, &match_info);
+    g_regex_match(regp[which], *str, 0, &match_info);
     while(g_match_info_matches(match_info)) {
         gchar *user = g_match_info_fetch(match_info, 1);
         twitter_debug("user = %s\n", user);
@@ -175,17 +142,20 @@
     twitter_debug("sender = %s\n", sender);
 
     if(!strcmp(proto, "prpl-jabber") && !strcmp(sender, "twitter@twitter.com")) {
-        if(purple_prefs_get_bool(OPT_PLAYSOUND)) {
-            /* playsound */
-            playsound(buffer);
+        /* playsound */
+        if(purple_prefs_get_bool(OPT_PLAYSOUND_SENDER)) {
+            playsound(buffer, SENDER);
         }
+        if(purple_prefs_get_bool(OPT_PLAYSOUND_RECIPIENT)) {
+            playsound(buffer, RECIPIENT);
+        }
+
+        /* translate */
         if(purple_prefs_get_bool(OPT_TRANSLATE_SENDER)) {
-            /* translate */
-            translate_sender(buffer);
+            translate(buffer, SENDER);
         }
         if(purple_prefs_get_bool(OPT_TRANSLATE_RECIPIENT)) {
-            /* translate */
-            translate_recipient(buffer);
+            translate(buffer, RECIPIENT);
         }
     }
     return FALSE;
@@ -206,8 +176,8 @@
 {
     twitter_debug("pidgin-twitter unload called\n");
 
-    g_regex_unref(regrcpt);
-    g_regex_unref(regsndr);
+    g_regex_unref(regp[RECIPIENT]);
+    g_regex_unref(regp[SENDER]);
 
     return TRUE;
 }
@@ -233,8 +203,13 @@
     purple_plugin_pref_frame_add(frame, pref);
 
     pref = purple_plugin_pref_new_with_name_and_label(
-        OPT_PLAYSOUND,
-        "Play a sound on a reply to the user in the Userlist");
+        OPT_PLAYSOUND_RECIPIENT,
+        "Play a sound on an arrival of reply to the user in the Userlist");
+    purple_plugin_pref_frame_add(frame, pref);
+
+    pref = purple_plugin_pref_new_with_name_and_label(
+        OPT_PLAYSOUND_SENDER,
+        "Play a sound if sender of a message is in the Userlist");
     purple_plugin_pref_frame_add(frame, pref);
 
     /* sound id selector */
@@ -279,9 +254,9 @@
     PURPLE_PRIORITY_DEFAULT,    /**< priority	*/
     PIDGINTWITTER_PLUGIN_ID,    /**< id		*/
     "Pidgin-Twitter",           /**< name	*/
-    "0.3.0",                    /**< version	*/
-    "replaces @username in a message with link to the user", /**  summary	*/
-    "replaces @username in a message with link to the user", /**  desc	*/
+    "0.3.1",                    /**< version	*/
+    "replaces @username to a link and play sound", /**  summary	*/
+    "replaces @username in a link and play sound", /**  desc	*/
     "Yoshiki Yazawa (yaz@honeyplanet.jp)",     /**< author	*/
     "http://www.honeyplanet.jp/",   /**< homepage	*/
     load_plugin,                    /**< load	*/
@@ -303,13 +278,14 @@
     purple_prefs_add_bool(OPT_TRANSLATE_RECIPIENT, TRUE);
     purple_prefs_add_bool(OPT_TRANSLATE_SENDER, TRUE);
 
-    purple_prefs_add_bool(OPT_PLAYSOUND, TRUE);
+    purple_prefs_add_bool(OPT_PLAYSOUND_RECIPIENT, TRUE);
+    purple_prefs_add_bool(OPT_PLAYSOUND_SENDER, FALSE);
     purple_prefs_add_int(OPT_SOUNDID, PURPLE_SOUND_POUNCE_DEFAULT);
     purple_prefs_add_string(OPT_USERLIST, DEFAULT_LIST);
 
     /* compile regex */
-    regrcpt = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL);
-    regsndr = g_regex_new("^<body>([A-Za-z0-9_]+): ", 0, 0, NULL);
+    regp[RECIPIENT] = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL);
+    regp[SENDER]    = g_regex_new("^<body>([A-Za-z0-9_]+): ", 0, 0, NULL);
 }
 
 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)