162 self.send ("%s\n" % line) |
166 self.send ("%s\n" % line) |
163 self.send_buffer = [] |
167 self.send_buffer = [] |
164 |
168 |
165 def handle_read (self): |
169 def handle_read (self): |
166 lines = self.recv (4096).splitlines() |
170 lines = self.recv (4096).splitlines() |
167 for line in lines: |
171 for utfline in lines: |
|
172 line = utfline.decode("utf-8").encode("ascii","ignore") |
168 print "[%s] -> %s" % (self.name, line) |
173 print "[%s] -> %s" % (self.name, line) |
169 |
174 |
170 if line.startswith ("PING :"): |
175 if line.startswith ("PING :"): |
171 self.write ("PONG :%s" % line[6:]) |
176 self.write ("PONG :%s" % line[6:]) |
172 continue |
177 continue |
239 elif command == "msg": |
244 elif command == "msg": |
240 check_admin (sender, ident, host, command) |
245 check_admin (sender, ident, host, command) |
241 if len(args) < 2: |
246 if len(args) < 2: |
242 raise logical_exception ("usage: .%s <target> <message...>" % command) |
247 raise logical_exception ("usage: .%s <target> <message...>" % command) |
243 self.privmsg (args[0], " ".join (args[1:])) |
248 self.privmsg (args[0], " ".join (args[1:])) |
244 elif command == "ticket": |
249 elif command == 'ticket': |
245 if len(args) != 1: |
250 if len(args) != 1: |
246 raise logical_exception ("usage: .%s <ticket>" % command) |
251 raise logical_exception ("usage: .%s <ticket>" % command) |
247 self.get_ticket_data (replyto, args[0], True) |
252 self.get_ticket_data (replyto, args[0], True) |
|
253 elif command == 'idgames': |
|
254 try: |
|
255 if len(args) < 1: |
|
256 raise logical_exception ('usage: .%s <keywords>' % command) |
|
257 |
|
258 url = g_idgamesSearchURL % urllib.quote (" ".join (args[0:])) |
|
259 response = urllib2.urlopen (url).read() |
|
260 data = json.loads (response) |
|
261 |
|
262 if 'content' in data and 'file' in data['content']: |
|
263 if type (data['content']['file']) is list: |
|
264 files = data['content']['file'] |
|
265 else: |
|
266 files = [data['content']['file']] |
|
267 |
|
268 i = 0 |
|
269 for filedata in files: |
|
270 if i >= 5: |
|
271 break |
|
272 |
|
273 self.privmsg (replyto, '- %s: \'%s\' by \'%s\', rating: %s: %s' % \ |
|
274 (filedata['filename'], filedata['title'], filedata['author'], filedata['rating'], filedata['url'])) |
|
275 |
|
276 i += 1 |
|
277 self.privmsg (replyto, "(%d / %d results posted)" % (i, len(files))) |
|
278 elif 'warning' in data and 'message' in data['warning']: |
|
279 raise logical_exception (data['warning']['message']) |
|
280 elif 'error' in data and 'message' in data['error']: |
|
281 raise logical_exception (data['error']['message']) |
|
282 else: |
|
283 raise logical_exception ("Incomplete JSON response from doomworld.com/idgames") |
|
284 except logical_exception as e: |
|
285 raise e |
|
286 except Exception as e: |
|
287 raise logical_exception ('Search failed: %s' % `e`) |
248 else: |
288 else: |
249 raise logical_exception ("unknown command `.%s`" % command) |
289 raise logical_exception ("unknown command `.%s`" % command) |
250 |
290 |
251 def handle_error(self): |
291 def handle_error(self): |
252 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) |
292 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) |