changeset 103:8de54d420328

added preliminary identi.ca icon support.
author Yoshiki Yazawa <yaz@honeyplnaet.jp>
date Mon, 14 Jul 2008 00:17:18 +0900
parents 55c466028d48
children 084e1c6de8ca
files pidgin-twitter.c pidgin-twitter.h
diffstat 2 files changed, 88 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/pidgin-twitter.c	Sun Jul 13 18:53:45 2008 +0900
+++ b/pidgin-twitter.c	Mon Jul 14 00:17:18 2008 +0900
@@ -21,11 +21,11 @@
 #include "pidgin-twitter.h"
 
 /* globals */
-static GRegex *regp[8];
+static GRegex *regp[9];
 static gboolean suppress_oops = FALSE;
-static GHashTable *icon_data_by_user = NULL;
-static GHashTable *icon_data_by_user2 = NULL;
-static GHashTable *icon_data_by_user3 = NULL;
+static GHashTable *icon_data_by_user  = NULL; // twitter
+static GHashTable *icon_data_by_user2 = NULL; // wassr
+static GHashTable *icon_data_by_user3 = NULL; // identi.ca
 static GHashTable *conv_hash = NULL;
 
 #define WASSR_POST_LEN (255 * 4)
@@ -1325,8 +1325,60 @@
 }
 
 static void
+got_page_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
+            const gchar *url_text, gsize len, const gchar *error_message)
+{
+    got_icon_data *gotdata = (got_icon_data *)user_data;
+    gchar *user_name = gotdata->user_name;
+//    gint service = gotdata->service;
+    GMatchInfo *match_info = NULL;
+    icon_data *data = NULL;
+    gchar *url = NULL;
+
+    data = (icon_data *)g_hash_table_lookup(icon_data_by_user3, user_name);
+
+    if(!url_text) {
+        if(data) {
+            data->requested = FALSE;
+            data->fetch_data = NULL;
+        }
+        g_free(gotdata->user_name);
+        g_free(gotdata);
+        return;
+    }
+
+    /* setup image url */
+    g_regex_match(regp[IMAGE_IDENTICA], url_text, 0, &match_info);
+    if(!g_match_info_matches(match_info)) {
+        twitter_debug("no image found\n");
+
+        g_match_info_free(match_info);
+        if(data) {
+            data->requested = FALSE;
+            data->fetch_data = NULL;
+        }
+        g_free(gotdata->user_name);
+        g_free(gotdata);
+        return;
+    }
+    url = g_match_info_fetch(match_info, 1);
+    g_match_info_free(match_info);
+
+    /* request fetch image */
+    if(url) {
+        /* reuse gotdata. just pass given one */
+        /* gotdata will be released in got_icon_cb */
+        data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
+                                                 got_icon_cb, gotdata);
+        g_free(url); url = NULL;
+
+        twitter_debug("request %s's icon\n", user_name);
+    }
+}
+
+static void
 got_icon_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
-                   const gchar *url_text, gsize len, const gchar *error_message)
+            const gchar *url_text, gsize len, const gchar *error_message)
 {
     got_icon_data *gotdata = (got_icon_data *)user_data;
     gchar *user_name = gotdata->user_name;
@@ -1539,7 +1591,7 @@
                               user_name);
         break;
     case identica_service:
-        twitter_debug("identica icon support has not implemented yet.\n");
+        url = g_strdup_printf("http://identi.ca/%s", user_name);
         break;
     default:
         twitter_debug("unknown service\n");
@@ -1552,8 +1604,14 @@
         gotdata->service = service;
 
         /* gotdata will be released in got_icon_cb */
-        data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
-                                                 got_icon_cb, gotdata);
+        if(service == identica_service) {
+            data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
+                                                     got_page_cb, gotdata);
+        }
+        else {
+            data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE,
+                                                     got_icon_cb, gotdata);
+        }
         g_free(url); url = NULL;
 
         twitter_debug("request %s's icon\n", user_name);
@@ -1738,6 +1796,7 @@
     regp[USER_FIRST_LINE] = g_regex_new(P_USER_FIRST_LINE, 0, 0, NULL);
     regp[USER_FORMATTED]  = g_regex_new(P_USER_FORMATTED, G_REGEX_RAW, 0, NULL);
     regp[CHANNEL]   = g_regex_new(P_CHANNEL, 0, 0, NULL);
