minor network/socket changes

This commit is contained in:
Dpeta 2022-06-16 20:25:24 +02:00
parent 5c4d21640d
commit 1b479031fd
3 changed files with 31 additions and 29 deletions

12
irc.py
View file

@ -46,19 +46,16 @@ class PesterIRC(QtCore.QThread):
self.NickServ = services.NickServ()
self.ChanServ = services.ChanServ()
def IRCConnect(self):
with open(_datadir + "server.json", "r") as server_file:
read_file = server_file.read()
server_file.close()
server_obj = json.loads(read_file)
server = self.config.server()
port = self.config.port()
ssl = self.config.ssl()
self.cli = IRCClient(PesterHandler,
host=server,
port=int(port),
port=port,
nick=self.mainwindow.profile().handle,
real_name='pcc31',
timeout=120,
ssl=server_obj['TLS'])
ssl=ssl)
self.cli.command_handler.parent = self
self.cli.command_handler.mainwindow = self.mainwindow
self.cli.connect()
@ -79,7 +76,7 @@ class PesterIRC(QtCore.QThread):
self.cli.close()
self.stopIRC = "%s, %s" % (type(se), se)
return
except (OSError, IndexError) as se:
except (OSError, IndexError, ValueError) as se:
self.stopIRC = "%s, %s" % (type(se), se)
PchumLog.debug("socket error, exiting thread")
return
@ -543,6 +540,7 @@ class PesterHandler(DefaultCommandHandler):
#mychumhandle = self.mainwindow.profile().handle
mymood = self.mainwindow.profile().mood.value()
if not self.mainwindow.config.lowBandwidth():
time.sleep(0.413 + 0.097) # <--- somehow, this actually helps.
helpers.join(self.client, "#pesterchum")
# Moods via metadata
helpers.metadata(self.client, '*', 'sub', 'mood')

View file

@ -152,8 +152,11 @@ class IRCClient:
# socket.timeout is deprecated in 3.10
PchumLog.warning("TimeoutError in on send, " + str(e))
raise socket.timeout
except (OSError, IndexError) as e:
except (OSError,
IndexError,
ValueError) as e:
# Unknown error, might as well retry?
# index/value can happen if the socket breaks.
PchumLog.warning("Unkown error on send, " + str(e))
if tries >= 9:
raise e
@ -162,7 +165,7 @@ class IRCClient:
time.sleep(0.1)
PchumLog.debug("ready_to_write (len %s): " % str(len(ready_to_write)) + str(ready_to_write))
except OSError as se:
except Exception as se:
PchumLog.warning("socket.error %s" % str(se))
if not self.blocking and se.errno == 11:
pass
@ -176,8 +179,14 @@ class IRCClient:
if self.ssl == True:
context = ssl.create_default_context()
# Checking relies on the system
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
bare_sock = socket.create_connection(("%s" % self.host, self.port))
self.socket = context.wrap_socket(bare_sock, server_hostname=self.host, do_handshake_on_connect=False)
self.socket = context.wrap_socket(bare_sock,
server_hostname=self.host,
do_handshake_on_connect=False)
while True:
try:
self.socket.do_handshake()

View file

@ -369,9 +369,7 @@ with a backup from: <a href='%s'>%s</a></h3></html>" % (_datadir, self.filename,
read_file = server_file.read()
server_file.close()
server_obj = json.loads(read_file)
server = str(server_obj['server'])
#print("Server is: " + server)
return server
return server_obj['server']
except:
try:
with open(_datadir + "server.json", "w") as server_file:
@ -383,7 +381,6 @@ with a backup from: <a href='%s'>%s</a></h3></html>" % (_datadir, self.filename,
server_file.write(json.dumps(json_server_file, indent = 4) )
server_file.close()
server = "irc.pesterchum.xyz"
#print("Failed to read server, defaulting to irc.pesterchum.xyz")
except:
return self.config.get('server', "irc.pesterchum.xyz")
def port(self):
@ -394,24 +391,22 @@ with a backup from: <a href='%s'>%s</a></h3></html>" % (_datadir, self.filename,
read_file = server_file.read()
server_file.close()
server_obj = json.loads(read_file)
port = str(server_obj['port'])
#print("Port is: " + port)
port = server_obj['port']
return port
except:
return self.config.get('port', '6697')
## def TLS(self):
## if hasattr(self.parent, 'tlsOverride'):
## return self.parent.tlsOverride
## try:
## with open(_datadir + "server.json", "r") as server_file:
## read_file = server_file.read()
## server_file.close()
## server_obj = json.loads(read_file)
## TLS = str(server_obj['TLS'])
## print("TLS-status is: " + TLS)
## return TLS
## except:
## return self.config.get('TLS', True)
def ssl(self):
#if hasattr(self.parent, 'tlsOverride'):
# return self.parent.tlsOverride
try:
with open(_datadir + "server.json", "r") as server_file:
read_file = server_file.read()
server_file.close()
server_obj = json.loads(read_file)
return server_obj['TLS']
except:
return self.config.get('TLS', True)
def soundOn(self):
if 'soundon' not in self.config:
self.set('soundon', True)