changeset 36:ae1d059fa6fe

added oops message suppression functionality. it discards oops message if a sent message is more than 140 bytes and less than 140 letters.
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Sun, 11 May 2008 03:52:06 +0900
parents 7d07fcb9c616
children bafe2abf2d3b
files pidgin-twitter.c
diffstat 1 files changed, 52 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sat May 10 23:08:16 2008 +0900
+++ b/pidgin-twitter.c	Sun May 11 03:52:06 2008 +0900
@@ -55,7 +55,7 @@
 #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 ' ,:;')"
-
+#define OOPS_MESSAGE            "<body>Oops! Your update was over 140 characters. We sent the short version to your friends (they can view the entire update on the web).<BR></body>"
 
 #define twitter_debug(fmt, ...)	purple_debug(PURPLE_DEBUG_INFO, PLUGIN_NAME, \
                                              fmt, ## __VA_ARGS__);
@@ -63,6 +63,13 @@
 
 /* globals */
 static GRegex *regp[5];
+static gboolean suppress_oops = FALSE;
+
+/* prototypes */
+static gboolean is_twitter_conv(PurpleConversation *conv);
+static void attach_to_gtkconv(PidginConversation *gtkconv, gpointer null);
+static void detach_from_gtkconv(PidginConversation *gtkconv, gpointer null);
+static void escape(gchar **str);
 
 static void
 delete_sender_with_link(gchar **str)
@@ -78,13 +85,6 @@
     *str = newstr;
 }
 
-/* prototypes */
-static gboolean is_twitter_conv(PurpleConversation *conv);
-static void attach_to_gtkconv(PidginConversation *gtkconv, gpointer null);
-static void detach_from_gtkconv(PidginConversation *gtkconv, gpointer null);
-static void escape(gchar **str);
-
-
 static void
 escape(gchar **str)
 {
@@ -149,7 +149,15 @@
         if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
             escape(buffer);
         }
+        /* strip excessive link from @username */
         delete_sender_with_link(buffer);
+
+        /* try to suppress Oops message */
+        gint utflen = g_utf8_strlen(*buffer, -1);
+        gint bytes  = strlen(*buffer);
+        twitter_debug("utflen = %d bytes = %d\n", utflen, bytes);
+        if(bytes > 140 && utflen <= 140)
+            suppress_oops = TRUE;
     }
     return FALSE;
 }
@@ -182,7 +190,7 @@
                                   -1,       // length of the subject string
                                   0,        // start position
                                   0,        // match options
-                                  eval,     // function to call for each match
+                                  eval,     // function to be called for each match
                                   &which,   // user data
                                   NULL);    // error handler
 
@@ -286,7 +294,7 @@
 //                  new_text, g_utf8_strlen(new_text, -1), new_text_length);
 
     markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
-                                     count<=140 ? "black" : "red",
+                                     count <= 140 ? "black" : "red",
                                      count);
 
 	box = gtkconv->toolbar;
@@ -313,7 +321,7 @@
              gtk_text_iter_get_offset(start_pos));
 
     markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
-                                     count<=140 ? "black" : "red",
+                                     count <= 140 ? "black" : "red",
                                      count);
 
 	box = gtkconv->toolbar;
@@ -456,6 +464,25 @@
 }
 
 static gboolean
+receiving_im_cb(PurpleAccount *account, char **sender, char **buffer,
+                PurpleConversation *conv, PurpleMessageFlags *flags, void *data)
+{
+    twitter_debug("receiving_im_cb: buffer = %s\n", *buffer);
+
+    twitter_debug("receiving_im_cb: suppress_oops = %d\n", suppress_oops);
+    if(!suppress_oops)
+        return FALSE;
+
+    if(strstr(*buffer, OOPS_MESSAGE)) {
+        twitter_debug("receiving_im_cb: clearing sender and buffer\n");
+        g_free(*sender); *sender = NULL;
+        g_free(*buffer); *buffer = NULL;
+        suppress_oops = FALSE;
+    }
+    return FALSE;
+}
+
+static gboolean
 load_plugin(PurplePlugin * plugin)
 {
     /* connect to signal */
@@ -466,6 +493,8 @@
 	purple_signal_connect(purple_conversations_get_handle(),
                           "conversation-created",
                           plugin, PURPLE_CALLBACK(conv_created_cb), NULL);
+    purple_signal_connect(purple_conversations_get_handle(), "receiving-im-msg",
+                          plugin, PURPLE_CALLBACK(receiving_im_cb), NULL);
 
     /* compile regex */
     regp[RECIPIENT] = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL);
@@ -503,6 +532,9 @@
 	purple_signal_disconnect(purple_conversations_get_handle(),
                              "conversation-created",
                              plugin, PURPLE_CALLBACK(conv_created_cb));
+    purple_signal_disconnect(purple_conversations_get_handle(),
+                             "receiving-im-msg",
+                             plugin, PURPLE_CALLBACK(receiving_im_cb));
 
     /* unreference regp */
     g_regex_unref(regp[RECIPIENT]);
@@ -534,11 +566,12 @@
 get_plugin_pref_frame(PurplePlugin * plugin)
 {
     /* create gtk elements for the plugin preferences */
-
     PurplePluginPref *pref;
     PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new();
 
-    /* frame title */
+    /************************/
+    /* translatione heading */
+    /************************/
     pref = purple_plugin_pref_new_with_label("Translation Configurations");
     purple_plugin_pref_frame_add(frame, pref);
 
@@ -554,10 +587,9 @@
     purple_plugin_pref_frame_add(frame, pref);
 
 
-
-
-
-    /* frame title */
+    /*************************/
+    /* miscellaneous heading */
+    /*************************/
     pref = purple_plugin_pref_new_with_label("Miscellaneous Configurations");
     purple_plugin_pref_frame_add(frame, pref);
 
@@ -573,15 +605,12 @@
         "Show text counter");
     purple_plugin_pref_frame_add(frame, pref);
 
-    /* xxx */
 	purple_prefs_connect_callback(plugin, OPT_COUNTER,
 								counter_prefs_cb, NULL);
 
-
-
-
-
-    /* frame title */
+    /*****************/
+    /* sound heading */
+    /*****************/
     pref = purple_plugin_pref_new_with_label("Sound Configurations");
     purple_plugin_pref_frame_add(frame, pref);
 
@@ -615,7 +644,6 @@
 
 	purple_plugin_pref_frame_add(frame, pref);
 
-
     /* sound setting for sender */
     pref = purple_plugin_pref_new_with_name_and_label(
         OPT_PLAYSOUND_SENDER,