ffpb.py 30 KB

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