Port decode fix and disconnect detection to wikibot
This commit is contained in:
parent
67bc1d1b54
commit
ddaaaafa3f
28
bot.py
28
bot.py
|
@ -17,8 +17,24 @@ class Socket:
|
|||
if lines[-1]:
|
||||
self._read_buffer=lines[-1]
|
||||
lines.pop(-1)
|
||||
lines = [line.decode("utf-8") for line in lines]
|
||||
return lines
|
||||
out = []
|
||||
for line in lines:
|
||||
# try to decode as UTF-8, then CP1252, then ISO-8859-1
|
||||
# if none of them work then just ignore the line
|
||||
try:
|
||||
line = line.decode("utf-8")
|
||||
out.append(line)
|
||||
except:
|
||||
try:
|
||||
line = line.decode("cp1252")
|
||||
out.append(line)
|
||||
except:
|
||||
try:
|
||||
line = line.decode("iso-8859-1")
|
||||
out.append(line)
|
||||
except:
|
||||
continue
|
||||
return out
|
||||
def send(self,line):
|
||||
self.sock.send(line.encode("utf-8"))
|
||||
def close(self):
|
||||
|
@ -166,9 +182,11 @@ class IRCBot:
|
|||
self.running=True
|
||||
while self.running:
|
||||
lines = self.socket.read()
|
||||
if lines:
|
||||
for line in lines:
|
||||
self.handle_line(line)
|
||||
if lines is None: # socket dead for some reason
|
||||
self.running = False
|
||||
continue # exit loop
|
||||
for line in lines:
|
||||
self.handle_line(line)
|
||||
self.socket.close()
|
||||
del self.socket
|
||||
|
||||
|
|
Loading…
Reference in New Issue