Index: trunk/debs/ircecho/ircecho |
— | — | @@ -15,6 +15,9 @@ |
16 | 16 | from optparse import OptionParser |
17 | 17 | from ircbot import SingleServerIRCBot |
18 | 18 | |
| 19 | +global bot |
| 20 | +global reader |
| 21 | + |
19 | 22 | class EchoNotifier(threading.Thread): |
20 | 23 | def __init__(self, notifier): |
21 | 24 | threading.Thread.__init__(self) |
— | — | @@ -56,6 +59,9 @@ |
57 | 60 | self.notifiers = [] |
58 | 61 | self.associations = {} |
59 | 62 | self.files = {} |
| 63 | + # infiles format: |
| 64 | + # file[:channel,channel,...;file[:channel,channel,...];...] |
| 65 | + # , ; : can be escaped with \ |
60 | 66 | infiles = self.escape(self.infile) |
61 | 67 | for filechan in infiles.split(';'): |
62 | 68 | temparr = filechan.split(':') |
— | — | @@ -71,7 +77,7 @@ |
72 | 78 | pass |
73 | 79 | wm = pyinotify.WatchManager() |
74 | 80 | mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE |
75 | | - wm.watch_transient_file(filename, mask, EventHandler) |
| 81 | + wm.watch_transient_file(filename, mask, EchoHandler) |
76 | 82 | notifier = EchoNotifier(pyinotify.Notifier(wm)) |
77 | 83 | self.notifiers.append(notifier) |
78 | 84 | # Does this file have channel associations? |
— | — | @@ -82,6 +88,7 @@ |
83 | 89 | print "Starting notifier loop" |
84 | 90 | notifier.start() |
85 | 91 | else: |
| 92 | + # Read from stdin |
86 | 93 | while True: |
87 | 94 | try: |
88 | 95 | s = raw_input() |
— | — | @@ -92,6 +99,7 @@ |
93 | 100 | break; |
94 | 101 | except Exception: |
95 | 102 | pass |
| 103 | + |
96 | 104 | def readfile(self, filename): |
97 | 105 | if self.files[filename]: |
98 | 106 | return self.files[filename].read() |
— | — | @@ -102,6 +110,7 @@ |
103 | 111 | if filename in self.associations: |
104 | 112 | return self.associations[filename] |
105 | 113 | else: |
| 114 | + # If file has no associated channels, return all channels |
106 | 115 | return bot.chans |
107 | 116 | |
108 | 117 | class EchoBot(SingleServerIRCBot): |
— | — | @@ -118,14 +127,18 @@ |
119 | 128 | for chan in [self.chans]: |
120 | 129 | c.join(chan) |
121 | 130 | |
122 | | -class EventHandler(pyinotify.ProcessEvent): |
| 131 | +class EchoHandler(pyinotify.ProcessEvent): |
123 | 132 | def process_IN_MODIFY(self, event): |
124 | 133 | s = reader.readfile(event.pathname) |
125 | 134 | if s: |
126 | 135 | chans = reader.getchannels(event.pathname) |
127 | | - bot.connection.privmsg(chans, s) |
| 136 | + try: |
| 137 | + bot.connection.privmsg(chans, s) |
| 138 | + except Exception: |
| 139 | + pass |
128 | 140 | |
129 | 141 | def process_IN_CREATE(self, event): |
| 142 | + # File has been deleted and re-created |
130 | 143 | try: |
131 | 144 | print "Reopening file: " + event.pathname |
132 | 145 | reader.files[event.pathname] = open(event.pathname) |
— | — | @@ -133,16 +146,18 @@ |
134 | 147 | print "Failed to reopen file: " + event.pathname |
135 | 148 | pass |
136 | 149 | |
137 | | -parser = OptionParser(conflict_handler="resolve") |
138 | | -parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>") |
139 | | -parser.add_option("--infile", dest="infile", help="Read input from the specific file instead of from stdin") |
140 | | -(options, args) = parser.parse_args() |
141 | | -chans = args[0] |
142 | | -nickname = args[1] |
143 | | -server = args[2] |
144 | | -global bot |
145 | | -bot = EchoBot(chans, nickname, server) |
146 | | -global reader |
147 | | -reader = EchoReader(options.infile) |
148 | | -reader.start() |
149 | | -bot.start() |
| 150 | +def main(): |
| 151 | + parser = OptionParser(conflict_handler="resolve") |
| 152 | + parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>") |
| 153 | + parser.add_option("--infile", dest="infile", help="Read input from the specific file instead of from stdin") |
| 154 | + (options, args) = parser.parse_args() |
| 155 | + chans = args[0] |
| 156 | + nickname = args[1] |
| 157 | + server = args[2] |
| 158 | + bot = EchoBot(chans, nickname, server) |
| 159 | + reader = EchoReader(options.infile) |
| 160 | + reader.start() |
| 161 | + bot.start() |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |