changeset 82:c9600d64781a

work in progress adaptation to wassr service. cleanups required: - sender/recipient translation have been added. - added work around for "parrot problem" in sending to a channel.
author Yoshiki Yazawa <yaz@honeyplnaet.jp>
date Sat, 05 Jul 2008 16:26:39 +0900
parents f1163f2e4920
children 15f4886720e4
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 114 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sat Jul 05 13:47:26 2008 +0900
+++ b/pidgin-twitter.c	Sat Jul 05 16:26:39 2008 +0900
@@ -25,6 +25,9 @@
 static gboolean suppress_oops = FALSE;
 static GHashTable *icon_data_by_user = NULL;
 
+#define WASSR_POST_LEN (255 * 4)
+static gchar wassr_post[WASSR_POST_LEN + 1];
+
 typedef struct icon_data {
     gint icon_id;        // image id
     gboolean requested;  // TRUE if download icon has been requested
@@ -32,6 +35,15 @@
     PurpleUtilFetchUrlData *fetch_data;          // icon fetch data
 } icon_data;
 
+enum {
+    TWITTER_SERVICE,
+    WASSR_SERVICE
+}
+
+typedef struct _eval_data {
+    int which;
+    int service;
+} eval_data;
 
 /* this function is a modified clone of purple_markup_strip_html() */
 static char *
@@ -446,6 +458,12 @@
 
     twitter_debug("called\n");
 
+    if(is_wassr_account(account, recipient)) {
+        /* store sending message to address parrot problem */
+        g_strlcpy(wassr_post, *buffer, WASSR_POST_LEN);
+        twitter_debug("parrot pushed:%s\n", *buffer);
+    }
+
     /* check if the message is from twitter */
     if(!is_twitter_account(account, recipient))
         return FALSE;
@@ -454,7 +472,8 @@
     strip_markup(buffer);
 
     /* escape pseudo command */
-    if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
+    if(is_twitter_account(account, recipient) &&
+       purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
         escape(buffer);
     }
 
@@ -480,21 +499,35 @@
 static gboolean
 eval(const GMatchInfo *match_info, GString *result, gpointer user_data)
 {
-    int which = *(int *)user_data;
+    eval_data *data = (eval_data *)user_data;
+    int which = data->which;
+    int service = data->service;
     gchar sub[128];
 
+    twitter_debug("which = %d service = %d\n", which, service);
+
     if(which == RECIPIENT) {
         gchar *match = g_match_info_fetch(match_info, 1);
 
-        snprintf(sub, 128, RECIPIENT_FORMAT, match, match);
+        if(service == TWITTER_SERVICE)
+            snprintf(sub, 128, RECIPIENT_FORMAT, match, match);
+        else if(service == WASSR_SERVICE)
+            snprintf(sub, 128, RECIPIENT_FORMAT_WASSR, match, match);
+
         g_free(match);
     }
     else if(which == SENDER) {
         gchar *match1 = g_match_info_fetch(match_info, 1); //preceding CR|LF
         gchar *match2 = g_match_info_fetch(match_info, 2); //sender
 
-        snprintf(sub, 128, SENDER_FORMAT, match1 ? match1: "",
-                 match2, match2);
+        if(service == TWITTER_SERVICE) {
+            snprintf(sub, 128, SENDER_FORMAT, match1 ? match1: "",
+                     match2, match2);
+        }
+        else if(service == WASSR_SERVICE) {
+            snprintf(sub, 128, SENDER_FORMAT_WASSR, match1 ? match1: "",
+                     match2, match2);
+        }
         g_free(match1);
         g_free(match2);
     }
@@ -506,19 +539,26 @@
 }
 
 static void
-translate(gchar **str, int which)
+translate(gchar **str, int which, int service)
 {
     gchar *newstr;
 
+    eval_data *data = g_new0(eval_data, 1);
+
+    data->which = which;
+    data->service = service;
+
     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 be called for each match
-                                  &which,   // user data
+                                  data,   // user data
                                   NULL);    // error handler
 
+    g_free(data); data = NULL;
+
     twitter_debug("which = %d *str = %s newstr = %s\n", which, *str, newstr);
 
     g_free(*str);
@@ -575,9 +615,8 @@
 {
     twitter_debug("called\n");
 
-    /* check if the message is from twitter */
-    if(!is_twitter_conv(conv))
-        /*  if(!is_twitter_account(account, sender)) */
+    /* check if the conversation is between twitter */
+    if(!is_twitter_conv(conv) && !is_wassr_conv(conv))
         return FALSE;
 
     /* strip all markups */
@@ -591,16 +630,23 @@
         playsound(buffer, RECIPIENT);
     }
 
