From 5499be89203a18ca61a21cfc7266cf0f4ebe6547 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Thu, 15 Dec 2011 23:18:21 +0100 Subject: refractoring --- module/interaction/InteractionTask.py | 129 ++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 module/interaction/InteractionTask.py (limited to 'module/interaction/InteractionTask.py') diff --git a/module/interaction/InteractionTask.py b/module/interaction/InteractionTask.py new file mode 100644 index 000000000..97cb16794 --- /dev/null +++ b/module/interaction/InteractionTask.py @@ -0,0 +1,129 @@ +# -*- 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 . + + @author: RaNaN +""" + +from module.Api import InteractionTask as BaseInteractionTask +from module.Api import Input, Output + +#noinspection PyUnresolvedReferences +class InteractionTask(BaseInteractionTask): + """ + General Interaction Task extends ITask defined by thrift with additional fields and methods. + """ + #: 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 + #: 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 + + 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 "" % self.id -- cgit v1.2.3 From 4df2b77fdf42046fe19bd371be7c7255986b5980 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Tue, 6 Mar 2012 13:36:39 +0100 Subject: renamed hooks to addons, new filemanager and database, many new api methods you will loose ALL your LINKS, webinterface will NOT work --- module/interaction/InteractionTask.py | 87 +++++++---------------------------- 1 file changed, 17 insertions(+), 70 deletions(-) (limited to 'module/interaction/InteractionTask.py') 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 "" % self.id + return "" % self.id \ No newline at end of file -- cgit v1.2.3 From 50d4df8b4d48b855bd18e9922355b7f3f2b4da4e Mon Sep 17 00:00:00 2001 From: RaNaN Date: Tue, 20 Mar 2012 14:57:45 +0100 Subject: captcha decrypting for all plugin types, new interaction manager --- module/interaction/InteractionTask.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'module/interaction/InteractionTask.py') diff --git a/module/interaction/InteractionTask.py b/module/interaction/InteractionTask.py index 7963a5c72..b372321b0 100644 --- a/module/interaction/InteractionTask.py +++ b/module/interaction/InteractionTask.py @@ -30,12 +30,14 @@ class InteractionTask(BaseInteractionTask): storage = None #: Timestamp when task expires wait_until = 0 - #: The received result as string representation + #: The received result result = None #: List of registered handles handler = None #: Error Message error = None + #: Timeout locked + locked = False def __init__(self, *args, **kwargs): BaseInteractionTask.__init__(self, *args, **kwargs) @@ -46,25 +48,28 @@ class InteractionTask(BaseInteractionTask): self.wait_until = 0 def convertResult(self, value): + #TODO: convert based on input/output return value def getResult(self): return self.result def setResult(self, value): - pass + self.result = self.convertResult(value) - def setWaiting(self, sec): - self.wait_until = max(time() + sec, self.wait_until) + def setWaiting(self, sec, lock=False): + if not self.locked: + self.wait_until = max(time() + sec, self.wait_until) + if lock: self.locked = True - def isWaiting(self, sec): + def isWaiting(self): if self.result or self.error or time() > self.waitUntil: return False return True def timedOut(self): - return time() > self.waitUntil + return time() > self.wait_until > 0 def correct(self): [x.taskCorrect(self) for x in self.handler] -- cgit v1.2.3