File indexing completed on 2023-12-03 08:28:37

0001 /*
0002     Copyright (C) 2014  Marcin ZiemiƄski   <zieminn@gmail.com>
0003 
0004     This program is free software: you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published by
0006     the Free Software Foundation, either version 2 of the License, or
0007     (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "utils.h"
0019 
0020 #include "constants.h"
0021 
0022 #include <TelepathyQt/Connection>
0023 
0024 #include <KLocalizedString>
0025 
0026 namespace KTp {
0027 namespace Utils {
0028 
0029     Tp::UIntList getPendingMessagesIDs(const QList<Tp::ReceivedMessage> &messageQueue)
0030     {
0031         Tp::UIntList ids;
0032         Q_FOREACH(const Tp::ReceivedMessage &mes, messageQueue) {
0033             ids << getId(mes.parts());
0034         }
0035         return ids;
0036     }
0037 
0038     uint getId(const Tp::MessagePartList &message) {
0039         return message.first()[QLatin1String("pending-message-id")].variant().toUInt(nullptr);
0040     }
0041 
0042     QString getOtrProxyObjectPathFor(const Tp::TextChannelPtr &textChannel)
0043     {
0044         int index;
0045         QString connectionId = textChannel->connection()->objectPath();
0046         index = connectionId.lastIndexOf(QChar::fromLatin1('/'));
0047         connectionId = connectionId.mid(index+1);
0048 
0049         QString channelId = textChannel->objectPath();
0050         index = channelId.lastIndexOf(QChar::fromLatin1('/'));
0051         channelId = channelId.mid(index+1);
0052 
0053         return QString::fromLatin1("%1%2/%3").arg(KTP_PROXY_CHANNEL_OBJECT_PATH_PREFIX, connectionId, channelId);
0054     }
0055 
0056     bool isOtrMessage(const QString &text)
0057     {
0058         return text.startsWith(QLatin1String("?OTR"));
0059     }
0060 
0061     bool isOtrEvent(const Tp::ReceivedMessage &message)
0062     {
0063         return message.part(0).contains(OTR_MESSAGE_EVENT_HEADER);
0064     }
0065 
0066     QString processOtrMessage(const Tp::ReceivedMessage &message)
0067     {
0068         Tp::MessagePart messagePart = message.part(0);
0069         OTRMessageEvent otrEvent = static_cast<OTRMessageEvent>(
0070                 messagePart[OTR_MESSAGE_EVENT_HEADER].variant().toUInt(nullptr));
0071 
0072         switch(otrEvent) {
0073 
0074             case KTp::OTRL_MSGEVENT_SETUP_ERROR:
0075             case KTp::OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR:
0076                 {
0077                     QString otrError = messagePart[OTR_ERROR_HEADER].variant().toString();
0078                     return i18n("OTR error: %1", otrError);
0079                 }
0080 
0081             case KTp::OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED:
0082                 {
0083                     QString unencryptedMessage = messagePart[OTR_UNENCRYPTED_MESSAGE_HEADER].variant().toString();
0084                     return i18n("Received unencrypted message: [%1]", unencryptedMessage);
0085                 }
0086 
0087             default:
0088                 return message.text();
0089         }
0090     }
0091 }
0092 }