Rev 278 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
#! /usr/bin/env python
# This file is part of Tsim Apiak.
#
# Tsim Apiak is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public Licence as published by
# the Free Software Foundation, either version 3 of the Licence, or
# (at your option) any later version.
#
# In addition to this, you must also comply with clause 4 of the
# Apache Licence, version 2.0, concerning attribution. Where there
# is a contradiction between the two licences, the GPL
# takes preference.
#
# Tsim Apiak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tsim Apiak. If not, see <http://www.gnu.org/licenses/>.#
from irc.bot import SingleServerIRCBot
from irc.client import nm_to_n
from tsimapiak import translate
from threading import *
class Bot(SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
def pingit(self, c):
c.ping("testing")
t = Timer(30, self.pingit, [c])
t.start()
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")
t = Timer(30, self.pingit, [c])
t.start()
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 = "en"
if len(cmd.split(" ")) > 1 and cmd.split(" ")[1].startswith("-"):
if cmd.split(" ")[1][1:] in ("hu", "de", "ptbr", "est", "sv", "nl"):
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)
def main():
#bot = Bot("#tim32", "TsimApiak", "irc.tim32.org", 6667)
bot = Bot("#na'vi", "TsimApiak", "ikranakel.learnnavi.org", 6667)
bot.start()
if __name__ == "__main__":
main()