comparison pidgin-twitter.c @ 19:0d7cbc984570

escape pseudo command feature has been added
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Sun, 04 May 2008 17:38:15 +0900
parents 728c068534d1
children 4c236a7a128f
comparison
equal deleted inserted replaced
18:d69c47bb8408 19:0d7cbc984570
30 #include "version.h" 30 #include "version.h"
31 #include "sound.h" 31 #include "sound.h"
32 32
33 #define RECIPIENT 0 33 #define RECIPIENT 0
34 #define SENDER 1 34 #define SENDER 1
35 #define COMMAND 2
36 #define PSEUDO 3
35 37
36 #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter" 38 #define PIDGINTWITTER_PLUGIN_ID "pidgin_twitter"
37 #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter" 39 #define OPT_PIDGINTWITTER "/plugins/pidgin_twitter"
38 #define OPT_TRANSLATE_RECIPIENT OPT_PIDGINTWITTER "/translate_recipient" 40 #define OPT_TRANSLATE_RECIPIENT OPT_PIDGINTWITTER "/translate_recipient"
39 #define OPT_TRANSLATE_SENDER OPT_PIDGINTWITTER "/translate_sender" 41 #define OPT_TRANSLATE_SENDER OPT_PIDGINTWITTER "/translate_sender"
40 #define OPT_PLAYSOUND_RECIPIENT OPT_PIDGINTWITTER "/playsound_recipient" 42 #define OPT_PLAYSOUND_RECIPIENT OPT_PIDGINTWITTER "/playsound_recipient"
41 #define OPT_PLAYSOUND_SENDER OPT_PIDGINTWITTER "/playsound_sender" 43 #define OPT_PLAYSOUND_SENDER OPT_PIDGINTWITTER "/playsound_sender"
42 #define OPT_SOUNDID_RECIPIENT OPT_PIDGINTWITTER "/soundid_recipient" 44 #define OPT_SOUNDID_RECIPIENT OPT_PIDGINTWITTER "/soundid_recipient"
43 #define OPT_SOUNDID_SENDER OPT_PIDGINTWITTER "/soundid_sender" 45 #define OPT_SOUNDID_SENDER OPT_PIDGINTWITTER "/soundid_sender"
46 #define OPT_ESCAPE_PSEUDO OPT_PIDGINTWITTER "/escape_pseudo"
44 #define OPT_USERLIST_RECIPIENT OPT_PIDGINTWITTER "/userlist_recipient" 47 #define OPT_USERLIST_RECIPIENT OPT_PIDGINTWITTER "/userlist_recipient"
45 #define OPT_USERLIST_SENDER OPT_PIDGINTWITTER "/userlist_sender" 48 #define OPT_USERLIST_SENDER OPT_PIDGINTWITTER "/userlist_sender"
46 #define RECIPIENT_FORMAT "@<a href='http://twitter.com/%s'>%s</a>" 49 #define RECIPIENT_FORMAT "@<a href='http://twitter.com/%s'>%s</a>"
47 #define SENDER_FORMAT "<a href='http://twitter.com/%s'>%s</a>: " 50 #define SENDER_FORMAT "<a href='http://twitter.com/%s'>%s</a>: "
48 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" 51 #define DEFAULT_LIST "(list of users: separated with ' ,:;')"
49 52
53 #if 0
54 gchar *formats[] = {
55 "@<a href='http://twitter.com/%s'>%s</a>",
56 "<a href='http://twitter.com/%s'>%s</a>: ",
57 };
58 #endif
59
50 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \ 60 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PIDGINTWITTER_PLUGIN_ID, \
51 fmt, ## __VA_ARGS__); 61 fmt, ## __VA_ARGS__);
52 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \ 62 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PIDGINTWITTER_PLUGIN_ID, \
53 63
54 /* globals */ 64 /* globals */
55 static GRegex *regp[2]; 65 static GRegex *regp[4];
56 66
57 static gboolean 67 static gboolean
58 eval(const GMatchInfo * match_info, GString * result, gpointer user_data) 68 eval(const GMatchInfo * match_info, GString * result, gpointer user_data)
59 { 69 {
60 gchar sub[128]; 70 gchar sub[128];
131 g_match_info_free(match_info); 141 g_match_info_free(match_info);
132 } 142 }
133 143
134 144
135 static gboolean 145 static gboolean
136 process_im_cb(PurpleAccount * account, char *sender, char **buffer, 146 writing_im_cb(PurpleAccount * account, char *sender, char **buffer,
137 PurpleConversation * conv, int *flags, void *data) 147 PurpleConversation * conv, int *flags, void *data)
138 { 148 {
139 const gchar *proto; 149 const gchar *proto;
140 150
141 /* check if the message is from twitter */ 151 /* check if the message is from twitter */
161 } 171 }
162 } 172 }
163 return FALSE; 173 return FALSE;
164 } 174 }
165 175
176 static void
177 escape(gchar **str)
178 {
179 GMatchInfo *match_info = NULL;
180 gchar *newstr = NULL, *match = NULL;
181 gboolean flag = FALSE;
182
183 /* search genuine command first */
184 g_regex_match(regp[COMMAND], *str, 0, &match_info);
185 while(g_match_info_matches(match_info)) {
186 match = g_match_info_fetch(match_info, 1);
187 twitter_debug("command = %s\n", match);
188 g_free(match);
189 g_match_info_next(match_info, NULL);
190 flag = TRUE;
191 }
192 if(flag) {
193 g_match_info_free(match_info);
194 match_info = NULL;
195 return;
196 }
197
198 /* if not found, check "pseudo command" */
199 g_regex_match(regp[PSEUDO], *str, 0, &match_info);
200 while(g_match_info_matches(match_info)) {
201 match = g_match_info_fetch(match_info, 1);
202 twitter_debug("pseudo = %s\n", match);
203 g_free(match);
204 g_match_info_next(match_info, NULL);
205 flag = TRUE;
206 }
207 /* there is pseudo one, escape it */
208 if(flag) {
209 /* put ". " into the beginning of buffer */
210 newstr = g_strdup_printf(". %s", *str);
211 g_free(*str);
212 str = &newstr;
213 }
214
215 g_match_info_free(match_info);
216 }
217
218 static gboolean
219 sending_im_cb(PurpleAccount * account, char *recipient, char **buffer,
220 void *data)
221 {
222 const gchar *proto;
223
224 /* check if the message is from twitter */
225 proto = purple_account_get_protocol_id(account);
226 twitter_debug("proto = %s\n", proto);
227 twitter_debug("recipient = %s\n", recipient);
228
229 if(!strcmp(proto, "prpl-jabber") && !strcmp(recipient, "twitter@twitter.com")) {
230 /* escape */
231 if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) {
232 escape(buffer);
233 }
234 }
235 return FALSE;
236 }
237
166 static gboolean 238 static gboolean
167 load_plugin(PurplePlugin * plugin) 239 load_plugin(PurplePlugin * plugin)
168 { 240 {
169 /* connect to signal */ 241 /* connect to signal */
170 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg", 242 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg",
171 plugin, PURPLE_CALLBACK(process_im_cb), NULL); 243 plugin, PURPLE_CALLBACK(writing_im_cb), NULL);
244 purple_signal_connect(purple_conversations_get_handle(), "sending-im-msg",
245 plugin, PURPLE_CALLBACK(sending_im_cb), NULL);
172 246
173 return TRUE; 247 return TRUE;
174 } 248 }
175 249
176 static gboolean 250 static gboolean
206 pref = purple_plugin_pref_new_with_name_and_label( 280 pref = purple_plugin_pref_new_with_name_and_label(
207 OPT_TRANSLATE_SENDER, 281 OPT_TRANSLATE_SENDER,
208 "Translate sender name to link"); 282 "Translate sender name to link");
209 purple_plugin_pref_frame_add(frame, pref); 283 purple_plugin_pref_frame_add(frame, pref);
210 284
285 /* escape pseudo command settings */
286 pref = purple_plugin_pref_new_with_name_and_label(
287 OPT_ESCAPE_PSEUDO,
288 "Escape pseudo command string");
289 purple_plugin_pref_frame_add(frame, pref);
211 290
212 /* sound settings for recipient */ 291 /* sound settings for recipient */
213 pref = purple_plugin_pref_new_with_name_and_label( 292 pref = purple_plugin_pref_new_with_name_and_label(
214 OPT_PLAYSOUND_RECIPIENT, 293 OPT_PLAYSOUND_RECIPIENT,
215 "Play sound on a reply to the user in the recipient list"); 294 "Play sound on a reply to the user in the recipient list");
280 static PurplePluginInfo info = { 359 static PurplePluginInfo info = {
281 PURPLE_PLUGIN_MAGIC, 360 PURPLE_PLUGIN_MAGIC,
282 PURPLE_MAJOR_VERSION, 361 PURPLE_MAJOR_VERSION,
283 PURPLE_MINOR_VERSION, 362 PURPLE_MINOR_VERSION,
284 PURPLE_PLUGIN_STANDARD, /**< type */ 363 PURPLE_PLUGIN_STANDARD, /**< type */
285 NULL, /**< ui_req */ 364 NULL, /**< ui_req */
286 0, /**< flags */ 365 0, /**< flags */
287 NULL, /**< deps */ 366 NULL, /**< deps */
288 PURPLE_PRIORITY_DEFAULT, /**< priority */ 367 PURPLE_PRIORITY_DEFAULT, /**< priority */
289 PIDGINTWITTER_PLUGIN_ID, /**< id */ 368 PIDGINTWITTER_PLUGIN_ID, /**< id */
290 "Pidgin-Twitter", /**< name */ 369 "Pidgin-Twitter", /**< name */
291 "0.4.0", /**< version */ 370 "0.5.0", /**< version */
292 "replaces @username to a link and play sound", /** summary */ 371 "replaces @username to a link and play sound", /** summary */
293 "replaces @username in a link and play sound", /** desc */ 372 "replaces @username in a link and play sound", /** desc */
294 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ 373 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */
295 "http://www.honeyplanet.jp/", /**< homepage */ 374 "http://www.honeyplanet.jp/", /**< homepage */
296 load_plugin, /**< load */ 375 load_plugin, /**< load */
297 unload_plugin, /**< unload */ 376 unload_plugin, /**< unload */
298 NULL, /**< destroy */ 377 NULL, /**< destroy */
299 NULL, /**< ui_info */ 378 NULL, /**< ui_info */
300 NULL, /**< extra_info */ 379 NULL, /**< extra_info */
301 &pref_info, /**< pref info */ 380 &pref_info, /**< pref info */
302 NULL 381 NULL
303 }; 382 };
304 383
305 static void 384 static void
306 init_plugin(PurplePlugin * plugin) 385 init_plugin(PurplePlugin * plugin)
309 388
310 /* add plugin preferences */ 389 /* add plugin preferences */
311 purple_prefs_add_none(OPT_PIDGINTWITTER); 390 purple_prefs_add_none(OPT_PIDGINTWITTER);
312 purple_prefs_add_bool(OPT_TRANSLATE_RECIPIENT, TRUE); 391 purple_prefs_add_bool(OPT_TRANSLATE_RECIPIENT, TRUE);
313 purple_prefs_add_bool(OPT_TRANSLATE_SENDER, TRUE); 392 purple_prefs_add_bool(OPT_TRANSLATE_SENDER, TRUE);
393 purple_prefs_add_bool(OPT_ESCAPE_PSEUDO, TRUE);
314 394
315 purple_prefs_add_bool(OPT_PLAYSOUND_RECIPIENT, TRUE); 395 purple_prefs_add_bool(OPT_PLAYSOUND_RECIPIENT, TRUE);
316 purple_prefs_add_bool(OPT_PLAYSOUND_SENDER, TRUE); 396 purple_prefs_add_bool(OPT_PLAYSOUND_SENDER, TRUE);
317 purple_prefs_add_int(OPT_SOUNDID_RECIPIENT, PURPLE_SOUND_POUNCE_DEFAULT); 397 purple_prefs_add_int(OPT_SOUNDID_RECIPIENT, PURPLE_SOUND_POUNCE_DEFAULT);
318 purple_prefs_add_string(OPT_USERLIST_RECIPIENT, DEFAULT_LIST); 398 purple_prefs_add_string(OPT_USERLIST_RECIPIENT, DEFAULT_LIST);
320 purple_prefs_add_string(OPT_USERLIST_SENDER, DEFAULT_LIST); 400 purple_prefs_add_string(OPT_USERLIST_SENDER, DEFAULT_LIST);
321 401
322 /* compile regex */ 402 /* compile regex */
323 regp[RECIPIENT] = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL); 403 regp[RECIPIENT] = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL);
324 regp[SENDER] = g_regex_new("<body>([A-Za-z0-9_]+): ", 0, 0, NULL); 404 regp[SENDER] = g_regex_new("<body>([A-Za-z0-9_]+): ", 0, 0, NULL);
405
406 regp[COMMAND] = g_regex_new(
407 "^(?:\\s*)([dDfFgGlLmMnNtTwW]{1})(?:\\s+[A-Za-z0-9_]+\\Z)",
408 G_REGEX_RAW, 0, NULL);
409
410 regp[PSEUDO] = g_regex_new(
411 "^(?:\\s*[\"#$\%&\'()*+,-./:;<=>\?\[\\\]_`{|}~]*[^\\s\\x21-\\x7E]*)([dDfFgGlLmMnNtTwW]{1})(?:\\Z|\\s+|[^\\x21-\\x7E]+\\Z)",
412 G_REGEX_RAW, 0, NULL);
325 } 413 }
326 414
327 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info) 415 PURPLE_INIT_PLUGIN(pidgin_twitter, init_plugin, info)