# -*- coding: utf-8 -*- from __future__ import print_function import time import willie from ffpb import \ ffpb_findnode_from_botparam, \ ffpb_get_batcave_nodefield, \ mac2ipv6, playitsafe, pretty_date def setup(bot): """Called by willie upon loading this plugin.""" pass def shutdown(bot): """Called by willie upon unloading this plugin.""" 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: # skip some fields if key in ['hostname']: continue bot.say("{0}.{1} = {2}".format(node['hostname'], key, 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"]: if "release" in node["software"]["firmware"]: info_fw = " firmware=" + str(node["software"]["firmware"]["release"]) else: info_fw = " unknown firmware" if "autoupdater" in node["software"]: autoupdater = node["software"]["autoupdater"]["branch"] if node["software"]["autoupdater"]["enabled"] else "off" info_update = " (autoupdater="+autoupdater+")" info_uptime = "" uptime = -1 if "statistics" in node and "uptime" in node["statistics"]: uptime = int(float(node["statistics"]["uptime"])) elif 'uptime' in node: uptime = int(float(node['uptime'])) if uptime > 0: days, rem_d = divmod(uptime, 86400) hours, rem_h = divmod(rem_d, 3600) minutes, _ = divmod(rem_h, 60) if days > 0: info_uptime = ' up {0}d {1}h'.format(days, hours) elif hours > 0: info_uptime = ' up {0}h {1}m'.format(hours, minutes) else: info_uptime = ' up {0}m'.format(minutes) 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('last-seen') @willie.module.commands('last_seen') @willie.module.commands('lastseen') def ffpb_lastseen(bot, trigger): """Display when the given node has last been seen.""" # identify node or bail out target_name = trigger.group(2) node = ffpb_findnode_from_botparam(bot, target_name) if node is None: return last_seen = ffpb_get_batcave_nodefield(node['node_id'], '__UPDATED__') a_value = int(last_seen['alfred']) if (not last_seen is None) and 'alfred' in last_seen else None a_delta = time.time() - a_value if not a_value is None else None b_value = int(last_seen['batadv']) if (not last_seen is None) and 'batadv' in last_seen else None b_delta = time.time() - b_value if not b_value is None else None if a_value is None and b_value is None: bot.say('{0} wurde offenbar noch gar nicht gesehen?'.format(node['hostname'])) return if a_delta < 30 and b_delta < 30: bot.say('{0} wurde gerade eben gesehen.'.format(node['hostname'])) return if a_value is not None and b_value is not None and abs(a_value - b_value) < 60: bot.say('{0} wurde zuletzt gesehen: {1}'.format( node['hostname'], pretty_date((a_value + b_value) / 2))) else: bot.say('{0} wurde zuletzt gesehen: {1} (ALFRED,) bzw. {2} (BATMAN)'.format( node['hostname'], pretty_date(a_value) if not a_value is None else "nie", pretty_date(b_value) if not b_value is None else "nie" )) @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: uptime = int(float(u_raw)) days, rem_d = divmod(uptime, 86400) hours, rem_h = divmod(rem_d, 3600) minutes, _ = divmod(rem_h, 60) if days > 0: info_uptime += '{0}d '.format(days) if hours > 0: info_uptime += '{0}h '.format(hours) info_uptime += '{0}m'.format(minutes) 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))