diff --git a/backends/libcommuni/session.cpp b/backends/libcommuni/session.cpp index c13297ff73cabc7df41022f85a9cc60d4b4690e3..2231eccc2807c50b85ec1182987c09dae43edb82 100644 --- a/backends/libcommuni/session.cpp +++ b/backends/libcommuni/session.cpp @@ -1,11 +1,21 @@ -/* - * Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com +/** + * XMPP - libpurple transport * - * This example is free, and not covered by LGPL license. There is no - * restriction applied to their modification, redistribution, using and so on. - * You can study them, modify them, use them in your own program - either - * completely or partially. By using it you may give me some credits in your - * program, but you don't have to. + * Copyright (C) 2013, Jan Kaluza + * + * 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 */ #include "session.h" @@ -70,7 +80,31 @@ void MyIrcSession::on_connected() { } void MyIrcSession::on_socketError(QAbstractSocket::SocketError error) { - on_disconnected(); + std::string reason; + switch(error) { + case QAbstractSocket::ConnectionRefusedError: reason = "The connection was refused by the peer (or timed out)."; break; + case QAbstractSocket::RemoteHostClosedError: reason = "The remote host closed the connection."; break; + case QAbstractSocket::HostNotFoundError: reason = "The host address was not found."; break; + case QAbstractSocket::SocketAccessError: reason = "The socket operation failed because the application lacked the required privileges."; break; + case QAbstractSocket::SocketResourceError: reason = "The local system ran out of resources."; break; + case QAbstractSocket::SocketTimeoutError: reason = "The socket operation timed out."; break; + case QAbstractSocket::DatagramTooLargeError: reason = "The datagram was larger than the operating system's limit."; break; + case QAbstractSocket::NetworkError: reason = "An error occurred with the network."; break; + case QAbstractSocket::SslHandshakeFailedError: reason = "The SSL/TLS handshake failed, so the connection was closed"; break; + case QAbstractSocket::UnknownSocketError: reason = "An unidentified error occurred."; break; + default: reason= "Unknown error."; break; + }; + + if (!suffix.empty()) { + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { + np->handleParticipantChanged(user, TO_UTF8(nickName()), it->second->getChannel() + suffix, pbnetwork::PARTICIPANT_FLAG_ROOM_NOT_FOUND, pbnetwork::STATUS_NONE, reason); + } + } + else { + np->handleDisconnected(user, 0, reason); + np->tryNextServer(); + } + m_connected = false; } void MyIrcSession::on_disconnected() { @@ -96,9 +130,8 @@ bool MyIrcSession::correctNickname(std::string &nickname) { void MyIrcSession::on_joined(IrcMessage *message) { IrcJoinMessage *m = (IrcJoinMessage *) message; - bool op = 0; std::string nickname = TO_UTF8(m->sender().name()); - op = correctNickname(nickname); + bool op = correctNickname(nickname); getIRCBuddy(TO_UTF8(m->channel().toLower()), nickname).setOp(op); np->handleParticipantChanged(user, nickname, TO_UTF8(m->channel().toLower()) + suffix, op, pbnetwork::STATUS_ONLINE); LOG4CXX_INFO(logger, user << ": " << nickname << " joined " << TO_UTF8(m->channel().toLower()) + suffix); @@ -107,9 +140,8 @@ void MyIrcSession::on_joined(IrcMessage *message) { void MyIrcSession::on_parted(IrcMessage *message) { IrcPartMessage *m = (IrcPartMessage *) message; - bool op = 0; std::string nickname = TO_UTF8(m->sender().name()); - op = correctNickname(nickname); + bool op = correctNickname(nickname); removeIRCBuddy(TO_UTF8(m->channel().toLower()), nickname); LOG4CXX_INFO(logger, user << ": " << nickname << " parted " << TO_UTF8(m->channel().toLower()) + suffix); np->handleParticipantChanged(user, nickname, TO_UTF8(m->channel().toLower()) + suffix, op, pbnetwork::STATUS_NONE, TO_UTF8(m->reason())); @@ -117,13 +149,13 @@ void MyIrcSession::on_parted(IrcMessage *message) { void MyIrcSession::on_quit(IrcMessage *message) { IrcQuitMessage *m = (IrcQuitMessage *) message; + std::string nickname = TO_UTF8(m->sender().name()); + bool op = correctNickname(nickname); + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { - bool op = 0; - std::string nickname = TO_UTF8(m->sender().name()); if (!hasIRCBuddy(it->second->getChannel(), nickname)) { continue; } - op = correctNickname(nickname); removeIRCBuddy(it->second->getChannel(), nickname); LOG4CXX_INFO(logger, user << ": " << nickname << " quit " << it->second->getChannel() + suffix); np->handleParticipantChanged(user, nickname, it->second->getChannel() + suffix, op, pbnetwork::STATUS_NONE, TO_UTF8(m->reason())); @@ -132,9 +164,10 @@ void MyIrcSession::on_quit(IrcMessage *message) { void MyIrcSession::on_nickChanged(IrcMessage *message) { IrcNickMessage *m = (IrcNickMessage *) message; + std::string nickname = TO_UTF8(m->sender().name()); + correctNickname(nickname); for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { - std::string nickname = TO_UTF8(m->sender().name()); if (!hasIRCBuddy(it->second->getChannel(), nickname)) { continue; } @@ -152,20 +185,23 @@ void MyIrcSession::on_modeChanged(IrcMessage *message) { std::string mode = TO_UTF8(m->mode()); if (nickname.empty()) return; - LOG4CXX_INFO(logger, user << ": " << nickname << " changed mode to " << mode); - for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { - if (!hasIRCBuddy(it->second->getChannel(), nickname)) { - continue; - } - IRCBuddy &buddy = getIRCBuddy(it->second->getChannel(), nickname); - if (mode == "+o") { - buddy.setOp(true); - } - else { - buddy.setOp(false); - } - np->handleParticipantChanged(user, nickname, it->second->getChannel() + suffix,(int) buddy.isOp(), pbnetwork::STATUS_ONLINE, ""); + + correctNickname(nickname); + + if (!hasIRCBuddy(TO_UTF8(m->target().toLower()), nickname)) { + return; } + IRCBuddy &buddy = getIRCBuddy(TO_UTF8(m->target().toLower()), nickname); + if (mode == "+o") { + buddy.setOp(true); + } + else { + buddy.setOp(false); + } + + np->handleParticipantChanged(user, nickname, TO_UTF8(m->target().toLower()) + suffix,(int) buddy.isOp(), pbnetwork::STATUS_ONLINE, ""); + + LOG4CXX_INFO(logger, user << ": " << nickname << " changed mode to " << mode << " in " << TO_UTF8(m->target().toLower())); } void MyIrcSession::on_topicChanged(IrcMessage *message) { @@ -203,8 +239,22 @@ void MyIrcSession::on_messageReceived(IrcMessage *message) { else { std::string nickname = TO_UTF8(m->sender().name()); correctNickname(nickname); - LOG4CXX_INFO(logger, nickname + suffix); - np->handleMessage(user, nickname + suffix, TO_UTF8(msg)); + if (m_pms.find(nickname) != m_pms.end()) { + if (hasIRCBuddy(m_pms[nickname], nickname)) { + LOG4CXX_INFO(logger, nickname); + np->handleMessage(user, m_pms[nickname] + suffix, TO_UTF8(msg), nickname, "", "", false, true); + return; + } + else { + nickname = nickname + suffix; + } + } + else { + nickname = nickname + suffix; + } + + LOG4CXX_INFO(logger, nickname); + np->handleMessage(user, nickname, TO_UTF8(msg)); } } @@ -313,8 +363,6 @@ void MyIrcSession::on_numericMessageReceived(IrcMessage *message) { if (m->code() >= 400 && m->code() < 500) { LOG4CXX_INFO(logger, user << ": Error message received: " << message->toData().data()); } - - //qDebug() << "numeric message received:" << receiver() << origin << code << params; } void MyIrcSession::awayTimeout() {