Mercurial > pidgin-twitter
comparison pidgin-twitter.c @ 36:ae1d059fa6fe
added oops message suppression functionality. it discards oops message if a sent message is more than 140 bytes and less than 140 letters.
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Sun, 11 May 2008 03:52:06 +0900 |
parents | 7d07fcb9c616 |
children | bafe2abf2d3b |
comparison
equal
deleted
inserted
replaced
35:7d07fcb9c616 | 36:ae1d059fa6fe |
---|---|
53 #define OPT_COUNTER OPT_PIDGINTWITTER "/counter" | 53 #define OPT_COUNTER OPT_PIDGINTWITTER "/counter" |
54 | 54 |
55 #define RECIPIENT_FORMAT "@<a href='http://twitter.com/%s'>%s</a>" | 55 #define RECIPIENT_FORMAT "@<a href='http://twitter.com/%s'>%s</a>" |
56 #define SENDER_FORMAT "<a href='http://twitter.com/%s'>%s</a>: " | 56 #define SENDER_FORMAT "<a href='http://twitter.com/%s'>%s</a>: " |
57 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" | 57 #define DEFAULT_LIST "(list of users: separated with ' ,:;')" |
58 | 58 #define OOPS_MESSAGE "<body>Oops! Your update was over 140 characters. We sent the short version to your friends (they can view the entire update on the web).<BR></body>" |
59 | 59 |
60 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PLUGIN_NAME, \ | 60 #define twitter_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, PLUGIN_NAME, \ |
61 fmt, ## __VA_ARGS__); | 61 fmt, ## __VA_ARGS__); |
62 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PLUGIN_NAME, \ | 62 #define twitter_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, PLUGIN_NAME, \ |
63 | 63 |
64 /* globals */ | 64 /* globals */ |
65 static GRegex *regp[5]; | 65 static GRegex *regp[5]; |
66 | 66 static gboolean suppress_oops = FALSE; |
67 static void | |
68 delete_sender_with_link(gchar **str) | |
69 { | |
70 gchar *newstr = NULL; | |
71 | |
72 /* replace senders with link */ | |
73 newstr = g_regex_replace(regp[SENDER_WITH_LINK], *str, -1, 0, "@\\1", 0, NULL); | |
74 twitter_debug("*str = %s\n", *str); | |
75 twitter_debug("rnewstr = %s\n", newstr); | |
76 | |
77 g_free(*str); | |
78 *str = newstr; | |
79 } | |
80 | 67 |
81 /* prototypes */ | 68 /* prototypes */ |
82 static gboolean is_twitter_conv(PurpleConversation *conv); | 69 static gboolean is_twitter_conv(PurpleConversation *conv); |
83 static void attach_to_gtkconv(PidginConversation *gtkconv, gpointer null); | 70 static void attach_to_gtkconv(PidginConversation *gtkconv, gpointer null); |
84 static void detach_from_gtkconv(PidginConversation *gtkconv, gpointer null); | 71 static void detach_from_gtkconv(PidginConversation *gtkconv, gpointer null); |
85 static void escape(gchar **str); | 72 static void escape(gchar **str); |
86 | 73 |
74 static void | |
75 delete_sender_with_link(gchar **str) | |
76 { | |
77 gchar *newstr = NULL; | |
78 | |
79 /* replace senders with link */ | |
80 newstr = g_regex_replace(regp[SENDER_WITH_LINK], *str, -1, 0, "@\\1", 0, NULL); | |
81 twitter_debug("*str = %s\n", *str); | |
82 twitter_debug("rnewstr = %s\n", newstr); | |
83 | |
84 g_free(*str); | |
85 *str = newstr; | |
86 } | |
87 | 87 |
88 static void | 88 static void |
89 escape(gchar **str) | 89 escape(gchar **str) |
90 { | 90 { |
91 GMatchInfo *match_info = NULL; | 91 GMatchInfo *match_info = NULL; |
147 !strcmp(recipient, "twitter@twitter.com")) { | 147 !strcmp(recipient, "twitter@twitter.com")) { |
148 /* escape */ | 148 /* escape */ |
149 if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) { | 149 if(purple_prefs_get_bool(OPT_ESCAPE_PSEUDO)) { |
150 escape(buffer); | 150 escape(buffer); |
151 } | 151 } |
152 /* strip excessive link from @username */ | |
152 delete_sender_with_link(buffer); | 153 delete_sender_with_link(buffer); |
154 | |
155 /* try to suppress Oops message */ | |
156 gint utflen = g_utf8_strlen(*buffer, -1); | |
157 gint bytes = strlen(*buffer); | |
158 twitter_debug("utflen = %d bytes = %d\n", utflen, bytes); | |
159 if(bytes > 140 && utflen <= 140) | |
160 suppress_oops = TRUE; | |
153 } | 161 } |
154 return FALSE; | 162 return FALSE; |
155 } | 163 } |
156 | 164 |
157 static gboolean | 165 static gboolean |
180 newstr = g_regex_replace_eval(regp[which], // compiled regex | 188 newstr = g_regex_replace_eval(regp[which], // compiled regex |
181 *str, // subject string | 189 *str, // subject string |
182 -1, // length of the subject string | 190 -1, // length of the subject string |
183 0, // start position | 191 0, // start position |
184 0, // match options | 192 0, // match options |
185 eval, // function to call for each match | 193 eval, // function to be called for each match |
186 &which, // user data | 194 &which, // user data |
187 NULL); // error handler | 195 NULL); // error handler |
188 | 196 |
189 twitter_debug("newstr = %s\n", newstr); | 197 twitter_debug("newstr = %s\n", newstr); |
190 | 198 |
284 | 292 |
285 // twitter_debug("new_text = %s utf8_strlen = %ld new_text_length = %d\n", | 293 // twitter_debug("new_text = %s utf8_strlen = %ld new_text_length = %d\n", |
286 // new_text, g_utf8_strlen(new_text, -1), new_text_length); | 294 // new_text, g_utf8_strlen(new_text, -1), new_text_length); |
287 | 295 |
288 markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>", | 296 markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>", |
289 count<=140 ? "black" : "red", | 297 count <= 140 ? "black" : "red", |
290 count); | 298 count); |
291 | 299 |
292 box = gtkconv->toolbar; | 300 box = gtkconv->toolbar; |
293 counter = g_object_get_data(G_OBJECT(box), | 301 counter = g_object_get_data(G_OBJECT(box), |
294 PLUGIN_ID "-counter"); | 302 PLUGIN_ID "-counter"); |
311 guint count = gtk_text_buffer_get_char_count(textbuffer) - | 319 guint count = gtk_text_buffer_get_char_count(textbuffer) - |
312 (gtk_text_iter_get_offset(end_pos) - | 320 (gtk_text_iter_get_offset(end_pos) - |
313 gtk_text_iter_get_offset(start_pos)); | 321 gtk_text_iter_get_offset(start_pos)); |
314 | 322 |
315 markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>", | 323 markup = g_markup_printf_escaped("<span color=\"%s\">%u</span>", |
316 count<=140 ? "black" : "red", | 324 count <= 140 ? "black" : "red", |
317 count); | 325 count); |
318 | 326 |
319 box = gtkconv->toolbar; | 327 box = gtkconv->toolbar; |
320 counter = g_object_get_data(G_OBJECT(box), | 328 counter = g_object_get_data(G_OBJECT(box), |
321 PLUGIN_ID "-counter"); | 329 PLUGIN_ID "-counter"); |
454 } | 462 } |
455 } | 463 } |
456 } | 464 } |
457 | 465 |
458 static gboolean | 466 static gboolean |
467 receiving_im_cb(PurpleAccount *account, char **sender, char **buffer, | |
468 PurpleConversation *conv, PurpleMessageFlags *flags, void *data) | |
469 { | |
470 twitter_debug("receiving_im_cb: buffer = %s\n", *buffer); | |
471 | |
472 twitter_debug("receiving_im_cb: suppress_oops = %d\n", suppress_oops); | |
473 if(!suppress_oops) | |
474 return FALSE; | |
475 | |
476 if(strstr(*buffer, OOPS_MESSAGE)) { | |
477 twitter_debug("receiving_im_cb: clearing sender and buffer\n"); | |
478 g_free(*sender); *sender = NULL; | |
479 g_free(*buffer); *buffer = NULL; | |
480 suppress_oops = FALSE; | |
481 } | |
482 return FALSE; | |
483 } | |
484 | |
485 static gboolean | |
459 load_plugin(PurplePlugin * plugin) | 486 load_plugin(PurplePlugin * plugin) |
460 { | 487 { |
461 /* connect to signal */ | 488 /* connect to signal */ |
462 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg", | 489 purple_signal_connect(purple_conversations_get_handle(), "writing-im-msg", |
463 plugin, PURPLE_CALLBACK(writing_im_cb), NULL); | 490 plugin, PURPLE_CALLBACK(writing_im_cb), NULL); |
464 purple_signal_connect(purple_conversations_get_handle(), "sending-im-msg", | 491 purple_signal_connect(purple_conversations_get_handle(), "sending-im-msg", |
465 plugin, PURPLE_CALLBACK(sending_im_cb), NULL); | 492 plugin, PURPLE_CALLBACK(sending_im_cb), NULL); |
466 purple_signal_connect(purple_conversations_get_handle(), | 493 purple_signal_connect(purple_conversations_get_handle(), |
467 "conversation-created", | 494 "conversation-created", |
468 plugin, PURPLE_CALLBACK(conv_created_cb), NULL); | 495 plugin, PURPLE_CALLBACK(conv_created_cb), NULL); |
496 purple_signal_connect(purple_conversations_get_handle(), "receiving-im-msg", | |
497 plugin, PURPLE_CALLBACK(receiving_im_cb), NULL); | |
469 | 498 |
470 /* compile regex */ | 499 /* compile regex */ |
471 regp[RECIPIENT] = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL); | 500 regp[RECIPIENT] = g_regex_new("@([A-Za-z0-9_]+)", 0, 0, NULL); |
472 regp[SENDER] = g_regex_new("<body>([A-Za-z0-9_]+): ", 0, 0, NULL); | 501 regp[SENDER] = g_regex_new("<body>([A-Za-z0-9_]+): ", 0, 0, NULL); |
473 regp[COMMAND] = g_regex_new( | 502 regp[COMMAND] = g_regex_new( |
501 "sending-im-msg", | 530 "sending-im-msg", |
502 plugin, PURPLE_CALLBACK(sending_im_cb)); | 531 plugin, PURPLE_CALLBACK(sending_im_cb)); |
503 purple_signal_disconnect(purple_conversations_get_handle(), | 532 purple_signal_disconnect(purple_conversations_get_handle(), |
504 "conversation-created", | 533 "conversation-created", |
505 plugin, PURPLE_CALLBACK(conv_created_cb)); | 534 plugin, PURPLE_CALLBACK(conv_created_cb)); |
535 purple_signal_disconnect(purple_conversations_get_handle(), | |
536 "receiving-im-msg", | |
537 plugin, PURPLE_CALLBACK(receiving_im_cb)); | |
506 | 538 |
507 /* unreference regp */ | 539 /* unreference regp */ |
508 g_regex_unref(regp[RECIPIENT]); | 540 g_regex_unref(regp[RECIPIENT]); |
509 g_regex_unref(regp[SENDER]); | 541 g_regex_unref(regp[SENDER]); |
510 g_regex_unref(regp[COMMAND]); | 542 g_regex_unref(regp[COMMAND]); |
532 | 564 |
533 static PurplePluginPrefFrame * | 565 static PurplePluginPrefFrame * |
534 get_plugin_pref_frame(PurplePlugin * plugin) | 566 get_plugin_pref_frame(PurplePlugin * plugin) |
535 { | 567 { |
536 /* create gtk elements for the plugin preferences */ | 568 /* create gtk elements for the plugin preferences */ |
537 | |
538 PurplePluginPref *pref; | 569 PurplePluginPref *pref; |
539 PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new(); | 570 PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new(); |
540 | 571 |
541 /* frame title */ | 572 /************************/ |
573 /* translatione heading */ | |
574 /************************/ | |
542 pref = purple_plugin_pref_new_with_label("Translation Configurations"); | 575 pref = purple_plugin_pref_new_with_label("Translation Configurations"); |
543 purple_plugin_pref_frame_add(frame, pref); | 576 purple_plugin_pref_frame_add(frame, pref); |
544 | 577 |
545 /* translation settings */ | 578 /* translation settings */ |
546 pref = purple_plugin_pref_new_with_name_and_label( | 579 pref = purple_plugin_pref_new_with_name_and_label( |
552 OPT_TRANSLATE_SENDER, | 585 OPT_TRANSLATE_SENDER, |
553 "Translate sender name to link"); | 586 "Translate sender name to link"); |
554 purple_plugin_pref_frame_add(frame, pref); | 587 purple_plugin_pref_frame_add(frame, pref); |
555 | 588 |
556 | 589 |
557 | 590 /*************************/ |
558 | 591 /* miscellaneous heading */ |
559 | 592 /*************************/ |
560 /* frame title */ | |
561 pref = purple_plugin_pref_new_with_label("Miscellaneous Configurations"); | 593 pref = purple_plugin_pref_new_with_label("Miscellaneous Configurations"); |
562 purple_plugin_pref_frame_add(frame, pref); | 594 purple_plugin_pref_frame_add(frame, pref); |
563 | 595 |
564 /* escape pseudo command setting */ | 596 /* escape pseudo command setting */ |
565 pref = purple_plugin_pref_new_with_name_and_label( | 597 pref = purple_plugin_pref_new_with_name_and_label( |
571 pref = purple_plugin_pref_new_with_name_and_label( | 603 pref = purple_plugin_pref_new_with_name_and_label( |
572 OPT_COUNTER, | 604 OPT_COUNTER, |
573 "Show text counter"); | 605 "Show text counter"); |
574 purple_plugin_pref_frame_add(frame, pref); | 606 purple_plugin_pref_frame_add(frame, pref); |
575 | 607 |
576 /* xxx */ | |
577 purple_prefs_connect_callback(plugin, OPT_COUNTER, | 608 purple_prefs_connect_callback(plugin, OPT_COUNTER, |
578 counter_prefs_cb, NULL); | 609 counter_prefs_cb, NULL); |
579 | 610 |
580 | 611 /*****************/ |
581 | 612 /* sound heading */ |
582 | 613 /*****************/ |
583 | |
584 /* frame title */ | |
585 pref = purple_plugin_pref_new_with_label("Sound Configurations"); | 614 pref = purple_plugin_pref_new_with_label("Sound Configurations"); |
586 purple_plugin_pref_frame_add(frame, pref); | 615 purple_plugin_pref_frame_add(frame, pref); |
587 | 616 |
588 /* sound settings for recipient */ | 617 /* sound settings for recipient */ |
589 pref = purple_plugin_pref_new_with_name_and_label( | 618 pref = purple_plugin_pref_new_with_name_and_label( |
612 purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8)); | 641 purple_plugin_pref_add_choice(pref, "Chat Someone Say", GINT_TO_POINTER(8)); |
613 purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9)); | 642 purple_plugin_pref_add_choice(pref, "Pounce Default", GINT_TO_POINTER(9)); |
614 purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10)); | 643 purple_plugin_pref_add_choice(pref, "Chat Nick Said", GINT_TO_POINTER(10)); |
615 | 644 |
616 purple_plugin_pref_frame_add(frame, pref); | 645 purple_plugin_pref_frame_add(frame, pref); |
617 | |
618 | 646 |
619 /* sound setting for sender */ | 647 /* sound setting for sender */ |
620 pref = purple_plugin_pref_new_with_name_and_label( | 648 pref = purple_plugin_pref_new_with_name_and_label( |
621 OPT_PLAYSOUND_SENDER, | 649 OPT_PLAYSOUND_SENDER, |
622 "Play sound if sender of a message is in the sender list"); | 650 "Play sound if sender of a message is in the sender list"); |