diff options
Diffstat (limited to 'module/interaction')
-rw-r--r-- | module/interaction/CaptchaManager.py | 158 | ||||
-rw-r--r-- | module/interaction/EventManager.py | 12 | ||||
-rw-r--r-- | module/interaction/InteractionManager.py | 33 | ||||
-rw-r--r-- | module/interaction/InteractionTask.py | 87 |
4 files changed, 50 insertions, 240 deletions
diff --git a/module/interaction/CaptchaManager.py b/module/interaction/CaptchaManager.py deleted file mode 100644 index 02cd10a11..000000000 --- a/module/interaction/CaptchaManager.py +++ /dev/null @@ -1,158 +0,0 @@ -# -*- coding: utf-8 -*- - -""" - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, see <http://www.gnu.org/licenses/>. - - @author: mkaay, RaNaN -""" - -from time import time -from traceback import print_exc -from threading import Lock - -class CaptchaManager(): - def __init__(self, core): - self.lock = Lock() - self.core = core - self.tasks = [] #task store, for outgoing tasks only - - self.ids = 0 #only for internal purpose - - def newTask(self, img, format, file, result_type): - task = CaptchaTask(self.ids, img, format, file, result_type) - self.ids += 1 - return task - - def removeTask(self, task): - self.lock.acquire() - if task in self.tasks: - self.tasks.remove(task) - self.lock.release() - - def getTask(self): - self.lock.acquire() - for task in self.tasks: - if task.status in ("waiting", "shared-user"): - self.lock.release() - return task - self.lock.release() - return None - - def getTaskByID(self, tid): - self.lock.acquire() - for task in self.tasks: - if task.id == str(tid): #task ids are strings - self.lock.release() - return task - self.lock.release() - return None - - def handleCaptcha(self, task): - cli = self.core.isClientConnected() - - if cli: #client connected -> should solve the captcha - task.setWaiting(50) #wait 50 sec for response - - for plugin in self.core.hookManager.activePlugins(): - try: - plugin.newCaptchaTask(task) - except: - if self.core.debug: - print_exc() - - if task.handler or cli: #the captcha was handled - self.tasks.append(task) - return True - - task.error = _("No Client connected for captcha decrypting") - - return False - - -class CaptchaTask(): - def __init__(self, id, img, format, file, result_type='textual'): - self.id = str(id) - self.captchaImg = img - self.captchaFormat = format - self.captchaFile = file - self.captchaResultType = result_type - self.handler = [] #the hook plugins that will take care of the solution - self.result = None - self.waitUntil = None - self.error = None #error message - - self.status = "init" - self.data = {} #handler can store data here - - def getCaptcha(self): - return self.captchaImg, self.captchaFormat, self.captchaResultType - - def setResult(self, text): - if self.isTextual(): - self.result = text - if self.isPositional(): - try: - parts = text.split(',') - self.result = (int(parts[0]), int(parts[1])) - except: - self.result = None - - def getResult(self): - try: - res = self.result.encode("utf8", "replace") - except: - res = self.result - - return res - - def getStatus(self): - return self.status - - def setWaiting(self, sec): - """ let the captcha wait secs for the solution """ - self.waitUntil = max(time() + sec, self.waitUntil) - self.status = "waiting" - - def isWaiting(self): - if self.result or self.error or time() > self.waitUntil: - return False - - return True - - def isTextual(self): - """ returns if text is written on the captcha """ - return self.captchaResultType == 'textual' - - def isPositional(self): - """ returns if user have to click a specific region on the captcha """ - return self.captchaResultType == 'positional' - - def setWatingForUser(self, exclusive): - if exclusive: - self.status = "user" - else: - self.status = "shared-user" - - def timedOut(self): - return time() > self.waitUntil - - def invalid(self): - """ indicates the captcha was not correct """ - [x.captchaInvalid(self) for x in self.handler] - - def correct(self): - [x.captchaCorrect(self) for x in self.handler] - - def __str__(self): - return "<CaptchaTask '%s'>" % self.id diff --git a/module/interaction/EventManager.py b/module/interaction/EventManager.py index 38faa3c46..02ecb82fb 100644 --- a/module/interaction/EventManager.py +++ b/module/interaction/EventManager.py @@ -1,14 +1,17 @@ # -*- coding: utf-8 -*- +from threading import Lock from traceback import print_exc from time import time +from module.utils import lock + class EventManager: """ Handles all Event related task, also stores an Event queue for clients, so they can retrieve them later. **Known Events:** - Most hook methods exists as events. These are some additional known events. + Most addon methods exists as events. These are some additional known events. ===================== ================ =========================================================== Name Arguments Description @@ -38,6 +41,8 @@ class EventManager: self.clients = {} self.events = {"metaEvent": []} + self.lock = Lock() + def getEvents(self, uuid): """ Get accumulated events for uuid since last call, this also registeres new client """ if uuid not in self.clients: @@ -80,6 +85,10 @@ class EventManager: if self.core.debug: print_exc() + self.updateClients(event, args) + + @lock + def updateClients(self, event, args): # append to client event queue if event in self.CLIENT_EVENTS: for uuid, client in self.clients.items(): @@ -88,7 +97,6 @@ class EventManager: else: client.append(event, args) - def removeFromEvents(self, func): """ Removes func from all known events """ for name, events in self.events.iteritems(): diff --git a/module/interaction/InteractionManager.py b/module/interaction/InteractionManager.py index 5ebcd1fcd..c547e1c97 100644 --- a/module/interaction/InteractionManager.py +++ b/module/interaction/InteractionManager.py @@ -15,10 +15,13 @@ @author: RaNaN """ -from utils import lock from traceback import print_exc from threading import Lock +from module.utils import lock, bits_set + +from InteractionTask import InteractionTask + class InteractionManager: """ Class that gives ability to interact with the user. @@ -30,13 +33,25 @@ class InteractionManager: self.core = core self.tasks = [] #task store, for outgoing tasks only + self.last_clients = {} + self.ids = 0 #only for internal purpose + def work(self): - """Mainloop that gets the work done""" + pass + + @lock + def newNotification(self): + pass + + @lock + def newQueryTask(self): + pass - def newTask(self, img, format, file, result_type): - task = CaptchaTask(self.ids, img, format, file, result_type) + @lock + def newCaptchaTask(self, img, format, file, result_type): + task = InteractionTask(self.ids, img, format, file, result_type) self.ids += 1 return task @@ -48,14 +63,12 @@ class InteractionManager: @lock def getTask(self): for task in self.tasks: - if task.status in ("waiting", "shared-user"): - return task + return task @lock - def getTaskByID(self, tid): + def getTaskByID(self, iid): for task in self.tasks: - if task.id == str(tid): #task ids are strings - self.lock.release() + if task.id == iid: return task def handleCaptcha(self, task): @@ -64,7 +77,7 @@ class InteractionManager: if cli: #client connected -> should solve the captcha task.setWaiting(50) #wait 50 sec for response - for plugin in self.core.hookManager.activePlugins(): + for plugin in self.core.addonManager.activePlugins(): try: plugin.newCaptchaTask(task) except: diff --git a/module/interaction/InteractionTask.py b/module/interaction/InteractionTask.py index 97cb16794..7963a5c72 100644 --- a/module/interaction/InteractionTask.py +++ b/module/interaction/InteractionTask.py @@ -16,6 +16,8 @@ @author: RaNaN """ +from time import time + from module.Api import InteractionTask as BaseInteractionTask from module.Api import Input, Output @@ -27,103 +29,48 @@ class InteractionTask(BaseInteractionTask): #: Plugins can put needed data here storage = None #: Timestamp when task expires - waitUntil = 0 - #: Default data to be used, or True if preset should be used - default = None + wait_until = 0 #: The received result as string representation result = None #: List of registered handles handler = None - #: Callback functions - callbacks = None #: Error Message error = None - #: Status string - status = None def __init__(self, *args, **kwargs): BaseInteractionTask.__init__(self, *args, **kwargs) # additional internal attributes self.storage = {} - self.default = [] self.handler = [] - self.callbacks = [] - - -class CaptchaTask: - def __init__(self, id, img, format, file, result_type='textual'): - self.id = str(id) - self.captchaImg = img - self.captchaFormat = format - self.captchaFile = file - self.captchaResultType = result_type - self.handler = [] #the hook plugins that will take care of the solution - self.result = None - self.waitUntil = None - self.error = None #error message - - self.status = "init" - self.data = {} #handler can store data here - - def getCaptcha(self): - return self.captchaImg, self.captchaFormat, self.captchaResultType - - def setResult(self, text): - if self.isTextual(): - self.result = text - if self.isPositional(): - try: - parts = text.split(',') - self.result = (int(parts[0]), int(parts[1])) - except: - self.result = None + self.wait_until = 0 - def getResult(self): - try: - res = self.result.encode("utf8", "replace") - except: - res = self.result + def convertResult(self, value): + return value - return res + def getResult(self): + return self.result - def getStatus(self): - return self.status + def setResult(self, value): + pass def setWaiting(self, sec): - """ let the captcha wait secs for the solution """ - self.waitUntil = max(time() + sec, self.waitUntil) - self.status = "waiting" + self.wait_until = max(time() + sec, self.wait_until) - def isWaiting(self): + def isWaiting(self, sec): if self.result or self.error or time() > self.waitUntil: return False return True - def isTextual(self): - """ returns if text is written on the captcha """ - return self.captchaResultType == 'textual' - - def isPositional(self): - """ returns if user have to click a specific region on the captcha """ - return self.captchaResultType == 'positional' - - def setWatingForUser(self, exclusive): - if exclusive: - self.status = "user" - else: - self.status = "shared-user" - def timedOut(self): return time() > self.waitUntil - def invalid(self): - """ indicates the captcha was not correct """ - [x.captchaInvalid(self) for x in self.handler] - def correct(self): - [x.captchaCorrect(self) for x in self.handler] + [x.taskCorrect(self) for x in self.handler] + + def invalid(self): + [x.taskInvalid(self) for x in self.handler] def __str__(self): - return "<CaptchaTask '%s'>" % self.id + return "<InteractionTask '%s'>" % self.id
\ No newline at end of file |