Browse Source

store seen_nodes and notify new ones in msg_target channel

Helge Jung 9 years ago
parent
commit
3a46ecbea9
2 changed files with 28 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 27 0
      modules/ffpb.py

+ 1 - 0
.gitignore

@@ -7,3 +7,4 @@ logs/
 alfred.json
 highscoredata
 monitorednodes
+nodes.seen

+ 27 - 0
modules/ffpb.py

@@ -82,6 +82,9 @@ def setup(bot):
 
 	monitored_nodes = shelve.open('monitorednodes', writeback=True)
 
+	seen_nodes = shelve.open('nodes.seen', writeback=True)
+	bot.memory['seen_nodes'] = seen_nodes
+
 	if not bot.config.has_section('ffpb'):
 		bot.memory['ffpb_in_setup'] = False
 		return
@@ -123,6 +126,11 @@ def shutdown(bot):
 		monitored_nodes.close()
 		monitored_nodes = None
 
+	if 'seen_nodes' in bot.memory and bot.memory['seen_nodes'] != None:
+		bot.memory['seen_nodes'].close()
+		bot.memory['seen_nodes'] = None
+		del(bot.memory['seen_nodes'])
+
 	if not msgserver is None:
 		msgserver.shutdown()
 		print("Closed messaging server.")
@@ -289,6 +297,25 @@ def ffpb_updatealfred(bot):
 		print("Failed to parse ALFRED data: " + str(e))
 		return
 
+	seen_nodes = bot.memory['seen_nodes'] if 'seen_nodes' in bot.memory else None
+	if not seen_nodes is None:
+		new = []
+		for nodeid in alfred_data:
+			nodeid = str(nodeid)
+			if not nodeid in seen_nodes:
+				seen_nodes[nodeid] = updated
+				new.append((nodeid,alfred_data[nodeid]['hostname']))
+				print('First time seen: ' + str(nodeid))
+		if len(new) > 0 and not bot.memory['ffpb_in_setup']:
+			action_msg = 'bemerkt '
+			if len(new) == 1:
+				action_msg += 'den neuen Knoten \'' + str(new[0][1]) + '\''
+			else:
+				action_msg += 'die neuen Knoten \'' + '\', \''.join([ str(x[1]) for x in new[0:-1] ]) + '\' und \'' + str(new[-1][1]) + '\''
+			action_target = bot.config.ffpb.msg_target
+			bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
+
+
 def ffpb_alfred_data_outdated():
 	timeout = datetime.datetime.now() - datetime.timedelta(minutes=5)
 	is_outdated = timeout > alfred_update