-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathircbot.cpp
More file actions
301 lines (281 loc) · 8.52 KB
/
ircbot.cpp
File metadata and controls
301 lines (281 loc) · 8.52 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* 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 "ircbot.hpp"
const QString IrcBot::_configPath = "config.json";
const QString IrcBot::_banlistPath = "banlist.rx";
IrcBot::IrcBot(QObject* parent) : QObject(parent), _server(&_banlist, &_password)
{
loadConfig();
loadBanlist();
connect(&_server, &IrcServer::clientConnected, this, &IrcBot::ircClientConnected);
connect(&_server, &IrcServer::clientDisconnected, this, &IrcBot::ircClientDisconnected);
connect(&_server, &IrcServer::clientMessageReceived, this, &IrcBot::ircClientMessageReceived);
_port = 6667;
_publicChannel = nullptr;
discordLogin();
qDebug()<<this<<"constructed.";
}
void IrcBot::loadConfig()
{
qDebug()<<"Loading configuration.";
QFile configFile(this);
configFile.setFileName(_configPath);
if(!configFile.exists())
{
qDebug()<<"No configuration file exists. Creating"<<_configPath<<"with default values.";
createDefaultConfig();
exit(1);
}
if(!configFile.open(QFile::ReadOnly))
{
qDebug()<<"Could not open configuration file"<<_configPath<<"for reading.";
qDebug()<<"Reason:"<<configFile.errorString();
exit(1);
}
if(configFile.bytesAvailable() > 5242880)
{
qDebug()<<"Configuration file"<<_configPath<<"larger than 5 MB, overwriting with default values to avoid overload.";
configFile.close();
createDefaultConfig();
exit(1);
}
QJsonDocument document = QJsonDocument::fromJson(configFile.readAll());
configFile.close();
setCurrentConfigToObject(document.object());
}
void IrcBot::loadBanlist()
{
qDebug()<<"Loading ban list.";
QFile banFile(this);
banFile.setFileName(_banlistPath);
if(!banFile.exists())
{
qDebug()<<"Banfile"<<_banlistPath<<"does not exist, creating empty list.";
if(!banFile.open(QFile::WriteOnly|QFile::Truncate))
{
qDebug()<<"Could not open banfile"<<_banlistPath<<"for writing.";
qDebug()<<"Reason:"<<banFile.errorString();
}
else banFile.close();
}
_banlist.clear();
if(!banFile.open(QFile::ReadOnly))
{
qDebug()<<"Could not open banfile"<<_banlistPath<<"for reading.";
qDebug()<<"Reason:"<<banFile.errorString();
}
else
{
while(!banFile.atEnd())
{
QString line = banFile.readLine().trimmed();
if(!_banlist.contains(line))
_banlist.append(line);
}
banFile.close();
}
}
void IrcBot::discordLogin()
{
connect(&_discord, &QDiscord::loginSuccess, this, &IrcBot::discordLoginFinished);
connect(&_discord, &QDiscord::loginFailed, this, &IrcBot::discordLoginFailed);
connect(_discord.state(), &QDiscordStateComponent::guildAvailable, this, &IrcBot::discordGuildAvailable);
connect(_discord.state(), &QDiscordStateComponent::messageCreated, this, &IrcBot::discordMessageCreated);
_discord.login(_loginToken);
}
void IrcBot::discordLoginFinished()
{
qDebug()<<"Starting server.";
_server.setHostname(_hostname);
_server.setMotd(_motd);
_server.setServerName(_serverName);
_server.listen(QHostAddress::AnyIPv4, _port);
}
void IrcBot::discordLoginFailed()
{
qDebug()<<"Discord login failed.";
exit(1);
}
void IrcBot::discordGuildAvailable(QDiscordGuild* guild)
{
if(guild)
if(guild->id() == _guildId)
for(int i = 0; i < guild->channels().keys().length(); i++)
if(guild->channels().values()[i]->id() == _publicChannelId)
{
_publicChannel = guild->channels().values()[i];
_server.setPublicChannel("#" + makeValidNick(_publicChannel->name()));
qDebug()<<"Found public channel with name:"<<_publicChannel->name();
break;
}
}
void IrcBot::discordMessageCreated(QDiscordMessage message)
{
if(message.channel()->id() == _publicChannelId && message.author()->id() != _discord.state()->self()->id())
_server.broadcastMessage(":"+makeValidNick(message.author()->username())+"_"+message.author()->discriminator()+
" PRIVMSG #"+makeValidNick(_publicChannel->name())+" :"+message.content()+"\r\n");
}
void IrcBot::addBan(const QString& ban)
{
if(_banlist.contains(ban.trimmed()))
{
qDebug()<<"Ban already in list, ignoring.";
return;
}
qDebug()<<"Adding ban.";
QFile banFile(this);
banFile.setFileName(_banlistPath);
if(!banFile.open(QFile::WriteOnly|QFile::Append))
{
qDebug()<<"Could not open banfile"<<_banlistPath<<"for writing. Adding ban without updating file.";
qDebug()<<"Reason:"<<banFile.errorString();
}
else
{
banFile.write(ban.trimmed().append('\n').toUtf8());
banFile.flush();
banFile.close();
}
_banlist.append(ban.trimmed());
}
void IrcBot::removeBan(const QString& ban)
{
if(!_banlist.contains(ban.trimmed()))
{
qDebug()<<"Ban already not in list, ignoring.";
return;
}
qDebug()<<"Removing ban.";
_banlist.removeAll(ban.trimmed());
QFile banFile(this);
banFile.setFileName(_banlistPath);
if(!banFile.open(QFile::WriteOnly|QFile::Truncate))
{
qDebug()<<"Could not open banfile"<<_banlistPath<<"for writing. Removing ban without updating file.";
qDebug()<<"Reason:"<<banFile.errorString();
}
else
{
for(int i = 0; i < _banlist.length(); i++)
banFile.write(QString(_banlist[i] + '\n').toUtf8());
banFile.flush();
banFile.close();
}
}
void IrcBot::createDefaultConfig()
{
QJsonDocument document;
document.setObject(currentConfigToObject());
QFile configFile(this);
configFile.setFileName(_configPath);
if(!configFile.open(QFile::WriteOnly|QFile::Truncate))
{
qDebug()<<"Could not open configuration file"<<_configPath<<"for writing.";
qDebug()<<"Reason:"<<configFile.errorString();
exit(1);
}
configFile.write(document.toJson(QJsonDocument::Indented));
configFile.flush();
configFile.close();
}
QJsonObject IrcBot::currentConfigToObject()
{
QJsonObject object;
object["Port"] = _port;
object["Login token"] = _loginToken;
object["Public channel ID"] = _publicChannelId;
object["Guild ID"] = _guildId;
object["Password"] = _password;
object["Announce connections"] = _announceConnections;
object["Hostname"] = _hostname;
object["Server name"] = _serverName;
object["MOTD"] = _motd;
return object;
}
void IrcBot::setCurrentConfigToObject(const QJsonObject& object)
{
_port = object["Port"].toInt(6667);
_loginToken = object["Login token"].toString("");
_publicChannelId = object["Public channel ID"].toString("");
_guildId = object["Guild ID"].toString("");
_password = object["Password"].toString("");
_announceConnections = object["Announce connections"].toBool(false);
_hostname = object["Hostname"].toString("");
_serverName = object["Server name"].toString("");
_motd = object["MOTD"].toString("");
}
void IrcBot::ircClientConnected(IrcClient* client)
{
if(!client)
return;
if(client->username() == "" || client->nickname() == "" || client->state() != IrcClient::ClientState::Connected)
return;
if(_announceConnections)
_discord.rest()->sendMessage
(
QString("**" + client->nickname() + " (" + client->username() + ")** " + "has connected."),
_publicChannel
);
}
void IrcBot::ircClientDisconnected(IrcClient* client)
{
_server.removeClient(client);
if(!client)
{
client->deleteLater();
return;
}
if(client->username() == "" || client->nickname() == "" || client->state() != IrcClient::ClientState::Connected)
{
client->deleteLater();
return;
}
if(_announceConnections)
_discord.rest()->sendMessage
(
QString("**" + client->nickname() + " (" + client->username() + ")** " + "has disconnected."),
_publicChannel
);
client->deleteLater();
}
void IrcBot::ircClientMessageReceived(IrcClient* client, QString message)
{
_discord.rest()->sendMessage(QString("**"+client->nickname()+" ("+client->username()+")**: "+message), _publicChannel);
}
QString IrcBot::makeValidNick(QString nick)
{
if(nick.length() < 3)
return "___";
if(nick[0] == '#')
nick[0] = '_';
for(int i = 0; i < nick.length(); i++)
nick[i] = makeValidChar(nick[i]);
return nick;
}
QChar IrcBot::makeValidChar(QChar c)
{
if ('a' <= c && c <= 'z')
return c;
if ('A' <= c && c <= 'Z')
return c;
if ('0' <= c && c <= '9')
return c;
if (c == '-' || c == '_' || c == '*' || c == '&' || c == '#')
return c;
return '_';
}