Mercurial > pidgin-twitter
comparison pidgin-twitter.c @ 9:c6b80f47d4df
added capability to translate sender name into link
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 01 May 2008 05:50:48 +0900 |
parents | 2c7c9eb4cdda |
children | 4bd8c89b4749 |
comparison
equal
deleted
inserted
replaced
8:2c7c9eb4cdda | 9:c6b80f47d4df |
---|---|
28 #include "debug.h" | 28 #include "debug.h" |
29 #include "connection.h" | 29 #include "connection.h" |
30 #include "version.h" | 30 #include "version.h" |
31 #include "sound.h" | 31 #include "sound.h" |
32 | 32 |
33 extern gchar *botch_utf(const gchar *msg, gsize len, gsize * newlen) | |
34 __attribute__ ((weak)); | |
35 | 33 |
36 #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter" | 34 #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter" |
37 #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter" | 35 #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter" |
38 #define OPT_TRANSLATE OPT_PIDGINTWITTER "/translate" | 36 #define OPT_TRANSLATE_RECIPIENT OPT_PIDGINTWITTER "/translate_recipient" |
39 #define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound" | 37 #define OPT_TRANSLATE_SENDER OPT_PIDGINTWITTER "/translate_sender" |
40 #define OPT_SOUNDID OPT_PIDGINTWITTER "/soundid" | 38 #define OPT_PLAYSOUND OPT_PIDGINTWITTER "/playsound" |
41 #define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" | 39 #define OPT_SOUNDID OPT_PIDGINTWITTER "/soundid" |
42 #define TWITTER_FORMAT "@<a href='http://twitter.com/%s'>%s</a>" | 40 #define OPT_USERLIST OPT_PIDGINTWITTER "/userlist" |
43 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" | 41 #define RECIPIENT_FORMAT "@<a href='http://twitter.com/%s'>%s</a>" |
42 #define SENDER_FORMAT "<a href='http://twitter.com/%s'>%s</a> :" | |
43 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" | |
44 | 44 |
45 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \ | 45 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \ |
46 fmt, ## __VA_ARGS__); | 46 fmt, ## __VA_ARGS__); |
47 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \ | 47 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \ |
48 fmt, ## __VA_ARGS__); | 48 fmt, ## __VA_ARGS__); |
49 | 49 |
50 /* globals */ | 50 /* globals */ |
51 static GRegex *preg; | 51 static GRegex *regrcpt; |
52 | 52 static GRegex *regsndr; |
53 static gboolean | 53 |
54 eval(const GMatchInfo * match_info, GString * result, gpointer user_data) | 54 static gboolean |
55 eval_sender(const GMatchInfo * match_info, GString * result, gpointer user_data) | |
55 { | 56 { |
56 gchar sub[128]; | 57 gchar sub[128]; |
57 gchar *match = g_match_info_fetch(match_info, 0); | 58 gchar *match = g_match_info_fetch(match_info, 1); |
58 | 59 |
59 snprintf(sub, 128, TWITTER_FORMAT, match + 1, // +1 is to strip preceding '@'. | 60 snprintf(sub, 128, SENDER_FORMAT, match, match); |
60 match + 1); | |
61 twitter_debug("sub = %s\n", sub); | 61 twitter_debug("sub = %s\n", sub); |
62 g_string_append(result, sub); | 62 g_string_append(result, sub); |
63 g_free(match); | 63 g_free(match); |
64 | 64 |
65 return FALSE; | 65 return FALSE; |
66 } | 66 } |
67 | 67 |
68 static void | 68 static void |
69 translate(gchar **str) | 69 translate_sender(gchar **str) |
70 { | 70 { |
71 gchar *newstr; | 71 gchar *newstr; |
72 | 72 |
73 twitter_debug("*str = %s\n", *str); | 73 twitter_debug("*str = %s\n", *str); |
74 | 74 |
75 newstr = g_regex_replace_eval(preg, // compiled regex | 75 newstr = g_regex_replace_eval(regsndr, // compiled regex |
76 *str, // subject string | 76 *str, // subject string |
77 -1, // length of the subject string | 77 -1, // length of the subject string |
78 0, // start position | 78 0, // start position |
79 0, // match options | 79 0, // match options |
80 eval, // function to call for each match | 80 eval_sender, // function to call for each match |
81 NULL, // user data (not used) | |
82 NULL); // error handler | |
83 | |
84 twitter_debug("newstr = %s\n", newstr); | |
85 | |
86 g_free(*str); | |
87 *str = newstr; | |
88 } | |
89 | |
90 static gboolean | |
91 eval_recipient(const GMatchInfo * match_info, GString * result, gpointer user_data) | |
92 { | |
93 gchar sub[128]; | |
94 gchar *match = g_match_info_fetch(match_info, 1); | |
95 | |
96 snprintf(sub, 128, RECIPIENT_FORMAT, match, match); | |
97 twitter_debug("sub = %s\n", sub); | |
98 g_string_append(result, sub); | |
99 g_free(match); | |
100 | |
101 return FALSE; | |
102 } | |
103 | |
104 static void | |
105 translate_recipient(gchar **str) | |
106 { | |
107 gchar *newstr; | |
108 | |
109 twitter_debug("*str = %s\n", *str); | |
110 | |
111 newstr = g_regex_replace_eval(regrcpt, // compiled regex | |
112 *str, // subject string | |
113 -1, // length of the subject string | |
114 0, // start position | |
115 0, // match options | |
116 eval_recipient, // function to call for each match | |
81 NULL, // user data (not used) | 117 NULL, // user data (not used) |
82 NULL); // error handler | 118 NULL); // error handler |
83 | 119 |
84 twitter_debug("newstr = %s\n", newstr); | 120 twitter_debug("newstr = %s\n", newstr); |
85 | 121 |
101 | 137 |
102 candidates = g_strsplit_set(list, " ,:;", 0); | 138 candidates = g_strsplit_set(list, " ,:;", 0); |
103 if(!candidates) | 139 if(!candidates) |
104 return; | 140 return; |
105 | 141 |
106 g_regex_match(preg, *str, 0, &match_info); | 142 g_regex_match(regrcpt, *str, 0, &match_info); |
107 while(g_match_info_matches(match_info)) { | 143 while(g_match_info_matches(match_info)) { |
108 gchar *user = g_match_info_fetch(match_info, 0); | 144 gchar *user = g_match_info_fetch(match_info, 1); |
109 twitter_debug("user = %s\n", user); | 145 twitter_debug("user = %s\n", user); |
110 | 146 |
111 for(candidate = candidates; *candidate ; candidate++) { | 147 for(candidate = candidates; *candidate ; candidate++) { |
112 if(!strcmp(*candidate, "")) | 148 if(!strcmp(*candidate, "")) |
113 continue; | 149 continue; |
114 twitter_debug("candidate = %s\n", *candidate); | 150 twitter_debug("candidate = %s\n", *candidate); |
115 if(!strcmp(user, *candidate) || | 151 if(!strcmp(user, *candidate)) { |
116 !strcmp(user + 1, *candidate)) { | |
117 twitter_debug("match. play sound\n"); | 152 twitter_debug("match. play sound\n"); |
118 purple_sound_play_event( | 153 purple_sound_play_event( |
119 purple_prefs_get_int(OPT_SOUNDID), NULL); | 154 purple_prefs_get_int(OPT_SOUNDID), NULL); |
120 break; | 155 break; |
121 } | 156 } |
142 if(!strcmp(proto, "prpl-jabber") && !strcmp(sender, "twitter@twitter.com")) { | 177 if(!strcmp(proto, "prpl-jabber") && !strcmp(sender, "twitter@twitter.com")) { |
143 if(purple_prefs_get_bool(OPT_PLAYSOUND)) { | 178 if(purple_prefs_get_bool(OPT_PLAYSOUND)) { |
144 /* playsound */ | 179 /* playsound */ |
145 playsound(buffer); | 180 playsound(buffer); |
146 } | 181 } |
147 if(purple_prefs_get_bool(OPT_TRANSLATE)) { | 182 if(purple_prefs_get_bool(OPT_TRANSLATE_SENDER)) { |
148 /* translate */ | 183 /* translate */ |
149 translate(buffer); | 184 translate_sender(buffer); |
185 } | |
186 if(purple_prefs_get_bool(OPT_TRANSLATE_RECIPIENT)) { | |
187 /* translate */ | |
188 translate_recipient(buffer); | |
150 } | 189 } |
151 } | 190 } |
152 return FALSE; | 191 return FALSE; |
153 } | 192 } |
154 | 193 |
165 static gboolean | 204 static gboolean |
166 unload_plugin(PurplePlugin * plugin) | 205 unload_plugin(PurplePlugin * plugin) |
167 { | 206 { |
168 twitter_debug("pidgin-twitter unload called\n"); | 207 twitter_debug("pidgin-twitter unload called\n"); |
169 | 208 |
170 g_regex_unref(preg); | 209 g_regex_unref(regrcpt); |
171 preg = NULL; | 210 g_regex_unref(regsndr); |
211 | |
172 return TRUE; | 212 return TRUE; |
173 } | 213 } |
174 | 214 |
175 static PurplePluginPrefFrame * | 215 static PurplePluginPrefFrame * |
176 get_plugin_pref_frame(PurplePlugin * plugin) | 216 get_plugin_pref_frame(PurplePlugin * plugin) |
181 /* create gtk elements for the plugin preferences */ | 221 /* create gtk elements for the plugin preferences */ |
182 pref = purple_plugin_pref_new_with_label("Pidgin-Twitter Configuration"); | 222 pref = purple_plugin_pref_new_with_label("Pidgin-Twitter Configuration"); |
183 purple_plugin_pref_frame_add(frame, pref); | 223 purple_plugin_pref_frame_add(frame, pref); |
184 | 224 |
185 pref = purple_plugin_pref_new_with_name_and_label( | 225 pref = purple_plugin_pref_new_with_name_and_label( |
186 OPT_TRANSLATE, | 226 OPT_TRANSLATE_RECIPIENT, |
187 "Translate @username to the link to the user"); | 227 "Translate @username to the link to the user"); |
228 purple_plugin_pref_frame_add(frame, pref); | |
229 | |
230 pref = purple_plugin_pref_new_with_name_and_label( | |
231 OPT_TRANSLATE_SENDER, | |
232 "Translate sender name to the link"); | |
188 purple_plugin_pref_frame_add(frame, pref); | 233 purple_plugin_pref_frame_add(frame, pref); |
189 | 234 |
190 pref = purple_plugin_pref_new_with_name_and_label( | 235 pref = purple_plugin_pref_new_with_name_and_label( |
191 OPT_PLAYSOUND, | 236 OPT_PLAYSOUND, |
192 "Play a sound on a reply to the user in the Userlist"); | 237 "Play a sound on a reply to the user in the Userlist"); |
193 purple_plugin_pref_frame_add(frame, pref); | 238 purple_plugin_pref_frame_add(frame, pref); |
194 | 239 |
195 /* sound id selector */ | 240 /* sound id selector */ |
196 pref = purple_plugin_pref_new_with_name_and_label( | 241 pref = purple_plugin_pref_new_with_name_and_label(OPT_SOUNDID, "Sound"); |
197 OPT_SOUNDID, | 242 |
198 "Sound"); | |
199 purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE); | 243 purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE); |
200 purple_plugin_pref_add_choice(pref, "Arrive", GINT_TO_POINTER(0)); | 244 purple_plugin_pref_add_choice(pref, "Arrive", GINT_TO_POINTER(0)); |
201 purple_plugin_pref_add_choice(pref, "Leave", GINT_TO_POINTER(1)); | 245 purple_plugin_pref_add_choice(pref, "Leave", GINT_TO_POINTER(1)); |
202 purple_plugin_pref_add_choice(pref, "Receive", GINT_TO_POINTER(2)); | 246 purple_plugin_pref_add_choice(pref, "Receive", GINT_TO_POINTER(2)); |
203 purple_plugin_pref_add_choice(pref, "Fist Receive", GINT_TO_POINTER(3)); | 247 purple_plugin_pref_add_choice(pref, "Fist Receive", GINT_TO_POINTER(3)); |
206 purple_plugin_pref_add_choice(pref, "Chat Leave", GINT_TO_POINTER(6)); | 250 purple_plugin_pref_add_choice(pref, "Chat Leave", GINT_TO_POINTER(6)); |
207 purple_plugin_pref_add_choice(pref, "Chat You Say", GINT_TO_POINTER(7)); | 251 purple_plugin_pref_add_choice(pref, "Chat You Say", GINT_TO_POINTER(7)); |
208 purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8)); | 252 purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8)); |
209 purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9)); | 253 purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9)); |
210 purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10)); | 254 purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10)); |
255 | |
211 purple_plugin_pref_frame_add(frame, pref); | 256 purple_plugin_pref_frame_add(frame, pref); |
212 | 257 |
213 /* user list */ | 258 /* user list */ |
214 pref = purple_plugin_pref_new_with_name_and_label( | 259 pref = purple_plugin_pref_new_with_name_and_label( |
215 OPT_USERLIST, | 260 OPT_USERLIST, |
232 0, /**< flags */ | 277 0, /**< flags */ |
233 NULL, /**< deps */ | 278 NULL, /**< deps */ |
234 PURPLE_PRIORITY_DEFAULT, /**< priority */ | 279 PURPLE_PRIORITY_DEFAULT, /**< priority */ |
235 PIDGINTWITTER_PLUGIN_ID, /**< id */ | 280 PIDGINTWITTER_PLUGIN_ID, /**< id */ |
236 "Pidgin-Twitter", /**< name */ | 281 "Pidgin-Twitter", /**< name */ |
237 "0.2.0", /**< version */ | 282 "0.3.0", /**< version */ |
238 "replaces @username in a message with link to the user", /** summary */ | 283 "replaces @username in a message with link to the user", /** summary */ |
239 "replaces @username in a message with link to the user", /** desc */ | 284 "replaces @username in a message with link to the user", /** desc */ |
240 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ | 285 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ |
241 "http://www.honeyplanet.jp/", /**< homepage */ | 286 "http://www.honeyplanet.jp/", /**< homepage */ |
242 load_plugin, /**< load */ | 287 load_plugin, /**< load */ |
253 { | 298 { |
254 g_type_init(); | 299 g_type_init(); |
255 | 300 |
256 /* add plugin preferences */ | 301 /* add plugin preferences */ |
257 purple_prefs_add_none(OPT_PIDGINTWITTER); | 302 purple_prefs_add_none(OPT_PIDGINTWITTER); |
258 purple_prefs_add_bool(OPT_TRANSLATE, TRUE); | 303 purple_prefs_add_bool(OPT_TRANSLATE_RECIPIENT, TRUE); |
304 purple_prefs_add_bool(OPT_TRANSLATE_SENDER, TRUE); | |
305 | |
259 purple_prefs_add_bool(OPT_PLAYSOUND, TRUE); | 306 purple_prefs_add_bool(OPT_PLAYSOUND, TRUE); |
260 purple_prefs_add_int(OPT_SOUNDID, PURPLE_SOUND_POUNCE_DEFAULT); | 307 purple_prefs_add_int(OPT_SOUNDID, PURPLE_SOUND_POUNCE_DEFAULT); |
261 purple_prefs_add_string(OPT_USERLIST, DEFAULT_LIST); | 308 purple_prefs_add_string(OPT_USERLIST, DEFAULT_LIST); |
262 | 309 |
263 /* compile regex */ | 310 /* compile regex */ |
264 preg = g_regex_new("@[A-Za-z0-9_]+", 0, 0, NULL); | 311 regrcpt = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL); |
312 regsndr = g_regex_new("^<body>([A-Za-z0-9_]+): ", 0, 0, NULL); | |
265 } | 313 } |
266 | 314 |
267 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info) | 315 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info) |