Пример #1
0
 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
Пример #2
0
 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
Пример #3
0
 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()
Пример #4
0
 def event(self, txt=""): 
     e = Event(txt=txt)
     e.txt = txt
     e.origin = "test@bot"
     e._bot = self
     e.parse()
     return e
Пример #5
0
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))
Пример #6
0
Файл: cli.py Проект: bthate/meds
 def event(self):
     event = Event()
     event._bot = self
     event.txt = input(self.prompt())
     event.origin = "root@shell"
     event.parse()
     return event
Пример #7
0
 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
Пример #8
0
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)
Пример #9
0
    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
Пример #10
0
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)
Пример #11
0
 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) 
Пример #12
0
Файл: irc.py Проект: bthate/meds
 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
Пример #13
0
 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
Пример #14
0
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()
Пример #15
0
 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)
Пример #16
0
 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)
Пример #17
0
Файл: irc.py Проект: bthate/meds
 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()