Subversion Repositories navi

Rev

Rev 246 | Rev 270 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
214 szabot 1
#! /usr/bin/env python
247 muzer 2
#    This file is part of Tsim Apiak.
214 szabot 3
#
247 muzer 4
#    Tsim Apiak is free software: you can redistribute it and/or modify
5
#    it under the terms of the GNU General Public Licence as published by
6
#    the Free Software Foundation, either version 3 of the Licence, or
7
#    (at your option) any later version.
214 szabot 8
#
247 muzer 9
#    In addition to this, you must also comply with clause 4 of the
10
#    Apache Licence, version 2.0, concerning attribution. Where there
11
#    is a contradiction between the two licences, the GPL
12
#    takes preference.
13
#
14
#    Tsim Apiak is distributed in the hope that it will be useful,
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
#    GNU General Public License for more details.
18
#
19
#    You should have received a copy of the GNU General Public License
20
#    along with Tsim Apiak.  If not, see <http://www.gnu.org/licenses/>.#
214 szabot 21
 
22
from ircbot import SingleServerIRCBot
246 szabot 23
from irclib import nm_to_n
214 szabot 24
from tsimapiak import translate
25
 
26
class Bot(SingleServerIRCBot):
27
    def __init__(self, channel, nickname, server, port=6667):
28
        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
29
        self.channel = channel
30
 
31
    def on_nicknameinuse(self, c, e):
32
        c.nick(c.get_nickname() + "_")
33
 
34
    def on_welcome(self, c, e):
35
        c.join(self.channel)
36
        c.privmsg("NiceBot", "asztallab")
37
 
38
    def on_privmsg(self, c, e):
246 szabot 39
        self.do_command(e, e.arguments()[0], True)
214 szabot 40
 
41
    def on_pubmsg(self, c, e):
42
        a = e.arguments()[0]
43
        if a[0] == "!":
246 szabot 44
            self.do_command(e, a[1:].strip(), False)
214 szabot 45
        return
46
 
47
    def do_command(self, e, cmd, priv):
48
        try:
49
            cmd = cmd.decode("utf-8")
50
        except:
51
            cmd = cmd.decode("iso-8859-1")
52
        if priv:
53
            nick = nm_to_n(e.source())
54
        else:
55
            nick = self.channel
56
        c = self.connection
57
 
58
        if (cmd.split(" ")[0] == "tr") or (cmd.split(" ")[0] == "translate"):
59
            lang = "eng"
238 muzer 60
            if len(cmd.split(" ")) > 1 and cmd.split(" ")[1].startswith("-"):
235 muzer 61
                if cmd.split(" ")[1][1:] in ("hu", "de", "ptbr", "est", "sv"):
214 szabot 62
                    lang = cmd.split(" ")[1][1:]
63
                sent = " ".join(cmd.split(" ")[2:])
64
            else:
65
                sent = " ".join(cmd.split(" ")[1:])
66
            translated = []
67
            for word in translate.translatesent(sent, lang):
68
                translated.append(word["translated"])
69
            translated = nm_to_n(e.source()) + ": " + " | ".join(translated)
70
            c.privmsg(nick, translated.encode("utf-8"))
71
 
72
def main():
73
    #bot = Bot("#tim32", "TsimApiak", "irc.tim32.org", 6667)
74
    bot = Bot("#na'vi", "TsimApiak", "irc.learnnavi.org", 6667)
75
    bot.start()
76
 
77
if __name__ == "__main__":
78
    main()