From 87a25a6527bcd9fd9c7ffa6eea9668e4bfd4b718 Mon Sep 17 00:00:00 2001 From: makefu Date: Sat, 17 Sep 2011 12:06:21 +0200 Subject: Reaktor/UDP: ubot - initial commit update specs which now decribe how the head of the conf entry may look like ubot is currenly an executable skeleton which important functions marked as to be implemented. --- Reaktor/UDP/index | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 Reaktor/UDP/index (limited to 'Reaktor/UDP/index') diff --git a/Reaktor/UDP/index b/Reaktor/UDP/index new file mode 100755 index 00000000..950f0d21 --- /dev/null +++ b/Reaktor/UDP/index @@ -0,0 +1,57 @@ +#!/usr/bin/python + +from asycore import asychat +import logging +import sys +log = logging.getLogger('asybot') + +def enable_syslog(logger): + import logging.handlers as handlers + from logging import Formatter + hdlr = handlers.SysLogHandler( + facility=handlers.SysLogHandler.LOG_DAEMON) + formatter = Formatter( + '%(filename)s: %(levelname)s: %(message)s') + hdlr.setFormatter(formatter) + logger.addHandler(hdlr) + +class ubot(asychat): + """ UDP Bot """ + def __init__(self, port,pattern,action,bind="0.0.0.0",terminator='\r\n'): + asychat.__init__(self) + self.bind = bind + self.port = port + self.data = '' + self.set_terminator('\r\n') + self.bind_socket() + + def bind_socket(self): + """ + if the socket is already bound we want to reuse this socket anyway + """ + log.error("not yet implemented") + sys.exit(23) + def collect_incoming_data(self,data): + self.data += data + + def found_terminator(self): + """ + TODO what happens if, for example in an html message we find our pattern a hundred times. + should the actions been called a hundred times or just once for the connect? + """ + log.debug('<< %s' % self.data) + + message = self.data + self.data = '' + if self.find_pattern(message): + self.start_action(message) + + def find_pattern(self,message): + """ returns true if own pattern is found""" + log.error("not yet implemented") + sys.exit(23) + + def start_action(self,message): + """ runs all the defined actions""" + log.error("not yet implemented") + sys.exit(23) -- cgit v1.2.3 From c614e25114faafb4795338ecb93c284835a677ce Mon Sep 17 00:00:00 2001 From: makefu Date: Sat, 17 Sep 2011 13:43:22 +0200 Subject: Reaktor/UDP: rewrite ubot replace the ubot code with acutally working code --- Reaktor/UDP/index | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'Reaktor/UDP/index') diff --git a/Reaktor/UDP/index b/Reaktor/UDP/index index 950f0d21..c46202b8 100755 --- a/Reaktor/UDP/index +++ b/Reaktor/UDP/index @@ -1,6 +1,6 @@ #!/usr/bin/python -from asycore import asychat +import asyncore, socket import logging import sys log = logging.getLogger('asybot') @@ -15,43 +15,48 @@ def enable_syslog(logger): hdlr.setFormatter(formatter) logger.addHandler(hdlr) -class ubot(asychat): +class ubot(asyncore.dispatcher): """ UDP Bot """ - def __init__(self, port,pattern,action,bind="0.0.0.0",terminator='\r\n'): - asychat.__init__(self) - self.bind = bind + def __init__(self, port,pattern,action,bind_addr="",): + asyncore.dispatcher.__init__(self) + self.bind_addr = bind_addr self.port = port self.data = '' - self.set_terminator('\r\n') self.bind_socket() def bind_socket(self): """ if the socket is already bound we want to reuse this socket anyway """ - log.error("not yet implemented") - sys.exit(23) - def collect_incoming_data(self,data): - self.data += data - - def found_terminator(self): - """ - TODO what happens if, for example in an html message we find our pattern a hundred times. - should the actions been called a hundred times or just once for the connect? - """ + + self.create_socket(socket.AF_INET,socket.SOCK_DGRAM) + self.set_reuse_addr() + self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) + log.info("Binding Socket at %s:%d" %(self.bind_addr,self.port)) + self.bind( (self.bind_addr,self.port) ) + + def handle_connect(self): + log.info("Server Started") + def handle_read(self): + self.data,addr = self.recvfrom(2048) log.debug('<< %s' % self.data) - message = self.data - self.data = '' - if self.find_pattern(message): - self.start_action(message) + if self.find_pattern(): + self.start_action() - def find_pattern(self,message): + def find_pattern(self): """ returns true if own pattern is found""" log.error("not yet implemented") sys.exit(23) - def start_action(self,message): + def start_action(self): """ runs all the defined actions""" log.error("not yet implemented") sys.exit(23) + +if __name__ == "__main__": + import os + lol = logging.DEBUG if os.environ.get('debug',False) else logging.INFO + logging.basicConfig(level=lol) + ubot(1337,r'',{}) + asyncore.loop() -- cgit v1.2.3 From 2a54f1cef0d547be126d77a315afdf5dff58d1c2 Mon Sep 17 00:00:00 2001 From: makefu Date: Sat, 17 Sep 2011 14:22:22 +0200 Subject: Reaktor/UDP: add matcher function for ubot --- Reaktor/UDP/index | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'Reaktor/UDP/index') diff --git a/Reaktor/UDP/index b/Reaktor/UDP/index index c46202b8..b7a21508 100755 --- a/Reaktor/UDP/index +++ b/Reaktor/UDP/index @@ -2,8 +2,9 @@ import asyncore, socket import logging +import re import sys -log = logging.getLogger('asybot') +log = logging.getLogger('ubot') def enable_syslog(logger): import logging.handlers as handlers @@ -15,6 +16,7 @@ def enable_syslog(logger): hdlr.setFormatter(formatter) logger.addHandler(hdlr) + class ubot(asyncore.dispatcher): """ UDP Bot """ def __init__(self, port,pattern,action,bind_addr="",): @@ -22,21 +24,25 @@ class ubot(asyncore.dispatcher): self.bind_addr = bind_addr self.port = port self.data = '' + self.pattern = pattern + self.action = action self.bind_socket() def bind_socket(self): """ if the socket is already bound we want to reuse this socket anyway """ - self.create_socket(socket.AF_INET,socket.SOCK_DGRAM) self.set_reuse_addr() - self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1) - log.info("Binding Socket at %s:%d" %(self.bind_addr,self.port)) + self.socket.setsockopt( + socket.SOL_SOCKET,socket.SO_BROADCAST,1) + log.info("Binding Socket at %s:%d" + %(self.bind_addr,self.port)) self.bind( (self.bind_addr,self.port) ) def handle_connect(self): log.info("Server Started") + def handle_read(self): self.data,addr = self.recvfrom(2048) log.debug('<< %s' % self.data) @@ -46,17 +52,25 @@ class ubot(asyncore.dispatcher): def find_pattern(self): """ returns true if own pattern is found""" - log.error("not yet implemented") - sys.exit(23) + log.debug("Pattern is %s" %self.pattern) + ret = re.search(self.pattern,self.data) + if ret: + log.info("Match \"%s\" with pattern \"%s\"" % ((ret.string.strip()),self.pattern)) + else: + log.info("No Match") + return ret + def start_action(self): """ runs all the defined actions""" + log.debug("Actions: %s" % str(self.action)) log.error("not yet implemented") sys.exit(23) if __name__ == "__main__": import os + #enable_syslog(log) lol = logging.DEBUG if os.environ.get('debug',False) else logging.INFO logging.basicConfig(level=lol) - ubot(1337,r'',{}) + ubot(1337,r'abc',{}) asyncore.loop() -- cgit v1.2.3 [cgit] Unable to lock slot /tmp/cgit/11300000.lock: Permission denied (13)