← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcpp-plugin-sdk-cpp/ChatPlugin] Rev 22: nick & hub matching

 

------------------------------------------------------------
revno: 22
committer: poy <poy@xxxxxxxxxx>
branch nick: ChatPlugin
timestamp: Sun 2013-05-05 17:10:04 +0200
message:
  nick & hub matching
modified:
  src/GUI.cpp
  src/Plugin.cpp
  src/Plugin.h
  src/Rule.cpp
  src/Rule.h
  src/RuleDlg.cpp


--
lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/ChatPlugin
https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/ChatPlugin

Your team Dcplusplus-team is subscribed to branch lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/ChatPlugin.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/ChatPlugin/+edit-subscription
=== modified file 'src/GUI.cpp'
--- src/GUI.cpp	2013-04-28 20:34:59 +0000
+++ src/GUI.cpp	2013-05-05 15:10:04 +0000
@@ -128,6 +128,8 @@
 
 	{
 		auto cur = grid->addChild(Grid::Seed(1, 5));
+		for(size_t i = 0; i < cur->columnCount(); ++i)
+			cur->column(i).mode = GridInfo::FILL;
 		cur->setSpacing(6);
 
 		cur->addChild(Button::Seed(_T("Add")))->onClicked([this] { add(); });
@@ -163,12 +165,12 @@
 
 		Button::Seed bs(_T("OK"));
 		bs.style |= BS_DEFPUSHBUTTON;
-		bs.padding.x = 20;
+		bs.padding.x = 60;
 		cur->addChild(bs)->onClicked([this] { ok(); });
 
 		bs.caption = _T("Cancel");
 		bs.style &= ~BS_DEFPUSHBUTTON;
-		bs.padding.x = 10;
+		bs.padding.x = 30;
 		cur->addChild(bs)->onClicked([] { window->close(true); });
 	}
 

=== modified file 'src/Plugin.cpp'
--- src/Plugin.cpp	2013-04-28 21:14:38 +0000
+++ src/Plugin.cpp	2013-05-05 15:10:04 +0000
@@ -108,7 +108,7 @@
 
 	// Start the plugin logic here; add hooks with functions from the Hooks interface.
 	loadConfig();
-	Hooks::UI::onChatTags([this](UserDataPtr, TagDataPtr tags, bool&) { return onChatTags(tags); });
+	Hooks::UI::onChatTags([this](UserDataPtr user, TagDataPtr tags, bool&) { return onChatTags(user, tags); });
 	UI::addCommand(commandName, [this] { onConfigure(); }, Config::getInstallPath() + "ChatPlugin.ico");
 
 	return true;
@@ -118,9 +118,11 @@
 	gui.create(rules);
 }
 
-bool Plugin::onChatTags(TagDataPtr tags) {
+bool Plugin::onChatTags(UserDataPtr user, TagDataPtr tags) {
+	string nick(user ? user->nick : "");
+	string hub(user ? user->hubHint : "");
 	for(auto& rule: rules) {
-		rule.match(tags);
+		rule.match(nick, hub, tags);
 	}
 	return false;
 }
@@ -136,6 +138,10 @@
 
 		rule.method = static_cast<Rule::Method>(Config::getIntConfig(conf("Method").c_str()));
 		rule.pattern = Config::getConfig(conf("Pattern").c_str());
+		rule.nickMethod = static_cast<Rule::Method>(Config::getIntConfig(conf("NickMethod").c_str()));
+		rule.nick = Config::getConfig(conf("Nick").c_str());
+		rule.hubMethod = static_cast<Rule::Method>(Config::getIntConfig(conf("HubMethod").c_str()));
+		rule.hub = Config::getConfig(conf("Hub").c_str());
 		rule.part = static_cast<Rule::Part>(Config::getIntConfig(conf("Part").c_str()));
 		if(Config::getBoolConfig(conf("Bold").c_str())) { rule.setFlag(Rule::Bold); }
 		if(Config::getBoolConfig(conf("Italic").c_str())) { rule.setFlag(Rule::Italic); }
@@ -164,6 +170,10 @@
 
 		Config::setConfig(conf("Method").c_str(), rule.method);
 		Config::setConfig(conf("Pattern").c_str(), rule.pattern);
+		Config::setConfig(conf("NickMethod").c_str(), rule.nickMethod);
+		Config::setConfig(conf("Nick").c_str(), rule.nick);
+		Config::setConfig(conf("HubMethod").c_str(), rule.hubMethod);
+		Config::setConfig(conf("Hub").c_str(), rule.hub);
 		Config::setConfig(conf("Part").c_str(), rule.part);
 		Config::setConfig(conf("Bold").c_str(), rule.isSet(Rule::Bold));
 		Config::setConfig(conf("Italic").c_str(), rule.isSet(Rule::Italic));

=== modified file 'src/Plugin.h'
--- src/Plugin.h	2013-04-28 20:34:59 +0000
+++ src/Plugin.h	2013-05-05 15:10:04 +0000
@@ -39,7 +39,7 @@
 
 	bool onLoad(DCCorePtr core, bool install);
 	void onConfigure();
-	bool onChatTags(TagDataPtr tags);
+	bool onChatTags(UserDataPtr user, TagDataPtr tags);
 
 	void loadConfig();
 	void saveConfig();

=== modified file 'src/Rule.cpp'
--- src/Rule.cpp	2013-04-29 19:33:24 +0000
+++ src/Rule.cpp	2013-05-05 15:10:04 +0000
@@ -40,6 +40,8 @@
 Rule::Rule() :
 	dcpp::Flags(),
 	method(CaseSensitive),
+	nickMethod(CaseSensitive),
+	hubMethod(CaseSensitive),
 	part(Matched),
 	textColor(-1),
 	bgColor(-1)
@@ -50,14 +52,22 @@
 	return pattern.empty();
 }
 
