Changeset - 65d5b4fe996f
[Not reviewed]
0 2 0
Jan Kaluza - 10 years ago 2016-02-18 14:30:18
jkaluza@redhat.com
Libpurple: When service.web_directory and service.web_url is set, PURPLE_MESSAGE_IMAGES are stored to web_directory and link is forwarded to the user. Tested only with Facebook for now. Fix #106
2 files changed with 100 insertions and 19 deletions:
0 comments (0 inline, 0 general)
backends/libpurple/main.cpp
Show inline comments
 
#include "utils.h"
 

	
 
#include "glib.h"
 

	
 
// win32/libc_interface.h defines its own socket(), read() and so on.
 
// We don't want to use it here.
 
#define _LIBC_INTERFACE_H_ 1
 

	
 
#include "purple.h"
 
#include <algorithm>
 
#include <iostream>
 
#include <fstream> 
 

	
 
#include "transport/NetworkPlugin.h"
 
#include "transport/Logging.h"
 
#include "transport/Config.h"
 
#include "geventloop.h"
 

	
 
// #include "valgrind/memcheck.h"
 
#if !defined(__FreeBSD__) && !defined(__APPLE__)
 
#include "malloc.h"
 
#endif
 
#include <algorithm>
 
#include "errno.h"
 
#include <boost/make_shared.hpp>
 

	
 
#ifdef WITH_LIBEVENT
 
#include <event.h>
 
#endif
 

	
 
#ifdef WIN32
 
#include "win32/win32dep.h"
 
#define close closesocket
 
#define ssize_t SSIZE_T
 
#include <process.h>
 
#define getpid _getpid
 
@@ -1102,107 +1103,185 @@ static void conv_write(PurpleConversation *conv, const char *who, const char *al
 

	
 
		std::string timestamp;
 
		if (mtime && (unsigned long) time(NULL)-10 > (unsigned long) mtime/* && (unsigned long) time(NULL) - 31536000 < (unsigned long) mtime*/) {
 
			char buf[80];
 
			strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", gmtime(&mtime));
 
			timestamp = buf;
 
		}
 

	
 
	// 	LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "'");
 

	
 
		if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_IM) {
 
			std::string w = purple_normalize_wrapped(account, who);
 
			size_t pos = w.find("/");
 
			if (pos != std::string::npos)
 
				w.erase((int) pos, w.length() - (int) pos);
 
			np->handleMessage(np->m_accounts[account], w, message_, "", xhtml_, timestamp);
 
		}
 
		else {
 
			LOG4CXX_INFO(logger, "Received message body='" << message_ << "' name='" << purple_conversation_get_name_wrapped(conv) << "' " << who);
 
			np->handleMessage(np->m_accounts[account], purple_conversation_get_name_wrapped(conv), message_, who, xhtml_, timestamp);
 
		}
 
	}
 
}
 

	
 
static char *calculate_data_hash(guchar *data, size_t len,
 
    const gchar *hash_algo)
 
{
 
	PurpleCipherContext *context;
 
	static gchar digest[129]; /* 512 bits hex + \0 */
 

	
 
	context = purple_cipher_context_new_by_name(hash_algo, NULL);
 
	if (context == NULL)
 
	{
 
		purple_debug_error("jabber", "Could not find %s cipher\n", hash_algo);
 
		g_return_val_if_reached(NULL);
 
	}
 

	
 
	/* Hash the data */
 
	purple_cipher_context_append(context, data, len);
 
	if (!purple_cipher_context_digest_to_str(context, sizeof(digest), digest, NULL))
 
	{
 
		purple_debug_error("jabber", "Failed to get digest for %s cipher.\n",
 
		    hash_algo);
 
		g_return_val_if_reached(NULL);
 
	}
 
	purple_cipher_context_destroy(context);
 

	
 
	return g_strdup(digest);
 
}
 

	
 
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;
 
	}
 
	PurpleAccount *account = purple_conversation_get_account_wrapped(conv);
 

	
 
