From e4b34548f6fe0fac11db01e298b80d5011987762 Mon Sep 17 00:00:00 2001 From: dexterlb Date: Thu, 28 Dec 2017 10:31:12 +0200 Subject: [PATCH 1/3] hack to handle incoming files which come as an absolute path --- backends/libpurple/main.cpp | 86 +++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index e0768a34a..3e210027f 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -27,6 +27,8 @@ #include #include #include +#include +#include #ifdef WITH_LIBEVENT #include @@ -1222,6 +1224,61 @@ static char *calculate_data_hash(guchar *data, size_t len, return g_strdup(digest); } +static std::string check_incoming_document(const char *msg) { + /* + Sometimes, when a user sends a file, we receive a link with an absolute + path to the saved file in the backend's directory. This is a hack + which matches those links, moves the file to the web directory + and sends a proper link to the user. + + This has to be fixed in the backend. + */ + if (strstr(msg, "[document") == NULL) { + return ""; + } + + static boost::regex document_expr("\[document (.*)[ ]*(.*)]"); + boost::smatch match; + std::string plain; + + if (!boost::regex_search(std::string(msg), match, document_expr)) { + return ""; + } + + std::string target_dir = CONFIG_STRING(config, "service.web_directory"); + std::string local_file = match[1]; + std::string hash = match[2]; + std::string basename = match[3]; + std::string file_type = match[4]; + + if (!target_dir.empty()) { + target_dir += "/" + hash; // todo: escape + std::string target_file = target_dir + "/" + basename; + std::string link = CONFIG_STRING(config, "service.web_url") + "/" + hash + "/" + basename; + + boost::filesystem::create_directories( + boost::filesystem::path(target_dir) + ); + + boost::filesystem::rename( + boost::filesystem::path(local_file), + boost::filesystem::path(target_file) + ); + + plain + = std::string("file ") + + "\"" + basename + "\"" + + " of type [" + + file_type + + "]: " + + link; + } else { + plain = "[received a file]"; + } + + return plain; +} + 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)) { @@ -1297,15 +1354,26 @@ static void conv_write_im(PurpleConversation *conv, const char *who, const char g_free(strip); } else { - // Escape HTML characters. - char *newline = purple_strdup_withhtml_wrapped(msg); - char *strip, *xhtml; - purple_markup_html_to_xhtml_wrapped(newline, &xhtml, &strip); - message_ = strip; - xhtml_ = xhtml; - g_free(newline); - g_free(xhtml); - g_free(strip); + std::string file_link = check_incoming_document(msg); + + if (!file_link.empty()) { + char *strip, *xhtml; + purple_markup_html_to_xhtml_wrapped(file_link.c_str(), &xhtml, &strip); + message_ = strip; + xhtml_ = xhtml; + g_free(xhtml); + g_free(strip); + } else { + // Escape HTML characters. + char *newline = purple_strdup_withhtml_wrapped(msg); + char *strip, *xhtml; + purple_markup_html_to_xhtml_wrapped(newline, &xhtml, &strip); + message_ = strip; + xhtml_ = xhtml; + g_free(newline); + g_free(xhtml); + g_free(strip); + } } // AIM and XMPP adds ... here... From b70e97f844c5b7f4102a66f55b0ecf2cdcf2ba7a Mon Sep 17 00:00:00 2001 From: dexterlb Date: Wed, 13 Jun 2018 20:27:08 +0300 Subject: [PATCH 2/3] fix weird characters in URLs of uploaded files --- backends/libpurple/main.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index 432dfd940..73f6653b4 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -1283,7 +1283,7 @@ static std::string check_incoming_document(const char *msg) { return ""; } - static boost::regex document_expr("\[document (.*)[ ]*(.*)]"); + static boost::regex document_expr("\[[a-z]+ (.*)[ ]*(.*)]"); boost::smatch match; std::string plain; @@ -1297,6 +1297,9 @@ static std::string check_incoming_document(const char *msg) { std::string basename = match[3]; std::string file_type = match[4]; + static boost::regex bad_symbol_expr("[^0-9a-zA-Z]"); + basename = boost::regex_replace(basename, bad_symbol_expr, "_"); + if (!target_dir.empty()) { target_dir += "/" + hash; // todo: escape std::string target_file = target_dir + "/" + basename; @@ -1328,7 +1331,7 @@ static std::string check_incoming_document(const char *msg) { static void conv_write_im(PurpleConversation *conv, const char *who, const char *msg, PurpleMessageFlags flags, time_t mtime) { 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) @@ -1336,11 +1339,11 @@ static void conv_write_im(PurpleConversation *conv, const char *who, const char 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"); From 1fc7ec087c0a9736d9dfefd5a527bd580bed61de Mon Sep 17 00:00:00 2001 From: dexterlb Date: Sun, 1 Jul 2018 11:55:49 +0300 Subject: [PATCH 3/3] fix broken file extensions when uploading file via libpurple --- backends/libpurple/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index 73f6653b4..2b515f2ca 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -1297,7 +1297,7 @@ static std::string check_incoming_document(const char *msg) { std::string basename = match[3]; std::string file_type = match[4]; - static boost::regex bad_symbol_expr("[^0-9a-zA-Z]"); + static boost::regex bad_symbol_expr("[^0-9a-zA-Z._]"); basename = boost::regex_replace(basename, bad_symbol_expr, "_"); if (!target_dir.empty()) {