diff util.c @ 330:cc41ee1f5d3a

implemented reply, favorite, retweet functionalities. these are quite raw, be careful!
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 12 Oct 2009 21:51:13 +0900
parents e2156468f4e5
children b4c846870b3c
line wrap: on
line diff
--- a/util.c	Mon Oct 12 01:45:36 2009 +0900
+++ b/util.c	Mon Oct 12 21:51:13 2009 +0900
@@ -1,9 +1,12 @@
 #include "pidgin-twitter.h"
 
 extern GRegex *regp[];
+extern guint64 reply_to_msgid;
+extern PurpleAccount *account_for_twitter;
 
 /* prototypes */
 static gchar *twitter_memrchr(const gchar *s, int c, size_t n);
+void fav_with_api(guint64 id);
 
 
 /* functions */
@@ -432,3 +435,143 @@
 
     return service;
 }
+
+gboolean
+pt_uri_handler(const char *proto, const char *cmd, GHashTable *params)
+{
+    char *idstr = NULL;
+	const char *acct_id = NULL;
+	PurpleConversation *conv = NULL;
+	PidginConversation *gtkconv = NULL;
+    guint64 msgid = 0;
+    gchar *sender = NULL, *recipient = NULL, *msg = NULL;
+
+    if(g_ascii_strcasecmp(proto, "pt"))
+       return FALSE;
+
+    twitter_debug("params=%p\n", params);
+
+    acct_id = purple_prefs_get_string(OPT_SCREEN_NAME_TWITTER);
+    twitter_debug("acct_id=%s\n", acct_id);
+
+    if(strstr(cmd, "reply-twitter")) {
+        sender = g_hash_table_lookup(params, "user");
+        idstr = g_hash_table_lookup(params, "id");
+        if(idstr)
+            msgid = strtoull(idstr, NULL, 10);
+
+        /* find conv */
+        conv = purple_find_conversation_with_account(
+            PURPLE_CONV_TYPE_ANY, "twitter@twitter.com",
+            account_for_twitter); /* xxx */
+        twitter_debug("conv = %p\n", conv);
+        gtkconv = PIDGIN_CONVERSATION(conv);
+
+        twitter_debug("sender = %s, id = %llu\n", sender, (unsigned long long)msgid);
+
+        recipient = g_strdup_printf("@%s ", sender);
+        gtk_text_buffer_insert_at_cursor(gtkconv->entry_buffer,
+                                         recipient, -1);
+
+        gtk_widget_grab_focus(GTK_WIDGET(gtkconv->entry));
+        g_free(recipient);
+        reply_to_msgid = msgid; /* xxx */
+
+        return TRUE;
+    }
+    else if(strstr(cmd, "fav-twitter")) {
+        idstr = g_hash_table_lookup(params, "id");
+        fav_with_api(strtoull(idstr, NULL, 10));
+        return TRUE;
+    }
+    else if(strstr(cmd, "retweet-twitter")) {
+        sender = g_hash_table_lookup(params, "user");
+        idstr = g_hash_table_lookup(params, "id");
+        msg = g_hash_table_lookup(params, "msg");
+        if(idstr)
+            msgid = strtoull(idstr, NULL, 10);
+
+        /* find conv */
+        conv = purple_find_conversation_with_account(
+            PURPLE_CONV_TYPE_ANY, "twitter@twitter.com",
+            account_for_twitter); /* xxx */
+        twitter_debug("conv = %p\n", conv);
+        gtkconv = PIDGIN_CONVERSATION(conv);
+
+        twitter_debug("sender = %s, id = %llu\n", sender, (unsigned long long)msgid);
+
+        recipient = g_strdup_printf("RT @%s: %s", sender, msg);
+        gtk_text_buffer_insert_at_cursor(gtkconv->entry_buffer,
+                                         recipient, -1);
+
+        GtkTextIter iter;
+        gtk_text_buffer_get_start_iter(gtkconv->entry_buffer, &iter);
+        gtk_text_buffer_place_cursor(gtkconv->entry_buffer, &iter);
+
+        gtk_widget_grab_focus(GTK_WIDGET(gtkconv->entry));
+        g_free(recipient);
+
+        return TRUE;
+    }
+    return FALSE;
+}
+
+void
+twitter_add_links(gchar **str)
+{
+    GMatchInfo *match_info = NULL;
+    gchar *tmpstr0 = NULL, *tmpstr = NULL;
+    gchar *newstr = NULL, *match = NULL;
+    gchar *linkstr = NULL;
+    gchar *user = NULL;
+
+    twitter_debug("called\n");
+
+    /* buffer without ptmsgid=123 */
+    tmpstr0 = g_regex_replace(regp[SENDER], *str, -1, 0, "", 0, NULL);
+    tmpstr = g_regex_replace(regp[MESSAGE_ID], tmpstr0, -1, 0, "", 0, NULL);
+    g_free(tmpstr0);
+    tmpstr0 = NULL;
+    twitter_debug("tmpstr = %s\n", tmpstr);
+
+    /* sender */
+    g_regex_match(regp[SENDER], *str, 0, &match_info);
+    if(g_match_info_matches(match_info)) {
+        user = g_match_info_fetch(match_info, 2);
+        twitter_debug("user = %s\n", user);
+        g_match_info_free(match_info);
+        match_info = NULL;
+    }
+
+    /* link string */
+    g_regex_match(regp[MESSAGE_ID], *str, 0, &match_info);
+    if(match_info) {
+        match = g_match_info_fetch(match_info, 1);
+        linkstr = g_strdup_printf(LINK_FORMAT_TWITTER,
+                                 match, user,          /* Reply */
+                                 match,                /* Favorite */
+                                 match, user, tmpstr); /* Retweet */
+
+        twitter_debug("linkstr = %s\n", linkstr);
+
+        newstr = g_regex_replace(regp[MESSAGE_ID], *str, -1, 0,
+                                 linkstr,
+                                 0, NULL);
+
+        twitter_debug("newstr = %s\n", newstr);
+
+        g_free(*str);
+        *str = newstr;
+
+        g_free(linkstr);
+
+        g_free(match);
+        match = NULL;
+
+        g_match_info_free(match_info);
+        match_info = NULL;
+    }
+
+    g_free(user);
+    g_free(tmpstr);
+}