Browse Source

ffpb: show client count in \!info

Helge Jung 9 years ago
parent
commit
337917b3dd
1 changed files with 28 additions and 3 deletions
  1. 28 3
      modules/ffpb.py

+ 28 - 3
modules/ffpb.py

@@ -368,6 +368,22 @@ def ffpb_alfred_data_outdated():
 	#print("ALFRED outdated? {0} (timeout={1} vs. lastupdate={2})".format(is_outdated, timeout, alfred_update))
 	return is_outdated
 
+def ffpb_get_batcave_nodefield(nodeid, field):
+	raw_data = None
+	try:
+	        # query BATCAVE for node's field
+		raw_data = urllib2.urlopen('http://[fdca:ffee:ff12:a255::253]:8888/node/{0}/{1}'.format(nodeid, field))
+	except Exception as err:
+		print('Failed to contact BATCAVE for \'{0}\'->\'{1}\': {2}'.format(nodeid, field, err))
+		return None
+
+	try:
+        	return json.load(raw_data)
+
+	except:
+		print('Could not parse BATCAVE\'s response as JSON for \'{0}\'->\'{1}\''.format(nodeid, field))
+		return None
+
 @willie.module.commands('debug-alfred')
 def ffpb_debug_alfred(bot, trigger):
 	"""Show statistics of available ALFRED data."""
@@ -412,7 +428,8 @@ def ffpb_peerinfo(bot, trigger):
 
 	# read node information
 	info_mac = node['network']['mac'] if 'network' in node and 'mac' in node['network'] else '??:??:??:??:??:??'
-	info_name = node['hostname'] if 'hostname' in node else '?-' + (node['node_id'] if 'node_id' in node else info_mac.replace(':',''))
+	info_id = node['node_id'] if 'node_id' in node else info_mac.replace(':','')
+	info_name = node['hostname'] if 'hostname' in node else '?-' + info_id
 
 	info_hw = ""
 	if "hardware" in node:
@@ -449,7 +466,15 @@ def ffpb_peerinfo(bot, trigger):
 		else:
 			info_uptime = ' up {0}m'.format(m)
 
-	bot.say('[{1}]{2}{3}{4}{5}'.format(info_mac, info_name, info_hw, info_fw, info_update, info_uptime))
+	info_clients = ""
+	clientcount = ffpb_get_batcave_nodefield(info_id, 'clients.count')
+	if not clientcount is None:
+		clientcount = int(clientcount) -1
+		if clientcount == 0: info_clients = ' keine Clients'
+		elif clientcount == 1: info_clients = ' 1 Client'
+		else: info_clients = ' {0} Clients'.format(clientcount)
+
+	bot.say('[{1}]{2}{3}{4}{5}{6}'.format(info_mac, info_name, info_hw, info_fw, info_update, info_uptime, info_clients))
 
 @willie.module.commands('uptime')
 def ffpb_peeruptime(bot, trigger):
@@ -881,7 +906,7 @@ def ffpb_nodemesh(bot, trigger):
 		return
 
 	# query BATCAVE for node's neighbours (result is a list of MAC addresses)
-	cave_result = json.load(urllib2.urlopen('http://[fdca:ffee:ff12:a255::253]:8888/node/{0}/neighbours'.format(nodeid)))
+	cave_result = ffpb_get_batcave_nodefield(nodeid, 'neighbours')
 
 	# query BATCAVE for neighbour's names
 	d = '&'.join([ str(n) for n in cave_result ])