Mercurial > pidgin-twitter
changeset 1:799955af57ad
alpha version
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Tue, 29 Apr 2008 20:59:37 +0900 |
parents | 2413369d1b01 |
children | 53ee194b5ff5 |
files | Makefile.in configure.in pidgin-twitter.c |
diffstat | 3 files changed, 116 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile.in Tue Apr 29 14:35:36 2008 +0900 +++ b/Makefile.in Tue Apr 29 20:59:37 2008 +0900 @@ -1,10 +1,9 @@ PIDGIN_CFLAGS = @PIDGIN_CFLAGS@ -PCRE_CFLAGS = @PCRE_CFLAGS@ -CFLAGS = -fPIC -shared -Wall $(PIDGIN_CFLAGS) $(PCRE_CFLAGS) +GLIB_CFLAGS = @GLIB_CFLAGS@ +CFLAGS = -fPIC -shared -Wall $(PIDGIN_CFLAGS) $(GLIB_CFLAGS) -PCRE_LIBS = @AUD_LIBS@ -PCRE_LIB_DIR = @AUD_LIB_DIR@ -LDFLAGS = $(PCRE_LIBS) +GLIB_LIBS = @GLIB_LIBS@ +LDFLAGS = $(GLIB_LIBS) PIDGIN_LIB_DIR = @PIDGIN_LIB_DIR@ @@ -12,7 +11,7 @@ default: pidgin-twitter.so pidgin-twitter.so: pidgin-twitter.c - gcc -o $@ $< $(CFLAGS) $(LDFLAGS) -g -Wl,-rpath=$(PCRE_LIB_DIR) + gcc -o $@ $< $(CFLAGS) $(LDFLAGS) -g install: pidgin-twitter.so
--- a/configure.in Tue Apr 29 14:35:36 2008 +0900 +++ b/configure.in Tue Apr 29 20:59:37 2008 +0900 @@ -10,18 +10,19 @@ AC_PROG_INSTALL # Checks for libraries. -PKG_CHECK_MODULES(AUDACIOUS, [libpcre >= 6.0.0], , [ +PKG_CHECK_MODULES(GLIB, [glib-2.0 >= 2.14.0], , [ AC_MSG_RESULT(no) AC_MSG_ERROR([ -You must have libpcre >= 6.0.0 headers installed to build. +You must have glib >= 2.14.0 development headers installed to build. ])]) -PCRE_CFLAGS=`pkg-config --cflags libpcre 2> /dev/null` -PCRE_LIBS=`pkg-config --libs libpcre 2> /dev/null` -PCRE_LIB_DIR=`pkg-config --variable=lib_dir libpcre 2> /dev/null` -AC_SUBST(PCRE_CFLAGS) -AC_SUBST(PCRE_LIBS) -AC_SUBST(PCRE_LIB_DIR) + +GLIB_CFLAGS=`pkg-config --cflags glib-2.0 2> /dev/null` +GLIB_LIBS=`pkg-config --libs glib-2.0 2> /dev/null` +GLIB_LIB_DIR=`pkg-config --variable=lib_dir glib-2.0 2> /dev/null` +AC_SUBST(GLIB_CFLAGS) +AC_SUBST(GLIB_LIBS) +AC_SUBST(GLIB_LIB_DIR) PKG_CHECK_MODULES(PIDGIN, [pidgin >= 2.0.0], , [ AC_MSG_RESULT(no) @@ -48,5 +49,3 @@ #AC_CONFIG_FILES([Makefile]) AC_OUTPUT(Makefile) - -dnl LocalWords: PCRE
--- a/pidgin-twitter.c Tue Apr 29 14:35:36 2008 +0900 +++ b/pidgin-twitter.c Tue Apr 29 20:59:37 2008 +0900 @@ -28,12 +28,15 @@ #include "debug.h" #include "connection.h" #include "version.h" +#include "sound.h" extern gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen) __attribute__ ((weak)); #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter" #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter" #define OPT_TRANSLATE OPT_PIDGINTWITTER "/translate" +#define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound" +#define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, "Pidgin-Twitter", \ fmt, ## __VA_ARGS__); @@ -43,79 +46,106 @@ /* globals */ static GRegex *preg; +static gboolean +eval(const GMatchInfo *match_info, GString *result, gpointer user_data) +{ + gchar sub[128]; + gchar *match = g_match_info_fetch(match_info, 0); + + snprintf(sub, 128, "@<a href='http://twitter.com/%s'>%s</a>", + match + 1, //+1 is to strip preceding '@'. + match + 1); + twitter_debug("sub = %s\n", sub); + g_string_append(result, sub); + g_free(match); + + return FALSE; +} static void -do_translate(char *str) +translate(gchar **str) { - int i; - int rc; - int ovector[30]; - int soffset = 0; //start offset in str - int boffset = 0; //tail of buf - int len = strlen(str); - char *buf = calloc(1, strlen(str) + 1024); /* XXX should be reasonable size */ + gchar *newstr; + + twitter_debug("*str = %s\n", *str); - do { - rc = pcre_exec( - re, /* result of pcre_compile() */ - NULL, /* we didn't study the pattern */ - str, /* the subject string */ - len, /* the length of the subject string */ - soffset, /* start at offset 0 in the subject */ - 0, /* default options */ - ovector, /* vector of integers for substring information */ - 30); /* number of elements (NOT size in bytes) */ + newstr = g_regex_replace_eval( + preg, // compiled regex + *str, // subject string + -1, // length of the subject string + 0, // start position + 0, // match options + eval, // function to call for each match + NULL, // user data (not used) + NULL); // error handler + + twitter_debug("newstr = %s\n", newstr); + + g_free(*str); + *str = newstr; +} - printf("rc=%d\n",rc); - if(rc <= 0) { - printf("no match\n"); - break; - } else { - char *sub; +static void +playsound(gchar **str) +{ + GMatchInfo *match_info; + GList *list, *lp; - /* copy until precedings */ - strncpy(buf+boffset, str+soffset, ovector[0]-soffset); - boffset = boffset + ovector[0]-soffset; + list = purple_prefs_get_string_list(OPT_USERLIST); + + g_regex_match (preg, *str, 0, &match_info); + while (g_match_info_matches (match_info)) + { + gchar *user = g_match_info_fetch (match_info, 0); + twitter_debug("user = %s\n", user); - sub = (char *)malloc(128); - /* concatenate replacement substr */ - snprintf(sub, 128, "<a href=\"http://twitter.com/%s\"</a>", str+ovector[0]+1); //+1 is to strip preceding @. - strncat(buf, sub, strlen(sub)); - boffset += strlen(sub); - free(sub); + for(lp = list; lp; lp = g_list_next(lp)) { + twitter_debug("lp->data = %s\n", (char *)lp->data); + if(!strcmp(user, lp->data) || !strcmp(user+1, lp->data)) { + twitter_debug("match. play sound\n"); + purple_sound_play_event(PURPLE_SOUND_POUNCE_DEFAULT, NULL); + } + } - soffset = ovector[1]; - } - } while(soffset < len); + g_free (user); + g_match_info_next (match_info, NULL); + } + + g_match_info_free (match_info); } -static void -translate(PurpleAccount *account, char *sender, char *buffer, - PurpleConversation *conv, PurpleMessageFlags flags, void *data) +static gboolean +process_im_cb(PurpleAccount *account, char *sender, char **buffer, + PurpleConversation *conv, int *flags, void *data) { const gchar *proto; - /* check if the message is from twitter? */ + /* check if the message is from twitter */ proto = purple_account_get_protocol_id(account); - if (!g_utf8_collate(proto, "xmpp") && - !g_utf8_collate(sender, "twitter@twitter.com") && - purple_prefs_get_bool(OPT_TRANSLATE)) { + twitter_debug("proto = %s\n", proto); + twitter_debug("sender = %s\n", sender); - /* translate */ - do_translate(buffer); - + if (!strcmp(proto, "prpl-jabber") && + !strcmp(sender, "twitter@twitter.com")) { + if(purple_prefs_get_bool(OPT_PLAYSOUND)) { + /* playsound */ + playsound(buffer); + } + if(purple_prefs_get_bool(OPT_TRANSLATE)) { + /* translate */ + translate(buffer); + } } - + return FALSE; } static gboolean load_plugin(PurplePlugin *plugin) { - - /* connect to signing-off signal */ - purple_signal_connect(purple_connections_get_handle(), "received-im-msg", - plugin, PURPLE_CALLBACK(translate), NULL); + /* connect to signal */ + purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg", + plugin, PURPLE_CALLBACK(process_im_cb), NULL); return TRUE; } @@ -123,7 +153,7 @@ static gboolean unload_plugin(PurplePlugin *plugin) { - aud_debug("pidgin-twitter unload called\n"); + twitter_debug("pidgin-twitter unload called\n"); g_regex_unref(preg); preg = NULL; @@ -141,9 +171,15 @@ purple_plugin_pref_frame_add(frame, pref); pref = purple_plugin_pref_new_with_name_and_label(OPT_TRANSLATE, - "Translate @username to link"); + "Translate @username to the link to the user"); purple_plugin_pref_frame_add(frame, pref); + pref = purple_plugin_pref_new_with_name_and_label(OPT_PLAYSOUND, + "Play sound if a reply to listed users arrives"); + purple_plugin_pref_frame_add(frame, pref); + + // xxx add pref field for OPT_USERLIST (gtk packing is needed) --yaz + return frame; } @@ -159,14 +195,14 @@ PURPLE_MINOR_VERSION, PURPLE_PLUGIN_STANDARD, /**< type */ NULL, /**< ui_req */ - 0, /**< flags */ - NULL, /**< deps */ - PURPLE_PRIORITY_DEFAULT, /**< priority */ - PIDGINAUD_PLUGIN_ID, /**< id */ + 0, /**< flags */ + NULL, /**< deps */ + PURPLE_PRIORITY_DEFAULT, /**< priority */ + PIDGINTWITTER_PLUGIN_ID, /**< id */ "Pidgin-Twitter", /**< name */ "0.1.0", /**< version */ - "replaces @someone in incoming message with link to someone", /** summary */ - "replaces @someone in incoming message with link to someone", /** desc */ + "replaces @username in a message with link to the user", /** summary */ + "replaces @username in a message with link to the user", /** desc */ "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ "http://www.honeyplanet.jp/", /**< homepage */ load_plugin, /**< load */ @@ -181,13 +217,20 @@ static void init_plugin(PurplePlugin *plugin) { + GList *list; + g_type_init(); + list = g_list_alloc(); /* add plugin preferences */ purple_prefs_add_none(OPT_PIDGINTWITTER); purple_prefs_add_bool(OPT_TRANSLATE, TRUE); + purple_prefs_add_bool(OPT_PLAYSOUND, TRUE); + purple_prefs_add_string_list(OPT_USERLIST, list); + /* compile regex */ preg = g_regex_new("@[A-Za-z0-9-_]+", 0, 0, NULL); + } PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)