// 	char *striped = purple_markup_strip_html_wrapped(message);
 
// 	std::string msg = striped;
 
// 	g_free(striped);
 

	
 
	std::string message_;
 
	std::string xhtml_;
 

	
 
	if (flags & PURPLE_MESSAGE_IMAGES && !CONFIG_STRING(config, "service.web_directory").empty() && !CONFIG_STRING(config, "service.web_url").empty() ) {
 
		LOG4CXX_INFO(logger, "Received image body='" << msg << "'");
 
		std::string body = msg;
 
		std::string plain = msg;
 
		size_t i;
 
		while ((i = body.find("<img id=\"")) != std::string::npos) {
 
			int from = i + strlen("<img id=\"");
 
			int to = body.find("\"", from + 1);
 
			std::string id = body.substr(from, to - from);
 
			LOG4CXX_INFO(logger, "Image ID = '" << id << "' " << from << " " << to);
 

	
 
			PurpleStoredImage *image = purple_imgstore_find_by_id(atoi(id.c_str()));
 
			if (!image) {
 
				LOG4CXX_ERROR(logger, "Cannot find image with id " << id << ".");
 
				return;
 
			}
 

	
 
	// Escape HTML characters.
 
	char *newline = purple_strdup_withhtml_wrapped(msg);
 
	char *strip, *xhtml;
 
	purple_markup_html_to_xhtml_wrapped(newline, &xhtml, &strip);
 
// 	xhtml_linkified = spectrum_markup_linkify(xhtml);
 
	std::string message_(strip);
 
			std::string ext = "icon";
 
			std::string name;
 
			guchar * data = (guchar *) purple_imgstore_get_data_wrapped(image);
 
			size_t len = purple_imgstore_get_size_wrapped(image);
 
			if (len < 300000 && data) {
 
				ext = purple_imgstore_get_extension(image);
 
				char *hash = calculate_data_hash(data, len, "sha1");
 
				if (!hash) {
 
					return;
 
				}
 
				name = hash;
 
				g_free(hash);
 

	
 
	std::string xhtml_(xhtml);
 
	g_free(newline);
 
	g_free(xhtml);
 
// 	g_free(xhtml_linkified);
 
	g_free(strip);
 
				std::ofstream output;
 
				output.open(std::string(CONFIG_STRING(config, "service.web_directory") + "/" + name + "." + ext).c_str(), std::ios::out | std::ios::binary);
 
				output.write((char *)data, len);
 
				output.close();
 
			}
 
			else {
 
				purple_imgstore_unref_wrapped(image);
 
				return;
 
			}
 
			purple_imgstore_unref_wrapped(image);
 
			
 
			std::string src = CONFIG_STRING(config, "service.web_url") + "/" + name + "." + ext;
 
			std::string img = "<img src=\"" + src + "\"/>";
 
			boost::replace_all(body, "<img id=\"" + id + "\">", img);
 
			boost::replace_all(plain, "<img id=\"" + id + "\">", src);
 
		}
 
		LOG4CXX_INFO(logger, "New image body='" << body << "'");
 
		char *strip, *xhtml;
 
		purple_markup_html_to_xhtml_wrapped(body.c_str(), &xhtml, &strip);
 
		message_ = strip;
 
		if (message_.empty()) {
 
			message_ = plain;
 
		}
 
		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 <body>...</body> here...
 
	if (xhtml_.find("<body>") == 0) {
 
		xhtml_ = xhtml_.substr(6);
 
		if (xhtml_.find("</body>") != std::string::npos) {
 
			xhtml_ = xhtml_.substr(0, xhtml_.find("</body>"));
 
		}
 
	}
 

	
 
	if (xhtml_ == message_) {
 
		xhtml_ = "";
 
	}
 

	
 
	std::string timestamp;
 
	if (mtime && (unsigned long) time(NULL)-10 > (unsigned long) mtime/* && (unsigned long) time(NULL) - 31536000 < (unsigned long) mtime*/) {
 
		char buf[80];
 
		strftime(buf, sizeof(buf), "%Y%m%dT%H%M%S", gmtime(&mtime));
 
		timestamp = buf;
 
	}
 

	
 
// 	LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "'");
 

	
 
	if (purple_conversation_get_type_wrapped(conv) == PURPLE_CONV_TYPE_IM) {
 
		std::string w = purple_normalize_wrapped(account, who);
 
		std::string n;
 
		size_t pos = w.find("/");
 
		if (pos != std::string::npos) {
 
			n = w.substr((int) pos + 1, w.length() - (int) pos);
 
			w.erase((int) pos, w.length() - (int) pos);
 
		}
 
		LOG4CXX_INFO(logger, "Received message body='" << message_ << "' name='" << w);
 
		LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "' name='" << w << "'");
 
		np->handleMessage(np->m_accounts[account], w, message_, n, xhtml_, timestamp);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, "Received message body='" << message_ << "' name='" << purple_conversation_get_name_wrapped(conv) << "' " << who);
 
		LOG4CXX_INFO(logger, "Received message body='" << message_ << "' xhtml='" << xhtml_ << "' name='" << purple_conversation_get_name_wrapped(conv) << "' " << who);
 
		np->handleMessage(np->m_accounts[account], purple_conversation_get_name_wrapped(conv), message_, who, xhtml_, timestamp);
 
	}
 
}
 

	
 