+    /* XXX should cleanup */
+    int service = 0;
+    if(is_twitter_conv(conv))
+        service = TWITTER_SERVICE;
+    else if(is_wassr_conv(conv))
+        service = WASSR_SERVICE;
+
     /* translate */
     if(purple_prefs_get_bool(OPT_TRANSLATE_SENDER)) {
-        translate(buffer, SENDER);
+        translate(buffer, SENDER, service);
     }
     if(purple_prefs_get_bool(OPT_TRANSLATE_RECIPIENT)) {
-        translate(buffer, RECIPIENT);
+        translate(buffer, RECIPIENT, service);
     }
 
     /* escape pseudo command (to show same result to sending message) */
-    if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
+    if(is_twitter_conv(conv) && purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
         escape(buffer);
     }
 
@@ -611,7 +657,9 @@
 insert_text_cb(GtkTextBuffer *textbuffer, GtkTextIter *position,
                gchar *new_text, gint new_text_length, gpointer user_data)
 {
-    PidginConversation *gtkconv = (PidginConversation *)user_data;
+    PurpleConversation *conv = (PurpleConversation *)user_data;
+    PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
+
     GtkWidget *box, *counter = NULL;
     gchar *markup = NULL;
     guint count;
@@ -621,8 +669,14 @@
     count = gtk_text_buffer_get_char_count(textbuffer) +
         (unsigned int)g_utf8_strlen(new_text, -1);
 
-    markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
-                                     count <= 140 ? "black" : "red", count);
+    if(is_twitter_conv(conv)) {
+        markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
+                                         count <= 140 ? "black" : "red", count);
+    }
+    else if(is_wassr_conv(conv)) {
+        markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
+                                         count <= 255 ? "black" : "red", count);
+    }
 
     box = gtkconv->toolbar;
     counter = g_object_get_data(G_OBJECT(box), PLUGIN_ID "-counter");
@@ -636,7 +690,8 @@
 delete_text_cb(GtkTextBuffer *textbuffer, GtkTextIter *start_pos,
                GtkTextIter *end_pos, gpointer user_data)
 {
-    PidginConversation *gtkconv = (PidginConversation *)user_data;
+    PurpleConversation *conv = (PurpleConversation *)user_data;
+    PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
     GtkWidget *box, *counter = NULL;
     gchar *markup = NULL;
 
@@ -646,8 +701,14 @@
         (gtk_text_iter_get_offset(end_pos) -
          gtk_text_iter_get_offset(start_pos));
 
-    markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
-                                     count <= 140 ? "black" : "red", count);
+    if(is_twitter_conv(conv)) {
+        markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
+                                         count <= 140 ? "black" : "red", count);
+    }
+    else if(is_wassr_conv(conv)) {
+        markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>",
+                                         count <= 255 ? "black" : "red", count);
+    }
 
     box = gtkconv->toolbar;
     counter = g_object_get_data(G_OBJECT(box), PLUGIN_ID "-counter");
@@ -668,19 +729,22 @@
         PurpleConversation *conv =
             pidgin_conv_window_get_active_conversation(win);
         if(is_twitter_conv(conv))
-            detach_from_gtkconv(PIDGIN_CONVERSATION(conv), NULL);
+            detach_from_conv(conv, NULL);
+        if(is_wassr_conv(conv))
+            detach_from_conv(conv, NULL);
     }
 }
 
 static void
-detach_from_gtkconv(PidginConversation *gtkconv, gpointer null)
+detach_from_conv(PurpleConversation *conv, gpointer null)
 {
+    PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
     GtkWidget *box, *counter = NULL, *sep = NULL;
 
     g_signal_handlers_disconnect_by_func(G_OBJECT(gtkconv->entry_buffer),
-                                         (GFunc) insert_text_cb, gtkconv);
+                                         (GFunc) insert_text_cb, conv);
     g_signal_handlers_disconnect_by_func(G_OBJECT(gtkconv->entry_buffer),
-                                         (GFunc) delete_text_cb, gtkconv);
+                                         (GFunc) delete_text_cb, conv);
 
     box = gtkconv->toolbar;
 
@@ -765,13 +829,16 @@
             pidgin_conv_window_get_active_conversation(win);
         /* only attach to twitter conversation window */
         if(is_twitter_conv(conv))
-            attach_to_gtkconv(PIDGIN_CONVERSATION(conv), NULL);
+            attach_to_conv(conv, NULL);
+        if(is_wassr_conv(conv))
+            attach_to_conv(conv, NULL);
     }
 }
 
 static void
