Subversion Repositories navi

Rev

Rev 235 | Rev 247 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#! /usr/bin/env python
#
# Example program using ircbot.py.
#
# Joel Rosdahl <joel@rosdahl.net>

"""A simple example bot.

This is an example bot that uses the SingleServerIRCBot class from
ircbot.py.  The bot enters a channel and listens for commands in
private messages and channel traffic.  Commands in channel messages
are given by prefixing the text by the bot name followed by a colon."""


from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
from tsimapiak import translate

class Bot(SingleServerIRCBot):
    def __init__(self, channel, nickname, server, port=6667):
        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.channel = channel

    def on_nicknameinuse(self, c, e):
        c.nick(c.get_nickname() + "_")

    def on_welcome(self, c, e):
        c.join(self.channel)
        c.privmsg("NiceBot", "asztallab")

    def on_privmsg(self, c, e):
        self.do_command(e, e.arguments()[0],True)

    def on_pubmsg(self, c, e):
        a = e.arguments()[0]
        if a[0] == "!":
            self.do_command(e, a[1:].strip(),False)
        return

    def do_command(self, e, cmd, priv):
        try:
            cmd = cmd.decode("utf-8")
        except:
            cmd = cmd.decode("iso-8859-1")
        if priv:
            nick = nm_to_n(e.source())
        else:
            nick = self.channel
        c = self.connection

        if (cmd.split(" ")[0] == "tr") or (cmd.split(" ")[0] == "translate"):
            lang = "eng"
            if len(cmd.split(" ")) > 1 and cmd.split(" ")[1].startswith("-"):
                if cmd.split(" ")[1][1:] in ("hu", "de", "ptbr", "est", "sv"):
                    lang = cmd.split(" ")[1][1:]
                sent = " ".join(cmd.split(" ")[2:])
            else:
                sent = " ".join(cmd.split(" ")[1:])
            translated = []
            for word in translate.translatesent(sent, lang):
                translated.append(word["translated"])
            translated = nm_to_n(e.source()) + ": " + " | ".join(translated)
            c.privmsg(nick, translated.encode("utf-8"))

def main():
    #bot = Bot("#tim32", "TsimApiak", "irc.tim32.org", 6667)
    bot = Bot("#na'vi", "TsimApiak", "irc.learnnavi.org", 6667)
    bot.start()

if __name__ == "__main__":
    main()