changeset 19:0d7cbc984570

escape pseudo command feature has been added
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Sun, 04 May 2008 17:38:15 +0900
parents d69c47bb8408
children 4c236a7a128f
files pidgin-twitter.c
diffstat 1 files changed, 98 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Thu May 01 19:14:03 2008 +0900
+++ b/pidgin-twitter.c	Sun May 04 17:38:15 2008 +0900
@@ -32,6 +32,8 @@
 
 #define RECIPIENT   0
 #define SENDER      1
+#define COMMAND     2
+#define PSEUDO      3
 
 #define PIDGINTWITTER_PLUGIN_ID	"pidgin_twitter"
 #define OPT_PIDGINTWITTER 		"/plugins/pidgin_twitter"
@@ -41,18 +43,26 @@
 #define OPT_PLAYSOUND_SENDER    OPT_PIDGINTWITTER "/playsound_sender"
 #define OPT_SOUNDID_RECIPIENT   OPT_PIDGINTWITTER "/soundid_recipient"
 #define OPT_SOUNDID_SENDER      OPT_PIDGINTWITTER "/soundid_sender"
+#define OPT_ESCAPE_PSEUDO       OPT_PIDGINTWITTER "/escape_pseudo"
 #define OPT_USERLIST_RECIPIENT  OPT_PIDGINTWITTER "/userlist_recipient"
 #define OPT_USERLIST_SENDER     OPT_PIDGINTWITTER "/userlist_sender"
 #define RECIPIENT_FORMAT        "@<a href='http://twitter.com/%s'>%s</a>"
 #define SENDER_FORMAT           "<a href='http://twitter.com/%s'>%s</a>: "
 #define DEFAULT_LIST             "(list of users: separated with ' ,:;')"
 
+#if 0
+gchar *formats[] =  {
+    "@<a href='http://twitter.com/%s'>%s</a>",
+    "<a href='http://twitter.com/%s'>%s</a>: ",
+};
+#endif
+
 #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, \
 
 /* globals */
-static GRegex *regp[2];
+static GRegex *regp[4];
 
 static gboolean
 eval(const GMatchInfo * match_info, GString * result, gpointer user_data)
@@ -133,7 +143,7 @@
 
 
 static gboolean
-process_im_cb(PurpleAccount * account, char *sender, char **buffer,
+writing_im_cb(PurpleAccount * account, char *sender, char **buffer,
               PurpleConversation * conv, int *flags, void *data)
 {
     const gchar *proto;
@@ -163,12 +173,76 @@
     return FALSE;
 }
 
+static void
+escape(gchar **str)
+{
+    GMatchInfo *match_info = NULL;
+    gchar *newstr = NULL, *match = NULL;
+    gboolean flag = FALSE;
+
+    /* search genuine command first */
+    g_regex_match(regp[COMMAND], *str, 0, &match_info);
+    while(g_match_info_matches(match_info)) {
+        match = g_match_info_fetch(match_info, 1);
+        twitter_debug("command = %s\n", match);
+        g_free(match);
+        g_match_info_next(match_info, NULL);
+        flag = TRUE;
+    }
+    if(flag) {
+        g_match_info_free(match_info);
+        match_info = NULL;
+        return;
+    }
+
+    /* if not found, check "pseudo command" */
+    g_regex_match(regp[PSEUDO], *str, 0, &match_info);
+    while(g_match_info_matches(match_info)) {
+        match = g_match_info_fetch(match_info, 1);
+        twitter_debug("pseudo = %s\n", match);
+        g_free(match);
+        g_match_info_next(match_info, NULL);
+        flag = TRUE;
+    }
+    /* there is pseudo one, escape it */
+    if(flag) {
+        /* put ". " into the beginning of buffer */
+        newstr = g_strdup_printf(". %s", *str);
+        g_free(*str);
+        str = &newstr;
+    }
+
+    g_match_info_free(match_info);
+}
+
+static gboolean
+sending_im_cb(PurpleAccount * account, char *recipient, char **buffer,
+              void *data)
+{
+    const gchar *proto;
+
+    /* check if the message is from twitter */
+    proto = purple_account_get_protocol_id(account);
+    twitter_debug("proto = %s\n", proto);
+    twitter_debug("recipient = %s\n", recipient);
+
+    if(!strcmp(proto, "prpl-jabber") && !strcmp(recipient, "twitter@twitter.com")) {
+        /* escape */
+        if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
+            escape(buffer);
+        }
+    }
+    return FALSE;
+}
+
 static gboolean
 load_plugin(PurplePlugin * plugin)
 {
     /* connect to signal */
     purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg",
-                          plugin, PURPLE_CALLBACK(process_im_cb), NULL);
+                          plugin, PURPLE_CALLBACK(writing_im_cb), NULL);
+    purple_signal_connect(purple_conversations_get_handle(), "sending-im-msg",
+                          plugin, PURPLE_CALLBACK(sending_im_cb), NULL);
 
     return TRUE;
 }
