Files
@ 4c334e9f1c0b
Branch filter:
Location: libtransport.git/backends/libcommuni/ircnetworkplugin.cpp
4c334e9f1c0b
4.6 KiB
text/x-c++hdr
Move random port generating to util.cpp
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #include "ircnetworkplugin.h"
#include <IrcCommand>
#include <IrcMessage>
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->setNickName(QString::fromStdString(h.substr(0, h.find("%"))));
session->setHost(QString::fromStdString(h.substr(h.find("%") + 1)));
session->setPort(6667);
session->open();
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]->close();
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;
std::cout << "AAAAA " << legacyName << "\n";
if (!CONFIG_BOOL(config, "service.server_mode")) {
u = user + legacyName.substr(legacyName.find("@") + 1);
if (u.find("/") != std::string::npos) {
u = u.substr(0, u.find("/"));
}
}
if (m_sessions[u] == NULL) {
std::cout << "No session for " << u << "\n";
return;
}
std::string r = legacyName;
if (!CONFIG_BOOL(config, "service.server_mode")) {
if (legacyName.find("/") == std::string::npos) {
r = legacyName.substr(0, r.find("@"));
}
else {
r = legacyName.substr(legacyName.find("/") + 1);
}
}
std::cout << "MESSAGE " << u << " " << r << "\n";
m_sessions[u]->sendCommand(IrcCommand::createMessage(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->setNickName(QString::fromStdString(nickname));
session->setHost(QString::fromStdString(room.substr(room.find("@") + 1)));
session->setPort(6667);
session->open();
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(r);
m_sessions[u]->sendCommand(IrcCommand::createJoin(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]->nickName().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]->sendCommand(IrcCommand::createPart(QString::fromStdString(r)));
m_sessions[u]->removeAutoJoinChannel(r);
m_sessions[u]->rooms -= 1;
if (m_sessions[u]->rooms <= 0) {
m_sessions[u]->close();
m_sessions[u]->deleteLater();
m_sessions.erase(u);
}
}
|