-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathircserver.cpp
More file actions
101 lines (88 loc) · 2.9 KB
/
ircserver.cpp
File metadata and controls
101 lines (88 loc) · 2.9 KB
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
/*
* IRCBot - An IRC relay for the Yogstation Discord.
* Copyright (C) 2016 Yogstation13
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ircserver.hpp"
IrcServer::IrcServer(QStringList* banlist, QString* password, QObject* parent)
: QTcpServer(parent), _banlist(banlist), _password(password)
{
connect(this, &IrcServer::newConnection, this, &IrcServer::newConnectionReceived);
qDebug()<<this<<"constructed";
}
IrcServer::~IrcServer()
{
close();
}
bool IrcServer::listen(const QHostAddress& address, quint16 port)
{
qDebug()<<"Starting server on"<<address<<":"<<port;
bool result = QTcpServer::listen(address, port);
if(!result)
return result;
return result;
}
void IrcServer::close()
{
for(int i = 0; i < _clients.length(); i++)
_clients[i]->deleteLater();
_clients.clear();
QTcpServer::close();
}
void IrcServer::broadcastMessage(QString message)
{
for(int i = 0; i < _clients.length(); i++)
_clients[i]->sendMessage(message);
}
void IrcServer::removeClient(IrcClient* client)
{
_clients.removeAll(client);
}
void IrcServer::handleClientConnect()
{
IrcClient* client = static_cast<IrcClient*>(sender());
if(!client)
return;
emit clientConnected(client);
}
void IrcServer::handleClientDisconnect()
{
IrcClient* client = static_cast<IrcClient*>(sender());
if(!client)
return;
emit clientDisconnected(client);
}
void IrcServer::handleClientMessageReceived(QString message)
{
IrcClient* client = static_cast<IrcClient*>(sender());
if(!client)
return;
for(int i = 0; i < _clients.length(); i++)
if(_clients[i] != client)
_clients[i]->sendMessage(QString(":"+client->nickname()+"!"+client->username()+"@" + _hostname + " PRIVMSG " + _publicChannel + " :"+message+"\r\n"));
emit clientMessageReceived(client, message);
}
void IrcServer::newConnectionReceived()
{
IrcClient* client = new IrcClient(nextPendingConnection(), _banlist, _password, &_clients, _hostname, _motd, _serverName, _publicChannel, this);
connectClientSignals(client);
_clients.append(client);
}
void IrcServer::connectClientSignals(IrcClient* client)
{
connect(client, &IrcClient::connected, this, &IrcServer::handleClientConnect);
connect(client, &IrcClient::disconnected, this, &IrcServer::handleClientDisconnect);
connect(client, &IrcClient::messageReceived, this, &IrcServer::handleClientMessageReceived);
}