Files @ 07ee87c60d2d
Branch filter:

Location: libtransport.git/backends/libpurple/utils.cpp

Andrey Utkin
Define 'foreach' in backends/libcommuni/session.cpp

Fixes compilation issue

[ 64%] Building CXX object backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/session.cpp.o
cd /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni && /usr/bin/x86_64-pc-linux-gnu-g++ -DBOOST_FILESYSTEM_VERSION=3 -DDEBUG -DIRC_SHARED -DPURPLE_RUNTIME=0 -DQT_CORE_LIB -DQT_NETWORK_LIB -DQT_NO_KEYWORDS -DSPECTRUM_VERSION=\"2.0.5-git-b15fcedf\" -DSUPPORT_LEGACY_CAPS -DWITH_LIBEVENT -DWITH_LOG4CXX -DWITH_PQXX -DWITH_PROTOBUF -isystem /usr/include/qt4 -isystem /usr/include/qt4/QtCore -I/usr/include/libpurple -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -isystem /usr/include/qt4/QtNetwork -I/usr/include/qt4/Communi/IrcCore -I/usr/include/qt4/Communi/IrcUtil -I/usr/include/qt4/Communi/IrcModel -I/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include -isystem /usr/share/qt4/mkspecs/default -DBOOST_SIGNALS_NO_DEPRECATION_WARNING -O2 -pipe -O0 -ggdb -o CMakeFiles/spectrum2_libcommuni_backend.dir/session.cpp.o -c /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::on_socketError(QAbstractSocket::SocketError)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:22: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:23: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:23: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:55: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:55: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::on_namesMessageReceived(IrcMessage*)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:19: error: expected primary-expression before ‘*’ token
foreach (IrcUser *user, userModel->users()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:20: error: ‘user’ was not declared in this scope
foreach (IrcUser *user, userModel->users()) {
^~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:44: error: ‘foreach’ was not declared in this scope
foreach (IrcUser *user, userModel->users()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:44: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::sendMessageToFrontend(const string&, const string&, const string&)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:23: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:24: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:24: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:56: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:56: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::on_numericMessageReceived(IrcMessage*)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:23: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:24: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:24: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:56: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:56: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:490:23: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::awayTimeout()’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:21: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:22: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:22: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:54: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:54: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
make[2]: *** [backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/build.make:126: backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/session.cpp.o] Error 1
make[2]: Leaving directory '/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999'
make[1]: *** [CMakeFiles/Makefile2:646: backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/all] Error 2
make[1]: Leaving directory '/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999'
make: *** [Makefile:152: all] Error 2
/**
 * libtransport -- C++ library for easy XMPP Transports development
 *
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

// 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 "utils.h"

#include "glib.h"
#include "purple.h"
#include <algorithm>
#include <iostream>
#include <vector>

#include "errno.h"
#include <string.h>

#ifndef WIN32 
#include "sys/wait.h"
#include "sys/signal.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#else 
#include <process.h>
#define getpid _getpid 
#define ssize_t SSIZE_T
#include "win32/win32dep.h"
#define close closesocket
#endif

#include "purple_defs.h"

#include <boost/numeric/conversion/cast.hpp>

using std::vector;

static GHashTable *ui_info = NULL;

void execute_purple_plugin_action(PurpleConnection *gc, const std::string &name) {
	PurplePlugin *plugin = gc && PURPLE_CONNECTION_IS_CONNECTED_WRAPPED(gc) ? gc->prpl : NULL;
	if (plugin && PURPLE_PLUGIN_HAS_ACTIONS(plugin)) {
		PurplePluginAction *action = NULL;
		GList *actions, *l;

		actions = PURPLE_PLUGIN_ACTIONS(plugin, gc);

		for (l = actions; l != NULL; l = l->next) {
			if (l->data) {
				action = (PurplePluginAction *) l->data;
				action->plugin = plugin;
				action->context = gc;
				if ((std::string) action->label == name) {
					action->callback(action);
				}
				purple_plugin_action_free_wrapped(action);
			}
		}
	}
}

GHashTable *spectrum_ui_get_info(void)
{
	if(NULL == ui_info) {
		ui_info = g_hash_table_new(g_str_hash, g_str_equal);

		g_hash_table_insert(ui_info, g_strdup("name"), g_strdup("Spectrum"));
		g_hash_table_insert(ui_info, g_strdup("version"), g_strdup("0.5"));
		g_hash_table_insert(ui_info, g_strdup("website"), g_strdup("http://spectrum.im"));
		g_hash_table_insert(ui_info, g_strdup("dev_website"), g_strdup("http://spectrum.im"));
		g_hash_table_insert(ui_info, g_strdup("client_type"), g_strdup("pc"));

		/*
		 * This is the client key for "Pidgin."  It is owned by the AIM
		 * account "markdoliner."  Please don't use this key for other
		 * applications.  You can either not specify a client key, in
		 * which case the default "libpurple" key will be used, or you
		 * can register for your own client key at
		 * http://developer.aim.com/manageKeys.jsp
		 */
		g_hash_table_insert(ui_info, g_strdup("prpl-aim-clientkey"), g_strdup("ma1cSASNCKFtrdv9"));
		g_hash_table_insert(ui_info, g_strdup("prpl-icq-clientkey"), g_strdup("ma1cSASNCKFtrdv9"));

		/*
		 * This is the distid for Pidgin, given to us by AOL.  Please
		 * don't use this for other applications.  You can just not
		 * specify a distid and libpurple will use a default.
		 */
		g_hash_table_insert(ui_info, g_strdup("prpl-aim-distid"), GINT_TO_POINTER(1550));
		g_hash_table_insert(ui_info, g_strdup("prpl-icq-distid"), GINT_TO_POINTER(1550));
	}

	return ui_info;
}

#ifndef WIN32
void spectrum_sigchld_handler(int sig)
{
	int status;
	pid_t pid;

	do {
		pid = waitpid(-1, &status, WNOHANG);
	} while (pid != 0 && pid != (pid_t)-1);

	if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
		char errmsg[BUFSIZ];
		snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
		perror(errmsg);
	}
}
#endif

int create_socket(const char *host, int portno) {
	struct sockaddr_in stSockAddr;
	int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (-1 == SocketFD) {
		return 0;
	}

	hostent *hos;
	if ((hos = gethostbyname(host)) == NULL) {
		// strerror() will not work for gethostbyname() and hstrerror() 
		// is supposedly obsolete
		return 0;
	}

	memset(&stSockAddr, 0, sizeof(stSockAddr));

	stSockAddr.sin_family = AF_INET;
	stSockAddr.sin_port = htons(portno);
	memcpy(&(stSockAddr.sin_addr.s_addr), hos->h_addr,  hos->h_length);

	if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
		close(SocketFD);
		return 0;
	}

	return SocketFD;
}

#ifdef _WIN32
std::wstring utf8ToUtf16(const std::string& str)
{
	try
	{
		if (str.empty())
			return L"";

		// First request the size of the required UTF-16 buffer
		int numRequiredBytes = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast<int>(str.size()), NULL, 0);
		if (!numRequiredBytes)
			return L"";

		// Allocate memory for the UTF-16 string
		std::vector<wchar_t> utf16Str(numRequiredBytes);

		int numConverted = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), boost::numeric_cast<int>(str.size()), &utf16Str[0], numRequiredBytes);
		if (!numConverted)
			return L"";

		std::wstring wstr(&utf16Str[0], numConverted);
		return wstr;
	}
	catch (...)
	{
		// I don't believe libtransport is exception-safe so we'll just return an empty string instead
		return L"";
	}
}
#endif // _WIN32