示例#1
0
class UDP(Object):
    """ UDP class to echo txt through the bot, use the meds-udp program to send. """
    def __init__(self):
        super().__init__(self)
        self._cfg = Config(udp)
        self._cfg.load("udp")
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        self._sock.setblocking(1)
        self._status = "start"
        self._starttime = time.time()

    def start(self):
        """ start the UDP server. """
        logging.info("! start udp %s:%s" % (self._cfg.host, self._cfg.port))
        self._sock.bind((self._cfg.host, self._cfg.port))
        self.ready()
        self._status = "ok"
        while self._status:
            (txt, addr) = self._sock.recvfrom(64000)
            if not self._status: break
            data = str(txt.rstrip(), "utf-8")
            if not data: break
            self.output(data, addr)
        logging.info("! stop udp %s:%s" % (self._cfg.host, self._cfg.port))

    def exit(self):
        """ shutdown the UDP server. """
        self._status = ""
        self._sock.settimeout(1.0)
        self._sock.sendto(bytes("bla", "utf-8"),
                          (self._cfg.host, self._cfg.port))

    def output(self, txt, addr=None):
        """ output to all bots on fleet. """
        (passwd, text) = txt.split(" ", 1)
        text = text.replace("\00", "")
        if passwd == self._cfg.password:
            for bot in fleet:
                bot.announce(text)
示例#2
0
class Bot(Engine):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.channels = []
        self._cfg = Config()
        self._cfg.load(sname(self).lower())
        if not self._cfg.server: self._cfg.server = "host"

    def announce(self, txt):
        if self._cfg.silent: return
        if not self.channels:
            self.out(txt)
        for channel in self.channels:
            self.say(channel, txt)

    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 connect(self):
        pass

    def dispatch(self, event):
        event._server = self._cfg.server
        event.parse()
        event.dispatch()
        event.wait()
        #self.show(event)
        event.show()

    def join(self, channel):
        pass

    def joinall(self):
        for channel in self.channels:
            self.join(channel)

    def out(self, txt):
        print(txt)

    def prompt(self):
        if kernel._cfg.colors: txt = "%s>%s " % (YELLOW, ENDC)
        else: txt = "> "
        sys.stdout.write(txt)
        sys.stdout.flush()

    def run_forever(self):
        self.start()
        if not kernel._cfg.shell:
            if kernel._cfg.args:
                self.cmnd(" ".join(kernel._cfg.args))
        else:
            self.prompt()
            self.wait()

    def say(self, channel, txt):
        self.out(txt)

    def show(self, event):
        for txt in event._result:
            self.say(event.channel, txt)

    def start(self):
        if self not in fleet: fleet.append(self)
        super().start()