@@ -208,6 +282,11 @@
         "Translate sender name to link");
     purple_plugin_pref_frame_add(frame, pref);
 
+    /* escape pseudo command settings */
+    pref = purple_plugin_pref_new_with_name_and_label(
+        OPT_ESCAPE_PSEUDO,
+        "Escape pseudo command string");
+    purple_plugin_pref_frame_add(frame, pref);
 
     /* sound settings for recipient */
     pref = purple_plugin_pref_new_with_name_and_label(
@@ -282,23 +361,23 @@
     PURPLE_MAJOR_VERSION,
     PURPLE_MINOR_VERSION,
     PURPLE_PLUGIN_STANDARD,     /**< type	*/
-    NULL,                   /**< ui_req	*/
-    0,                  /**< flags	*/
-    NULL,               /**< deps	*/
+    NULL,                       /**< ui_req	*/
+    0,                          /**< flags	*/
+    NULL,                       /**< deps	*/
     PURPLE_PRIORITY_DEFAULT,    /**< priority	*/
     PIDGINTWITTER_PLUGIN_ID,    /**< id		*/
     "Pidgin-Twitter",           /**< name	*/
-    "0.4.0",                    /**< version	*/
+    "0.5.0",                    /**< 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	*/
-    unload_plugin,                  /**< unload	*/
+    load_plugin,                /**< load	*/
+    unload_plugin,              /**< unload	*/
     NULL,                       /**< destroy	*/
     NULL,                       /**< ui_info	*/
     NULL,                       /**< extra_info	*/
-    &pref_info,                     /**< pref info	*/
+    &pref_info,                 /**< pref info	*/
     NULL
 };
 
@@ -311,6 +390,7 @@
     purple_prefs_add_none(OPT_PIDGINTWITTER);
     purple_prefs_add_bool(OPT_TRANSLATE_RECIPIENT, TRUE);
     purple_prefs_add_bool(OPT_TRANSLATE_SENDER, TRUE);
+    purple_prefs_add_bool(OPT_ESCAPE_PSEUDO, TRUE);
 
     purple_prefs_add_bool(OPT_PLAYSOUND_RECIPIENT, TRUE);
     purple_prefs_add_bool(OPT_PLAYSOUND_SENDER, TRUE);
@@ -322,6 +402,14 @@
     /* compile regex */
     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);
+
+    regp[COMMAND]   = g_regex_new(
+        "^(?:\\s*)([dDfFgGlLmMnNtTwW]{1})(?:\\s+[A-Za-z0-9_]+\\Z)",
+        G_REGEX_RAW, 0, NULL);
+
+    regp[PSEUDO]    = g_regex_new(
+        "^(?:\\s*[\"#$\%&\'()*+,-./:;<=>\?\[\\\]_`{|}~]*[^\\s\\x21-\\x7E]*)([dDfFgGlLmMnNtTwW]{1})(?:\\Z|\\s+|[^\\x21-\\x7E]+\\Z)",
+        G_REGEX_RAW, 0, NULL);
 }
 
 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)