2017-06-25 12:05:18 -07:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2008-2017 The Communi Project *
|
|
|
|
|
* Copyright (C) 2017 by Fae - itsthefae@gmail.com *
|
2020-11-28 10:37:24 +00:00
|
|
|
* Copyright (C) 2020 by Stephen Lyons - slysven@virginmedia.com *
|
2017-06-25 12:05:18 -07:00
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation; either version 2 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 General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "ircmessageformatter.h"
|
|
|
|
|
|
|
|
|
|
#include <IrcTextFormat>
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatMessage(IrcMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
|
|
|
|
QString formatted;
|
|
|
|
|
QString color = "#f29010";
|
|
|
|
|
|
|
|
|
|
IrcNumericMessage* nMsg;
|
|
|
|
|
|
|
|
|
|
switch (message->type()) {
|
2017-06-25 15:14:01 -07:00
|
|
|
case IrcMessage::Away:
|
|
|
|
|
formatted = formatAwayMessage(static_cast<IrcAwayMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Invite:
|
|
|
|
|
formatted = formatInviteMessage(static_cast<IrcInviteMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Join:
|
|
|
|
|
formatted = formatJoinMessage(static_cast<IrcJoinMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Mode:
|
|
|
|
|
formatted = formatModeMessage(static_cast<IrcModeMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Motd:
|
|
|
|
|
formatted = formatMotdMessage(static_cast<IrcMotdMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Names:
|
|
|
|
|
formatted = formatNamesMessage(static_cast<IrcNamesMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Nick:
|
|
|
|
|
formatted = formatNickMessage(static_cast<IrcNickMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Notice:
|
|
|
|
|
formatted = formatNoticeMessage(static_cast<IrcNoticeMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Part:
|
|
|
|
|
formatted = formatPartMessage(static_cast<IrcPartMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Pong:
|
|
|
|
|
formatted = formatPongMessage(static_cast<IrcPongMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Private:
|
|
|
|
|
formatted = formatPrivateMessage(static_cast<IrcPrivateMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Quit:
|
|
|
|
|
formatted = formatQuitMessage(static_cast<IrcQuitMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Topic:
|
|
|
|
|
formatted = formatTopicMessage(static_cast<IrcTopicMessage*>(message), isForLua);
|
|
|
|
|
color = "#3283bc";
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Whois:
|
|
|
|
|
formatted = formatWhoisMessage(static_cast<IrcWhoisMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Whowas:
|
|
|
|
|
formatted = formatWhowasMessage(static_cast<IrcWhowasMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::WhoReply:
|
|
|
|
|
formatted = formatWhoReplyMessage(static_cast<IrcWhoReplyMessage*>(message), isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Error:
|
|
|
|
|
formatted = formatErrorMessage(static_cast<IrcErrorMessage*>(message), isForLua);
|
|
|
|
|
color = "indianred";
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Unknown:
|
|
|
|
|
formatted = formatUnknownMessage(message, isForLua);
|
|
|
|
|
break;
|
|
|
|
|
case IrcMessage::Numeric:
|
|
|
|
|
nMsg = static_cast<IrcNumericMessage*>(message);
|
|
|
|
|
formatted = formatNumericMessage(nMsg, isForLua);
|
|
|
|
|
// if you change this, change formatErrorMessage too
|
|
|
|
|
if (Irc::codeToString(nMsg->code()).startsWith("ERR_")) {
|
2017-06-25 12:05:18 -07:00
|
|
|
color = "indianred";
|
2017-06-25 15:14:01 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
2017-06-25 15:14:01 -07:00
|
|
|
return formatMessage(formatted, color, isForLua);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatMessage(const QString& message, QString color, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
|
|
|
|
if (!message.isEmpty()) {
|
2017-06-25 15:14:01 -07:00
|
|
|
if (isForLua) { // lua has no need for the timestamp or HTML added here.
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 12:05:18 -07:00
|
|
|
QString formatted = QString("[%1] %2").arg(QTime::currentTime().toString(), message);
|
2017-06-25 15:14:01 -07:00
|
|
|
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message.startsWith("!")) {
|
2017-06-25 12:05:18 -07:00
|
|
|
formatted = QString("<font color='gray'>%1</font>").arg(formatted);
|
2021-02-27 17:25:16 +01:00
|
|
|
} else if (message.startsWith("*")) {
|
2017-06-25 12:05:18 -07:00
|
|
|
formatted = QString("<font color='maroon'>%1</font>").arg(formatted);
|
2021-02-27 17:25:16 +01:00
|
|
|
} else if (message.startsWith("$")) {
|
2017-06-25 12:05:18 -07:00
|
|
|
formatted = QString("<font color='#3cc46e'>%1</font>").arg(formatted);
|
2021-02-27 17:25:16 +01:00
|
|
|
} else if (message.startsWith("[")) {
|
2017-06-25 12:05:18 -07:00
|
|
|
if (color.isEmpty()) {
|
|
|
|
|
color = "#f29010";
|
|
|
|
|
}
|
|
|
|
|
formatted = QString("<font color='%2'>%1</font>").arg(formatted, color);
|
|
|
|
|
}
|
|
|
|
|
return formatted;
|
|
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatAwayMessage(IrcAwayMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(message->content());
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(message->content());
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->flags() & IrcMessage::Own) {
|
2017-06-25 15:14:01 -07:00
|
|
|
return QObject::tr("! %1").arg(content);
|
2026-07-18 18:39:57 +02:00
|
|
|
}
|
|
|
|
|
if (!message->content().isEmpty()) {
|
2017-06-25 15:14:01 -07:00
|
|
|
return QObject::tr("! %1 is away (%2)").arg(message->nick(), content);
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %1 is back").arg(message->nick());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatInviteMessage(IrcInviteMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->isReply()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! invited %1 to %2").arg(message->user(), message->channel());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
|
|
|
|
|
return QObject::tr("! %2 invited to %3").arg(message->nick(), message->channel());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatJoinMessage(IrcJoinMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->flags() & IrcMessage::Own) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! You have joined %1 as %2").arg(message->channel(), message->nick());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2025-11-25 18:10:28 +01:00
|
|
|
return QObject::tr("! %1 has joined %2").arg(message->nick(), message->channel());
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatKickMessage(IrcKickMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %1 kicked %2").arg(message->nick(), message->user());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatModeMessage(IrcModeMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2023-05-14 15:06:15 +02:00
|
|
|
const QString args = message->arguments().join(" ");
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->isReply()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %1 mode is %2 %3").arg(message->target(), message->mode(), args);
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2025-11-25 18:10:28 +01:00
|
|
|
return QObject::tr("! %1 sets mode %2 %3 %4").arg(message->nick(), message->target(), message->mode(), args);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatMotdMessage(IrcMotdMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
|
|
|
|
QString motdData;
|
2024-12-28 13:55:40 +01:00
|
|
|
for (const auto& line : message->lines()) {
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content, lineEnd;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
lineEnd = "\n";
|
|
|
|
|
content = IrcTextFormat().toPlainText(line);
|
|
|
|
|
} else {
|
|
|
|
|
lineEnd = "<br />\n";
|
|
|
|
|
content = IrcTextFormat().toHtml(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
motdData += QObject::tr("[MOTD] %1%2").arg(content, lineEnd);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
return motdData;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatNamesMessage(IrcNamesMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2017-06-25 15:14:01 -07:00
|
|
|
const QString count = QString::number(message->names().count());
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
// lua actually needs the names for parsing, since getting a names
|
|
|
|
|
// list from the UI userModel alone would be limiting to the IRC commands.
|
2023-05-14 15:06:15 +02:00
|
|
|
const QString nameList = message->names().join(" ");
|
2017-06-25 15:14:01 -07:00
|
|
|
return QObject::tr("! %1 has %2 users: %3").arg(message->channel(), count, nameList);
|
|
|
|
|
}
|
2025-11-25 18:10:28 +01:00
|
|
|
return QObject::tr("! %1 has %2 users").arg(message->channel(), count);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatNickMessage(IrcNickMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %1 has changed nick to %2").arg(message->oldNick(), message->newNick());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatNoticeMessage(IrcNoticeMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
|
|
|
|
if (message->isReply()) {
|
2020-12-03 01:34:57 +00:00
|
|
|
const QStringList params = message->content().split(" ", Qt::SkipEmptyParts);
|
2017-06-25 15:14:01 -07:00
|
|
|
const QString cmd = params.value(0);
|
|
|
|
|
if (cmd.toUpper() == "PING") {
|
|
|
|
|
const QString secs = formatSeconds(params.value(1).toInt());
|
|
|
|
|
return QObject::tr("! %1 replied in %2").arg(message->nick(), secs);
|
2026-07-18 18:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cmd.toUpper() == "TIME") {
|
2017-06-25 15:14:01 -07:00
|
|
|
const QString rest = QStringList(params.mid(1)).join(" ");
|
|
|
|
|
return QObject::tr("! %1 time is %2").arg(message->nick(), rest);
|
2026-07-18 18:39:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cmd.toUpper() == "VERSION") {
|
2017-06-25 12:05:18 -07:00
|
|
|
const QString rest = QStringList(params.mid(1)).join(" ");
|
|
|
|
|
return QObject::tr("! %1 version is %2").arg(message->nick(), rest);
|
2017-06-25 15:14:01 -07:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString pfx = message->statusPrefix();
|
2021-02-27 17:25:16 +01:00
|
|
|
if (!pfx.isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
pfx = ":" + pfx;
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
|
|
|
|
|
if (message->isPrivate()) {
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(message->content());
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(message->content());
|
|
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("[%1%2] %3").arg(message->nick(), pfx, content);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
if (isForLua) {
|
|
|
|
|
// lua only needs the message text.
|
|
|
|
|
return IrcTextFormat().toPlainText(message->content());
|
|
|
|
|
}
|
2026-07-18 18:39:57 +02:00
|
|
|
const QString content = IrcTextFormat().toHtml(message->content());
|
|
|
|
|
return QObject::tr("<%1%2> [%3] %4").arg(message->nick(), pfx, message->target(), content);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatNumericMessage(IrcNumericMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
|
|
|
|
if (message->code() < 300) {
|
|
|
|
|
const QString info = QStringList(message->parameters().mid(1)).join(" ");
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(info);
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(info);
|
|
|
|
|
}
|
|
|
|
|
return QObject::tr("[INFO] %1").arg(info);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (message->code()) {
|
2017-06-25 15:14:01 -07:00
|
|
|
case Irc::RPL_VERSION:
|
|
|
|
|
return QObject::tr("! %1 version is %2").arg(message->nick(), message->parameters().value(1));
|
2017-06-25 12:05:18 -07:00
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
case Irc::RPL_TIME:
|
|
|
|
|
return QObject::tr("! %1 time is %2").arg(message->parameters().value(1), message->parameters().value(2));
|
2017-06-25 12:05:18 -07:00
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
default:
|
|
|
|
|
break;
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->isComposed() || message->flags() & IrcMessage::Implicit) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QString();
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
|
|
|
|
|
// if you change this, change formatErrorMessage too
|
|
|
|
|
if (Irc::codeToString(message->code()).startsWith("ERR_")) {
|
|
|
|
|
const QString info = QStringList(message->parameters().mid(1)).join(" ");
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(info);
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(info);
|
|
|
|
|
}
|
|
|
|
|
return QObject::tr("[ERROR] %1").arg(content);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
if (message->code() == Irc::RPL_CHANNEL_URL) {
|
|
|
|
|
const QString info = QStringList(message->parameters().mid(1)).join(" ");
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(info);
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(info);
|
|
|
|
|
}
|
|
|
|
|
return QObject::tr("[Channel URL] %1").arg(content);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
const QString info = QStringList(message->parameters().mid(1)).join(" ");
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(info);
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(info);
|
|
|
|
|
}
|
|
|
|
|
return QObject::tr("[%1] %2").arg(QString::number(message->code()), content);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatErrorMessage(IrcErrorMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
// if you change this, change ERR_ in formatNumericMessage too
|
|
|
|
|
return QObject::tr("[ERROR] %1").arg(message->error());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatPartMessage(IrcPartMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->reason().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %1 has left %2").arg(message->nick(), message->channel());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2025-11-25 18:10:28 +01:00
|
|
|
return QObject::tr("! %1 has left %2 (%3)").arg(message->nick(), message->channel(), message->reason());
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatPongMessage(IrcPongMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2023-05-14 15:06:15 +02:00
|
|
|
quint64 const msec = message->timeStamp().toMSecsSinceEpoch();
|
|
|
|
|
quint64 const dms = (QDateTime::currentMSecsSinceEpoch() - msec);
|
2020-12-03 01:35:25 +00:00
|
|
|
return QObject::tr("! %1 replied in %2 seconds").arg(message->nick()).arg(dms / 1000.0, 4, 'f', 3, QLatin1Char('0'));
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
// Normal messages sent to channels are processed by our client as if they are private messages.
|
|
|
|
|
QString IrcMessageFormatter::formatPrivateMessage(IrcPrivateMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2017-06-25 15:14:01 -07:00
|
|
|
QString content;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
content = IrcTextFormat().toPlainText(message->content());
|
|
|
|
|
} else {
|
|
|
|
|
content = IrcTextFormat().toHtml(message->content());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message->isAction()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("* %1 %2").arg(message->nick(), content);
|
2017-06-25 15:14:01 -07:00
|
|
|
}
|
2026-07-18 18:39:57 +02:00
|
|
|
if (isForLua) {
|
|
|
|
|
// lua only needs the message text here. Nick and target are sent as arguments to postIrcMessage()
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
return QObject::tr("<b><%1></b> %2").arg(message->nick(), content);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatQuitMessage(IrcQuitMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->reason().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %1 has quit").arg(message->nick());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2025-11-25 18:10:28 +01:00
|
|
|
return QObject::tr("! %1 has quit (%2)").arg(message->nick(), message->reason());
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatTopicMessage(IrcTopicMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
|
|
|
|
if (message->isReply()) {
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->topic().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! no topic");
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 15:14:01 -07:00
|
|
|
|
|
|
|
|
QString topic;
|
|
|
|
|
if (isForLua) {
|
|
|
|
|
topic = IrcTextFormat().toPlainText(message->topic());
|
|
|
|
|
} else {
|
|
|
|
|
topic = IrcTextFormat().toHtml(message->topic());
|
|
|
|
|
}
|
|
|
|
|
return QObject::tr("[TOPIC] %1").arg(topic);
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->topic().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("! %2 cleared topic").arg(message->nick());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
|
|
|
|
|
return QObject::tr("! %2 changed topic").arg(message->nick());
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatUnknownMessage(IrcMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 15:14:01 -07:00
|
|
|
return QObject::tr("? %2 %3 %4").arg(message->nick(), message->command(), message->parameters().join(" "));
|
2017-06-25 12:05:18 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatWhoisMessage(IrcWhoisMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
QString wData;
|
|
|
|
|
wData = QObject::tr("[WHOIS] %1 is %2@%3 (%4)").arg(message->nick(), message->ident(), message->host(), message->realName());
|
|
|
|
|
wData += QObject::tr("[WHOIS] %1 is connected via %2 (%3)").arg(message->nick(), message->server(), message->info());
|
|
|
|
|
wData += QObject::tr("[WHOIS] %1 is connected since %2 (idle %3)").arg(message->nick(), message->since().toString(), formatDuration(message->idle()));
|
2021-02-27 17:25:16 +01:00
|
|
|
if (!message->awayReason().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
wData += QObject::tr("[WHOIS] %1 is away: %2").arg(message->nick(), message->awayReason());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
if (!message->account().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
wData += QObject::tr("[WHOIS] %1 is logged in as %2").arg(message->nick(), message->account());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
if (!message->address().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
wData += QObject::tr("[WHOIS] %1 is connected from %2").arg(message->nick(), message->address());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
if (message->isSecure()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
wData += QObject::tr("[WHOIS] %1 is using a secure connection").arg(message->nick());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
if (!message->channels().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
wData += QObject::tr("[WHOIS] %1 is on %2").arg(message->nick(), message->channels().join(" "));
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
return wData;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatWhowasMessage(IrcWhowasMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
QString wData;
|
|
|
|
|
wData = QObject::tr("[WHOWAS] %1 was %2@%3 (%4)").arg(message->nick(), message->ident(), message->host(), message->realName());
|
|
|
|
|
wData += QObject::tr("[WHOWAS] %1 was connected via %2 (%3)").arg(message->nick(), message->server(), message->info());
|
2021-02-27 17:25:16 +01:00
|
|
|
if (!message->account().isEmpty()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
wData += QObject::tr("[WHOWAS] %1 was logged in as %2").arg(message->nick(), message->account());
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
return wData;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 15:14:01 -07:00
|
|
|
QString IrcMessageFormatter::formatWhoReplyMessage(IrcWhoReplyMessage* message, bool isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
{
|
2020-11-28 10:37:24 +00:00
|
|
|
Q_UNUSED(isForLua)
|
2017-06-25 12:05:18 -07:00
|
|
|
QString format = QObject::tr("[WHO] %1 (%2)").arg(message->nick(), message->realName());
|
2021-02-27 17:25:16 +01:00
|
|
|
if (message->isAway()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
format += QObject::tr(" - away");
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
|
|
|
|
if (message->isServOp()) {
|
2017-06-25 12:05:18 -07:00
|
|
|
format += QObject::tr(" - server operator");
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString IrcMessageFormatter::formatSeconds(int secs)
|
|
|
|
|
{
|
2023-03-20 07:18:24 +01:00
|
|
|
const QDateTime time = QDateTime::fromSecsSinceEpoch(secs);
|
2017-06-25 12:05:18 -07:00
|
|
|
return QObject::tr("%1s").arg(time.secsTo(QDateTime::currentDateTime()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString IrcMessageFormatter::formatDuration(int secs)
|
|
|
|
|
{
|
|
|
|
|
QStringList idle;
|
2023-05-14 15:06:15 +02:00
|
|
|
if (const int days = secs / 86400) {
|
2017-06-25 12:05:18 -07:00
|
|
|
idle += QObject::tr("%1 days").arg(days);
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
secs %= 86400;
|
2023-05-14 15:06:15 +02:00
|
|
|
if (const int hours = secs / 3600) {
|
2017-06-25 12:05:18 -07:00
|
|
|
idle += QObject::tr("%1 hours").arg(hours);
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
secs %= 3600;
|
2023-05-14 15:06:15 +02:00
|
|
|
if (const int mins = secs / 60) {
|
2017-06-25 12:05:18 -07:00
|
|
|
idle += QObject::tr("%1 mins").arg(mins);
|
2021-02-27 17:25:16 +01:00
|
|
|
}
|
2017-06-25 12:05:18 -07:00
|
|
|
idle += QObject::tr("%1 secs").arg(secs % 60);
|
|
|
|
|
return idle.join(" ");
|
|
|
|
|
}
|