ffpb.py 30 KB

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