Rev 233 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
214 | szabot | 1 | #! /usr/bin/env python |
2 | # |
||
3 | # Example program using ircbot.py. |
||
4 | # |
||
5 | # Joel Rosdahl <joel@rosdahl.net> |
||
6 | |||
7 | """A simple example bot. |
||
8 | |||
9 | This is an example bot that uses the SingleServerIRCBot class from |
||
10 | ircbot.py. The bot enters a channel and listens for commands in |
||
11 | private messages and channel traffic. Commands in channel messages |
||
12 | are given by prefixing the text by the bot name followed by a colon.""" |
||
13 | |||
14 | from ircbot import SingleServerIRCBot |
||
15 | from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr |
||
16 | from tsimapiak import translate |
||
17 | |||
18 | class Bot(SingleServerIRCBot): |
||
19 | def __init__(self, channel, nickname, server, port=6667): |
||
20 | SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) |
||
21 | self.channel = channel |
||
22 | |||
23 | def on_nicknameinuse(self, c, e): |
||
24 | c.nick(c.get_nickname() + "_") |
||
25 | |||
26 | def on_welcome(self, c, e): |
||
27 | c.join(self.channel) |
||
28 | c.privmsg("NiceBot", "asztallab") |
||
29 | |||
30 | def on_privmsg(self, c, e): |
||
31 | self.do_command(e, e.arguments()[0],True) |
||
32 | |||
33 | def on_pubmsg(self, c, e): |
||
34 | a = e.arguments()[0] |
||
35 | if a[0] == "!": |
||
36 | self.do_command(e, a[1:].strip(),False) |
||
37 | return |
||
38 | |||
39 | def do_command(self, e, cmd, priv): |
||
40 | try: |
||
41 | cmd = cmd.decode("utf-8") |
||
42 | except: |
||
43 | cmd = cmd.decode("iso-8859-1") |
||
44 | if priv: |
||
45 | nick = nm_to_n(e.source()) |
||
46 | else: |
||
47 | nick = self.channel |
||
48 | c = self.connection |
||
49 | |||
50 | if (cmd.split(" ")[0] == "tr") or (cmd.split(" ")[0] == "translate"): |
||
51 | lang = "eng" |
||
52 | if cmd.split(" ")[1].startswith("-"): |
||
53 | if cmd.split(" ")[1][1:] in ("hu", "de", "ptbr", "est"): |
||
54 | lang = cmd.split(" ")[1][1:] |
||
55 | sent = " ".join(cmd.split(" ")[2:]) |
||
56 | else: |
||
57 | sent = " ".join(cmd.split(" ")[1:]) |
||
58 | translated = [] |
||
59 | for word in translate.translatesent(sent, lang): |
||
60 | translated.append(word["translated"]) |
||
61 | translated = nm_to_n(e.source()) + ": " + " | ".join(translated) |
||
62 | c.privmsg(nick, translated.encode("utf-8")) |
||
63 | |||
64 | def main(): |
||
65 | #bot = Bot("#tim32", "TsimApiak", "irc.tim32.org", 6667) |
||
66 | bot = Bot("#na'vi", "TsimApiak", "irc.learnnavi.org", 6667) |
||
67 | bot.start() |
||
68 | |||
69 | if __name__ == "__main__": |
||
70 | main() |