Index: trunk/debs/ircecho/ircecho |
— | — | @@ -8,33 +8,26 @@ |
9 | 9 | |
10 | 10 | import sys |
11 | 11 | import pyinotify |
12 | | -import ircbot |
13 | | -import irclib |
14 | 12 | import threading |
15 | | - |
16 | 13 | from optparse import OptionParser |
| 14 | +sys.path.append('/usr/ircecho/lib') |
| 15 | +from ircbot import SingleServerIRCBot |
17 | 16 | |
18 | 17 | class EchoReader(threading.Thread): |
19 | | - def __init__(self, bot, chans, infile=''): |
| 18 | + def __init__(self, infile=''): |
20 | 19 | threading.Thread.__init__(self) |
21 | | - self.bot = bot |
22 | | - self.chans = chans |
23 | 20 | self.infile = infile |
24 | 21 | |
25 | 22 | def run(self): |
26 | 23 | if self.infile: |
27 | 24 | print "Using infile" |
| 25 | + self.f = open(self.infile) |
| 26 | + # Seek to the end of the file |
| 27 | + self.f.seek(0,2) |
28 | 28 | wm = pyinotify.WatchManager() |
29 | | - mask = pyinotify.IN_MODIFY |
30 | | - f = open(self.infile) |
31 | | - # Seek to the end of the file |
32 | | - f.seek(0,2) |
33 | | - handler = EventHandler() |
34 | | - handler.infile = f |
35 | | - handler.bot = self.bot |
36 | | - handler.chans = self.chans |
37 | | - notifier = pyinotify.Notifier(wm, handler) |
38 | | - wdd = wm.add_watch(self.infile, mask) |
| 29 | + notifier = pyinotify.Notifier(wm) |
| 30 | + mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE |
| 31 | + wdd = wm.watch_transient_file(self.infile, mask, EventHandler) |
39 | 32 | notifier.loop() |
40 | 33 | else: |
41 | 34 | while True: |
— | — | @@ -48,10 +41,10 @@ |
49 | 42 | except Exception: |
50 | 43 | pass |
51 | 44 | |
52 | | -class EchoBot(ircbot.SingleServerIRCBot): |
| 45 | +class EchoBot(SingleServerIRCBot): |
53 | 46 | def __init__(self, chans, nickname, server): |
54 | 47 | print "*** Connecting to IRC server %s..." % server |
55 | | - ircbot.SingleServerIRCBot.__init__(self, [(server, 6667)], nickname, "IRC echo bot") |
| 48 | + SingleServerIRCBot.__init__(self, [(server, 6667)], nickname, "IRC echo bot") |
56 | 49 | self.chans = chans |
57 | 50 | |
58 | 51 | def on_nicknameinuse(self, c, e): |
— | — | @@ -59,14 +52,22 @@ |
60 | 53 | |
61 | 54 | def on_welcome(self, c, e): |
62 | 55 | print "*** Connected" |
63 | | - for chan in self.chans: |
| 56 | + for chan in [self.chans]: |
64 | 57 | c.join(chan) |
65 | 58 | |
66 | 59 | class EventHandler(pyinotify.ProcessEvent): |
67 | 60 | def process_IN_MODIFY(self, event): |
68 | | - s = self.infile.read() |
69 | | - self.bot.connection.privmsg(self.chans, s) |
| 61 | + s = reader.f.read() |
| 62 | + bot.connection.privmsg(bot.chans, s) |
70 | 63 | |
| 64 | + def process_IN_CREATE(self, event): |
| 65 | + try: |
| 66 | + print "Reopening file" |
| 67 | + reader.f = open(reader.infile) |
| 68 | + except IOError: |
| 69 | + print "Failed to reopen file" |
| 70 | + pass |
| 71 | + |
71 | 72 | parser = OptionParser(conflict_handler="resolve") |
72 | 73 | parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>") |
73 | 74 | parser.add_option("--infile", dest="infile", help="Read input from the specific file instead of from stdin") |
— | — | @@ -74,7 +75,9 @@ |
75 | 76 | chans = args[0] |
76 | 77 | nickname = args[1] |
77 | 78 | server = args[2] |
78 | | -bot = EchoBot([chans], nickname, server) |
79 | | -sthr = EchoReader(bot, chans, options.infile) |
80 | | -sthr.start() |
| 79 | +global bot |
| 80 | +bot = EchoBot(chans, nickname, server) |
| 81 | +global reader |
| 82 | +reader = EchoReader(options.infile) |
| 83 | +reader.start() |
81 | 84 | bot.start() |