ffpb.py 29 KB

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