-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitchconnector.cpp
More file actions
524 lines (438 loc) · 16.7 KB
/
twitchconnector.cpp
File metadata and controls
524 lines (438 loc) · 16.7 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/****************************************************************************
* Copyright (C) 2018 by Sebastian Ziganki *
* *
* This file is part of Lpz3ncLittleHelper. *
* *
* Lpz3ncLittleHelper is free software: you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License. *
* *
* Lpz3ncLittleHelper 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with Box. If not, see http://www.gnu.org/licenses . *
****************************************************************************/
/**
* /file twitchconnector.cpp
* /author Hackspider
* /brief Source file of the twitch connector
*/
/* Necessary dependencies to Qt framework */
#include <QTextStream>
#include <QRegularExpression>
/* Necessary internal dependencies */
#include "twitchconnector.h"
/**
* /brief TwitchConnector definition
*/
TwitchConnector::TwitchConnector(
QString connectURL,
quint16 port,
QString loginName,
QString loginPass,
QStandardItemModel* channelModel,
QLabel* connectionIcon,
QPushButton* connectButton,
QObject *parent)
: QObject(parent),
mSocket(nullptr),
mConnectURL(connectURL),
mPort(port),
mCyclicTimer(nullptr),
mState(DISCONNECTED),
mLoginName(loginName),
mLoginPass(loginPass),
mChannelModel(channelModel),
mConnectionIcon(connectionIcon),
mConnectionButton(connectButton)
{
/* Make sure no channel is tracked */
mConnectedChannels.clear();
/* Create a new tcp socket */
mSocket = new QTcpSocket();
/* Create a new cyclic timer */
mCyclicTimer = new QTimer();
/* Set intervall to 3 minutes for PING/PONG */
mCyclicTimer->setInterval(180000);
/* Connect the cyclic timer */
connect(mCyclicTimer, SIGNAL(timeout()), this, SLOT(pingTimer()));
/* Connect the tcp socket signals to corresponding methods */
connect(mSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(mSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
connect(mSocket, SIGNAL(readyRead()), this, SLOT(readyRead()));
/* Connect the channel model signals to corresponding methods */
connect(mChannelModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(channelModelItemChanged(QStandardItem*)));
connect(mChannelModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(channelRowsChanged(QModelIndex,int,int)));
connect(mChannelModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(channelRowsChanged(QModelIndex,int,int)));
/* Set the ui elements to disabled */
disconnectUI();
}
/**
* /brief ~TwitchConnector definition
*/
TwitchConnector::~TwitchConnector()
{
/* Check for null pointer and delete elements */
if (mSocket != nullptr)
{
mSocket->disconnectFromHost();
delete mSocket;
}
if (mCyclicTimer != nullptr)
{
delete mCyclicTimer;
}
}
/**
* /brief connected definition
*/
void TwitchConnector::connected()
{
/* Create pass and nick strings */
QString pass = "PASS " + mLoginPass + "\r\n";
QString nick = "NICK " + mLoginName + "\r\n";
/* Write pass and nick to tcp socket */
mSocket->write(pass.toLocal8Bit());
mSocket->write(nick.toLocal8Bit());
}
/**
* /brief disconnected definition
*/
void TwitchConnector::disconnected()
{
/* Set the ui elements to disabled */
disconnectUI();
}
/**
* /brief error definition
*/
void TwitchConnector::error(QAbstractSocket::SocketError error)
{
/* In case of error disconnect from host */
mSocket->disconnectFromHost();
}
/**
* /brief readyRead definition
*/
void TwitchConnector::readyRead()
{
/* Read all available data into a text stream */
QTextStream textStream(mSocket->readAll().data());
/* Iterate over all lines received */
while (!textStream.atEnd())
{
/* Read a single line from received data */
QString line = textStream.readLine();
/* Parse successful connection message using regex */
QRegularExpression successConnectMessage("(:tmi\\.twitch\\.tv)\\s([0-9]{3,3})\\s(.+?)\\s:(Welcome,\\sGLHF!)");
QRegularExpressionMatch successConnectMessageMatch = successConnectMessage.match(line);
/* If the regex matches */
if (successConnectMessageMatch.hasMatch())
{
/* Set the ui elements to connected */
connectUI();
/* Start the ping/pong timer */
mCyclicTimer->start();
/* Iterate over all channels */
for (int i=0; i<mChannelModel->rowCount(); i++)
{
/* If the channel shall be connected */
if (Qt::Checked == mChannelModel->item(i)->checkState() )
{
/* Create a join string */
QString join = "JOIN #" + mChannelModel->item(i)->text() + "\r\n";
/* Write join string to tcp socket */
mSocket->write(join.toLocal8Bit());
}
}
}
/* Parse oauth failed message using regex */
QRegularExpression oauthFailedMessage("(:tmi.twitch.tv)\\s(NOTICE)\\s(\\*)\\s(:Improperly formatted auth)");
QRegularExpressionMatch oauthFailedMessageMatch = oauthFailedMessage.match(line);
/* Parse connect failed message using regex */
QRegularExpression connectFailedMessage("(:tmi.twitch.tv)\\s(NOTICE)\\s(\\*)\\s(:Login authentication failed)");
QRegularExpressionMatch connectFailedMessageMatch = connectFailedMessage.match(line);
/* If one of the regex matches */
if ( oauthFailedMessageMatch.hasMatch() ||
connectFailedMessageMatch.hasMatch() )
{
/* Set the ui elements to connected */
disconnectUI();
/* Stop the cyclic ping/pong timer */
mCyclicTimer->stop();
}
/* Parse channel connected message using regex */
QRegularExpression channelConnected(":(.+)(.tmi.twitch.tv)\\s([0-9]{3,3})\\s(.+)\\s=\\s\\#(.+)\\s:(.+)");
QRegularExpressionMatch channelConnectedMatch = channelConnected.match(line);
/* If the regex matches */
if ( channelConnectedMatch.hasMatch() )
{
/* Local channel name variable */
QString channelName;
/* The captured groups must be exactly 6 */
if ( channelConnectedMatch.lastCapturedIndex() == 6 )
{
/* Channel name equals group nr. 5 */
channelName = channelConnectedMatch.captured(5);
/* Iterate over all channel items */
for (int i=0; i<mChannelModel->rowCount(); i++)
{
/* If channel name matches */
if ( 0 == mChannelModel->item(i)->text().compare(channelName) )
{
/* Set the channel icon to OK */
mChannelModel->item(i)->setIcon(QIcon(":/icons/images/OK.png"));
/* Keep track of the connected channels */
mConnectedChannels.insert(channelName);
}
}
}
}
/* Parse channel disconnect message using regex */
QRegularExpression channelDisconnected(":(.+)\\!(.+)\\@(.+)\\.tmi.twitch.tv\\sPART\\s\\#(.+)");
QRegularExpressionMatch channelDisconnectedMatch = channelDisconnected.match(line);
/* If the regex matches */
if (channelDisconnectedMatch.hasMatch())
{
/* Local channel name variable */
QString channelName;
/* The captured groups must be exactly 4 */
if ( channelDisconnectedMatch.lastCapturedIndex() == 4 )
{
/* Channel name equals group nr. 4 */
channelName = channelDisconnectedMatch.captured(4);
/* Iterate over all channel items */
for (int i=0; i<mChannelModel->rowCount(); i++)
{
/* If channel name matches */
if ( 0 == mChannelModel->item(i)->text().compare(channelName) )
{
/* Set the channel icon to NOK */
mChannelModel->item(i)->setIcon(QIcon(":/icons/images/NOK.png"));
/* Keep track of the connected channels */
mConnectedChannels.remove(channelName);
}
}
}
}
}
}
/**
* /brief pingTimer definition
*/
void TwitchConnector::pingTimer()
{
/* Check if the socket is available and the twitch service is connected */
if ( mState == CONNECTED &&
mSocket != nullptr )
{
/* Create ping string */
QString ping = "PING\r\n";
/* write ping string to tcp socket */
mSocket->write(ping.toLocal8Bit());
}
}
/**
* /brief channelModelItemChanged definition
*/
void TwitchConnector::channelModelItemChanged(QStandardItem *item)
{
/* Delegate to sub function */
updateChannels();
}
/**
* /brief channelRowsChanged definition
*/
void TwitchConnector::channelRowsChanged(const QModelIndex &parent, int start, int end)
{
/* Delegate to sub function */
updateChannels();
}
/**
* /brief updateChannels definition
*/
void TwitchConnector::updateChannels()
{
/* Check if socket is available and if twitch service is connected */
if (mSocket == nullptr || mState != CONNECTED)
{
return;
}
/* Iterate over all channels */
for (int i=0; i<mChannelModel->rowCount(); i++)
{
/* Get a single item from model */
QStandardItem* item = mChannelModel->item(i);
/* Get the channel name from item */
QString channel = item->text();
/* If the channel is connected and the check box is unchecked */
if ( mConnectedChannels.contains(channel) &&
item->checkState() == Qt::Unchecked )
{
/* Create disconnect string */
QString disconnectString = "PART #" + channel + "\r\n";
/* Write disconnect string to tcp socket */
mSocket->write(disconnectString.toLocal8Bit());
}
/* If the channel is disconnected and the check box is checked */
else if ( !mConnectedChannels.contains(channel) &&
item->checkState() == Qt::Checked )
{
/* Create connect string */
QString connectString = "JOIN #" + channel + "\r\n";
/* Write connect string to tcp socket */
mSocket->write(connectString.toLocal8Bit());
}
}
}
/**
* /brief connectingUI definition
*/
void TwitchConnector::connectingUI()
{
/* Set internal state to connecting */
mState = CONNECTING;
/* Set connection icon to connecting */
mConnectionIcon->setPixmap(QIcon(":/icons/images/refresh.png").pixmap(16,16));
/* Disable the connection button while connecting */
mConnectionButton->setEnabled(false);
/* Change the connect button text to connecting */
mConnectionButton->setText("Connecting");
}
/**
* /brief disconnectUI definition
*/
void TwitchConnector::disconnectUI()
{
/* Set internal state to disconnected */
mState = DISCONNECTED;
/* Remove all channels */
mConnectedChannels.clear();
/* Stop the ping/pong cyclic timer */
mCyclicTimer->stop();
/* Set connection icon to disconnected */
mConnectionIcon->setPixmap(QIcon(":/icons/images/NOK.png").pixmap(32,32));
/* Enable the connection button */
mConnectionButton->setEnabled(true);
/* Change the connect button text to connect */
mConnectionButton->setText("Connect");
/* Iterate over all channel items */
for (int i=0; i<mChannelModel->rowCount(); i++)
{
/* Set all channel icons to disconnected */
mChannelModel->item(i)->setIcon(QIcon(":/icons/images/NOK.png"));
}
}
/**
* /brief connectUI definition
*/
void TwitchConnector::connectUI()
{
/* Set internal state to connected */
mState = CONNECTED;
/* Remove all channels */
mConnectedChannels.clear();
/* Set connection icon to connected */
mConnectionIcon->setPixmap(QIcon(":/icons/images/OK.png").pixmap(32,32));
/* Enable the connection button */
mConnectionButton->setEnabled(true);
/* Change the connect button text to disconnect */
mConnectionButton->setText("Disconnect");
}
/**
* /brief banUser definition
*/
QSet<QString> TwitchConnector::banUser(QString userName)
{
/* Check if the socket and twitch service is available */
if (mSocket == nullptr || mState != CONNECTED)
{
return QSet<QString>();
}
/* Local variable to gather all channels where the user is banned */
QSet<QString> bannedChannels;
/* Iterate over all connected channels */
foreach (const QString& channel, mConnectedChannels)
{
/* Create ban command */
QString banCommand = "PRIVMSG #"+ channel + " :.ban " + userName +"\r\n";
/* Write ban command to tcp socket */
mSocket->write(banCommand.toUtf8());
/* Add the channel to keep track in which channels the user is banned */
bannedChannels.insert(channel);
}
/* Return set of channels where the user actually has been banned */
return bannedChannels;
}
/**
* /brief unbanUser definition
*/
QSet<QString> TwitchConnector::unbanUser(QString userName)
{
/* Check if the socket and twitch service is available */
if (mSocket == nullptr || mState != CONNECTED)
{
return QSet<QString>();
}
/* Local variable to gather all channels where the user is unbanned */
QSet<QString> unbannedChannels;
/* Iterate over all connected channels */
foreach (const QString& channel, mConnectedChannels)
{
/* Create unban command */
QString unbanCommand = "PRIVMSG #"+ channel + " :.unban " + userName + "\r\n";
/* Write unban command to tcp socket */
mSocket->write(unbanCommand.toUtf8());
/* Add the channel to keep track in which channels the user is unbanned */
unbannedChannels.insert(channel);
}
/* Return set of channels where the user actually has been unbanned */
return unbannedChannels;
}
/**
* /brief Connect definition
*/
void TwitchConnector::Connect()
{
/* Set the ui elements to connecting */
connectingUI();
/* Connect to twitch service*/
mSocket->connectToHost( mConnectURL, mPort );
}
/**
* /brief Disconnect definition
*/
void TwitchConnector::Disconnect()
{
/* Disconnect from twitch service */
mSocket->disconnectFromHost();
}
/**
* /brief GetConnectionState definition
*/
ConnectionState TwitchConnector::GetConnectionState()
{
/* Return the internal twitch service connection state */
return mState;
}
/**
* /brief SetLoginName definition
*/
void TwitchConnector::SetLoginName(QString loginName)
{
/* Save the new login name internally */
mLoginName = loginName;
}
/**
* /brief SetOauth2 definition
*/
void TwitchConnector::SetOauth2(QString oauth2)
{
/* Save the new login pass internally */
mLoginPass = oauth2;
}