TTW_Bot/twitchmessage.cpp

118 lines
4.4 KiB
C++

// TwitchMessage.cpp
#include "twitchmessage.h"
#include <QRegularExpression>
#include <QRegularExpressionMatch>
TwitchMessage TwitchMessage::parse(const QString &rawMessage)
{
TwitchMessage msg;
// Basic IRC parsing
QString remaining = rawMessage.trimmed();
// Parse tags (start with @)
if (remaining.startsWith('@')) {
int spaceIndex = remaining.indexOf(' ');
if (spaceIndex != -1) {
QString tagsStr = remaining.mid(1, spaceIndex - 1);
remaining = remaining.mid(spaceIndex + 1);
// Parse individual tags
QStringList tags = tagsStr.split(';');
for (const QString &tag : tags) {
int eqIndex = tag.indexOf('=');
if (eqIndex != -1) {
QString key = tag.left(eqIndex);
QString value = tag.mid(eqIndex + 1);
if (key == "badges") {
if (!value.isEmpty()) {
QStringList badgeList = value.split(',');
for (const QString &badge : badgeList) {
QStringList parts = badge.split('/');
if (parts.size() == 2) {
TwitchMessage::Badge b;
b.name = parts[0];
b.version = parts[1].toInt();
msg.badges.append(b);
if (b.name == "vip") {
msg.isVIP = true;
}
}
}
}
} else if (key == "badge-info") {
msg.badgeInfo = value;
} else if (key == "client-nonce") {
msg.clientNonce = value;
} else if (key == "color") {
msg.color = value;
} else if (key == "display-name") {
msg.displayName = value;
} else if (key == "emotes") {
msg.emotes = value;
} else if (key == "first-msg") {
msg.firstMsg = (value == "1");
} else if (key == "flags") {
msg.flags = value;
} else if (key == "id") {
msg.messageId = value;
} else if (key == "mod") {
msg.isMod = (value == "1");
} else if (key == "vip") {
msg.isVIP = (value == "1");
} else if (key == "returning-chatter") {
msg.returningChatter = (value == "1");
} else if (key == "room-id") {
msg.roomId = value.toLongLong();
} else if (key == "subscriber") {
msg.isSubscriber = (value == "1");
} else if (key == "tmi-sent-ts") {
msg.tmiSentTimestamp = value.toLongLong();
} else if (key == "turbo") {
msg.isTurbo = (value == "1");
} else if (key == "user-id") {
msg.userId = value.toLongLong();
} else if (key == "user-type") {
msg.userType = value;
}
}
}
}
}
// Parse prefix (starts with : after tags)
if (remaining.startsWith(':')) {
int spaceIndex = remaining.indexOf(' ');
if (spaceIndex != -1) {
msg.ircPrefix = remaining.mid(1, spaceIndex - 1);
remaining = remaining.mid(spaceIndex + 1);
}
}
// Parse command
int spaceIndex = remaining.indexOf(' ');
if (spaceIndex != -1) {
msg.ircCommand = remaining.left(spaceIndex);
remaining = remaining.mid(spaceIndex + 1);
}
// Parse channel (starts with #)
if (remaining.startsWith('#')) {
spaceIndex = remaining.indexOf(' ');
if (spaceIndex != -1) {
msg.channel = remaining.left(spaceIndex);
remaining = remaining.mid(spaceIndex + 1);
}
}
// Parse message (starts with :)
if (remaining.startsWith(':')) {
msg.message = remaining.mid(1);
}
return msg;
}