Refactor parserTests so the class can be used by another script
[mediawiki.git] / irc / mxircecho.py
blobb8b38332bb528bf5e250e415ae25695467c3316f
1 #! /usr/bin/env python
3 # usage: mxircecho.py nickname server
4 import sys
5 sys.path.append('/home/kate/pylib/lib/python2.2/site-packages')
7 from ircbot import SingleServerIRCBot
8 import threading
10 class EchoReader(threading.Thread):
11 def __init__(self, bot):
12 threading.Thread.__init__(self)
13 self.abot = bot
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]
24 if channel not in bot.chans:
25 bot.chans.append(channel)
26 bot.connection.join(channel)
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
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
43 def on_nicknameinuse(self, c, e):
44 c.nick(c.get_nickname() + "_")
46 def on_welcome(self, c, e):
47 print "*** Connected"
48 for chan in self.chans:
49 c.join(chan)
51 bot = EchoBot([], sys.argv[1], sys.argv[2]);
52 sthr = EchoReader(bot)
53 sthr.start()
54 bot.start()