Пример #1
0
	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));
	}
Пример #2
0
	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));
	}
Пример #3
0
	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);
	}
Пример #4
0
void Plugin::hookFormatBodyEnd (IHookProxy_ptr proxy,
                                QObject *chatTab, 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->GetEntryID ().contains ("*****@*****.**"))
        return;

    proxy->SetValue ("body", FormatBody (body));
}
Пример #5
0
	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));
	}
Пример #6
0
	bool Plugin::ShouldHandle (QObject* msgObj,
			IMessage::Direction direction, IMessage::Type type)
	{
		IMessage *msg = qobject_cast<IMessage*> (msgObj);

		if (!msg)
		{
			qWarning () << Q_FUNC_INFO
				<< "unable to cast"
				<< msgObj
				<< "to IMessage";
			return false;
		}

		if (msg->GetDirection () != direction ||
			msg->GetMessageType () != type)
		{
			return false;
		}

		ICLEntry *other = qobject_cast<ICLEntry*> (msg->OtherPart ());

		if (!other)
		{
			qWarning () << Q_FUNC_INFO
				<< "unable to cast"
				<< msg->OtherPart ()
				<< "to ICLEntry";
			return false;
		}

		if (!other->GetEntryID ().contains ("*****@*****.**"))
			return false;

		return true;

	}
Пример #7
0
	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 ();
		}
	}