From a9e884c4fa09ead7ab9668a0281fd1a3da142bf6 Mon Sep 17 00:00:00 2001 From: Daniel afx Date: Sun, 25 Oct 2020 22:05:46 +0200 Subject: [PATCH] implement cnc dump log --- cncbot.py | 42 ++++++++++++++++++++++++++++++++++-------- forestmapview.kv | 4 ++-- forestmapview.py | 3 ++- main.py | 12 ++++++------ requierments.txt | 2 +- treemarker.kv | 2 +- 6 files changed, 46 insertions(+), 19 deletions(-) diff --git a/cncbot.py b/cncbot.py index a2df6df..78e956e 100644 --- a/cncbot.py +++ b/cncbot.py @@ -1,6 +1,9 @@ import pydle +import asyncio +from asyncio import new_event_loop, gather, get_event_loop, sleep import random import string +import time CNC_HOST='dark.deflax.net' CNC_CHANNEL='#izba' @@ -12,7 +15,7 @@ def get_random_string(length): result_str = ''.join(random.choice(letters) for i in range(length)) return result_str -class ForestBot(pydle.Client): +class ForestBot(pydle.Client): async def on_connect(self): await super().on_connect() await self.join(CNC_CHANNEL) @@ -25,14 +28,12 @@ class ForestBot(pydle.Client): See pydle's documentation on blocking functionality for details. """ admin = False - # Check the WHOIS info to see if the source has identified with NickServ. # This is a blocking operation, so use yield. if nickname in ADMIN_NICKNAMES: info = await self.whois(nickname) print('] Detected privileged request from {}'.format(info)) admin = info['identified'] - return admin async def on_message(self, target, source, message): @@ -43,6 +44,7 @@ class ForestBot(pydle.Client): await self.message(target, '] cnc usage:') await self.message(target, '] help - displays help') await self.message(target, '] id - cnc user info') + await self.message(target, '] dump - dump cnc log') # Show user info if message.startswith('id {}'.format(self.nickname)): @@ -51,11 +53,35 @@ class ForestBot(pydle.Client): await self.message(target, '] You are an administrator. :)'.format(source)) else: await self.message(target, '] You are NOT an administrator. :('.format(source)) - -def CnCApp(): - cnc = ForestBot('cnc_' + get_random_string(6)) - cnc.run(CNC_HOST, tls=False, tls_verify=False) + + if message.startswith('dump {}'.format(self.nickname)): + admin = await self.is_admin(source) + if admin: + await self.dumplog(target) + + async def dumplog(self, target): + while True: + msg = self.queue.get() + time.sleep(1) + await self.message(target, msg) + if len(msg) == 0: + break + + def run(self, *args, **kwargs): + self.queue = kwargs['extqueue'] + """ Connect and run bot in event loop. """ + #self.eventloop.run_until_complete(self.connect(*args, **kwargs)) + self.eventloop.run_until_complete(self.connect(*args)) + try: + self.eventloop.run_forever() + finally: + self.eventloop.stop() + +def CnCApp(queue): + botnick = 'cnc_' + get_random_string(8) + botnick_fallback = [botnick + '_re'] + cnc = ForestBot(botnick, botnick_fallback, botnick, botnick, None) + cnc.run(CNC_HOST, tls=False, tls_verify=False, extqueue=queue) if __name__ == '__main__': CnCApp() - \ No newline at end of file diff --git a/forestmapview.kv b/forestmapview.kv index f4d7b9a..8b8521b 100644 --- a/forestmapview.kv +++ b/forestmapview.kv @@ -11,10 +11,10 @@ max_zoom: 17 snap_to_zoom: True pause_on_action: True - #on_zoom: + on_zoom: #self.zoom = 10 if self.zoom < 10 else self.zoom - #self.print_current_zoom(self.zoom) #self.zoom = 17 + self.print_current_zoom(self.zoom) on_lat: self.start_get_fov_trees() on_lon: diff --git a/forestmapview.py b/forestmapview.py index fb21fe0..53d84ea 100644 --- a/forestmapview.py +++ b/forestmapview.py @@ -13,6 +13,7 @@ class ForestMapView(MapView): def print_current_zoom(self, *args): print("] Zoom level: " + str(self.zoom)) + #pqueue.put("] Zoom level: " + str(self.zoom)) def start_get_fov_trees(self): # After one second get the trees in field of view @@ -34,7 +35,7 @@ class ForestMapView(MapView): trees = app.cursor.fetchall() for tree in trees: name = tree[0] - #print("] Fountain detected: " + str(tree)) + #print("] Tree detected: " + str(tree)) if name in self.tree_names: continue else: diff --git a/main.py b/main.py index 3b31b3b..87bb5eb 100644 --- a/main.py +++ b/main.py @@ -2,13 +2,11 @@ __author__ = "afx" __version__ = "0.2" # Import system modules -import multiprocessing +from multiprocessing import Process, Queue import random import sqlite3 # Import Kivy modules -#from kivy.app import App -#from kivy.app import async_runTouchApp from kivymd.app import MDApp from kivy.lang import Builder @@ -71,6 +69,7 @@ class MainApp(MDApp): def on_start(self): # Welcome print(MOTD) + queue.put('] Framework started.') self.theme_cls.primary_palette = 'BlueGray' @@ -84,9 +83,10 @@ class MainApp(MDApp): # Instantiate SearchPopupMenu #self.search_menu = SearchPopupMenu() -if __name__ == '__main__': +if __name__ == '__main__': # Start the Command and Control bot. Mainly for reporting. - cnc = multiprocessing.Process(target=CnCApp) + queue = Queue() + cnc = Process(target=CnCApp, args=((queue),)) cnc.start() # Start the Kivy Framework main loop @@ -94,5 +94,5 @@ if __name__ == '__main__': kivyapp.run() # Cleanup subprocesses - print('] Terminating subprocesses.') + print('] Framework terminated.') cnc.kill() diff --git a/requierments.txt b/requierments.txt index c6511cb..c8b7e61 100644 --- a/requierments.txt +++ b/requierments.txt @@ -1,5 +1,5 @@ Kivy==2.0.0rc4 -pydle ffpyplayer sqlite_utils mapview +pure-sasl diff --git a/treemarker.kv b/treemarker.kv index a7ea7bc..4996412 100644 --- a/treemarker.kv +++ b/treemarker.kv @@ -1,2 +1,2 @@ : - source: 'assets/images/markers/fountain.png' \ No newline at end of file + source: 'assets/images/markers/tree.png' \ No newline at end of file