# HG changeset patch # User Yoshiki Yazawa # Date 1209588648 -32400 # Node ID c6b80f47d4df5142d4f5a36db1f49bb53fd91539 # Parent 2c7c9eb4cdda7890f18ab2613e324f9556f45fa5 added capability to translate sender name into link diff -r 2c7c9eb4cdda -r c6b80f47d4df pidgin-twitter.c --- a/pidgin-twitter.c Thu May 01 02:00:45 2008 +0900 +++ b/pidgin-twitter.c Thu May 01 05:50:48 2008 +0900 @@ -30,17 +30,17 @@ #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_SOUNDID OPT_PIDGINTWITTER "/soundid" -#define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" -#define TWITTER_FORMAT "@%s" -#define DEFAULT_LIST "(list of users: separated with ' ,:;')" +#define OPT_TRANSLATE_RECIPIENT OPT_PIDGINTWITTER "/translate_recipient" +#define OPT_TRANSLATE_SENDER OPT_PIDGINTWITTER "/translate_sender" +#define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound" +#define OPT_SOUNDID OPT_PIDGINTWITTER "/soundid" +#define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" +#define RECIPIENT_FORMAT "@%s" +#define SENDER_FORMAT "%s :" +#define DEFAULT_LIST "(list of users: separated with ' ,:;')" #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \ fmt, ## __VA_ARGS__); @@ -48,16 +48,16 @@ fmt, ## __VA_ARGS__); /* globals */ -static GRegex *preg; +static GRegex *regrcpt; +static GRegex *regsndr; static gboolean -eval(const GMatchInfo * match_info, GString * result, gpointer user_data) +eval_sender(const GMatchInfo * match_info, GString * result, gpointer user_data) { gchar sub[128]; - gchar *match = g_match_info_fetch(match_info, 0); + gchar *match = g_match_info_fetch(match_info, 1); - snprintf(sub, 128, TWITTER_FORMAT, match + 1, // +1 is to strip preceding '@'. - match + 1); + snprintf(sub, 128, SENDER_FORMAT, match, match); twitter_debug("sub = %s\n", sub); g_string_append(result, sub); g_free(match); @@ -66,18 +66,54 @@ } static void -translate(gchar **str) +translate_sender(gchar **str) { gchar *newstr; twitter_debug("*str = %s\n", *str); - newstr = g_regex_replace_eval(preg, // compiled regex + newstr = g_regex_replace_eval(regsndr, // compiled regex *str, // subject string -1, // length of the subject string 0, // start position 0, // match options - eval, // function to call for each match + eval_sender, // 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; +} + +static gboolean +eval_recipient(const GMatchInfo * match_info, GString * result, gpointer user_data) +{ + gchar sub[128]; + gchar *match = g_match_info_fetch(match_info, 1); + + snprintf(sub, 128, RECIPIENT_FORMAT, match, match); + twitter_debug("sub = %s\n", sub); + g_string_append(result, sub); + g_free(match); + + return FALSE; +} + +static void +translate_recipient(gchar **str) +{ + gchar *newstr; + + twitter_debug("*str = %s\n", *str); + + newstr = g_regex_replace_eval(regrcpt, // compiled regex + *str, // subject string + -1, // length of the subject string + 0, // start position + 0, // match options + eval_recipient, // function to call for each match NULL, // user data (not used) NULL); // error handler @@ -103,17 +139,16 @@ if(!candidates) return; - g_regex_match(preg, *str, 0, &match_info); + g_regex_match(regrcpt, *str, 0, &match_info); while(g_match_info_matches(match_info)) { - gchar *user = g_match_info_fetch(match_info, 0); + gchar *user = g_match_info_fetch(match_info, 1); twitter_debug("user = %s\n", user); for(candidate = candidates; *candidate ; candidate++) { if(!strcmp(*candidate, "")) continue; twitter_debug("candidate = %s\n", *candidate); - if(!strcmp(user, *candidate) || - !strcmp(user + 1, *candidate)) { + if(!strcmp(user, *candidate)) { twitter_debug("match. play sound\n"); purple_sound_play_event( purple_prefs_get_int(OPT_SOUNDID), NULL); @@ -144,9 +179,13 @@ /* playsound */ playsound(buffer); } - if(purple_prefs_get_bool(OPT_TRANSLATE)) { + if(purple_prefs_get_bool(OPT_TRANSLATE_SENDER)) { /* translate */ - translate(buffer); + translate_sender(buffer); + } + if(purple_prefs_get_bool(OPT_TRANSLATE_RECIPIENT)) { + /* translate */ + translate_recipient(buffer); } } return FALSE; @@ -167,8 +206,9 @@ { twitter_debug("pidgin-twitter unload called\n"); - g_regex_unref(preg); - preg = NULL; + g_regex_unref(regrcpt); + g_regex_unref(regsndr); + return TRUE; } @@ -183,19 +223,23 @@ purple_plugin_pref_frame_add(frame, pref); pref = purple_plugin_pref_new_with_name_and_label( - OPT_TRANSLATE, + OPT_TRANSLATE_RECIPIENT, "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_TRANSLATE_SENDER, + "Translate sender name to the link"); + purple_plugin_pref_frame_add(frame, pref); + + pref = purple_plugin_pref_new_with_name_and_label( OPT_PLAYSOUND, "Play a sound on a reply to the user in the Userlist"); purple_plugin_pref_frame_add(frame, pref); /* sound id selector */ - pref = purple_plugin_pref_new_with_name_and_label( - OPT_SOUNDID, - "Sound"); + pref = purple_plugin_pref_new_with_name_and_label(OPT_SOUNDID, "Sound"); + purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE); purple_plugin_pref_add_choice(pref, "Arrive", GINT_TO_POINTER(0)); purple_plugin_pref_add_choice(pref, "Leave", GINT_TO_POINTER(1)); @@ -208,6 +252,7 @@ purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8)); purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9)); purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10)); + purple_plugin_pref_frame_add(frame, pref); /* user list */ @@ -234,7 +279,7 @@ PURPLE_PRIORITY_DEFAULT, /**< priority */ PIDGINTWITTER_PLUGIN_ID, /**< id */ "Pidgin-Twitter", /**< name */ - "0.2.0", /**< version */ + "0.3.0", /**< version */ "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 */ @@ -255,13 +300,16 @@ /* add plugin preferences */ purple_prefs_add_none(OPT_PIDGINTWITTER); - purple_prefs_add_bool(OPT_TRANSLATE, TRUE); + purple_prefs_add_bool(OPT_TRANSLATE_RECIPIENT, TRUE); + purple_prefs_add_bool(OPT_TRANSLATE_SENDER, TRUE); + purple_prefs_add_bool(OPT_PLAYSOUND, TRUE); purple_prefs_add_int(OPT_SOUNDID, PURPLE_SOUND_POUNCE_DEFAULT); purple_prefs_add_string(OPT_USERLIST, DEFAULT_LIST); /* compile regex */ - preg = g_regex_new("@[A-Za-z0-9_]+", 0, 0, NULL); + regrcpt = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL); + regsndr = g_regex_new("^([A-Za-z0-9_]+): ", 0, 0, NULL); } PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)