Browse Source

ffpb_findnode() always has 'node_id' set in its response

Helge Jung 9 years ago
parent
commit
f591feb4e7
1 changed files with 23 additions and 7 deletions
  1. 23 7
      modules/ffpb.py

+ 23 - 7
modules/ffpb.py

@@ -335,6 +335,19 @@ def ffpb_nodeacl(bot, trigger):
 
 	bot.say('Unbekanntes Kommando. Probier "list [user]", "add user value" oder "del user value". Value kann node_id oder hostname-Maske sein.')
 
+def ffpb_ensurenodeid(nodedata):
+	if 'node_id' in nodedata:
+		return nodedata
+
+	# derive node's id
+	nodeid = node['network']['mac'].replace(':','') if 'network' in node and 'mac' in node['network'] else None
+
+	# assemble extended data
+	result = { 'node_id': nodeid }
+	for key in nodedata: result[key] = nodedata[key]
+
+	return result
+
 def ffpb_findnode(name):
 	"""helper: try to identify the node the user meant by the given name"""
 
@@ -351,21 +364,23 @@ def ffpb_findnode(name):
 	if (not m is None):
 		mac = m.group(0).lower()
 		if mac in alfred_data:
-			return alfred_data[mac]
+			return ffpb_ensurenodeid(alfred_data[mac])
 
 		# try to find alias MAC in ALFRED data
 		for nodeid in alfred_data:
 			node = alfred_data[nodeid]
 			if "network" in node:
 				if "mac" in node["network"] and node["network"]["mac"].lower() == mac:
-					return node
+					return ffpb_ensurenodeid(node)
 				if "mesh_interfaces" in node["network"]:
 					for mim in node["network"]["mesh_interfaces"]:
 						if mim.lower() == mac:
-							return node
+							return ffpb_ensurenodeid(node)
 
+		nodeid = mac.replace(':','').lower()
 		return {
-			'hostname': '?-' + mac.replace(':','').lower(),
+			'nodeid': nodeid,
+			'hostname': '?-' + nodeid,
 			'network': { 'addresses': [ mac2ipv6(mac, 'fdca:ffee:ff12:132:') ], 'mac': mac, },
 			'hardware': { 'model': 'derived-from-mac' },
 		}
@@ -401,6 +416,7 @@ def ffpb_findnode(name):
 
 		if not (peer_mac is None):
 			return {
+				'node_id': peer_mac.replace(':', ''),
 				'hostname': peer_name,
 				'network': { 'addresses': [ mac2ipv6(peer_mac, 'fdca:ffee:ff12:132:') ], 'mac': peer_mac },
 				'hardware': { 'model': 'derived-from-vpnkeys' },
@@ -411,7 +427,7 @@ def ffpb_findnode(name):
 	print('findnode: Fuzzy matching \'{0}\' got {1} entries: {2}'.format(name, len(possibilities), ', '.join(possibilities)))
 	if len(possibilities) == 1:
 		# if we got exactly one candidate that might be it
-		return alfred_data[names[possibilities[0]]]
+		return ffpb_ensurenodeid(alfred_data[names[possibilities[0]]])
 
 	# none of the above was able to identify the requested node
 	return None
@@ -1044,9 +1060,9 @@ def ffpb_nodemesh(bot, trigger):
 
 	# derive node's id
 	nodeid = node['node_id'] if 'node_id' in node else None
-	if nodeid is None: nodeid = node['network']['mac'].replace(':','') if 'network' in node and 'mac' in node['network'] else None
 	if nodeid is None:
-		bot.say('Mist, ich habe gerade den Zettel verlegt auf dem die Node-ID von \'{0}\' steht, bitte frag später noch einmal.'.format(node['hostname'] if 'hostname' in node else target_name))
+		msg = 'Mist, ich habe gerade den Zettel verlegt auf dem die Node-ID von \'{0}\' steht, bitte frag später noch einmal.'
+		bot.say(msg.format(node['hostname'] if 'hostname' in node else target_name))
 		return
 
 	# query BATCAVE for node's neighbours (result is a list of MAC addresses)