splitter: Fix compatibility with OTR (and play nice with other plugins by hooking in last)
1.1 --- a/splitter/splitter.c Mon Dec 28 23:09:22 2009 -0800
1.2 +++ b/splitter/splitter.c Wed Dec 30 19:48:39 2009 -0800
1.3 @@ -75,6 +75,9 @@
1.4 gint end;
1.5 } message_slice;
1.6
1.7 +/* Global variable to block infinite loops. Single-threaded is nice */
1.8 +static gboolean splitter_injected_message = FALSE;
1.9 +
1.10 /* plugin preference variables */
1.11 static gint current_split_size;
1.12
1.13 @@ -123,7 +126,6 @@
1.14 PurpleAccount *account;
1.15 PurpleConnection *gc;
1.16 char *displayed = NULL, *sent = NULL;
1.17 - gint err = 0;
1.18
1.19 if (strlen(message) == 0)
1.20 return;
1.21 @@ -149,60 +151,18 @@
1.22
1.23 msgflags |= PURPLE_MESSAGE_SEND;
1.24
1.25 + splitter_injected_message = TRUE;
1.26 +
1.27 if (type == PURPLE_CONV_TYPE_IM) {
1.28 - PurpleConvIm *im = PURPLE_CONV_IM(conv);
1.29 -
1.30 - if (sent != NULL && sent[0] != '\0') {
1.31 -
1.32 - err = serv_send_im(gc, purple_conversation_get_name(conv),
1.33 - sent, msgflags);
1.34 -
1.35 - if ((err > 0) && (displayed != NULL))
1.36 - purple_conv_im_write(im, NULL, displayed, msgflags, time(NULL));
1.37 -
1.38 - purple_signal_emit(purple_conversations_get_handle(), "sent-im-msg",
1.39 - account,
1.40 - purple_conversation_get_name(conv), sent);
1.41 - }
1.42 + if (sent != NULL && sent[0] != '\0')
1.43 + purple_conv_im_send_with_flags(PURPLE_CONV_IM(conv), sent, msgflags);
1.44 }
1.45 else {
1.46 - if (sent != NULL && sent[0] != '\0') {
1.47 - err = serv_chat_send(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)), sent, msgflags);
1.48 -
1.49 - purple_signal_emit(purple_conversations_get_handle(), "sent-chat-msg",
1.50 - account, sent,
1.51 - purple_conv_chat_get_id(PURPLE_CONV_CHAT(conv)));
1.52 - }
1.53 + if (sent != NULL && sent[0] != '\0')
1.54 + purple_conv_chat_send_with_flags(PURPLE_CONV_CHAT(conv), sent, msgflags);
1.55 }
1.56
1.57 - if (err < 0) {
1.58 - const char *who;
1.59 - const char *msg;
1.60 -
1.61 - who = purple_conversation_get_name(conv);
1.62 -
1.63 - if (err == -E2BIG) {
1.64 - msg = _("Unable to send message: The message is too large.");
1.65 -
1.66 - if (!purple_conv_present_error(who, account, msg)) {
1.67 - char *msg2 = g_strdup_printf(_("Unable to send message to %s."), who);
1.68 - purple_notify_error(gc, NULL, msg2, _("The message is too large."));
1.69 - g_free(msg2);
1.70 - }
1.71 - }
1.72 - else if (err == -ENOTCONN) {
1.73 - purple_debug_error("conversation", "Not yet connected.\n");
1.74 - }
1.75 - else {
1.76 - msg = _("Unable to send message.");
1.77 -
1.78 - if (!purple_conv_present_error(who, account, msg)) {
1.79 - char *msg2 = g_strdup_printf(_("Unable to send message to %s."), who);
1.80 - purple_notify_error(gc, NULL, msg2, NULL);
1.81 - g_free(msg2);
1.82 - }
1.83 - }
1.84 - }
1.85 + splitter_injected_message = FALSE;
1.86
1.87 g_free(displayed);
1.88 g_free(sent);
1.89 @@ -443,6 +403,9 @@
1.90 sending_chat_msg_cb(PurpleAccount *account, const char **message, int id) {
1.91 message_to_conv *msg_to_conv;
1.92
1.93 + if (splitter_injected_message)
1.94 + return;
1.95 +
1.96 purple_debug_misc("purple-splitter", "splitter plugin invoked\n");
1.97
1.98 g_return_if_fail(account != NULL);
1.99 @@ -466,6 +429,9 @@
1.100 {
1.101 message_to_conv *msg_to_conv;
1.102
1.103 + if (splitter_injected_message)
1.104 + return;
1.105 +
1.106 purple_debug_misc("purple-splitter", "splitter plugin invoked\n");
1.107
1.108 g_return_if_fail(account != NULL);
1.109 @@ -473,6 +439,10 @@
1.110 g_return_if_fail(message != NULL);
1.111 g_return_if_fail(*message != NULL);
1.112
1.113 + /* OTR compatibility hack */
1.114 + if (0 == strncmp(*message, "?OTR", strlen("?OTR")))
1.115 + return;
1.116 +
1.117 msg_to_conv = g_new0(message_to_conv, 1);
1.118
1.119 msg_to_conv->sender_username = g_strdup(account->username);
1.120 @@ -491,13 +461,13 @@
1.121 plugin,
1.122 PURPLE_CALLBACK(sending_im_msg_cb),
1.123 NULL,
1.124 - PURPLE_SIGNAL_PRIORITY_LOWEST);
1.125 + PURPLE_SIGNAL_PRIORITY_HIGHEST);
1.126 purple_signal_connect_priority(purple_conversations_get_handle(),
1.127 "sending-chat-msg",
1.128 plugin,
1.129 PURPLE_CALLBACK(sending_chat_msg_cb),
1.130 NULL,
1.131 - PURPLE_SIGNAL_PRIORITY_LOWEST);
1.132 + PURPLE_SIGNAL_PRIORITY_HIGHEST);
1.133
1.134 return TRUE;
1.135 }