def event(self): event = Event() event.btype = sname(self) event.txt = str(os.read(fd, 512), "utf-8") event.txt = event.txt.rstrip() event.origin = "root@shell" yield event
def event(self): e = Event() e.server = self._cfg.server e.btype = sname(self) e.txt = input() e.txt = e.txt.rstrip() e.origin = "root@shell" return e
def dccloop(self, event, s): sockfile = s.makefile('rw') #s.setblocking(True) self.register_fd(s) while 1: try: res = sockfile.readline() if not res: break res = res.rstrip() logging.info("< %s %s" % (event.origin, res)) e = Event() e.btype = "DCC" e.txt = res e.outer = sockfile e.origin = event.origin super().dispatch(e) except socket.timeout: time.sleep(0.01) except socket.error as ex: if ex.errno in [ socket.EAGAIN, ]: continue else: raise except Exception as ex: logging.error(get_exception()) sockfile.close()
def event(self, txt=""): e = Event(txt=txt) e.txt = txt e.origin = "test@bot" e._bot = self e.parse() return e
def timer(event): """ echo txt on a given time. """ if not event._parsed.rest: return seconds = 0 line = "" for word in event._parsed.args: if word.startswith("+"): try: seconds = int(word[1:]) except: event.reply("%s is not an integer" % seconds) return else: line += word + " " if seconds: target = time.time() + seconds else: try: target = get_day(event._parsed.rest) except ENODATE: target = to_day(day()) hour = get_hour(event._parsed.rest) if hour: target += hour if not target or time.time() > target: event.reply("already passed given time.") return e = Event() e.services = "clock" e.prefix = "timer" e.txt = event._parsed.rest e.time = target e.done = False e.save() timer = Timer(target, e.reply, e.txt) launcher.launch(timer.start) event.ok(time.ctime(target))
def event(self): event = Event() event._bot = self event.txt = input(self.prompt()) event.origin = "root@shell" event.parse() return event
def cmnd(self, txt): event = Event() event._server = self._cfg.server event.btype = sname(self) event.origin = "user@bot" event.txt = txt self.dispatch(event) return event
def testcmnds(event): cfg.workdir = "test.data" for cmnd in sorted(kernel._handlers.keys()): e = Event() e.btype = event.btype e.txt = "%s %s" % (cmnd, randomname()) e.origin = "test@bot" e.parse() event.reply("put %s" % e.txt) kernel.put(e)
def cmnd(self, txt, bot=None): from meds.event import Event event = Event() event.origin = "user@bot" event._bot = self event.txt = txt event.parse() scheduler.put(event) return event
def cmndrun(event): for cmnd in sorted(list(cmnds.handlers.keys())): if cmnd in exclude: continue e = Event() e.txt = ("!" + cmnd) e._bot = event._bot e.workdir = "test.data" e.origin = "test@bot" e.parse() logging.warn("< %s" % e.txt) scheduler.dispatch(e)
def test_cmnds(self): for cmnd in sorted(cmnds.handlers.keys()): if cmnd in ["test", "fetcher"]: continue event = Event() event.txt = cmnd + " arg1" event._bot = bot event.origin = "tester@bot" event.parse() logging.warn("< %s" % event.txt) func = cmnds.get(event.cmnd) if func: launcher.launch(func, event) events.append(event) for event in events: event.wait(3.0)
def parse(self, txt): rawstr = str(txt) logging.debug("< %s/parse %s" % (self.type, rawstr)) obj = Event() obj._bot = self obj.arguments = rawstr.split(":")[0].split() if not obj.arguments: obj.arguments = rawstr.split(":")[1].split() obj.txt = rawstr.split(":", 2)[-1] if rawstr[0] == ":": obj.origin = obj.arguments[0] ; obj.cmnd = obj.arguments[1] else: obj.origin = self.server ; obj.cmnd = obj.arguments[0] try: obj.nick, obj.userhost = obj.origin.split("!") except: pass obj.target = obj.arguments[-1] if obj.target.startswith("#"): obj.channel = obj.target return obj
def parsing(self, txt): rawstr = str(txt) obj = Event() obj.server = self._cfg.server obj.cc = self.cc obj.btype = sname(self) obj.arguments = [] arguments = rawstr.split() obj.origin = arguments[0] if obj.origin.startswith(":"): obj.origin = obj.origin[1:] if len(arguments) > 1: obj.command = arguments[1] if len(arguments) > 2: txtlist = [] adding = False for arg in arguments[2:]: if arg.startswith(":"): adding = True txtlist.append(arg[1:]) continue if adding: txtlist.append(arg) else: obj.arguments.append(arg) obj.txt = " ".join(txtlist) else: obj.command = obj.origin obj.origin = self._cfg.server try: obj.nick, obj.userhost = obj.origin.split("!") except: pass if obj.arguments: obj.target = obj.arguments[-1] else: obj.target = "" if obj.target.startswith("#"): obj.channel = obj.target if not obj.txt and len(arguments) == 1: obj.txt = arguments[1] if not obj.txt: obj.txt = rawstr.split(":")[-1] return obj
def timer(event): if not event.rest: return day = get_day(event.rest) or today() hour = get_hour(event.rest) target = day + get_hour(event.rest) if time.time() > target: event.reply("already passed given time.") ; return event.reply("time is %s" % time.ctime(target)) e = Event() e._bot = event._bot e.services = "clock" e.prefix = "timer" e.txt = event.rest e.time = target e.done = False e.save() timer = Timer(target, e.parse()) launcher.launch(timer.start) e.ok()
def presenced(self, data): from meds.mods import cmnds logging.debug("< %s.read %s" % (self.type, data)) o = Event() o.update(data) o._bot = self o["from"] = str(o["from"]) o.origin = o["from"] if "txt" not in o: o.txt = "" o.element = "presence" if o.type == 'subscribe': pres = Event({'to': o["from"], 'type': 'subscribed'}) self.client.send_presence(pres) pres = Event({'to': o["from"], 'type': 'subscribe'}) self.client.send_presence(pres) elif o.type == "unavailable" and o.origin in self.channels: self.channels.remove(o.origin) elif o.origin != self.user and o.origin not in self.channels: self.channels.append(o.origin) o.no_dispatch = True logging.info("< %s.%s %s" % (self.type, o.type, o.origin)) o.parse() func = cmnds.get(o.cmnd) if func: func(o)
def messaged(self, data): from meds.mods import cmnds logging.debug("< %s.read %s" % (self.type, data)) m = Event() m.update(data) m._bot = self if m.type == "error": logging.error("^ %s" % m.error) ; return m.cc = self.cc m["from"] = str(m["from"]) if self.user in m["from"]: logging.info("< %s.%s %s" % (self.type, m.type, m["from"])) ; return m.origin = m["from"] m.channel = m.origin m.to = m.origin m.element = "message" m.txt = m["body"] if '<delay xmlns="urn:xmpp:delay"' in str(data): logging.info("# %s.ignore %s %s" % (self.type, m.type, m.origin)) return logging.info("< %s.%s %s" % (self.type, m.type, m.origin)) m.parse() func = cmnds.get(m.cmnd) if func: func(m)
def dccloop(self, event, s): from meds.mods import cmnds sockfile = s.makefile('rw') s.setblocking(True) while 1: try: res = sockfile.readline() if not res: break res = res.rstrip() logging.info("< %s.loop %s" % (self.type, event.origin)) e = Event() e._bot = self e.txt = res e.outer = sockfile e.origin = event.origin e.parse() func = cmnds.get(e.cmnd) if func: func(e) except socket.timeout: time.sleep(0.01) except socket.error as ex: if ex.errno in [socket.EAGAIN, ]: continue else: raise except Exception as ex: logging.error(get_exception()) sockfile.close()