ffpb.py 31 KB

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