Fix plain text in eventToString().

square-messages
Black Hat 2019-04-21 13:06:48 +08:00
parent de3a8b9b69
commit aa3309a184
3 changed files with 172 additions and 160 deletions

View File

@ -18,7 +18,8 @@
#include "utils.h" #include "utils.h"
SpectralRoom::SpectralRoom(Connection* connection, QString roomId, SpectralRoom::SpectralRoom(Connection* connection,
QString roomId,
JoinState joinState) JoinState joinState)
: Room(connection, std::move(roomId), joinState) { : Room(connection, std::move(roomId), joinState) {
connect(this, &SpectralRoom::notificationCountChanged, this, connect(this, &SpectralRoom::notificationCountChanged, this,
@ -73,18 +74,25 @@ void SpectralRoom::chooseAndUploadFile() {
void SpectralRoom::saveFileAs(QString eventId) { void SpectralRoom::saveFileAs(QString eventId) {
auto fileName = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save File as"), auto fileName = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save File as"),
fileNameToDownload(eventId)); fileNameToDownload(eventId));
if (!fileName.isEmpty()) downloadFile(eventId, QUrl::fromLocalFile(fileName)); if (!fileName.isEmpty())
downloadFile(eventId, QUrl::fromLocalFile(fileName));
} }
void SpectralRoom::acceptInvitation() { connection()->joinRoom(id()); } void SpectralRoom::acceptInvitation() {
connection()->joinRoom(id());
}
void SpectralRoom::forget() { connection()->forgetRoom(id()); } void SpectralRoom::forget() {
connection()->forgetRoom(id());
}
bool SpectralRoom::hasUsersTyping() { bool SpectralRoom::hasUsersTyping() {
QList<User*> users = usersTyping(); QList<User*> users = usersTyping();
if (users.isEmpty()) return false; if (users.isEmpty())
return false;
int count = users.length(); int count = users.length();
if (users.contains(localUser())) count--; if (users.contains(localUser()))
count--;
return count != 0; return count != 0;
} }
@ -104,7 +112,8 @@ void SpectralRoom::sendTypingNotification(bool isTyping) {
} }
QString SpectralRoom::lastEvent() { QString SpectralRoom::lastEvent() {
if (timelineSize() == 0) return ""; if (timelineSize() == 0)
return "";
const RoomEvent* lastEvent = messageEvents().rbegin()->get(); const RoomEvent* lastEvent = messageEvents().rbegin()->get();
return user(lastEvent->senderId())->displayname() + ": " + return user(lastEvent->senderId())->displayname() + ": " +
utils::removeReply(eventToString(*lastEvent)); utils::removeReply(eventToString(*lastEvent));
@ -116,7 +125,8 @@ bool SpectralRoom::isEventHighlighted(const RoomEvent* e) const {
void SpectralRoom::checkForHighlights(const QMatrixClient::TimelineItem& ti) { void SpectralRoom::checkForHighlights(const QMatrixClient::TimelineItem& ti) {
auto localUserId = localUser()->id(); auto localUserId = localUser()->id();
if (ti->senderId() == localUserId) return; if (ti->senderId() == localUserId)
return;
if (auto* e = ti.viewAs<RoomMessageEvent>()) { if (auto* e = ti.viewAs<RoomMessageEvent>()) {
const auto& text = e->plainBody(); const auto& text = e->plainBody();
if (text.contains(localUserId) || if (text.contains(localUserId) ||
@ -142,8 +152,10 @@ void SpectralRoom::countChanged() {
} }
} }
void SpectralRoom::sendReply(QString userId, QString eventId, void SpectralRoom::sendReply(QString userId,
QString replyContent, QString sendContent) { QString eventId,
QString replyContent,
QString sendContent) {
QJsonObject json{ QJsonObject json{
{"msgtype", "m.text"}, {"msgtype", "m.text"},
{"body", "> <" + userId + "> " + replyContent + "\n\n" + sendContent}, {"body", "> <" + userId + "> " + replyContent + "\n\n" + sendContent},
@ -159,7 +171,8 @@ void SpectralRoom::sendReply(QString userId, QString eventId,
} }
QDateTime SpectralRoom::lastActiveTime() { QDateTime SpectralRoom::lastActiveTime() {
if (timelineSize() == 0) return QDateTime(); if (timelineSize() == 0)
return QDateTime();
return messageEvents().rbegin()->get()->timestamp(); return messageEvents().rbegin()->get()->timestamp();
} }
@ -205,15 +218,19 @@ QVariantList SpectralRoom::getUsers(const QString& prefix) {
} }
QString SpectralRoom::postMarkdownText(const QString& markdown) { QString SpectralRoom::postMarkdownText(const QString& markdown) {
unsigned char *sequence = (unsigned char *) qstrdup(markdown.toUtf8().constData()); unsigned char* sequence =
qint64 length = strlen((char *) sequence); (unsigned char*)qstrdup(markdown.toUtf8().constData());
qint64 length = strlen((char*)sequence);
hoedown_renderer* renderer = hoedown_html_renderer_new(HOEDOWN_HTML_USE_XHTML, 32); hoedown_renderer* renderer =
hoedown_extensions extensions = (hoedown_extensions) ((HOEDOWN_EXT_BLOCK | HOEDOWN_EXT_SPAN | HOEDOWN_EXT_MATH_EXPLICIT) & ~HOEDOWN_EXT_QUOTE); hoedown_html_renderer_new(HOEDOWN_HTML_USE_XHTML, 32);
hoedown_extensions extensions = (hoedown_extensions)(
(HOEDOWN_EXT_BLOCK | HOEDOWN_EXT_SPAN | HOEDOWN_EXT_MATH_EXPLICIT) &
~HOEDOWN_EXT_QUOTE);
hoedown_document* document = hoedown_document_new(renderer, extensions, 32); hoedown_document* document = hoedown_document_new(renderer, extensions, 32);
hoedown_buffer* html = hoedown_buffer_new(length); hoedown_buffer* html = hoedown_buffer_new(length);
hoedown_document_render(document, html, sequence, length); hoedown_document_render(document, html, sequence, length);
QString result = QString::fromUtf8((char *) html->data, html->size); QString result = QString::fromUtf8((char*)html->data, html->size);
free(sequence); free(sequence);
hoedown_buffer_free(html); hoedown_buffer_free(html);

View File

@ -8,12 +8,12 @@
#include <QPointer> #include <QPointer>
#include <QTimer> #include <QTimer>
#include <events/roommessageevent.h>
#include <events/redactionevent.h> #include <events/redactionevent.h>
#include <events/roomavatarevent.h> #include <events/roomavatarevent.h>
#include <events/roommemberevent.h>
#include <events/simplestateevents.h>
#include <events/roomcreateevent.h> #include <events/roomcreateevent.h>
#include <events/roommemberevent.h>
#include <events/roommessageevent.h>
#include <events/simplestateevents.h>
using namespace QMatrixClient; using namespace QMatrixClient;
@ -30,7 +30,8 @@ class SpectralRoom : public Room {
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged) Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
public: public:
explicit SpectralRoom(Connection* connection, QString roomId, explicit SpectralRoom(Connection* connection,
QString roomId,
JoinState joinState = {}); JoinState joinState = {});
const QString& cachedInput() const { return m_cachedInput; } const QString& cachedInput() const { return m_cachedInput; }
@ -89,147 +90,140 @@ class SpectralRoom : public Room {
bool prettyPrint = (format == Qt::RichText); bool prettyPrint = (format == Qt::RichText);
using namespace QMatrixClient; using namespace QMatrixClient;
return visit(evt return visit(
, [this, prettyPrint] (const RoomMessageEvent& e) { evt,
using namespace MessageEventContent; [this, prettyPrint](const RoomMessageEvent& e) {
using namespace MessageEventContent;
if (e.hasTextContent() && e.mimeType().name() != "text/plain") if (prettyPrint && e.hasTextContent() &&
return static_cast<const TextContent*>(e.content())->body; e.mimeType().name() != "text/plain")
if (e.hasFileContent()) return static_cast<const TextContent*>(e.content())->body;
{ if (e.hasFileContent()) {
auto fileCaption = auto fileCaption =
e.content()->fileInfo()->originalName.toHtmlEscaped(); e.content()->fileInfo()->originalName.toHtmlEscaped();
if (fileCaption.isEmpty()) if (fileCaption.isEmpty()) {
fileCaption = this->prettyPrint(e.plainBody()); if (prettyPrint)
return !fileCaption.isEmpty() ? fileCaption : tr("a file"); fileCaption = this->prettyPrint(e.plainBody());
else
fileCaption = e.plainBody();
} }
return this->prettyPrint(e.plainBody()); return !fileCaption.isEmpty() ? fileCaption : tr("a file");
} }
, [this] (const RoomMemberEvent& e) { return prettyPrint ? this->prettyPrint(e.plainBody()) : e.plainBody();
// FIXME: Rewind to the name that was at the time of this event },
auto subjectName = this->user(e.userId())->displayname(); [this](const RoomMemberEvent& e) {
// The below code assumes senderName output in AuthorRole // FIXME: Rewind to the name that was at the time of this event
switch( e.membership() ) auto subjectName = this->user(e.userId())->displayname();
{ // The below code assumes senderName output in AuthorRole
case MembershipType::Invite: switch (e.membership()) {
if (e.repeatsState()) case MembershipType::Invite:
return tr("reinvited %1 to the room").arg(subjectName); if (e.repeatsState())
FALLTHROUGH; return tr("reinvited %1 to the room").arg(subjectName);
case MembershipType::Join: FALLTHROUGH;
{ case MembershipType::Join: {
if (e.repeatsState()) if (e.repeatsState())
return tr("joined the room (repeated)"); return tr("joined the room (repeated)");
if (!e.prevContent() || if (!e.prevContent() ||
e.membership() != e.prevContent()->membership) e.membership() != e.prevContent()->membership) {
{ return e.membership() == MembershipType::Invite
return e.membership() == MembershipType::Invite ? tr("invited %1 to the room").arg(subjectName)
? tr("invited %1 to the room").arg(subjectName) : tr("joined the room");
: tr("joined the room"); }
} QString text{};
QString text {}; if (e.isRename()) {
if (e.isRename()) if (e.displayName().isEmpty())
{ text = tr("cleared the display name");
if (e.displayName().isEmpty()) else
text = tr("cleared the display name"); text = tr("changed the display name to %1")
else .arg(e.displayName().toHtmlEscaped());
text = tr("changed the display name to %1") }
.arg(e.displayName().toHtmlEscaped()); if (e.isAvatarUpdate()) {
} if (!text.isEmpty())
if (e.isAvatarUpdate()) text += " and ";
{ if (e.avatarUrl().isEmpty())
if (!text.isEmpty()) text += tr("cleared the avatar");
text += " and "; else
if (e.avatarUrl().isEmpty()) text += tr("updated the avatar");
text += tr("cleared the avatar"); }
else return text;
text += tr("updated the avatar"); }
} case MembershipType::Leave:
return text; if (e.prevContent() &&
} e.prevContent()->membership == MembershipType::Invite) {
case MembershipType::Leave: return (e.senderId() != e.userId())
if (e.prevContent() && ? tr("withdrew %1's invitation").arg(subjectName)
e.prevContent()->membership == MembershipType::Invite) : tr("rejected the invitation");
{ }
return (e.senderId() != e.userId())
? tr("withdrew %1's invitation").arg(subjectName)
: tr("rejected the invitation");
}
if (e.prevContent() && if (e.prevContent() &&
e.prevContent()->membership == MembershipType::Ban) e.prevContent()->membership == MembershipType::Ban) {
{ return (e.senderId() != e.userId())
return (e.senderId() != e.userId()) ? tr("unbanned %1").arg(subjectName)
? tr("unbanned %1").arg(subjectName) : tr("self-unbanned");
: tr("self-unbanned"); }
} return (e.senderId() != e.userId())
return (e.senderId() != e.userId()) ? tr("has put %1 out of the room: %2")
? tr("has put %1 out of the room: %2") .arg(subjectName, e.contentJson()["reason"_ls]
.arg(subjectName, .toString()
e.contentJson()["reason"_ls] .toHtmlEscaped())
.toString().toHtmlEscaped()) : tr("left the room");
: tr("left the room"); case MembershipType::Ban:
case MembershipType::Ban: return (e.senderId() != e.userId())
return (e.senderId() != e.userId()) ? tr("banned %1 from the room: %2")
? tr("banned %1 from the room: %2") .arg(subjectName, e.contentJson()["reason"_ls]
.arg(subjectName, .toString()
e.contentJson()["reason"_ls] .toHtmlEscaped())
.toString().toHtmlEscaped()) : tr("self-banned from the room");
: tr("self-banned from the room"); case MembershipType::Knock:
case MembershipType::Knock: return tr("knocked");
return tr("knocked"); default:;
default: }
; return tr("made something unknown");
} },
return tr("made something unknown"); [](const RoomAliasesEvent& e) {
} return tr("has set room aliases on server %1 to: %2")
, [] (const RoomAliasesEvent& e) { .arg(e.stateKey(), QLocale().createSeparatedList(e.aliases()));
return tr("has set room aliases on server %1 to: %2") },
.arg(e.stateKey(), [](const RoomCanonicalAliasEvent& e) {
QLocale().createSeparatedList(e.aliases())); return (e.alias().isEmpty())
} ? tr("cleared the room main alias")
, [] (const RoomCanonicalAliasEvent& e) { : tr("set the room main alias to: %1").arg(e.alias());
return (e.alias().isEmpty()) },
? tr("cleared the room main alias") [](const RoomNameEvent& e) {
: tr("set the room main alias to: %1").arg(e.alias()); return (e.name().isEmpty()) ? tr("cleared the room name")
} : tr("set the room name to: %1")
, [] (const RoomNameEvent& e) { .arg(e.name().toHtmlEscaped());
return (e.name().isEmpty()) },
? tr("cleared the room name") [this, prettyPrint](const RoomTopicEvent& e) {
: tr("set the room name to: %1") return (e.topic().isEmpty())
.arg(e.name().toHtmlEscaped()); ? tr("cleared the topic")
} : tr("set the topic to: %1")
, [this] (const RoomTopicEvent& e) { .arg(prettyPrint ? this->prettyPrint(e.topic())
return (e.topic().isEmpty()) : e.topic());
? tr("cleared the topic") },
: tr("set the topic to: %1") [](const RoomAvatarEvent&) { return tr("changed the room avatar"); },
.arg(this->prettyPrint(e.topic())); [](const EncryptionEvent&) {
} return tr("activated End-to-End Encryption");
, [] (const RoomAvatarEvent&) { },
return tr("changed the room avatar"); [](const RoomCreateEvent& e) {
} return (e.isUpgrade() ? tr("upgraded the room to version %1")
, [] (const EncryptionEvent&) { : tr("created the room, version %1"))
return tr("activated End-to-End Encryption"); .arg(e.version().isEmpty() ? "1" : e.version().toHtmlEscaped());
} },
, [] (const RoomCreateEvent& e) { [](const StateEventBase& e) {
return (e.isUpgrade() // A small hack for state events from TWIM bot
? tr("upgraded the room to version %1") return e.stateKey() == "twim"
: tr("created the room, version %1") ? tr("updated the database",
).arg(e.version().isEmpty() "TWIM bot updated the database")
? "1" : e.version().toHtmlEscaped()); : e.stateKey().isEmpty()
} ? tr("updated %1 state", "%1 - Matrix event type")
, [] (const StateEventBase& e) { .arg(e.matrixType())
// A small hack for state events from TWIM bot : tr("updated %1 state for %2",
return e.stateKey() == "twim" "%1 - Matrix event type, %2 - state key")
? tr("updated the database", "TWIM bot updated the database") .arg(e.matrixType(),
: e.stateKey().isEmpty() e.stateKey().toHtmlEscaped());
? tr("updated %1 state", "%1 - Matrix event type") },
.arg(e.matrixType()) tr("Unknown event"));
: tr("updated %1 state for %2",
"%1 - Matrix event type, %2 - state key")
.arg(e.matrixType(), e.stateKey().toHtmlEscaped());
}
, tr("Unknown event")
);
} }
private: private:
@ -261,7 +255,9 @@ class SpectralRoom : public Room {
void acceptInvitation(); void acceptInvitation();
void forget(); void forget();
void sendTypingNotification(bool isTyping); void sendTypingNotification(bool isTyping);
void sendReply(QString userId, QString eventId, QString replyContent, void sendReply(QString userId,
QString eventId,
QString replyContent,
QString sendContent); QString sendContent);
}; };

View File

@ -26,7 +26,6 @@ static const QRegularExpression userPillRegExp{
QString removeReply(const QString& text); QString removeReply(const QString& text);
QString cleanHTML(const QString& text, QMatrixClient::Room* room); QString cleanHTML(const QString& text, QMatrixClient::Room* room);
} // namespace utils } // namespace utils
#endif #endif