-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathqtwitchchat.cpp
490 lines (389 loc) · 15.3 KB
/
qtwitchchat.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
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
#include <QTcpSocket>
#include <QSettings>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonArray>
#include <QTimerEvent>
#include <QApplication>
#include <QDir>
#include <QFileInfo>
#include "settingsconsts.h"
#include "qtwitchchat.h"
const QString DEFAULT_TWITCH_CHAT_HOST_NAME = "irc.twitch.tv";
const int DEFAULT_TWITCH_CHAT_PORT_NUMBER = 6667;
const QString DEFAULT_OAUTH_STRING = "oauth:enfcwtwscgdoicn80068eildnqtshu";
const QString DEFAULT_USER_NICK_NAME = "broadcasterchat";
const QString DEFAULT_TWITCH_SELF_PREFIX = "https://api.twitch.tv/kraken/chat/";
const QString DEFAULT_TWITCH_STATISTIC_PREFIX = "https://api.twitch.tv/kraken/streams/";
const int DEFAULT_TWITCH_RECONNECT_INTERVAL = 10000;
const int DEFAULT_TWITCH_STATISTIC_INTERVAL = 10000;
const QString TWITCH_USER = "TWITCH";
const QString TWITCH_SERVICE = "twitch";
QTwitchChat::QTwitchChat( QObject *parent )
: QChatService( parent )
, socket_( 0 )
, nam_( new QNetworkAccessManager( this ) )
, oauthString_()
, nickName_()
, channelName_()
, selfLink_()
, emotIconsLink_()
, badgesLink_()
, emotIcons_()
, reconnectTimerId_( -1 )
, reconnectInterval_( DEFAULT_TWITCH_RECONNECT_INTERVAL )
, statisticTimerId_( -1 )
, statisticInterval_( DEFAULT_TWITCH_STATISTIC_INTERVAL )
{
//loadSettings();
}
QTwitchChat::~QTwitchChat()
{
disconnect();
}
void QTwitchChat::connect()
{
if( channelName_ == "" )
return;
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Connecting to " + channelName_ + "...", "", this ) );
if( socket_)
{
if( socket_->state() == QAbstractSocket::ConnectedState )
return;
/*else
reconnect();*/
}
socket_ = new QTcpSocket();
QObject::connect( socket_, SIGNAL( connected() ), this, SLOT( onConnected() ) );
QObject::connect( socket_, SIGNAL( readyRead() ), this, SLOT( parseMessage() ) );
QObject::connect( socket_, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( onSocketError() ) );
//QObject::connect( socket_, SIGNAL( disconnected() ), this, SLOT( reconnect() ) );
socket_->connectToHost( DEFAULT_TWITCH_CHAT_HOST_NAME, DEFAULT_TWITCH_CHAT_PORT_NUMBER );
}
void QTwitchChat::onConnected()
{
if( !socket_ )
return;
loadSettings();
socket_->write( QString( "PASS " + oauthString_ + "\r\n" ).toStdString().c_str() );
socket_->write( QString( "NICK " + nickName_ + "\r\n" ).toStdString().c_str() );
}
void QTwitchChat::disconnect()
{
if( reconnectTimerId_ >= 0 )
{
killTimer( reconnectTimerId_ );
reconnectTimerId_ = -1;
//qDebug() << "Twitch::disconnect(): reconnectTimerId_ = " << reconnectTimerId_;
}
if( statisticTimerId_ >= 0 )
{
killTimer( statisticTimerId_ );
statisticTimerId_ = -1;
}
//if( !socket_ )
// return;
if( socket_ && socket_->state() == QAbstractSocket::ConnectedState )
{
socket_->write( "QUIT\r\n" );
socket_->flush();
socket_->disconnect();
}
safeDeleteSocket();
emit newStatistic( new QChatStatistic( TWITCH_SERVICE, "", this ) );
}
void QTwitchChat::reconnect()
{
QString oldChannelName = channelName_;
disconnect();
loadSettings();
if( channelName_ != "" && oldChannelName != "" )
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Reconnecting...", "", this ) );
connect();
}
void QTwitchChat::parseMessage()
{
if( !socket_ )
return;
if( socket_->state() == QAbstractSocket::ConnectedState )
{
QString line = socket_->readLine();
//qDebug() << line;
//поадекватнее надо быть
if( line.contains ( "376 " + DEFAULT_USER_NICK_NAME ) )
{
socket_->write( QString( "JOIN #" + channelName_ + "\r\n" ).toStdString().c_str() );
getSelf();
//emit newMessage( new QChatMessage( "qrc:/resources/twitchlogo.png", TWITCH_USER, "Connecting to " + channelName_ + "...", this ) );
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Connected to " + channelName_ + "...", "", this ) );
getStatistic();
if( statisticTimerId_ )
statisticTimerId_ = startTimer( statisticInterval_ );
}
else if( line.contains( "PING" ) )
{
qDebug() << line;
//socket_->write( "PONG tmi.twitch.tv\r\n" );
socket_->write( "PONG :tmi.twitch.tv\r\n" );
}
else
{
//TODO: parse message
if( line.contains( "PRIVMSG" ) && !line.contains( "HISTORYEND" ) && !line.contains( "USERCOLOR") )
{
QString nickName = line.mid( 1, line.indexOf( '!' ) - 1 );
QString message = line.right( line.size() - line.indexOf( ':', 1 ) - 1 );
//\r\n
message = message.left( message.size() - 2 );
//qDebug() << message;
message = QChatMessage::replaceEscapeCharecters( message );
message = insertEmotIcons( message );
//message = QChatMessage::insertLinks( message );
bool blackListUser = blackList().contains( nickName );
bool supportersListUser = supportersList().contains( nickName );
if( !isRemoveBlackListUsers() || !blackListUser )
{
if( blackListUser )
{
emit newMessage( new QChatMessage( TWITCH_SERVICE, nickName, message, "ignore", this ) );
}
else
{
if( supportersListUser )
{
emit newMessage( new QChatMessage( TWITCH_SERVICE, nickName, message, "supporter", this ) );
}
else
{
if( isContainsAliases( message ) )
{
emit newMessage( new QChatMessage( TWITCH_SERVICE, nickName, message, "alias", this ) );
}
else
{
emit newMessage( new QChatMessage( TWITCH_SERVICE, nickName, message, "", this ) );
}
}
}
}
}
}
if( socket_->canReadLine() )
{
parseMessage();
}
}
}
void QTwitchChat::safeDeleteSocket()
{
/*
delete socket_;
socket_ = 0;
*/
if( socket_ )
socket_->deleteLater();
socket_ = 0;
}
void QTwitchChat::getSelf()
{
QNetworkRequest request( QUrl( DEFAULT_TWITCH_SELF_PREFIX + channelName_ ) );
QNetworkReply *reply = nam_->get( request );
QObject::connect( reply, SIGNAL( finished() ), this, SLOT( onSelfLoaded() ) );
QObject::connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onSelfLoadError() ) );
}
void QTwitchChat::onSelfLoaded()
{
QNetworkReply *reply = qobject_cast< QNetworkReply * >( sender() );
QJsonParseError parseError;
QJsonDocument jsonDoc;
jsonDoc = QJsonDocument::fromJson( reply->readAll(), &parseError );
if( parseError.error == QJsonParseError::NoError )
{
if( jsonDoc.isObject() )
{
QJsonObject _links = ( jsonDoc.object() )[ "_links" ].toObject() ;
selfLink_ = _links[ "self" ].toString();
emotIconsLink_ = _links[ "emoticons" ].toString();
badgesLink_ = _links[ "badges" ].toString();
getEmotIcons();
}
}
reply->deleteLater();
}
void QTwitchChat::onSelfLoadError()
{
QNetworkReply *reply = qobject_cast< QNetworkReply * >( sender() );
//reconnect();
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Can not connect to " + channelName_ + "..." + reply->errorString(), "", this ) );
//reconnect();
if( reconnectTimerId_ == -1 )
reconnectTimerId_ = startTimer( reconnectInterval_ );
//qDebug() << "Twitch::onSelfLoadError(): reconnectTimerId_" << reconnectTimerId_;
reply->deleteLater();
}
void QTwitchChat::getEmotIcons()
{
QNetworkRequest request( QUrl( emotIconsLink_ + "" ) );
QNetworkReply *reply = nam_->get( request );
QObject::connect( reply, SIGNAL( finished() ), this, SLOT( onEmotIconsLoaded() ) );
QObject::connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onEmotIconsLoadError() ) );
}
void QTwitchChat::onEmotIconsLoaded()
{
QNetworkReply *reply = qobject_cast< QNetworkReply * >( sender() );
QJsonParseError parseError;
QJsonDocument jsonDoc;
jsonDoc = QJsonDocument::fromJson( reply->readAll(), &parseError );
if( parseError.error == QJsonParseError::NoError )
{
if( jsonDoc.isObject() )
{
emotIcons_.clear();
QJsonArray jsonEmotIcons = jsonDoc.object()[ "emoticons" ].toArray();
foreach( const QJsonValue &value, jsonEmotIcons )
{
QJsonObject jsonEmotIcon = value.toObject();
QChatSmile smile( jsonEmotIcon[ "regex" ].toString(),
jsonEmotIcon[ "url" ].toString(),
jsonEmotIcon[ "width" ].toInt(),
jsonEmotIcon[ "height" ].toInt() );
//emotIcons_.append( smile );
emotIcons_.insert( smile.name(), smile );
}
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Smiles ready...", "", this ) );
}
}
//own smiles code
QString smilesPath = QApplication::applicationDirPath() + "/smiles";
QStringList extList;
extList << "*.svg" << "*.png" << "*.gif" << "*.jpg";
QDir smilesDir( smilesPath );
QStringList smileFiles = smilesDir.entryList( extList, QDir::Files | QDir::NoSymLinks );
foreach( const QString& smileName, smileFiles )
{
QChatSmile smile( ":" + smileName.left( smileName.length() - 4 ) + ":",
"file:///" + smilesPath + "/" + smileName, 0, 0 );
emotIcons_.insert( smile.name(), smile );
//qDebug() << smile.name() << smile.link();
}
reply->deleteLater();
}
void QTwitchChat::onEmotIconsLoadError()
{
QNetworkReply *reply = qobject_cast< QNetworkReply * >( sender() );
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Can not load smiles..." + reply->errorString(), "", this ) );
//TODO: timer for smiles loading
//getEmotIcons();
//reconnect();
reply->deleteLater();
}
void QTwitchChat::getBadges()
{
}
void QTwitchChat::onBadgesLoaded()
{
}
void QTwitchChat::onBadgesLoadError()
{
QNetworkReply *reply = qobject_cast< QNetworkReply * >( sender() );
//reconnect();
//getBadges();
reply->deleteLater();
}
void QTwitchChat::getStatistic()
{
QNetworkRequest request( QUrl( DEFAULT_TWITCH_STATISTIC_PREFIX + channelName_ ) );
QNetworkReply *reply = nam_->get( request );
QObject::connect( reply, SIGNAL( finished() ), this, SLOT( onStatisticLoaded() ) );
QObject::connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( onStatisticLoadError() ) );
}
void QTwitchChat::onStatisticLoaded()
{
QNetworkReply *reply = qobject_cast< QNetworkReply* >( sender() );
//qDebug() << reply->readAll();
QJsonParseError parseError;
QJsonDocument jsonDoc = QJsonDocument::fromJson( reply->readAll(), &parseError );
if( parseError.error == QJsonParseError::NoError )
{
if( jsonDoc.isObject() )
{
QJsonObject jsonObj = jsonDoc.object();
QJsonObject jsonStreamObj = jsonObj[ "stream" ].toObject();
QString statistic = QString::number( jsonStreamObj[ "viewers" ].toInt() );
emit newStatistic( new QChatStatistic( TWITCH_SERVICE, statistic, this ) );
//emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, QString( "Viewers: " + statistic ), "", this ) );
}
}
reply->deleteLater();
}
void QTwitchChat::onStatisticLoadError()
{
QNetworkReply *reply = qobject_cast< QNetworkReply* >( sender() );
reply->deleteLater();
}
QString QTwitchChat::insertEmotIcons( const QString& message ) const
{
//TODO: думать
QString convertedMessage = message;
QStringList tokens = convertedMessage.split( QRegExp( "\\s" ) );
QStringList convertedTokens = tokens;
for( int i = 0; i < tokens.size(); ++i )
{
if ( !QChatMessage::isLink( tokens.at( i ) ) )//не ссылка
{
foreach( const QChatSmile &smile, emotIcons_ )
{
if( tokens.at( i ) == smile.name() )
convertedTokens[ i ].replace( smile.name(), "<img class = \"smile\" src=\"" + smile.link() + "\"></img>" );
}
}
}
for( int i = 0; i < tokens.size(); ++i )
{
convertedMessage.replace( tokens.at( i ), convertedTokens.at( i ) );
}
return convertedMessage;
}
void QTwitchChat::timerEvent( QTimerEvent *event )
{
//qDebug() << "Twitch::timerEvent()";
if( event->timerId() == statisticTimerId_ )
{
getStatistic();
}
else if( event->timerId() == reconnectTimerId_ )
{
//qDebug() << "Twitch::timerEvent(): reconnectTimerId_" << reconnectTimerId_;
reconnect();
}
}
void QTwitchChat::loadSettings()
{
QSettings settings;
oauthString_ = settings.value( "/Settings/Twitch/oauth", DEFAULT_OAUTH_STRING ).toString();
nickName_ = settings.value( "/Settings/Twitch/nickName", DEFAULT_USER_NICK_NAME ).toString();
channelName_= settings.value( "/Settings/Twitch/Channel", DEFAULT_TWITCH_CHANNEL_NAME ).toString();
setAliasesList( settings.value( TWITCH_ALIASES_SETTING_PATH, BLANK_STRING ).toString() );
setSupportersList( settings.value( TWITCH_SUPPORTERS_LIST_SETTING_PATH, BLANK_STRING ).toString() );
setBlackList( settings.value( TWITCH_BLACK_LIST_SETTING_PATH, BLANK_STRING ).toString() );
setRemoveBlackListUsers( settings.value( TWITCH_REMOVE_BLACK_LIST_USERS_SETTING_PATH, false ).toBool() );
}
void QTwitchChat::onSocketError()
{
if( isShowSystemMessages() )
emit newMessage( new QChatMessage( TWITCH_SERVICE, TWITCH_USER, "Socket Error ..." + socket_->errorString(), "", this ) );
if( reconnectTimerId_ == -1 )
reconnectTimerId_ = startTimer( reconnectInterval_ );
//qDebug() << "Twitch::onSocketError: reconnectTimerId_ = " << reconnectTimerId_;
}