static void conv_chat_add_users(PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals) {
 
	PurpleAccount *account = purple_conversation_get_account_wrapped(conv);
 

	
 
	GList *l = cbuddies;
 
	while (l != NULL) {
 
		PurpleConvChatBuddy *cb = (PurpleConvChatBuddy *)l->data;
 
		std::string name(cb->name);
 
		std::string alias = cb->alias ? cb->alias : cb->name;
 
		int flags = GPOINTER_TO_INT(cb->flags);
 
		if (flags & PURPLE_CBFLAGS_OP || flags & PURPLE_CBFLAGS_HALFOP) {
 
// 			item->addAttribute("affiliation", "admin");
 
// 			item->addAttribute("role", "moderator");
 
			flags = 1;
 
		}
 
		else if (flags & PURPLE_CBFLAGS_FOUNDER) {
 
// 			item->addAttribute("affiliation", "owner");
 
// 			item->addAttribute("role", "moderator");
 
			flags = 1;
 
		}
 
		else {
libtransport/Config.cpp
Show inline comments
 
@@ -83,48 +83,50 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
 
		("service.pidfile", value<std::string>()->default_value("/var/run/spectrum2/$jid.pid"), "Full path to pid file")
 
		("service.portfile", value<std::string>()->default_value("/var/run/spectrum2/$jid.port"), "File to store backend_port to. It's used by spectrum2_manager.")
 
		("service.working_dir", value<std::string>()->default_value("/var/lib/spectrum2/$jid"), "Working dir")
 
		("service.allowed_servers", value<std::vector<std::string> >()->multitoken(), "Only users from these servers can connect")
 
		("service.server_mode", value<bool>()->default_value(false), "True if Spectrum should behave as server")
 
		("service.users_per_backend", value<int>()->default_value(100), "Number of users per one legacy network backend")
 
		("service.backend_host", value<std::string>()->default_value("localhost"), "Host to bind backend server to")
 
		("service.backend_port", value<std::string>()->default_value("0"), "Port to bind backend server to")
 
		("service.cert", value<std::string>()->default_value(""), "PKCS#12 Certificate.")
 
		("service.cert_password", value<std::string>()->default_value(""), "PKCS#12 Certificate password.")
 
		("service.admin_jid", value<std::vector<std::string> >()->multitoken(), "Administrator jid.")
 
		("service.admin_password", value<std::string>()->default_value(""), "Administrator password.")
 
		("service.reuse_old_backends", value<bool>()->default_value(true), "True if Spectrum should use old backends which were full in the past.")
 
		("service.idle_reconnect_time", value<int>()->default_value(0), "Time in seconds after which idle users are reconnected to let their backend die.")
 
		("service.memory_collector_time", value<int>()->default_value(0), "Time in seconds after which backend with most memory is set to die.")
 
		("service.more_resources", value<bool>()->default_value(false), "Allow more resources to be connected in server mode at the same time.")
 
		("service.enable_privacy_lists", value<bool>()->default_value(true), "")
 
		("service.enable_xhtml", value<bool>()->default_value(true), "")
 
		("service.max_room_list_size", value<int>()->default_value(100), "")
 
		("service.login_delay", value<int>()->default_value(0), "")
 
		("service.jid_escaping", value<bool>()->default_value(true), "")
 
		("service.vip_only", value<bool>()->default_value(false), "")
 
		("service.vip_message", value<std::string>()->default_value(""), "")
 
		("service.reconnect_all_users", value<bool>()->default_value(false), "")
 
		("service.web_directory", value<std::string>()->default_value(""), "Full path to directory used to save files to which the links are sent to users.")
 
		("service.web_url", value<std::string>()->default_value(""), "URL on which files in web_directory are accessible.")
 
		("vhosts.vhost", value<std::vector<std::string> >()->multitoken(), "")
 
		("identity.name", value<std::string>()->default_value("Spectrum 2 Transport"), "Name showed in service discovery.")
 
		("identity.category", value<std::string>()->default_value("gateway"), "Disco#info identity category. 'gateway' by default.")
 
		("identity.type", value<std::string>()->default_value(""), "Type of transport ('icq','msn','gg','irc', ...)")
 
		("registration.enable_public_registration", value<bool>()->default_value(true), "True if users should be able to register.")
 
		("registration.language", value<std::string>()->default_value("en"), "Default language for registration form")
 
		("registration.instructions", value<std::string>()->default_value("Enter your legacy network username and password."), "Instructions showed to user in registration form")
 
		("registration.username_label", value<std::string>()->default_value("Legacy network username:"), "Label for username field")
 
		("registration.username_mask", value<std::string>()->default_value(""), "Username mask")
 
		("registration.allowed_usernames", value<std::string>()->default_value(""), "Allowed usernames")
 
		("registration.auto_register", value<bool>()->default_value(false), "Register new user automatically when the presence arrives.")
 
		("registration.encoding", value<std::string>()->default_value("utf8"), "Default encoding in registration form")
 
		("registration.require_local_account", value<bool>()->default_value(false), "True if users have to have a local account to register to this transport from remote servers.")
 
		("registration.notify_jid", value<std::vector<std::string> >()->multitoken(), "Send message to this JID if user registers/unregisters")
 
		("registration.local_username_label", value<std::string>()->default_value("Local username:"), "Label for local usernme field")
 
		("registration.local_account_server", value<std::string>()->default_value("localhost"), "The server on which the local accounts will be checked for validity")
 
		("registration.local_account_server_timeout", value<int>()->default_value(10000), "Timeout when checking local user on local_account_server (msecs)")
 
		("gateway_responder.prompt", value<std::string>()->default_value("Contact ID"), "Value of <prompt> </promt> field")
 
		("gateway_responder.label", value<std::string>()->default_value("Enter legacy network contact ID."), "Label for add contact ID field")
 
		("database.type", value<std::string>()->default_value("none"), "Database type.")
 
		("database.database", value<std::string>()->default_value("/var/lib/spectrum2/$jid/database.sql"), "Database used to store data")
 
		("database.server", value<std::string>()->default_value("localhost"), "Database server.")
 
		("database.user", value<std::string>()->default_value(""), "Database user.")
 
		("database.password", value<std::string>()->default_value(""), "Database Password.")
0 comments (0 inline, 0 general)