changeset 20:4c236a7a128f

- now escape feature works - rearrange the order of functions - some cleanups
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Sun, 04 May 2008 19:27:07 +0900
parents 0d7cbc984570
children 38fe566b5ee1
files pidgin-twitter.c
diffstat 1 files changed, 80 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sun May 04 17:38:15 2008 +0900
+++ b/pidgin-twitter.c	Sun May 04 19:27:07 2008 +0900
@@ -50,13 +50,6 @@
 #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, \
@@ -64,6 +57,73 @@
 /* globals */
 static GRegex *regp[4];
 
+static void
+escape(gchar **str)
+{
+    GMatchInfo *match_info = NULL;
+    gchar *newstr = NULL, *match = NULL;
+    gboolean flag = FALSE;
+
+    /* search genuine command */
+    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;
+    }
+    g_match_info_free(match_info);
+    match_info = NULL;
+
+    if(flag) {
+        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;
+    }
+    g_match_info_free(match_info);
+    match_info = NULL;
+
+    /* if there is pseudo one, escape it */
+    if(flag) {
+        /* put ". " to the beginning of buffer */
+        newstr = g_strdup_printf(". %s", *str);
+        twitter_debug("*str = %s\n", *str);
+        twitter_debug("newstr = %s\n", newstr);
+        g_free(*str);
+        *str = newstr;
+    }
+}
+
+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("sending_im_cb\n");
+    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
 eval(const GMatchInfo * match_info, GString * result, gpointer user_data)
 {
@@ -87,13 +147,13 @@
 
     twitter_debug("*str = %s\n", *str);
 
-    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, // function to call for each match
-                                  &which, // user data
+    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,     // function to call for each match
+                                  &which,   // user data
                                   NULL);    // error handler
 
     twitter_debug("newstr = %s\n", newstr);
@@ -115,8 +175,7 @@
         return;
 
     candidates = g_strsplit_set(list, " ,:;", 0);
-    if(!candidates)
-        return;
+    g_return_if_fail(candidates != NULL);
 
     g_regex_match(regp[which], *str, 0, &match_info);
     while(g_match_info_matches(match_info)) {
@@ -141,7 +200,6 @@
     g_match_info_free(match_info);
 }
 
-
 static gboolean
 writing_im_cb(PurpleAccount * account, char *sender, char **buffer,
               PurpleConversation * conv, int *flags, void *data)
@@ -150,10 +208,12 @@
 
     /* check if the message is from twitter */
     proto = purple_account_get_protocol_id(account);
+    twitter_debug("writing_im_cb\n");
     twitter_debug("proto = %s\n", proto);
     twitter_debug("sender = %s\n", sender);
 
-    if(!strcmp(proto, "prpl-jabber") && !strcmp(sender, "twitter@twitter.com")) {
+    if(!strcmp(sender, "twitter@twitter.com") &&
+       !strcmp(proto, "prpl-jabber")) {
         /* playsound */
         if(purple_prefs_get_bool(OPT_PLAYSOUND_SENDER)) {
             playsound(buffer, SENDER);
@@ -169,64 +229,7 @@
         if(purple_prefs_get_bool(OPT_TRANSLATE_RECIPIENT)) {
             translate(buffer, RECIPIENT);
         }
-    }
-    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);
@@ -404,11 +407,11 @@
     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)",
+        "^(?:\\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)",
+        "^(?:\\s*[\"#$\%&\'()*+,-./:;<=>\?\[\\]_`{|}~]*[^\\s\\x21-\\x7E]*)([dDfFgGlLmMnNtTwW]{1})(?:\\Z|\\s+|[^\\x21-\\x7E]+\\Z)",
         G_REGEX_RAW, 0, NULL);
 }