diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index 70ba33332ef5f0b9892680b9ab70905e95c3cc1e..13b0725518e1db3d92b7da865e10c36f2566e96d 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -45,6 +45,11 @@ DEFINE_LOGGER(logger_libpurple, "libpurple"); DEFINE_LOGGER(logger, "backend"); +/* Additional PURPLE_MESSAGE_* flags as a hack to track the origin of the message. */ +typedef enum { + PURPLE_MESSAGE_SPECTRUM2_ORIGINATED = 0x80000000, +} PurpleMessageSpectrum2Flags; + int main_socket; static int writeInput; bool firstPing = true; @@ -542,19 +547,19 @@ class SpectrumNetworkPlugin : public NetworkPlugin { if (xhtml.empty()) { gchar *_markup = purple_markup_escape_text_wrapped(message.c_str(), -1); if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_IM) { - purple_conv_im_send_wrapped(PURPLE_CONV_IM_WRAPPED(conv), _markup); + purple_conv_im_send_with_flags_wrapped(PURPLE_CONV_IM_WRAPPED(conv), _markup, static_cast(PURPLE_MESSAGE_SPECTRUM2_ORIGINATED)); } else if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_CHAT) { - purple_conv_chat_send_wrapped(PURPLE_CONV_CHAT_WRAPPED(conv), _markup); + purple_conv_chat_send_with_flags_wrapped(PURPLE_CONV_CHAT_WRAPPED(conv), _markup, static_cast(PURPLE_MESSAGE_SPECTRUM2_ORIGINATED)); } g_free(_markup); } else { if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_IM) { - purple_conv_im_send_wrapped(PURPLE_CONV_IM_WRAPPED(conv), xhtml.c_str()); + purple_conv_im_send_with_flags_wrapped(PURPLE_CONV_IM_WRAPPED(conv), xhtml.c_str(), static_cast(PURPLE_MESSAGE_SPECTRUM2_ORIGINATED)); } else if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_CHAT) { - purple_conv_chat_send_wrapped(PURPLE_CONV_CHAT_WRAPPED(conv), xhtml.c_str()); + purple_conv_chat_send_with_flags_wrapped(PURPLE_CONV_CHAT_WRAPPED(conv), xhtml.c_str(), static_cast(PURPLE_MESSAGE_SPECTRUM2_ORIGINATED)); } } } @@ -1138,7 +1143,11 @@ static PurpleBlistUiOps blistUiOps = NULL }; +static void conv_write_im(PurpleConversation *conv, const char *who, const char *msg, PurpleMessageFlags flags, time_t mtime); + static void conv_write(PurpleConversation *conv, const char *who, const char *alias, const char *msg, PurpleMessageFlags flags, time_t mtime) { + LOG4CXX_INFO(logger, "conv_write()"); + if (flags & PURPLE_MESSAGE_SYSTEM && CONFIG_STRING(config, "service.protocol") == "prpl-telegram") { PurpleAccount *account = purple_conversation_get_account_wrapped(conv); @@ -1194,6 +1203,10 @@ static void conv_write(PurpleConversation *conv, const char *who, const char *al np->handleMessage(np->m_accounts[account], np->NameToLegacyName(account, conversationName), message_, who, xhtml_, timestamp); } } + else { + //Handle all non-special cases by just passing them to conv_write_im + conv_write_im(conv, who, msg, flags, mtime); + } } static char *calculate_data_hash(guchar *data, size_t len, @@ -1223,9 +1236,26 @@ static char *calculate_data_hash(guchar *data, size_t len, } static void conv_write_im(PurpleConversation *conv, const char *who, const char *msg, PurpleMessageFlags flags, time_t mtime) { - // Don't forwards our own messages. - if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_IM && (flags & PURPLE_MESSAGE_SEND || flags & PURPLE_MESSAGE_SYSTEM)) { - return; + LOG4CXX_INFO(logger, "conv_write_im()"); + bool isCarbon = false; + + if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_IM) { + //Don't forwards our own messages, but do forward messages "from=us" which originated elsewhere + //(such as carbons of our messages from other legacy network clients) + if (flags & PURPLE_MESSAGE_SPECTRUM2_ORIGINATED) { + LOG4CXX_INFO(logger, "conv_write_im(): ignoring a message generated by us"); + return; + } + + //If this is a carbon of a message from us, mark it as such + if(flags & PURPLE_MESSAGE_SEND) + isCarbon = true; + + //Originally the transport had this filter too, I'm leaving it in for now: + if (flags & PURPLE_MESSAGE_SYSTEM) { + LOG4CXX_INFO(logger, "conv_write_im(): ignoring a system message"); + return; + } } PurpleAccount *account = purple_conversation_get_account_wrapped(conv); @@ -1336,12 +1366,12 @@ static void conv_write_im(PurpleConversation *conv, const char *who, const char w.erase((int) pos, w.length() - (int) pos); } LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "' name='" << w << "'"); - np->handleMessage(np->m_accounts[account], w, message_, n, xhtml_, timestamp); + np->handleMessage(np->m_accounts[account], w, message_, n, xhtml_, timestamp, false, false, isCarbon); } else { std::string conversationName = purple_conversation_get_name_wrapped(conv); LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "' name='" << conversationName << "' " << who); - np->handleMessage(np->m_accounts[account], np->NameToLegacyName(account, conversationName), message_, who, xhtml_, timestamp); + np->handleMessage(np->m_accounts[account], np->NameToLegacyName(account, conversationName), message_, who, xhtml_, timestamp, false, false, isCarbon); } }