void Core::Process (QObject *msgObj) { IMessage *msg = qobject_cast<IMessage*> (msgObj); if (msg->GetMessageType () != IMessage::Type::ChatMessage && msg->GetMessageType () != IMessage::Type::MUCMessage) return; if (msg->GetBody ().isEmpty ()) return; if (msg->GetDirection () == IMessage::Direction::Out && msg->GetMessageType () == IMessage::Type::MUCMessage) return; const double secsDiff = msg->GetDateTime ().secsTo (QDateTime::currentDateTime ()); if (msg->GetMessageType () == IMessage::Type::MUCMessage && std::abs (secsDiff) >= 2) return; ICLEntry *entry = qobject_cast<ICLEntry*> (msg->ParentCLEntry ()); if (!entry) { qWarning () << Q_FUNC_INFO << "message's other part doesn't implement ICLEntry" << msg->GetQObject () << msg->OtherPart (); return; } if (DisabledIDs_.contains (entry->GetEntryID ())) return; const auto acc = entry->GetParentAccount (); QVariantMap data; data ["EntryID"] = entry->GetEntryID (); data ["AccountID"] = acc->GetAccountID (); data ["DateTime"] = msg->GetDateTime (); data ["Direction"] = msg->GetDirection () == IMessage::Direction::In ? "IN" : "OUT"; data ["Body"] = msg->GetBody (); data ["OtherVariant"] = msg->GetOtherVariant (); data ["Type"] = static_cast<int> (msg->GetMessageType ()); data ["EscapePolicy"] = msg->GetEscapePolicy () == IMessage::EscapePolicy::Escape ? "Esc" : "NEs"; if (const auto irtm = qobject_cast<IRichTextMessage*> (msgObj)) data ["RichBody"] = irtm->GetRichBody (); if (entry->GetEntryType () == ICLEntry::EntryType::PrivateChat) { const auto parent = entry->GetParentCLEntry (); data ["VisibleName"] = parent->GetEntryName () + "/" + entry->GetEntryName (); } else data ["VisibleName"] = entry->GetEntryName (); QMetaObject::invokeMethod (StorageThread_->GetStorage (), "addMessage", Qt::QueuedConnection, Q_ARG (QVariantMap, data)); }
void Core::Process (QObject *msgObj) { IMessage *msg = qobject_cast<IMessage*> (msgObj); if (msg->GetMessageType () != IMessage::MTChatMessage && msg->GetMessageType () != IMessage::MTMUCMessage) return; if (msg->GetBody ().isEmpty ()) return; if (msg->GetDirection () == IMessage::DOut && msg->GetMessageType () == IMessage::MTMUCMessage) return; ICLEntry *entry = qobject_cast<ICLEntry*> (msg->ParentCLEntry ()); if (!entry) { qWarning () << Q_FUNC_INFO << "message's other part doesn't implement ICLEntry" << msg->GetObject () << msg->OtherPart (); return; } if (DisabledIDs_.contains (entry->GetEntryID ())) return; IAccount *acc = qobject_cast<IAccount*> (entry->GetParentAccount ()); if (!acc) { qWarning () << Q_FUNC_INFO << "message's account doesn't implement IAccount" << entry->GetParentAccount (); return; } QVariantMap data; data ["EntryID"] = entry->GetEntryID (); data ["VisibleName"] = entry->GetEntryName (); data ["AccountID"] = acc->GetAccountID (); data ["DateTime"] = msg->GetDateTime (); data ["Direction"] = msg->GetDirection () == IMessage::DIn ? "IN" : "OUT"; data ["Body"] = msg->GetBody (); data ["OtherVariant"] = msg->GetOtherVariant (); data ["MessageType"] = static_cast<int> (msg->GetMessageType ()); QMetaObject::invokeMethod (StorageThread_->GetStorage (), "addMessage", Qt::QueuedConnection, Q_ARG (QVariantMap, data)); }
void Plugin::hookMessageCreated (IHookProxy_ptr proxy, QObject*, QObject *msgObj) { if (ConvScriptPath_.isEmpty ()) return; if (!XmlSettingsManager::Instance () .property ("SubstituteOutgoing").toBool ()) return; if (HandledObjects_.contains (msgObj)) return; IMessage *msg = qobject_cast<IMessage*> (msgObj); if (!msg) { qWarning () << Q_FUNC_INFO << "message" << msgObj << "doesn't implement IMessage"; return; } IRichTextMessage *richMsg = qobject_cast<IRichTextMessage*> (msgObj); if (!richMsg) return; QString body = richMsg->GetRichBody (); if (body.isEmpty ()) body = msg->GetBody (); if (!body.contains ("$$")) return; const QString newBody = HandleBody (body); if (newBody == body) return; if (XmlSettingsManager::Instance () .property ("WarnInOutgoing").toBool ()) msg->SetBody (msg->GetBody () + " (this message contains " "inline formulas, enable XHTML-IM to view them)"); richMsg->SetRichBody (newBody); HandledObjects_ << msgObj; }
void Plugin::hookGotMessage (LeechCraft::IHookProxy_ptr proxy, QObject *message) { IMessage *msg = qobject_cast<IMessage*> (message); if (!msg) return; if (msg->GetMessageType () != IMessage::MTChatMessage && msg->GetMessageType () != IMessage::MTMUCMessage) return; if ((msg->GetMessageType () == IMessage::MTChatMessage && !XmlSettingsManager::Instance ().property ("EnableForChats").toBool ()) || (msg->GetMessageType () == IMessage::MTMUCMessage && !XmlSettingsManager::Instance ().property ("EnableForMUCs").toBool ())) return; QString str = msg->GetBody (); if (str.length () < 3) return; int caps = 0; int alphaLength = 0; for (int i = 0, size = str.length (); i < size; ++i) { if (!str.at (i).isLetter ()) continue; ++alphaLength; if (str.at (i).isUpper ()) ++caps; } double ratio = static_cast<double> (XmlSettingsManager::Instance () .property ("CapsPercentage").toInt ()) / 100; if (alphaLength > 3 && static_cast<double> (caps) / alphaLength < ratio) return; for (int i = 0, size = str.length (); i < size; ++i) str [i] = str.at (i).toLower (); msg->SetBody (str); }
void Plugin::hookFormatBodyEnd (IHookProxy_ptr proxy, QObject*, QString body, QObject *msgObj) { IMessage *msg = qobject_cast<IMessage*> (msgObj); if (msg->GetDirection () != IMessage::DIn || msg->GetMessageType () != IMessage::MTChatMessage) return; ICLEntry *other = qobject_cast<ICLEntry*> (msg->OtherPart ()); if (!other) { qWarning () << Q_FUNC_INFO << "NULL other part for message" << msgObj << msg->GetBody (); return; } if (!other->GetEntryID ().contains ("*****@*****.**")) return; proxy->SetValue ("body", FormatBody (body)); }
void Plugin::hookGotMessage (LeechCraft::IHookProxy_ptr proxy, QObject *message) { if (!IsConfValid ()) return; if (OurMessages_.contains (message)) { OurMessages_.remove (message); proxy->CancelDefault (); return; } IMessage *msg = qobject_cast<IMessage*> (message); if (!msg) { qWarning () << Q_FUNC_INFO << message << "doesn't implement IMessage"; return; } if (msg->GetMessageType () != IMessage::MTChatMessage) return; QObject *entryObj = msg->OtherPart (); ICLEntry *entry = qobject_cast<ICLEntry*> (entryObj); if (IsEntryAllowed (entryObj)) return; if (!AskedEntries_.contains (entryObj)) { AskedEntries_ << entryObj; const QString& text = tr ("Please answer to the following " "question to verify you are not a bot and is welcome " "to communicate with me:\n%1") .arg (ConfWidget_->GetQuestion ()); QObject *msgObj = entry->CreateMessage (IMessage::MTChatMessage, QString (), text); OurMessages_ << msgObj; qobject_cast<IMessage*> (msgObj)->Send (); proxy->CancelDefault (); } else if (ConfWidget_->GetAnswers ().contains (msg->GetBody ().toLower ())) { AllowedEntries_ << entryObj; AskedEntries_.remove (entryObj); const QString& text = tr ("Nice, seems like you've answered " "correctly. Please write again now what you wanted " "to write."); QObject *msgObj = entry->CreateMessage (IMessage::MTChatMessage, QString (), text); OurMessages_ << msgObj; qobject_cast<IMessage*> (msgObj)->Send (); } else { const QString& text = tr ("Sorry, you are wrong. Try again."); QObject *msgObj = entry->CreateMessage (IMessage::MTChatMessage, QString (), text); OurMessages_ << msgObj; qobject_cast<IMessage*> (msgObj)->Send (); proxy->CancelDefault (); } }