i always forget this..
[lhc/web/wiklou.git] / irc / mxircecho.py
1 #! /usr/bin/env python
2 #
3 # usage: mxircecho.py nickname server
4 import sys
5 sys.path.append('/home/kate/pylib/lib/python2.2/site-packages')
6
7 from ircbot import SingleServerIRCBot
8 import threading
9
10 class EchoReader(threading.Thread):
11 def __init__(self, bot):
12 threading.Thread.__init__(self)
13 self.abot = bot
14
15 def run(self):
16 while True:
17 try:
18 s = raw_input()
19 sp = s.split("\t")
20 if len(sp) == 2:
21 channel = sp[0]
22 text = sp[1]
23
24 if channel not in bot.chans:
25 bot.chans.append(channel)
26 bot.connection.join(channel)
27
28
29 # this throws an exception if not connected.
30 bot.connection.privmsg(channel, text)
31 except EOFError:
32 # Once the input is finished, the bot should exit
33 break
34 except:
35 pass
36
37 class EchoBot(SingleServerIRCBot):
38 def __init__(self, chans, nickname, server):
39 print "*** Connecting to IRC server %s..." % server
40 SingleServerIRCBot.__init__(self, [(server, 6667)], nickname, "IRC echo bot")
41 self.chans = chans
42
43 def on_nicknameinuse(self, c, e):
44 c.nick(c.get_nickname() + "_")
45
46 def on_welcome(self, c, e):
47 print "*** Connected"
48 for chan in self.chans:
49 c.join(chan)
50
51 bot = EchoBot([], sys.argv[1], sys.argv[2]);
52 sthr = EchoReader(bot)
53 sthr.start()
54 bot.start()