← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcpp-plugin-sdk-cpp/DescriptionRotator] Rev 33: NMDC

 

------------------------------------------------------------
revno: 33
committer: poy <poy@xxxxxxxxxx>
branch nick: DescriptionRotator
timestamp: Sat 2013-06-08 14:24:29 +0200
message:
  NMDC
modified:
  src/Plugin.cpp
  src/Plugin.h


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

Your team Dcplusplus-team is subscribed to branch lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/DescriptionRotator.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/DescriptionRotator/+edit-subscription
=== modified file 'src/Plugin.cpp'
--- src/Plugin.cpp	2013-06-08 11:49:39 +0000
+++ src/Plugin.cpp	2013-06-08 12:24:29 +0000
@@ -124,10 +124,24 @@
 	gui.create(rules);
 }
 
+namespace { std::pair<size_t, size_t> nmdcDescr(const string& cmd) {
+	if(cmd.size() > 20) {
+		auto space = cmd.find(' ', 13);
+		if(space != string::npos) {
+			auto dollar = cmd.find('$', space);
+			if(dollar != string::npos) {
+				auto tag = cmd.rfind('<', dollar);
+				return std::make_pair(space + 1, tag == string::npos ? dollar : tag);
+			}
+		}
+	}
+	return std::make_pair(0, 0);
+} }
+
 bool Plugin::onMinute(uint64_t tick) {
 	Lock lock(mutex);
 
-	for(auto& hub: hubs_adc) {
+	for(auto& hub: hubs) {
 
 		if(hub.second.next && hub.second.next < tick) {
 			continue;
@@ -138,6 +152,14 @@
 		hub.second.next = 0;
 
 		auto description = send ? hub.second.description : string();
+		if(!hub.second.sid && !hub.second.description.empty()) {
+			auto pos = nmdcDescr(description);
+			if(pos.first) {
+				description = description.substr(pos.first, pos.second - pos.first);
+			} else {
+				continue;
+			}
+		}
 
 		for(auto& rule: rules) {
 			if(rule.match(hub.first, description)) {
@@ -150,10 +172,23 @@
 
 			if(send) {
 				hub.second.sending = true;
-				Hubs::handle()->send_protocol_cmd(hub.second.hub,
-					AdcCommand(AdcCommand::CMD_INF, AdcCommand::TYPE_BROADCAST)
-					.addParam("DE", description)
-					.toString(hub.second.sid).c_str());
+
+				if(hub.second.sid) {
+					Hubs::handle()->send_protocol_cmd(hub.second.hub,
+						AdcCommand(AdcCommand::CMD_INF, AdcCommand::TYPE_BROADCAST)
+						.addParam("DE", description)
+						.toString(hub.second.sid).c_str());
+
+				} else {
+					auto cmd = hub.second.description;
+					auto pos = nmdcDescr(cmd);
+					if(pos.first) {
+						cmd.erase(pos.first, pos.second - pos.first);
+						cmd.insert(pos.first, description);
+						Hubs::handle()->send_protocol_cmd(hub.second.hub, cmd.c_str());
+					}
+				}
+
 				hub.second.sending = false;
 			}
 		}
@@ -163,24 +198,20 @@
 }
 
 bool Plugin::onHubOffline(HubDataPtr hub) {
-	Lock lock(mutex);
-	switch(hub->protocol) {
-	case PROTOCOL_ADC: hubs_adc.erase(hub->url); break;
-	case PROTOCOL_NMDC: hubs_nmdc.erase(hub->url); break;
-	}
+	hubs.erase(hub->url);
 	return false;
 }
 
 bool Plugin::onHubDataOut(HubDataPtr hub, char* message) {
+	{
+		Lock lock(mutex);
+		auto i = hubs.find(hub->url);
+		if(i != hubs.end() && i->second.sending) {
+			return false;
+		}
+	}
+
 	if(hub->protocol == PROTOCOL_ADC) {
-		{
-			Lock lock(mutex);
-			auto i = hubs_adc.find(hub->url);
-			if(i != hubs_adc.end() && i->second.sending) {
-				return false;
-			}
-		}
-
 		// AdcCommand doesn't like the terminating \n...
 		string msg = message;
 		if(msg.empty()) { return false; }
@@ -202,7 +233,7 @@
 					cmd.addParam("DE", param);
 
 					Lock lock(mutex);
-					auto& info = hubs_adc[hub->url];
+					auto& info = hubs[hub->url];
 					info.hub = hub;
 					info.sid = cmd.getFrom();
 					info.description = initial_desc;
@@ -217,7 +248,35 @@
 		}
 
 	} else if(hub->protocol == PROTOCOL_NMDC) {
-		// TODO fuck NMDC?
+		string cmd(message);
+
+		if(cmd.substr(0, 7) == "$MyINFO") {
+			auto pos = nmdcDescr(cmd);
+			if(pos.first) {
+				auto param = cmd.substr(pos.first, pos.second - pos.first);
+
+				auto matched = false;
+				for(auto& rule: rules) {
+					matched |= rule.match(hub->url, param);
+				}
+				if(matched) {
+					cmd.erase(pos.first, pos.second - pos.first);
+					cmd.insert(pos.first, param);
+
+					Lock lock(mutex);
+					auto& info = hubs[hub->url];
+					info.hub = hub;
+					info.sid = 0;
+					info.description = cmd;
+					info.next = 0;
+
+					info.sending = true;
+					Hubs::handle()->send_protocol_cmd(hub, cmd.c_str());
+					info.sending = false;
+					return true;
+				}
+			}
+		}
 	}
 	return false;
 }

=== modified file 'src/Plugin.h'
--- src/Plugin.h	2013-06-08 11:49:39 +0000
+++ src/Plugin.h	2013-06-08 12:24:29 +0000
@@ -68,8 +68,7 @@
 		string description;
 		uint64_t next;
 	};
-	unordered_map<string, HubInfo> hubs_adc;
-	unordered_map<string, HubInfo> hubs_nmdc;
+	unordered_map<string, HubInfo> hubs;
 
 	mutable std::recursive_mutex mutex;
 	typedef std::unique_lock<std::recursive_mutex> Lock;