# -*- coding: utf-8 -*- from __future__ import print_function import willie from ffpb import ffpb_findnode_from_botparam, ffpb_get_batcave_nodefield, mac2ipv6, playitsafe def setup(bot): pass def shutdown(bot): pass @willie.module.commands('alfred-data') def ffpb_peerdata(bot, trigger): """Show ALFRED data of the given node.""" # identify node or bail out target_name = trigger.group(2) node = ffpb_findnode_from_botparam(bot, target_name) if node is None: return # query must be a PM or as OP in the channel if not playitsafe(bot, trigger, via_privmsg=True, node=node): # the check function already gives a bot reply, just exit here return # reply each key in the node's data for key in node: if key in [ 'hostname' ]: continue bot.say("{0}.{1} = {2}".format(node['hostname'], key, str(node[key]))) @willie.module.commands('info') def ffpb_peerinfo(bot, trigger): """Show information of the given node.""" # identify node or bail out target_name = trigger.group(2) node = ffpb_findnode_from_botparam(bot, target_name) if node is None: return # read node information info_mac = node['network']['mac'] if 'network' in node and 'mac' in node['network'] else '??:??:??:??:??:??' 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: if "model" in node["hardware"]: model = node["hardware"]["model"] info_hw = " model='" + model + "'" info_fw = "" info_update = "" if "software" in node: if "firmware" in node["software"]: fwinfo = str(node["software"]["firmware"]["release"]) if "release" in node["software"]["firmware"] else "unknown" info_fw = " firmware=" + fwinfo if "autoupdater" in node["software"]: autoupdater = node["software"]["autoupdater"]["branch"] if node["software"]["autoupdater"]["enabled"] else "off" info_update = " (autoupdater="+autoupdater+")" info_uptime = "" u = -1 if "statistics" in node and "uptime" in node["statistics"]: u = int(float(node["statistics"]["uptime"])) elif 'uptime' in node: u = int(float(node['uptime'])) if u > 0: d, r1 = divmod(u, 86400) h, r2 = divmod(r1, 3600) m, s = divmod(r2, 60) if d > 0: info_uptime = ' up {0}d {1}h'.format(d,h) elif h > 0: info_uptime = ' up {0}h {1}m'.format(h,m) else: info_uptime = ' up {0}m'.format(m) info_clients = "" clientcount = ffpb_get_batcave_nodefield(info_id, 'clientcount') if not clientcount is None: clientcount = int(clientcount) info_clients = ' clients={0}'.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): """Display the uptime of the given node.""" # identify node or bail out target_name = trigger.group(2) node = ffpb_findnode_from_botparam(bot, target_name) if node is None: return # get name and raw uptime from node info_name = node["hostname"] info_uptime = '' u_raw = None if 'statistics' in node and 'uptime' in node['statistics']: u_raw = node['statistics']['uptime'] elif 'uptime' in node: u_raw = node['uptime'] # pretty print uptime if not u_raw is None: u = int(float(u_raw)) d, r1 = divmod(u, 86400) h, r2 = divmod(r1, 3600) m, s = divmod(r2, 60) if d > 0: info_uptime += '{0}d '.format(d) if h > 0: info_uptime += '{0}h '.format(h) info_uptime += '{0}m'.format(m) info_uptime += ' # raw: \'{0}\''.format(u_raw) else: info_uptime += '?' # reply to user bot.say('uptime(\'{0}\') = {1}'.format(info_name, info_uptime)) @willie.module.commands('link') def ffpb_peerlink(bot, trigger): """Display MAC and link to statuspage for the given node.""" # identify node or bail out target_name = trigger.group(2) node = ffpb_findnode_from_botparam(bot, target_name) if node is None: return # get node's MAC info_mac = node["network"]["mac"] info_name = node["hostname"] # get node's v6 address in the mesh (derived from MAC address) info_v6 = mac2ipv6(info_mac, 'fdca:ffee:ff12:132:') # reply to user bot.say('[{1}] mac {0} -> http://[{2}]/'.format(info_mac, info_name, info_v6))