ffpb.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import willie
  4. from datetime import datetime, timedelta
  5. import difflib
  6. from email.utils import mktime_tz
  7. from fnmatch import fnmatch
  8. import git
  9. import netaddr
  10. import json
  11. import urllib2
  12. import re
  13. import os
  14. import random
  15. import shelve
  16. import subprocess
  17. import time
  18. import dns.resolver, dns.reversename
  19. import SocketServer
  20. import threading
  21. msgserver = None
  22. peers_repo = None
  23. nodeaccess = None
  24. alfred_method = None
  25. ffpb_resolver = dns.resolver.Resolver()
  26. ffpb_resolver.nameservers = ['10.132.254.53']
  27. class MsgHandler(SocketServer.BaseRequestHandler):
  28. """Reads line from TCP stream and forwards it to configured IRC channels."""
  29. def handle(self):
  30. data = self.request.recv(2048).strip()
  31. sender = self.resolve_name(self.client_address[0])
  32. bot = self.server.bot
  33. if bot is None:
  34. print("ERROR: No bot in handle() :-(")
  35. return
  36. target = bot.config.core.owner
  37. if bot.config.has_section('ffpb'):
  38. is_public = data.lstrip().lower().startswith("public:")
  39. if is_public and not bot.config.ffpb.msg_target_public is None:
  40. data = data[7:].lstrip()
  41. target = bot.config.ffpb.msg_target_public
  42. elif not bot.config.ffpb.msg_target is None:
  43. target = bot.config.ffpb.msg_target
  44. bot.msg(target, "[{0}] {1}".format(sender, str(data)))
  45. def resolve_name(self, ipaddr):
  46. """
  47. Resolves the host name of the given IP address
  48. and strips away the suffix (.infra)?.ffpb
  49. """
  50. if ipaddr.startswith("127."):
  51. return "localhost"
  52. try:
  53. addr = dns.reversename.from_address(ipaddr)
  54. return re.sub("(.infra)?.ffpb.", "", str(ffpb_resolver.query(addr, "PTR")[0]))
  55. except dns.resolver.NXDOMAIN:
  56. return ipaddr
  57. class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
  58. """Defines a threaded TCP socket server."""
  59. bot = None
  60. def setup(bot):
  61. """Called by willie upon loading this plugin."""
  62. global msgserver, peers_repo, alfred_method, nodeaccess
  63. # signal begin of setup routine
  64. bot.memory['ffpb_in_setup'] = True
  65. # load list of seen nodes from disk
  66. seen_nodes = shelve.open('nodes.seen', writeback=True)
  67. bot.memory['seen_nodes'] = seen_nodes
  68. # load list of node ACL from disk (used in playitsafe())
  69. nodeaccess = shelve.open('nodes.acl', writeback=True)
  70. # no need to configure anything else if the ffpb config section is missing
  71. if not bot.config.has_section('ffpb'):
  72. bot.memory['ffpb_in_setup'] = False
  73. return
  74. # open the git repository containing the peers files
  75. if not bot.config.ffpb.peers_directory is None:
  76. peers_repo = git.Repo(bot.config.ffpb.peers_directory)
  77. assert peers_repo.bare is False
  78. # if configured, start the messaging server
  79. if int(bot.config.ffpb.msg_enable) == 1:
  80. host = "localhost"
  81. port = 2342
  82. if not bot.config.ffpb.msg_host is None:
  83. host = bot.config.ffpb.msg_host
  84. if not bot.config.ffpb.msg_port is None:
  85. port = int(bot.config.ffpb.msg_port)
  86. msgserver = ThreadingTCPServer((host, port), MsgHandler)
  87. msgserver.bot = bot
  88. ipaddr, port = msgserver.server_address
  89. print("Messaging server listening on {}:{}".format(ipaddr, port))
  90. msgserver_thread = threading.Thread(target=msgserver.serve_forever)
  91. msgserver_thread.daemon = True
  92. msgserver_thread.start()
  93. # initially fetch ALFRED data
  94. alfred_method = bot.config.ffpb.alfred_method
  95. if not 'alfred_data' in bot.memory:
  96. bot.memory['alfred_data'] = {}
  97. if not 'alfred_update' in bot.memory:
  98. bot.memory['alfred_update'] = datetime(1970, 1, 1, 23, 42)
  99. ffpb_updatealfred(bot)
  100. # signal end of setup routine
  101. bot.memory['ffpb_in_setup'] = False
  102. def shutdown(bot):
  103. global msgserver, nodeaccess
  104. # store node acl
  105. if not nodeaccess is None:
  106. nodeaccess.sync()
  107. nodeaccess.close()
  108. nodeaccess = None
  109. # store seen nodes
  110. if 'seen_nodes' in bot.memory and bot.memory['seen_nodes'] != None:
  111. bot.memory['seen_nodes'].close()
  112. bot.memory['seen_nodes'] = None
  113. del bot.memory['seen_nodes']
  114. # shutdown messaging server
  115. if not msgserver is None:
  116. msgserver.shutdown()
  117. print("Closed messaging server.")
  118. msgserver = None
  119. @willie.module.commands("help")
  120. @willie.module.commands("hilfe")
  121. @willie.module.commands("man")
  122. def ffpb_help(bot, trigger):
  123. """Display commony ulsed functions."""
  124. functions = {
  125. "!ping <knoten>": "Prüfe ob der Knoten erreichbar ist.",
  126. "!status": "Aktuellen Status des Netzwerks (insb. Anzahl Knoten und Clients) ausgegeben.",
  127. "!highscore": "Höchstwerte von Anzahl Knoten und Clients",
  128. "!info <knoten>": "Allgemeine Information zu dem Knoten anzeigen.",
  129. "!link <knoten>": "MAC-Adresse und Link zur Status-Seite des Knotens anzeigen.",
  130. "!mesh <knoten>": "Zeige Mesh-Partner eines Knotens",
  131. "!lastseen <knoten>": "Zeitpunkt an dem der Knoten zuletzt gesehen wurde",
  132. }
  133. param = trigger.group(2)
  134. if param is None:
  135. bot.say("Funktionen: " + str.join(", ", sorted(functions.keys())))
  136. return
  137. if param.startswith("!"):
  138. param = param[1:]
  139. for fun in functions.keys():
  140. if fun.startswith("!" + param + " "):
  141. bot.say("Hilfe zu '" + fun + "': " + functions[fun])
  142. return
  143. bot.say("Allgemeine Hilfe gibt's mit !help - ohne Parameter.")
  144. def playitsafe(bot, trigger,
  145. botadmin=False, admin_channel=False,
  146. via_channel=False, via_privmsg=False, need_op=False,
  147. node=None, reply_directly=True,
  148. debug_user=None, debug_ignorebotadmin=False):
  149. """
  150. helper: checks that the triggering user has the necessary rights
  151. Returns true if everything is okay.
  152. If it's not, a reply is send via the bot and false is returned.
  153. """
  154. if via_channel and via_privmsg:
  155. raise Exception('Der Entwickler ist ein dummer, dummer Junge. ' +
  156. '(playitsafe: via_channel && via_privmsg).')
  157. user = trigger.nick if debug_user is None else debug_user
  158. user = user.lower()
  159. # botadmin: you need to be configured as a bot admin
  160. if botadmin and not trigger.admin:
  161. if reply_directly:
  162. bot.say('Du brauchst Super-Kuh-Kräfte um dieses Kommando auszuführen.')
  163. return False
  164. # via_channel: the request must not be a private conversation
  165. if via_channel and trigger.is_privmsg:
  166. if reply_directly:
  167. bot.say('Bitte per Channel - mehr Transparenz wagen und so!')
  168. return False
  169. # via_privmsg: the request must be a private conversation
  170. if via_privmsg and not trigger.is_privmsg:
  171. if reply_directly:
  172. bot.say('Solche Informationen gibt es nur per PM, ' +
  173. 'da bin ich ja schon ein klein wenig sensibel ...')
  174. return False
  175. # need_op: if the message is in a channel, check that the user has OP there
  176. if need_op and (not trigger.is_privmsg) and \
  177. (not user in bot.ops[trigger.sender]):
  178. if reply_directly:
  179. bot.say('Keine Zimtschnecke, keine Kekse.')
  180. return False
  181. # node: check that the user is whitelisted (or is admin)
  182. if not node is None and (debug_ignorebotadmin or not trigger.admin):
  183. acluser = [x for x in nodeaccess if x.lower() == user]
  184. acluser = acluser[0] if len(acluser) == 1 else None
  185. if nodeaccess is None or acluser is None:
  186. if reply_directly:
  187. bot.reply('You! Shall! Not! Access!')
  188. return False
  189. nodeid = node['node_id'] if 'node_id' in node else None
  190. matched = False
  191. for x in nodeaccess[acluser]:
  192. if x == nodeid or fnmatch(node['hostname'], x):
  193. matched = True
  194. break
  195. if not matched:
  196. if reply_directly:
  197. bot.reply('Mach das doch bitte auf deinen Knoten, kthxbye.')
  198. return False
  199. return True
  200. @willie.module.commands('nodeacl')
  201. def ffpb_nodeacl(bot, trigger):
  202. """Configure ACL for nodes."""
  203. if not playitsafe(bot, trigger, botadmin=True):
  204. # the check function already gives a bot reply, just exit here
  205. return
  206. # ensure the user gave arguments
  207. if trigger.group(2) is None or len(trigger.group(2)) == 0:
  208. bot.say('Sag doch was du willst ... einmal mit Profis arbeiten, ey -.-')
  209. return
  210. # read additional arguments
  211. cmd = trigger.group(3).lower()
  212. if cmd == 'list':
  213. user = trigger.group(4)
  214. if user is None:
  215. usernames = [x for x in nodeaccess]
  216. bot.say('ACLs gesetzt für die User: ' + ', '.join(usernames))
  217. return
  218. user = user.lower()
  219. uid = [x for x in nodeaccess if x.lower() == user]
  220. if len(uid) == 0:
  221. bot.say('Für \'{0}\' ist keine Node ACL gesetzt.'.format(user))
  222. return
  223. bot.say('Node ACL für \'{0}\' = \'{1}\''.format(
  224. uid[0],
  225. '\', \''.join(nodeaccess[uid[0]]))
  226. )
  227. return
  228. if cmd in ['add', 'del', 'check']:
  229. user = trigger.group(4)
  230. value = trigger.group(5)
  231. if user is None or value is None:
  232. bot.say('Du bist eine Pappnase - User und Knoten, bitte.')
  233. return
  234. user = str(user)
  235. print('NodeACL ' + cmd + ' \'' + value + '\' for user \'' + user + '\'')
  236. uid = [x for x in nodeaccess if x == user or x.lower() == user]
  237. if cmd == 'add':
  238. uid = uid[0] if len(uid) > 0 else user
  239. if not uid in nodeaccess:
  240. nodeaccess[uid] = []
  241. if not value in nodeaccess[uid]:
  242. nodeaccess[uid].append(value)
  243. bot.say("201 nodeACL '{0}' +'{1}'".format(uid, value))
  244. else:
  245. bot.say("304 nodeACL '{0}' contains '{1}'".format(uid, value))
  246. elif cmd == 'del':
  247. if len(uid) == 0:
  248. bot.say("404 nodeACL '{0}'".format(uid))
  249. return
  250. if value in nodeaccess[uid]:
  251. nodeaccess[uid].remove(value)
  252. bot.say("200 nodeACL '{0}' -'{1}'".format(uid, value))
  253. else:
  254. bot.say("404 nodeACL '{0}' has no '{1}'".format(uid, value))
  255. elif cmd == 'check':
  256. if len(uid) == 0:
  257. bot.say('Nope, keine ACL gesetzt.')
  258. return
  259. node = ffpb_findnode(value)
  260. if node is None:
  261. bot.say('Nope, kein Plan was für ein Knoten das ist.')
  262. return
  263. result = playitsafe(bot, trigger,
  264. debug_user=uid[0], debug_ignorebotadmin=True,
  265. node=node, reply_directly=False)
  266. if result is True:
  267. bot.say('Jupp.')
  268. elif result is False:
  269. bot.say('Nope.')
  270. else:
  271. bot.say('Huh? result=' + str(result))
  272. return
  273. bot.say('Unbekanntes Kommando. Probier ' +
  274. '"list [user]", "add user value" oder "del user value". ' +
  275. 'Value kann node_id oder hostname-Maske sein.')
  276. def ffpb_ensurenodeid(nodedata):
  277. """Makes sure that the given dict has a 'node_id' field."""
  278. if 'node_id' in nodedata:
  279. return nodedata
  280. # derive node's id
  281. nodeid = None
  282. if 'network' in nodedata and 'mac' in nodedata['network']:
  283. nodeid = nodedata['network']['mac'].replace(':', '')
  284. # assemble extended data
  285. result = {'node_id': nodeid}
  286. for key in nodedata:
  287. result[key] = nodedata[key]
  288. return result
  289. def ffpb_findnode(name, alfred_data=None, allow_fuzzymatching=True):
  290. """helper: try to identify the node the user meant by the given name"""
  291. # no name, no node
  292. if name is None or len(name) == 0:
  293. return None
  294. name = str(name).strip()
  295. # disable fuzzy matching if name is enclosed in quotes
  296. if name.startswith('\'') and name.endswith('\'') or \
  297. name.startswith('"') and name.endswith('"'):
  298. name = name[1:-1]
  299. allow_fuzzymatching = False
  300. names = {}
  301. if not alfred_data is None:
  302. # try to match MAC
  303. m = re.search("^([0-9a-fA-F][0-9a-fA-F]:){5}[0-9a-fA-F][0-9a-fA-F]$", name)
  304. if not m is None:
  305. mac = m.group(0).lower()
  306. if mac in alfred_data:
  307. return ffpb_ensurenodeid(alfred_data[mac])
  308. # try to find alias MAC in ALFRED data
  309. for nodeid in alfred_data:
  310. node = alfred_data[nodeid]
  311. if "network" in node:
  312. if node["network"].get("mac", "").lower() == mac:
  313. return ffpb_ensurenodeid(node)
  314. if "mesh_interfaces" in node["network"]:
  315. for mim in node["network"]["mesh_interfaces"]:
  316. if mim.lower() == mac:
  317. return ffpb_ensurenodeid(node)
  318. nodeid = mac.replace(':', '').lower()
  319. return {
  320. 'nodeid': nodeid,
  321. 'hostname': '?-' + nodeid,
  322. 'network': {
  323. 'addresses': [mac2ipv6(mac, 'fdca:ffee:ff12:132:')],
  324. 'mac': mac,
  325. },
  326. 'hardware': {
  327. 'model': 'derived-from-mac',
  328. },
  329. }
  330. # look through the ALFRED peers
  331. for nodeid in alfred_data:
  332. node = alfred_data[nodeid]
  333. if 'hostname' in node:
  334. h = node['hostname']
  335. if h.lower() == name.lower():
  336. return node
  337. else:
  338. names[h] = nodeid
  339. # not found in ALFRED data -> try peers_repo
  340. if not peers_repo is None:
  341. peer_name = None
  342. peer_mac = None
  343. peer_file = None
  344. for b in peers_repo.heads.master.commit.tree.blobs:
  345. if b.name.lower() == name.lower():
  346. peer_name = b.name
  347. peer_file = b.abspath
  348. break
  349. if (not peer_file is None) and os.path.exists(peer_file):
  350. peerfile = open(peer_file, "r")
  351. for line in peerfile:
  352. if line.startswith("# MAC:"):
  353. peer_mac = line[6:].strip()
  354. peerfile.close()
  355. if not peer_mac is None:
  356. return {
  357. 'node_id': peer_mac.replace(':', ''),
  358. 'hostname': peer_name,
  359. 'network': {
  360. 'addresses': [mac2ipv6(peer_mac, 'fdca:ffee:ff12:132:'), ],
  361. 'mac': peer_mac,
  362. },
  363. 'hardware': {
  364. 'model': 'derived-from-vpnkeys',
  365. },
  366. }
  367. # do a similar name lookup in the ALFRED data
  368. if allow_fuzzymatching and not alfred_data is None:
  369. allnames = [x for x in names]
  370. possibilities = difflib.get_close_matches(name, allnames, cutoff=0.75)
  371. print('findnode: Fuzzy matching \'{0}\' got {1} entries: {2}'.format(
  372. name,
  373. len(possibilities), ', '.join(possibilities))
  374. )
  375. if len(possibilities) == 1:
  376. # if we got exactly one candidate that might be it
  377. return ffpb_ensurenodeid(alfred_data[names[possibilities[0]]])
  378. # none of the above was able to identify the requested node
  379. return None
  380. def ffpb_findnode_from_botparam(bot, name, ensure_recent_alfreddata=True):
  381. """helper: call ffpb_findnode() and give common answers via bot on error"""
  382. if name is None or len(name) == 0:
  383. if not bot is None:
  384. bot.reply("Grün.")
  385. return None
  386. alfred_data = get_alfred_data(bot, ensure_recent_alfreddata)
  387. if ensure_recent_alfreddata and alfred_data is None:
  388. if not bot is None:
  389. bot.say('Informationen sind ausverkauft bzw. veraltet, ' +
  390. 'daher sage ich mal lieber nichts zu \'' + name + '\'.')
  391. return None
  392. node = ffpb_findnode(name, alfred_data)
  393. if node is None:
  394. if not bot is None:
  395. bot.say("Kein Plan wer oder was mit '" + name + "' gemeint ist :(")
  396. return node
  397. def mac2ipv6(mac, prefix=None):
  398. """Calculate IPv6 address from given MAC,
  399. optionally replacing the fe80:: prefix with a given one."""
  400. result = str(netaddr.EUI(mac).ipv6_link_local())
  401. if (not prefix is None) and (result.startswith("fe80::")):
  402. result = prefix + result[6:]
  403. return result
  404. @willie.module.interval(30)
  405. def ffpb_updatealfred(bot):
  406. """Aktualisiere ALFRED-Daten"""
  407. if alfred_method is None or alfred_method == "None":
  408. return
  409. updated = None
  410. if alfred_method == "exec":
  411. rawdata = subprocess.check_output(['alfred-json', '-z', '-r', '158'])
  412. updated = datetime.now()
  413. elif alfred_method.startswith("http"):
  414. try:
  415. rawdata = urllib2.urlopen(alfred_method)
  416. except urllib2.URLError as err:
  417. print("Failed to download ALFRED data:" + str(err))
  418. return
  419. last_modified = rawdata.info().getdate_tz("Last-Modified")
  420. updated = datetime.fromtimestamp(mktime_tz(last_modified))
  421. else:
  422. print("Unknown ALFRED data method '{0}', cannot load new data.".format(alfred_method))
  423. alfred_data = None
  424. return
  425. try:
  426. alfred_data = json.load(rawdata)
  427. #print("Fetched new ALFRED data:", len(alfred_data), "entries")
  428. except ValueError as err:
  429. print("Failed to parse ALFRED data: " + str(err))
  430. return
  431. bot.memory['alfred_data'] = alfred_data
  432. bot.memory['alfred_update'] = updated
  433. seen_nodes = bot.memory.get('seen_nodes', None)
  434. if not seen_nodes is None:
  435. new = []
  436. for nodeid in alfred_data:
  437. nodeid = str(nodeid)
  438. if not nodeid in seen_nodes:
  439. seen_nodes[nodeid] = updated
  440. new.append((nodeid, alfred_data[nodeid]['hostname']))
  441. print('First time seen: ' + str(nodeid))
  442. if len(new) > 0 and not bot.memory['ffpb_in_setup']:
  443. action_msg = None
  444. if len(new) == 1:
  445. action_msg = random.choice((
  446. 'bemerkt den neuen Knoten {0}',
  447. 'entdeckt {0}',
  448. 'reibt sich die Augen und erblickt einen verpackungsfrischen Knoten {0}',
  449. u'heißt {0} im Mesh willkommen',
  450. 'freut sich, dass {0} aufgetaucht ist',
  451. 'traut seinen Augen kaum. {0} sagt zum ersten Mal: Hallo Freifunk Paderborn',
  452. u'sieht die ersten Herzschläge von {0}',
  453. u'stellt einen großen Pott Heißgetränk zu {0} und fragt ob es hier Meshpartner gibt.',
  454. )).format('\'' + str(new[0][1]) + '\'')
  455. # try to fetch location from BATCAVE in order to add a geomap URL
  456. location = ffpb_get_batcave_nodefield(str.replace(new[0][0], ':', ''), 'location')
  457. if not location is None:
  458. action_msg += ' http://map.paderborn.freifunk.net/geomap.html?lat=' + location['latitude'] + '&lon=' + location['longitude']
  459. else:
  460. action_msg = random.choice((
  461. 'bemerkt die neuen Knoten {0} und {1}',
  462. 'hat {0} und {1} entdeckt',
  463. 'bewundert {0} sowie {1}',
  464. 'freut sich, dass {0} und {1} nun auch online sind',
  465. u'heißt {0} und {1} im Mesh willkommen',
  466. 'fragt sich ob die noch jungen Herzen von {0} und {1} synchron schlagen',
  467. ))
  468. all_but_last = [str(x[1]) for x in new[0:-1]]
  469. last = str(new[-1][1])
  470. action_msg = action_msg.format(
  471. '\'' + '\', \''.join(all_but_last) + '\'',
  472. '\'' + last + '\''
  473. )
  474. action_target = bot.config.ffpb.msg_target
  475. if not bot.config.ffpb.msg_target_public is None:
  476. action_target = bot.config.ffpb.msg_target_public
  477. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
  478. def get_alfred_data(bot, ensure_not_outdated=True):
  479. """
  480. Retrieves the stored alfred_data and optionally checks
  481. that it has been updated no more than 5 minutes ago.
  482. """
  483. alfred_data = bot.memory.get('alfred_data', None)
  484. alfred_update = bot.memory.get('alfred_update', 0)
  485. if alfred_data is None:
  486. return None
  487. if ensure_not_outdated:
  488. timeout = datetime.now() - timedelta(minutes=5)
  489. is_outdated = timeout > alfred_update
  490. if is_outdated:
  491. return None
  492. return alfred_data
  493. def ffpb_get_batcave_nodefield(nodeid, field):
  494. """Query the given field for the given nodeid from the BATCAVE."""
  495. raw_data = None
  496. try:
  497. # query BATCAVE for node's field
  498. raw_data = urllib2.urlopen('http://[fdca:ffee:ff12:a255::253]:8888/node/{0}/{1}'.format(nodeid, field))
  499. except urllib2.URLError as err:
  500. print('Failed to contact BATCAVE for \'{0}\'->\'{1}\': {2}'.format(
  501. nodeid, field, err))
  502. return None
  503. try:
  504. return json.load(raw_data)
  505. except ValueError as err:
  506. print('Could not parse BATCAVE\'s response as JSON for \'{0}\'->\'{1}\':'.format(nodeid, field, err))
  507. return None
  508. @willie.module.commands('debug-alfred')
  509. def ffpb_debug_alfred(bot, trigger):
  510. """Show statistics of available ALFRED data."""
  511. alfred_data = get_alfred_data(bot)
  512. if alfred_data is None:
  513. bot.say("Keine ALFRED-Daten vorhanden.")
  514. else:
  515. bot.say("ALFRED Daten: count={0} lastupdate={1}".format(
  516. len(alfred_data), bot.memory['alfred_update']))
  517. @willie.module.interval(60)
  518. def ffpb_updatepeers(bot):
  519. """Refresh list of peers and message the diff."""
  520. if peers_repo is None:
  521. print('WARNING: peers_repo is None')
  522. return
  523. old_head = peers_repo.head.commit
  524. peers_repo.remotes.origin.pull()
  525. new_head = peers_repo.head.commit
  526. if new_head != old_head:
  527. print('git pull: from ' + str(old_head) + ' to ' + str(new_head))
  528. added = []
  529. changed = []
  530. renamed = []
  531. deleted = []
  532. for f in old_head.diff(new_head):
  533. if f.new_file:
  534. added.append(f.b_blob.name)
  535. elif f.deleted_file:
  536. deleted.append(f.a_blob.name)
  537. elif f.renamed:
  538. renamed.append([f.rename_from, f.rename_to])
  539. else:
  540. changed.append(f.a_blob.name)
  541. response = "Knoten-Update (VPN +{0} %{1} -{2}): ".format(
  542. len(added), len(renamed)+len(changed), len(deleted))
  543. for f in added:
  544. response += " +'{}'".format(f)
  545. for f in changed:
  546. response += " %'{}'".format(f)
  547. for f in renamed:
  548. response += " '{}'->'{}'".format(f[0], f[1])
  549. for f in deleted:
  550. response += " -'{}'".format(f)
  551. bot.msg(bot.config.ffpb.msg_target, response)
  552. def ffpb_fetch_stats(bot, url, memoryid):
  553. """Fetch a ffmap-style nodes.json from the given URL and
  554. store it in the bot's memory."""
  555. response = urllib2.urlopen(url)
  556. data = json.load(response)
  557. nodes_active = 0
  558. nodes_total = 0
  559. clients_count = 0
  560. for node in data['nodes']:
  561. if node['flags']['gateway'] or node['flags']['client']:
  562. continue
  563. nodes_total += 1
  564. if node['flags']['online']:
  565. nodes_active += 1
  566. if 'legacy' in node['flags'] and node['flags']['legacy']:
  567. clients_count -= 1
  568. for link in data['links']:
  569. if link['type'] == 'client':
  570. clients_count += 1
  571. if not memoryid in bot.memory:
  572. bot.memory[memoryid] = {}
  573. stats = bot.memory[memoryid]
  574. stats["fetchtime"] = time.time()
  575. stats["nodes_active"] = nodes_active
  576. stats["nodes_total"] = nodes_total
  577. stats["clients"] = clients_count
  578. return (nodes_active, nodes_total, clients_count)
  579. def pretty_date(timestamp=False):
  580. """
  581. Get a datetime object or a int() Epoch timestamp and return a
  582. pretty string like 'an hour ago', 'Yesterday', '3 months ago',
  583. 'just now', etc
  584. """
  585. now = datetime.now()
  586. compare = None
  587. if type(timestamp) is int:
  588. compare = datetime.fromtimestamp(timestamp)
  589. elif type(timestamp) is float:
  590. compare = datetime.fromtimestamp(int(timestamp))
  591. elif isinstance(timestamp, datetime):
  592. compare = timestamp
  593. elif not timestamp:
  594. compare = now
  595. diff = now - compare
  596. second_diff = diff.seconds
  597. day_diff = diff.days
  598. if day_diff < 0:
  599. return ''
  600. if day_diff == 0:
  601. if second_diff < 10:
  602. return "gerade eben"
  603. if second_diff < 60:
  604. return "vor " + str(second_diff) + " Sekunden"
  605. if second_diff < 120:
  606. return "vor einer Minute"
  607. if second_diff < 3600:
  608. return "vor " + str(second_diff / 60) + " Minuten"
  609. if second_diff < 7200:
  610. return "vor einer Stunde"
  611. if second_diff < 86400:
  612. return "vor " + str(second_diff / 3600) + " Stunden"
  613. if day_diff == 1:
  614. return "gestern"
  615. if day_diff < 7:
  616. return "vor " + str(day_diff) + " Tagen"
  617. return "am " + compare.strftime('%d.%m.%Y um %H:%M Uhr')
  618. @willie.module.commands('ping')
  619. def ffpb_ping(bot, trigger=None, target_name=None, reply_directly=True):
  620. """Ping the given node"""
  621. # identify node or bail out
  622. if target_name is None:
  623. target_name = trigger.group(2)
  624. node = ffpb_findnode_from_botparam(bot, target_name,
  625. ensure_recent_alfreddata=False)
  626. if node is None:
  627. return None
  628. # get the first non-linklocal address from the node
  629. target = [x for x in node["network"]["addresses"]
  630. if not x.lower().startswith("fe80:")][0]
  631. target_alias = node["hostname"]
  632. # execute the actual ping and reply the result
  633. print("pinging '{0}' at {1} ...".format(target_name, target))
  634. result = os.system('ping6 -c 2 -W 1 ' + target + ' >/dev/null')
  635. if result == 0:
  636. print("ping to '{0}' succeeded".format(target_name))
  637. if reply_directly:
  638. bot.say('Knoten "' + target_alias + '" antwortet \\o/')
  639. return True
  640. elif result == 1 or result == 256:
  641. print("ping to '{0}' failed".format(target_name))
  642. if reply_directly:
  643. bot.say('Keine Antwort von "' + target_alias + '" :-(')
  644. return False
  645. else:
  646. print("ping to '{0}' broken: result='{1}'".format(target_name, result))
  647. if reply_directly:
  648. bot.say('Uh oh, irgendwas ist kaputt. Chef, ping result = ' + str(result) + ' - darf ich das essen?')
  649. return None
  650. @willie.module.commands('mesh')
  651. def ffpb_nodemesh(bot, trigger):
  652. """Display mesh partners of the given node."""
  653. # identify node or bail out
  654. target_name = trigger.group(2)
  655. node = ffpb_findnode_from_botparam(bot, target_name,
  656. ensure_recent_alfreddata=False)
  657. if node is None:
  658. return None
  659. # derive node's id
  660. nodeid = node['node_id'] if 'node_id' in node else None
  661. if nodeid is None:
  662. msg = 'Mist, ich habe gerade den Zettel verlegt auf dem die Node-ID' + \
  663. ' von \'{0}\' steht, bitte frag später noch einmal.'
  664. bot.say(msg.format(node.get('hostname', target_name)))
  665. return
  666. # query BATCAVE for node's neighbours (result is a list of MAC addresses)
  667. cave_result = ffpb_get_batcave_nodefield(nodeid, 'neighbours')
  668. if cave_result is None:
  669. msg = 'Hm, scheinbar liegen zu \'{0}\' keine Daten vor. ' + \
  670. 'Klingt komisch, ist aber so.'
  671. bot.say(msg.format(node.get('hostname', target_name)))
  672. return
  673. # query BATCAVE for neighbour's names
  674. data = '&'.join([str(n) for n in cave_result])
  675. req = urllib2.urlopen('http://[fdca:ffee:ff12:a255::253]:8888/idmac2name', data)
  676. # filter out duplicate names
  677. neighbours = set()
  678. gateways = set()
  679. for line in req:
  680. ident, name = line.strip().split('=')
  681. if ident == name and ident.startswith('c0:ff:ee:ba:be:'):
  682. gateways.add('Gateway ' + ident[len('c0:ff:ee:ba:be:'):])
  683. else:
  684. neighbours.add(name)
  685. neighbours = [x for x in neighbours]
  686. gateways = sorted([x for x in gateways])
  687. # respond to the user
  688. reply = node['hostname']
  689. if len(neighbours) == 0:
  690. reply += ' hat keinen Mesh-Partner'
  691. elif len(neighbours) == 1:
  692. reply += u' mesht mit \'{0}\''.format(neighbours[0])
  693. else:
  694. all_except_last = '\', \''.join(neighbours[:-1])
  695. last = neighbours[-1]
  696. reply += ' mesht mit \'{0}\' und \'{1}\''.format(all_except_last, last)
  697. if len(gateways) > 0:
  698. if len(neighbours) == 0:
  699. reply += ', aber hat eine Verbindung zu '
  700. else:
  701. reply += ' und hat zudem eine Verbindung zu '
  702. if len(gateways) == 1:
  703. reply += gateways[0]
  704. else:
  705. all_except_last = '\', \''.join(gateways[:-1])
  706. last = gateways[-1]
  707. reply += '{0} und {1}'.format(all_except_last, last)
  708. bot.say(reply)
  709. @willie.module.commands('exec-on-peer')
  710. def ffpb_remoteexec(bot, trigger):
  711. """Remote execution on the given node"""
  712. bot_params = trigger.group(2).split(' ', 1) if trigger.group(2) is not None else []
  713. if len(bot_params) != 2:
  714. bot.say('Wenn du nicht sagst wo mach ich remote execution bei dir!')
  715. bot.say('Tipp: !exec-on-peer <peer> <cmd>')
  716. return
  717. target_name = bot_params[0]
  718. target_cmd = bot_params[1]
  719. # identify requested node or bail out
  720. node = ffpb_findnode_from_botparam(bot, target_name,
  721. ensure_recent_alfreddata=False)
  722. if node is None:
  723. return
  724. # check ACL
  725. if not playitsafe(bot, trigger, via_channel=True, node=node):
  726. return
  727. # use the node's first non-linklocal address
  728. naddrs = node["network"]["addresses"]
  729. target = [x for x in naddrs if not x.lower().startswith("fe80:")][0]
  730. target_alias = node["hostname"]
  731. # assemble SSH command
  732. cmd = [
  733. 'ssh',
  734. '-6n',
  735. '-l', 'root',
  736. '-o', 'BatchMode=yes',
  737. '-o', 'StrictHostKeyChecking=no',
  738. target,
  739. target_cmd,
  740. ]
  741. print("REMOTE EXEC = " + str(cmd))
  742. try:
  743. # call SSH
  744. result = subprocess.check_output(
  745. cmd,
  746. stderr=subprocess.STDOUT,
  747. shell=False,
  748. )
  749. # fetch results and sent at most 8 of them as response
  750. lines = str(result).splitlines()
  751. if len(lines) == 0:
  752. bot.say('exec-on-peer(' + target_alias + '): No output')
  753. return
  754. msg = 'exec-on-peer({0}): {1} Zeilen'.format(target_alias, len(lines))
  755. if len(lines) > 8:
  756. msg += ' (zeige max. 8)'
  757. bot.say(msg + ':')
  758. for line in lines[0:8]:
  759. bot.say(line)
  760. except subprocess.CalledProcessError as err:
  761. bot.say('Fehler {0} bei exec-on-peer({1}): {2}'.format(
  762. err.returncode,
  763. target_alias,
  764. err.output
  765. ))