-attach_to_gtkconv(PidginConversation *gtkconv, gpointer null)
+attach_to_conv(PurpleConversation *conv, gpointer null)
 {
+    PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
     GtkWidget *box, *sep, *counter, *menus;
     GtkIMHtml *imhtml;
 
@@ -820,9 +887,9 @@
 
     /* connect to signals */
     g_signal_connect(G_OBJECT(gtkconv->entry_buffer), "insert_text",
-                     G_CALLBACK(insert_text_cb), gtkconv);
+                     G_CALLBACK(insert_text_cb), conv);
     g_signal_connect(G_OBJECT(gtkconv->entry_buffer), "delete_range",
-                     G_CALLBACK(delete_text_cb), gtkconv);
+                     G_CALLBACK(delete_text_cb), conv);
 
     /* redraw window */
     gtk_widget_queue_draw(pidgin_conv_get_window(gtkconv)->window);
@@ -859,7 +926,7 @@
 
     twitter_debug("name  = %s proto = %s\n", name, proto);
 
-    if(!strcmp(name, "wassr-bot@wassr.jp/bot") &&
+    if(strstr(name, "wassr-bot@wassr.jp") &&
        !strcmp(proto, "prpl-jabber")) {
         return TRUE;
     }
@@ -884,7 +951,9 @@
 
     /* only attach to twitter conversation window */
     if(is_twitter_conv(conv))
-        attach_to_gtkconv(gtkconv, NULL);
+        attach_to_conv(conv, NULL);
+    if(is_wassr_conv(conv))
+        attach_to_conv(conv, NULL);
 }
 
 static void
@@ -896,6 +965,10 @@
     /* only attach to twitter conversation window */
     if(is_twitter_conv(conv))
         delete_requested_icon_marks(gtkconv);
+#if 0
+    if(is_wassr_conv(conv))
+        delete_requested_icon_marks(gtkconv);
+#endif
 }
 
 static gboolean
@@ -912,11 +985,17 @@
     /* quick hack to suppress annoying completion message from wassr */
     if(conv && is_wassr_conv(conv)) {
         if(strstr(*buffer, "<body>投稿完了:") ||
-	   strstr(*buffer, "<body>チャンネル投稿完了:")) {
+           strstr(*buffer, "<body>チャンネル投稿完了:")) {
             twitter_debug("clearing sender and buffer\n");
             g_free(*sender); *sender = NULL;
             g_free(*buffer); *buffer = NULL;
         }
+        /* fix for parrot problem during post to a channel */
+        else if(strstr(*buffer, wassr_post)) {
+            twitter_debug("parrot clearing: %s\n", *buffer);
+            g_free(*sender); *sender = NULL;
+            g_free(*buffer); *buffer = NULL;
+        }
     }
 
     if(!(conv && is_twitter_conv(conv))) {
--- a/pidgin-twitter.h	Sat Jul 05 13:47:26 2008 +0900
+++ b/pidgin-twitter.h	Sat Jul 05 16:26:39 2008 +0900
@@ -50,6 +50,8 @@
 /* formats and templates */
 #define RECIPIENT_FORMAT        "@<a href='http://twitter.com/%s'>%s</a>"
 #define SENDER_FORMAT           "%s<a href='http://twitter.com/%s'>%s</a>: "
+#define RECIPIENT_FORMAT_WASSR  "@<a href='http://wassr.jp/user/%s'>%s</a>"
+#define SENDER_FORMAT_WASSR     "%s<a href='http://wassr.jp/user/%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 EMPTY                   ""
@@ -71,16 +73,16 @@
 static void escape(gchar **str);
 static gboolean sending_im_cb(PurpleAccount *account, char *recipient, char **buffer, void *data);
 static gboolean eval(const GMatchInfo *match_info, GString *result, gpointer user_data);
-static void translate(gchar **str, int which);
+static void translate(gchar **str, int which, int service);
 static void playsound(gchar **str, int which);
 static gboolean writing_im_cb(PurpleAccount *account, char *sender, char **buffer, PurpleConversation *conv, int *flags, void *data);
 static void insert_text_cb(GtkTextBuffer *textbuffer, GtkTextIter *position, gchar *new_text, gint new_text_length, gpointer user_data);
 static void delete_text_cb(GtkTextBuffer *textbuffer, GtkTextIter *start_pos, GtkTextIter *end_pos, gpointer user_data);
 static void detach_from_window(void);
-static void detach_from_gtkconv(PidginConversation *gtkconv, gpointer null);
+static void detach_from_conv(PurpleConversation *conv, gpointer null);
 static void delete_requested_icon_marks(PidginConversation *gtkconv);
 static void attach_to_window(void);
-static void attach_to_gtkconv(PidginConversation *gtkconv, gpointer null);
+static void attach_to_conv(PurpleConversation *conv, gpointer null);
 static gboolean is_twitter_account(PurpleAccount *account, const char *name);
 static gboolean is_twitter_conv(PurpleConversation *conv);
 static gboolean is_wassr_account(PurpleAccount *account, const char *name);