Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OMEMO-encrypted MUC logging functionality #94

Merged
merged 11 commits into from
Aug 13, 2021
2 changes: 1 addition & 1 deletion generic/conferenceloggerplugin/conferenceloggerplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ bool ConferenceLogger::incomingStanza(int account, const QDomElement &stanza)
{
if (enabled) {
if (stanza.tagName() == "message") {
if (stanza.attribute("type") == "groupchat") {
if (stanza.attribute("type") == "groupchat" && stanza.firstChildElement("encrypted").isNull()) {
QString from = stanza.attribute("from");
QStringList List = from.split("/");
QString room = List.takeFirst();
Expand Down
76 changes: 74 additions & 2 deletions generic/omemoplugin/src/omemoplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,23 @@ bool OMEMOPlugin::decryptMessageElement(int account, QDomElement &message)
processEncryptedFile(account, message);
}

// logging functionality for OMEMO-encrypted groupchats
if (message.attribute("type") == QLatin1String("groupchat")) {
QString from = message.attribute("from");
QString room = from.section('/', 0, 0);
QString nickname = from.section('/', 1);
if (nickname != m_mucNicks.value(room)) {
QString Stamp = message.firstChildElement("x").attribute("stamp");
QDomElement body = message.firstChildElement("body");
if (!body.isNull()) {
QString Text = body.text();
QString MyJid = m_accountInfo->getJid(account);
MyJid = MyJid.replace("@", "_at_");
logMuc(room, nickname, MyJid, Text, Stamp);
}
}
}

return true;
}

Expand Down Expand Up @@ -313,8 +330,19 @@ bool OMEMOPlugin::outgoingStanza(int account, QDomElement &xml)
return false;
}

if (xml.nodeName() == "presence" && !xml.hasAttributes()) {
m_omemo->accountConnected(account, m_accountInfo->getJid(account));
if (xml.nodeName() == QLatin1String("presence")) {
if (!xml.hasAttributes())
m_omemo->accountConnected(account, m_accountInfo->getJid(account));
// get all MUC nicks of the current account for groupchat logging
// functionality
else {
QString room = xml.attribute("to").section('/', 0, 0);
QString nick = xml.attribute("to").section('/', 1);
if (m_contactInfo->isConference(account, room)) {
m_mucNicks.insert(room, nick);
}
}

}

return false;
Expand All @@ -330,6 +358,24 @@ bool OMEMOPlugin::encryptMessageElement(int account, QDomElement &message)
return false;
}

// logging functionality for OMEMO-encrypted groupchats
if (message.attribute("type") == QLatin1String("groupchat")) {
QString room = message.attribute("to");
QString from = m_mucNicks.value(room, m_accountInfo->getJid(account));
if (from==QLatin1String(""))
from = m_accountInfo->getJid(account);
if (m_omemo->isEnabledForUser(account, room)) { // only log if encryption is enabled
QString Stamp = message.firstChildElement("x").attribute("stamp");
QDomElement body = message.firstChildElement("body");
if (!body.isNull()) {
QString Text = body.text();
QString MyJid = m_accountInfo->getJid(account);
MyJid = MyJid.replace("@", "_at_");
logMuc(room, from, MyJid, Text, Stamp);
}
}
}

return m_omemo->encryptMessage(m_accountInfo->getJid(account), account, message);
}

Expand Down Expand Up @@ -544,4 +590,30 @@ bool OMEMOPlugin::execute(int account, const QHash<QString, QVariant> &args, QHa

return false;
}

// the code partly taken from the Conference Logger plugin
void OMEMOPlugin::logMuc(QString room, const QString &from, const QString &myJid,
const QString &text, QString stamp)
{
room = room.replace("@", "_at_");
room = "_in_" + room;
if (stamp.isEmpty()) {
stamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
} else {
stamp.insert(4, "-");
stamp.insert(7, "-");
stamp.replace("T", " ");
}
QFile file(m_applicationInfo->appHistoryDir() + QDir::separator() + myJid + room + ".conferencehistory");
if (file.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream out(&file);
out.setCodec("UTF-8");
out.setGenerateByteOrderMark(false);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
out << stamp << " " << from << ": " << text << Qt::endl;
#else
out << stamp << " " << from << ": " << text << endl;
#endif
}
}
}
2 changes: 2 additions & 0 deletions generic/omemoplugin/src/omemoplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private slots:
void updateAction(int account, const QString &user);
void processEncryptedFile(int account, QDomElement &xml);
void showOwnFingerprint(int account, const QString &jid);
void logMuc(QString room, const QString &from, const QString &myJid, const QString &text, QString stamp);

private:
bool m_enabled = false;
Expand All @@ -147,6 +148,7 @@ private slots:
EventCreatingHost * m_eventCreator = nullptr;
PsiAccountControllingHost * m_accountController = nullptr;
OptionAccessingHost * m_optionHost = nullptr;
QMap<QString, QString> m_mucNicks;
};
}
#endif // PSIOMEMO_OMEMOPLUGIN_H