+    regp[IMAGE_IDENTICA]  = g_regex_new(P_IMAGE_IDENTICA, 0, 0, NULL);
 
 
     icon_data_by_user = g_hash_table_new_full(g_str_hash, g_str_equal,
@@ -1814,6 +1873,7 @@
     g_regex_unref(regp[USER_FIRST_LINE]);
     g_regex_unref(regp[USER_FORMATTED]);
     g_regex_unref(regp[CHANNEL]);
+    g_regex_unref(regp[IMAGE_IDENTICA]);
 
     /* remove mark list in each hash entry */
     g_hash_table_foreach(icon_data_by_user, (GHFunc)remove_marks_func, NULL);
@@ -2070,8 +2130,6 @@
     purple_prefs_add_bool(OPT_PREVENT_NOTIFICATION, FALSE);
     purple_prefs_add_bool(OPT_SHOW_ICON, TRUE);
 
-
-
     purple_prefs_add_bool(OPT_API_BASE_POST, FALSE);
     purple_prefs_add_string(OPT_SCREEN_NAME, EMPTY);
     purple_prefs_add_string(OPT_PASSWORD, EMPTY);
--- a/pidgin-twitter.h	Sun Jul 13 18:53:45 2008 +0900
+++ b/pidgin-twitter.h	Mon Jul 14 00:17:18 2008 +0900
@@ -17,14 +17,17 @@
 #include "gtkimhtml.h"
 
 /* regp id */
-#define RECIPIENT        0
-#define SENDER           1
-#define COMMAND          2
-#define PSEUDO           3
-#define USER             4
-#define USER_FIRST_LINE  5
-#define USER_FORMATTED   6
-#define CHANNEL          7
+enum {
+    RECIPIENT = 0,
+    SENDER,
+    COMMAND,
+    PSEUDO,
+    USER,
+    USER_FIRST_LINE,
+    USER_FORMATTED,
+    CHANNEL,
+    IMAGE_IDENTICA
+};
 
 #define PLUGIN_ID	            "gtk-honeyplanet-pidgin_twitter"
 #define PLUGIN_NAME	            "pidgin-twitter"
@@ -63,14 +66,15 @@
 #define EMPTY                   ""
 
 /* patterns */
-#define P_RECIPIENT        "@([A-Za-z0-9_]+)"
-#define P_SENDER           "^(\\r?\\n?)([A-Za-z0-9_]+): "
-#define P_COMMAND          "^(?:\\s*)([dDfFgGlLmMnNtTwW]{1}\\s+[A-Za-z0-9_]+)(?:\\s*\\Z)"
-#define P_PSEUDO           "^\\s*(?:[\"#$%&'()*+,\\-./:;<=>?\\[\\\\\\]_`{|}~]|[^\\s\\x21-\\x7E])*([dDfFgGlLmMnNtTwW]{1})(?:\\Z|\\s+|[^\\x21-\\x7E]+\\Z)"
-#define P_USER             "^\\(.+?\\)\\s*([A-Za-z0-9_]+):"
-#define P_USER_FIRST_LINE  "^\\(.+?\\)\\s*.+:\\s*([A-Za-z0-9_]+):"
-#define P_USER_FORMATTED   "^.*?<a .+?>([A-Za-z0-9_]+)</a>:"
-#define P_CHANNEL          "^(.*?<a .+?>[A-Za-z0-9_]+</a>: #)([A-Za-z0-9_]+) "
+#define P_RECIPIENT         "@([A-Za-z0-9_]+)"
+#define P_SENDER            "^(\\r?\\n?)([A-Za-z0-9_]+): "
+#define P_COMMAND           "^(?:\\s*)([dDfFgGlLmMnNtTwW]{1}\\s+[A-Za-z0-9_]+)(?:\\s*\\Z)"
+#define P_PSEUDO            "^\\s*(?:[\"#$%&'()*+,\\-./:;<=>?\\[\\\\\\]_`{|}~]|[^\\s\\x21-\\x7E])*([dDfFgGlLmMnNtTwW]{1})(?:\\Z|\\s+|[^\\x21-\\x7E]+\\Z)"
+#define P_USER              "^\\(.+?\\)\\s*([A-Za-z0-9_]+):"
+#define P_USER_FIRST_LINE   "^\\(.+?\\)\\s*.+:\\s*([A-Za-z0-9_]+):"
+#define P_USER_FORMATTED    "^.*?<a .+?>([A-Za-z0-9_]+)</a>:"
+#define P_CHANNEL           "^(.*?<a .+?>[A-Za-z0-9_]+</a>: #)([A-Za-z0-9_]+) "
+#define P_IMAGE_IDENTICA    "<img src=\"(http://avatar.identi.ca/[0-9-]+.png)\" class=\"avatar profile\" width=\"96\" height=\"96\" alt=\"[A-Za-z0-0_]+\"/>"
 
 /* debug macros */
 #define twitter_debug(fmt, ...)	purple_debug(PURPLE_DEBUG_INFO, PLUGIN_NAME, "%s():%4d:  " fmt, __FUNCTION__, (int)__LINE__, ## __VA_ARGS__);