|
@@ -34,7 +34,7 @@ peers_repo = None
|
|
|
|
|
|
nodeaccess = None
|
|
|
|
|
|
-alfred_method = None
|
|
|
+__batcave = None
|
|
|
|
|
|
ffpb_resolver = dns.resolver.Resolver()
|
|
|
ffpb_resolver.nameservers = ['10.132.254.53']
|
|
@@ -88,7 +88,7 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
|
|
def setup(bot):
|
|
|
"""Called by willie upon loading this plugin."""
|
|
|
|
|
|
- global msgserver, peers_repo, alfred_method, nodeaccess
|
|
|
+ global __batcave, msgserver, peers_repo, nodeaccess
|
|
|
|
|
|
# signal begin of setup routine
|
|
|
bot.memory['ffpb_in_setup'] = True
|
|
@@ -128,13 +128,8 @@ def setup(bot):
|
|
|
msgserver_thread.daemon = True
|
|
|
msgserver_thread.start()
|
|
|
|
|
|
- # initially fetch ALFRED data
|
|
|
- alfred_method = bot.config.ffpb.alfred_method
|
|
|
- if not 'alfred_data' in bot.memory:
|
|
|
- bot.memory['alfred_data'] = {}
|
|
|
- if not 'alfred_update' in bot.memory:
|
|
|
- bot.memory['alfred_update'] = datetime(1970, 1, 1, 23, 42)
|
|
|
- ffpb_updatealfred(bot)
|
|
|
+ # initialize BATCAVE
|
|
|
+ __batcave = BatcaveClient(bot.config.ffpb.batcave_url)
|
|
|
|
|
|
# signal end of setup routine
|
|
|
bot.memory['ffpb_in_setup'] = False
|
|
@@ -375,7 +370,7 @@ def ffpb_ensurenodeid(nodedata):
|
|
|
return result
|
|
|
|
|
|
|
|
|
-def ffpb_findnode(name, alfred_data=None, allow_fuzzymatching=True):
|
|
|
+def ffpb_findnode(name, allow_fuzzymatching=True):
|
|
|
"""helper: try to identify the node the user meant by the given name"""
|
|
|
|
|
|
# no name, no node
|
|
@@ -390,27 +385,16 @@ def ffpb_findnode(name, alfred_data=None, allow_fuzzymatching=True):
|
|
|
name = name[1:-1]
|
|
|
allow_fuzzymatching = False
|
|
|
|
|
|
- names = {}
|
|
|
-
|
|
|
- if not alfred_data is None:
|
|
|
- # try to match MAC
|
|
|
- m = re.search("^([0-9a-fA-F][0-9a-fA-F]:){5}[0-9a-fA-F][0-9a-fA-F]$", name)
|
|
|
- if not m is None:
|
|
|
- mac = m.group(0).lower()
|
|
|
- if mac in alfred_data:
|
|
|
- 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 node["network"].get("mac", "").lower() == mac:
|
|
|
- return ffpb_ensurenodeid(node)
|
|
|
- if "mesh_interfaces" in node["network"]:
|
|
|
- for mim in node["network"]["mesh_interfaces"]:
|
|
|
- if mim.lower() == mac:
|
|
|
- return ffpb_ensurenodeid(node)
|
|
|
+ # try to match MAC
|
|
|
+ m = re.search("^([0-9a-fA-F][0-9a-fA-F]:){5}[0-9a-fA-F][0-9a-fA-F]$", name)
|
|
|
+ if not m is None:
|
|
|
+ mac = m.group(0).lower()
|
|
|
+ node = __batcave.find_node_by_mac(mac)
|
|
|
|
|
|
+ if node is not None:
|
|
|
+ return node
|
|
|
+
|
|
|
+ else:
|
|
|
nodeid = mac.replace(':', '').lower()
|
|
|
return {
|
|
|
'nodeid': nodeid,
|
|
@@ -424,15 +408,10 @@ def ffpb_findnode(name, alfred_data=None, allow_fuzzymatching=True):
|
|
|
},
|
|
|
}
|
|
|
|
|
|
- # look through the ALFRED peers
|
|
|
- for nodeid in alfred_data:
|
|
|
- node = alfred_data[nodeid]
|
|
|
- if 'hostname' in node:
|
|
|
- h = node['hostname']
|
|
|
- if h.lower() == name.lower():
|
|
|
- return node
|
|
|
- else:
|
|
|
- names[h] = nodeid
|
|
|
+ # try to find by NAME
|
|
|
+ node = __batcave.find_node_by_name(name, fuzzymatch=allow_fuzzymatching)
|
|
|
+ if node is not None:
|
|
|
+ return node
|
|
|
|
|
|
# not found in ALFRED data -> try peers_repo
|
|
|
if not peers_repo is None:
|
|
@@ -466,18 +445,6 @@ def ffpb_findnode(name, alfred_data=None, allow_fuzzymatching=True):
|
|
|
},
|
|
|
}
|
|
|
|
|
|
- # do a similar name lookup in the ALFRED data
|
|
|
- if allow_fuzzymatching and not alfred_data is None:
|
|
|
- allnames = [x for x in names]
|
|
|
- possibilities = difflib.get_close_matches(name, allnames, cutoff=0.75)
|
|
|
- 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 ffpb_ensurenodeid(alfred_data[names[possibilities[0]]])
|
|
|
-
|
|
|
# none of the above was able to identify the requested node
|
|
|
return None
|
|
|
|
|
@@ -490,14 +457,7 @@ def ffpb_findnode_from_botparam(bot, name, ensure_recent_alfreddata=True):
|
|
|
bot.reply("Grün.")
|
|
|
return None
|
|
|
|
|
|
- alfred_data = get_alfred_data(bot, ensure_recent_alfreddata)
|
|
|
- if ensure_recent_alfreddata and alfred_data is None:
|
|
|
- if not bot is None:
|
|
|
- bot.say('Informationen sind ausverkauft bzw. veraltet, ' +
|
|
|
- 'daher sage ich mal lieber nichts zu \'' + name + '\'.')
|
|
|
- return None
|
|
|
-
|
|
|
- node = ffpb_findnode(name, alfred_data)
|
|
|
+ node = ffpb_findnode(name)
|
|
|
if node is None:
|
|
|
if not bot is None:
|
|
|
bot.say("Kein Plan wer oder was mit '" + name + "' gemeint ist :(")
|
|
@@ -600,47 +560,6 @@ def ffpb_updatealfred(bot):
|
|
|
bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
|
|
|
|
|
|
|
|
|
-def get_alfred_data(bot, ensure_not_outdated=True):
|
|
|
- """
|
|
|
- Retrieves the stored alfred_data and optionally checks
|
|
|
- that it has been updated no more than 5 minutes ago.
|
|
|
- """
|
|
|
-
|
|
|
- alfred_data = bot.memory.get('alfred_data', None)
|
|
|
- alfred_update = bot.memory.get('alfred_update', 0)
|
|
|
-
|
|
|
- if alfred_data is None:
|
|
|
- return None
|
|
|
-
|
|
|
- if ensure_not_outdated:
|
|
|
- timeout = datetime.now() - timedelta(minutes=5)
|
|
|
- is_outdated = timeout > alfred_update
|
|
|
- if is_outdated:
|
|
|
- return None
|
|
|
-
|
|
|
- return alfred_data
|
|
|
-
|
|
|
-
|
|
|
-def ffpb_get_batcave_nodefield(nodeid, field):
|
|
|
- """Query the given field for the given nodeid from the BATCAVE."""
|
|
|
-
|
|
|
- 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 urllib2.URLError as err:
|
|
|
- print('Failed to contact BATCAVE for \'{0}\'->\'{1}\': {2}'.format(
|
|
|
- nodeid, field, err))
|
|
|
- return None
|
|
|
-
|
|
|
- try:
|
|
|
- return json.load(raw_data)
|
|
|
-
|
|
|
- except ValueError as err:
|
|
|
- print('Could not parse BATCAVE\'s response as JSON for \'{0}\'->\'{1}\':'.format(nodeid, field, err))
|
|
|
- return None
|
|
|
-
|
|
|
-
|
|
|
@willie.module.commands('debug-alfred')
|
|
|
def ffpb_debug_alfred(bot, trigger):
|
|
|
"""Show statistics of available ALFRED data."""
|
|
@@ -839,7 +758,7 @@ def ffpb_nodemesh(bot, trigger):
|
|
|
return
|
|
|
|
|
|
# query BATCAVE for node's neighbours (result is a list of MAC addresses)
|
|
|
- cave_result = ffpb_get_batcave_nodefield(nodeid, 'neighbours')
|
|
|
+ cave_result = node['neighbours']
|
|
|
if cave_result is None:
|
|
|
msg = 'Hm, scheinbar liegen zu \'{0}\' keine Daten vor. ' + \
|
|
|
'Klingt komisch, ist aber so.'
|