-void Rule::refresh() {
-	if(method == RegEx) {
+namespace { template<typename RegexT> void refreshRegex(RegexT& regex, Rule::Method method, const string& pattern) {
+	if(method == Rule::RegEx && !pattern.empty()) {
 		try {
-			regex.assign(pattern);
+			regex = boost::regex(pattern);
 		} catch(const std::runtime_error&) {
 			Logger::log(PLUGIN_NAME ": invalid Regular Expression: " + pattern);
 		}
+	} else {
+		regex.reset();
 	}
+} }
+
+void Rule::refresh() {
+	refreshRegex(regex, method, pattern);
+	refreshRegex(nickRegex, nickMethod, nick);
+	refreshRegex(hubRegex, hubMethod, hub);
 }
 
 namespace {
@@ -102,7 +112,67 @@
 
 } // unnamed namespace
 
-void Rule::match(TagDataPtr tags) {
+void Rule::match(const string& messageNick, const string& messageHub, TagDataPtr tags) {
+	if(!nick.empty()) {
+		if(messageNick.empty())
+			return;
+		switch(nickMethod) {
+		case CaseSensitive:
+			{
+				if(messageNick != nick)
+					return;
+				break;
+			}
+		case CaseInsensitive:
+			{
+				if(boost::to_lower_copy(messageNick) != boost::to_lower_copy(nick))
+					return;
+				break;
+			}
+		case RegEx:
+			{
+				try {
+					if(!boost::regex_search(messageNick.cbegin(), messageNick.cend(), nickRegex.get()))
+						return;
+				} catch(const std::runtime_error&) {
+					// most likely a stack overflow
+					return;
+				}
+				break;
+			}
+		}
+	}
+
+	if(!hub.empty()) {
+		if(messageHub.empty())
+			return;
+		switch(hubMethod) {
+		case CaseSensitive:
+			{
+				if(messageHub != hub)
+					return;
+				break;
+			}
+		case CaseInsensitive:
+			{
+				if(boost::to_lower_copy(messageHub) != boost::to_lower_copy(hub))
+					return;
+				break;
+			}
+		case RegEx:
+			{
+				try {
+					if(!boost::regex_search(messageHub.cbegin(), messageHub.cend(), hubRegex.get()))
+						return;
+				} catch(const std::runtime_error&) {
+					// most likely a stack overflow
+					return;
+				}
+				break;
+			}
+		}
+	}
+
 	string text(Tagger::handle()->get_text(tags));
 
 	auto pattern = this->pattern;
@@ -120,7 +190,7 @@
 		if(method == RegEx) {
 			boost::match_results<string::const_iterator> regex_match;
 			try {
-				if(!boost::regex_search(text.cbegin() + end, text.cend(), regex_match, regex))
+				if(!boost::regex_search(text.cbegin() + end, text.cend(), regex_match, regex.get()))
 					break;
 			} catch(const std::runtime_error&) {
 				// most likely a stack overflow

=== modified file 'src/Rule.h'
--- src/Rule.h	2013-04-28 21:14:38 +0000
+++ src/Rule.h	2013-05-05 15:10:04 +0000
@@ -19,6 +19,7 @@
 #ifndef PLUGIN_RULE_H
 #define PLUGIN_RULE_H
 
+#include <boost/optional.hpp>
 #include <boost/regex.hpp>
 
 #include "Flags.h"
@@ -28,7 +29,13 @@
 
 	bool empty() const;
 	void refresh();
-	void match(TagDataPtr tags);
+	void match(const string& messageNick, const string& messageHub, TagDataPtr tags);
+
+	enum Method {
+		CaseSensitive,
+		CaseInsensitive,
+		RegEx
+	};
 
 	enum {
 		Bold = 1 << 0,
@@ -36,14 +43,15 @@
 		Underline = 1 << 2
 	};
 
-	enum Method {
-		CaseSensitive,
-		CaseInsensitive,
-		RegEx
-	} method;
-
+	Method method;
 	string pattern;
 
+	Method nickMethod;
+	string nick;
+
+	Method hubMethod;
+	string hub;
+
 	enum Part {
 		Matched,
 		Word,
@@ -60,7 +68,9 @@
 	string notification;
 
 private:
-	boost::regex regex;
+	boost::optional<boost::regex> regex;
+	boost::optional<boost::regex> nickRegex;
+	boost::optional<boost::regex> hubRegex;
 };
 
 #endif

=== modified file 'src/RuleDlg.cpp'
--- src/RuleDlg.cpp	2013-05-05 13:48:21 +0000
+++ src/RuleDlg.cpp	2013-05-05 15:10:04 +0000
@@ -54,7 +54,7 @@
 }
 
 int RuleDlg::run() {
-	create(Seed(dwt::Point(600, 500)));
+	create(Seed(dwt::Point(700, 600)));
 	return show();
 }
 
@@ -62,8 +62,18 @@
 	return move(rule);
 }
 
+namespace { dwt::ComboBoxPtr methodCombo(dwt::GridPtr parent) {
+	ComboBox::Seed cs { };
+	cs.style |= CBS_DROPDOWNLIST;
+	auto combo = parent->addChild(cs);
+	combo->addValue(_T("Case-sensitive"));
+	combo->addValue(_T("Case-insensitive"));
+	combo->addValue(_T("Regular Expression"));
+	return combo;
+} }
+
 bool RuleDlg::handleInitDialog() {
-	grid = addChild(Grid::Seed(10, 1));
+	grid = addChild(Grid::Seed(12, 1));
 	grid->column(0).mode = GridInfo::FILL;
 	grid->setSpacing(6);
 
@@ -77,17 +87,42 @@
 		auto box = cur->addChild(ts);
 		box->onUpdated([this, box] { rule.pattern = Util::fromT(box->getText()); });
 
-		ComboBox::Seed cs { };
-		cs.style |= CBS_DROPDOWNLIST;
-		auto combo = cur->addChild(cs);
-		combo->addValue(_T("Case-sensitive"));
-		combo->addValue(_T("Case-insensitive"));
-		combo->addValue(_T("Regular Expression"));
+		auto combo = methodCombo(cur);
 		combo->setSelected(rule.method);
 		combo->onSelectionChanged([this, combo] { rule.method = static_cast<Rule::Method>(combo->getSelected()); });
 	}
 
 	{
+		auto cur = grid->addChild(GroupBox::Seed(_T("Only apply to users whose nick matches (empty = all users):")))->addChild(Grid::Seed(1, 2));
+		cur->column(0).mode = GridInfo::FILL;
+		cur->setSpacing(grid->getSpacing());
+
+		TextBox::Seed ts(Util::toT(rule.nick));
+		ts.style |= ES_AUTOHSCROLL;
+		auto box = cur->addChild(ts);
+		box->onUpdated([this, box] { rule.nick = Util::fromT(box->getText()); });
+
+		auto combo = methodCombo(cur);
+		combo->setSelected(rule.nickMethod);
+		combo->onSelectionChanged([this, combo] { rule.nickMethod = static_cast<Rule::Method>(combo->getSelected()); });
+	}
+
+	{
+		auto cur = grid->addChild(GroupBox::Seed(_T("Only apply to hubs whose address matches (empty = all users):")))->addChild(Grid::Seed(1, 2));
+		cur->column(0).mode = GridInfo::FILL;
+		cur->setSpacing(grid->getSpacing());
+
+		TextBox::Seed ts(Util::toT(rule.hub));
+		ts.style |= ES_AUTOHSCROLL;
+		auto box = cur->addChild(ts);
+		box->onUpdated([this, box] { rule.hub = Util::fromT(box->getText()); });
+
+		auto combo = methodCombo(cur);
+		combo->setSelected(rule.hubMethod);
+		combo->onSelectionChanged([this, combo] { rule.hubMethod = static_cast<Rule::Method>(combo->getSelected()); });
+	}
+
+	{
 		auto cur = grid->addChild(Grid::Seed(1, 2));
 		cur->setSpacing(grid->getSpacing());