Files
@ 8d72e074c0d5
Branch filter:
Location: libtransport.git/backends/libircclient-qt/ircnetworkplugin.cpp
8d72e074c0d5
4.2 KiB
text/x-c++hdr
IRC backend can connect more servers/rooms... PM still doesn't work correctly :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include "ircnetworkplugin.h"
IRCNetworkPlugin::IRCNetworkPlugin(Config *config, Swift::QtEventLoop *loop, const std::string &host, int port) {
this->config = config;
m_socket = new QTcpSocket();
m_socket->connectToHost(QString::fromStdString(host), port);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
void IRCNetworkPlugin::readData() {
size_t availableBytes = m_socket->bytesAvailable();
if (availableBytes == 0)
return;
std::cout << "READ\n";
std::string d = std::string(m_socket->readAll().data(), availableBytes);
handleDataRead(d);
}
void IRCNetworkPlugin::sendData(const std::string &string) {
m_socket->write(string.c_str(), string.size());
}
void IRCNetworkPlugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) {
// Server is in server-mode, so user is JID of server when we want to connect
if (CONFIG_BOOL(config, "service.server_mode")) {
MyIrcSession *session = new MyIrcSession(user, this);
std::string h = user.substr(0, user.find("@"));
session->setNick(QString::fromStdString(h.substr(0, h.find("%"))));
session->connectToServer(QString::fromStdString(h.substr(h.find("%") + 1)), 6667);
std::cout << "CONNECTING IRC NETWORK " << h.substr(h.find("%") + 1) << "\n";
m_sessions[user] = session;
}
else {
handleConnected(user);
}
}
void IRCNetworkPlugin::handleLogoutRequest(const std::string &user, const std::string &legacyName) {
if (m_sessions[user] == NULL)
return;
m_sessions[user]->disconnectFromServer();
m_sessions[user]->deleteLater();
m_sessions.erase(user);
}
void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &/*xhtml*/) {
std::string u = user;
if (!CONFIG_BOOL(config, "service.server_mode")) {
u = user + legacyName.substr(legacyName.find("@") + 1);
}
if (m_sessions[u] == NULL)
return;
std::string r = legacyName;
if (!CONFIG_BOOL(config, "service.server_mode")) {
r = legacyName.substr(0, r.find("@"));
}
std::cout << "MESSAGE " << u << " " << r << "\n";
m_sessions[u]->message(QString::fromStdString(r), QString::fromStdString(message));
std::cout << "SENT\n";
}
void IRCNetworkPlugin::handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) {
std::cout << "JOIN\n";
std::string r = room;
std::string u = user;
if (!CONFIG_BOOL(config, "service.server_mode")) {
u = user + room.substr(room.find("%") + 1);
r = room.substr(0, room.find("%"));
}
if (m_sessions[u] == NULL) {
// in gateway mode we want to login this user to network according to legacyName
if (room.find("%") != std::string::npos) {
// suffix is %irc.freenode.net to let MyIrcSession return #room%irc.freenode.net
MyIrcSession *session = new MyIrcSession(user, this, room.substr(room.find("%")));
session->setNick(QString::fromStdString(nickname));
session->connectToServer(QString::fromStdString(room.substr(room.find("%") + 1)), 6667);
std::cout << "CONNECTING IRC NETWORK " << room.substr(room.find("%") + 1) << "\n";
std::cout << "SUFFIX " << room.substr(room.find("%")) << "\n";
m_sessions[u] = session;
}
else {
return;
}
}
std::cout << "JOINING " << r << "\n";
m_sessions[u]->addAutoJoinChannel(QString::fromStdString(r));
m_sessions[u]->join(QString::fromStdString(r), QString::fromStdString(password));
m_sessions[u]->rooms += 1;
// update nickname, because we have nickname per session, no nickname per room.
handleRoomNicknameChanged(user, r, m_sessions[u]->nick().toStdString());
}
void IRCNetworkPlugin::handleLeaveRoomRequest(const std::string &user, const std::string &room) {
std::string r = room;
std::string u = user;
if (!CONFIG_BOOL(config, "service.server_mode")) {
r = room.substr(0, room.find("%"));
u = user + room.substr(room.find("%") + 1);
}
if (m_sessions[u] == NULL)
return;
m_sessions[u]->part(QString::fromStdString(r));
m_sessions[u]->removeAutoJoinChannel(QString::fromStdString(r));
m_sessions[u]->rooms -= 1;
if (m_sessions[u]->rooms <= 0) {
m_sessions[u]->disconnectFromServer();
m_sessions[u]->deleteLater();
m_sessions.erase(u);
}
}
|