From e07e685522312b9d2e811ca629acc71dab8033c3 Mon Sep 17 00:00:00 2001
From: Dpeta <jasprose@protonmail.com>
Date: Thu, 25 Mar 2021 23:08:08 +0100
Subject: [PATCH] Changed the way Pesterchum quits + TLS fix.

---
 CHANGELOG.md        |  2 ++
 TODO.md             |  7 ++++---
 irc.py              |  2 ++
 oyoyo/client.py     | 19 ++++++++++++++++---
 oyoyo/cmdhandler.py |  1 +
 oyoyo/helpers.py    |  7 ++++---
 pesterchum.py       |  6 ++++--
 7 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index db59aba..739d20c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
 - Added styleing/markup to "PESTER" and "ADD GROUP" menu options and some other previously unstyled elements :)
 - Added pesterchum.spec for use with pyinstaller.
 - Wrapped socket in SSL context and changed the port appropriately, hostname verification is turned off.
+- Pesterchum now sends a ``QUIT :reason`` to the server when shutting down instead of just quitting instantly.
 
 ### Changed
 - Transitioned to Python 3.
@@ -17,6 +18,7 @@
 ### Fixed
 - Fixed sRGB profile issue with certain images.
 - Fixed issue where Pesterchum crashed if a quirk was malformed.
+- Fixed Pesterchum icon getting stuck on the system tray even after shutdown on windows.
 
 ### Deprecated
 - Removed update system (it seemed to be non-functional).
diff --git a/TODO.md b/TODO.md
index 923c859..110dcdc 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,11 +1,12 @@
 # TODO LIST : )
+~~Crossed out~~ entries are completed :3c
 ## ADD
  - A proper prompt for choosing a server.
 
 ## FIX
-- Pesterchum gets stuck on the system tray even after shutdown on windows.
+- ~~Pesterchum gets stuck on the system tray even after shutdown on windows.~~
 - Audio mixer slider in options doesn't always work. (Not working on linux?)
-- QUIT not send to server on shutdown.
-- TLS randomly causing socket to break.
+- ~~QUIT not send to server on shutdown.~~
+- ~~TLS randomly causing socket to break.~~
 - Make setup.py work with 32-bit python.
 - Make setup.py work with py2app (might not be required if pyinstaller works better!!).
\ No newline at end of file
diff --git a/irc.py b/irc.py
index 2a50e09..9b3e3d3 100644
--- a/irc.py
+++ b/irc.py
@@ -420,6 +420,8 @@ class PesterHandler(DefaultCommandHandler):
         mychumhandle = self.mainwindow.profile().handle
         mymood = self.mainwindow.profile().mood.value()
         if not self.mainwindow.config.lowBandwidth():
+            from time import sleep
+            sleep(0.5) # To prevent TLS from dying </3
             helpers.join(self.client, "#pesterchum")
             helpers.msg(self.client, "#pesterchum", "MOOD >%d" % (mymood))
             
diff --git a/oyoyo/client.py b/oyoyo/client.py
index 163973a..ef1c581 100644
--- a/oyoyo/client.py
+++ b/oyoyo/client.py
@@ -84,8 +84,8 @@ class IRCClient:
         """
         self.context = ssl.create_default_context()
         self.context.check_hostname = False
-        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        self.socket = self.context.wrap_socket(self.socket)
+        self.bare_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.socket = self.context.wrap_socket(self.bare_socket)
         self.nick = None
         self.real_name = None
         self.host = None
@@ -221,9 +221,22 @@ class IRCClient:
         # with extreme prejudice
         if self.socket:
             logging.info('shutdown socket')
+            #print("shutdown socket")
             self._end = True
-            self.socket.shutdown(socket.SHUT_RDWR)
+            self.socket.shutdown(socket.SHUT_WR)
+            self.socket.close()
+            
+    def simple_send(self, message):
+        self.socket.send(bytes(message, "UTF-8"))
 
+    def quit(self, msg):
+        # I am going mad :)
+        # Why does this only work 33% of the time </3
+
+        # Somehow, kinda fixed :')
+        print("QUIT")
+        self.socket.send(bytes(msg + "\n", "UTF-8"))
+        
 class IRCApp:
     """ This class manages several IRCClient instances without the use of threads.
     (Non-threaded) Timer functionality is also included.
diff --git a/oyoyo/cmdhandler.py b/oyoyo/cmdhandler.py
index e54b74c..4d7fb42 100644
--- a/oyoyo/cmdhandler.py
+++ b/oyoyo/cmdhandler.py
@@ -101,6 +101,7 @@ class CommandHandler(object):
             return
 
         logging.debug('f %s' % f)
+        #logging.info(*args)
 
         # Because more than 5 arguments can be passed by channelmodeis
         try:
diff --git a/oyoyo/helpers.py b/oyoyo/helpers.py
index 62347d4..d12b929 100644
--- a/oyoyo/helpers.py
+++ b/oyoyo/helpers.py
@@ -77,9 +77,10 @@ def cs(cli, *args):
 def identify(cli, passwd, authuser="NickServ"):
     msg(cli, authuser, "IDENTIFY %s" % passwd)
 
-def quit(cli, msg='gone'):
-    #print(msg)
-    cli.send("QUIT :%s" % msg)
+def quit(cli, msg):
+    msg = "QUIT :%s" % msg
+    cli.quit(msg)
+    cli.close()
     cli._end = 1
 
 def user(cli, username, realname=None):
diff --git a/pesterchum.py b/pesterchum.py
index 671d744..4049abb 100644
--- a/pesterchum.py
+++ b/pesterchum.py
@@ -2934,10 +2934,12 @@ class PesterWindow(MovingWindow):
         # This seriously needs to be fixed but I don't feel like it </3
         pesterchum.irc.quit_dc()    # Actually send QUIT to server
         pesterchum.trayicon.hide()  # Hopefully,
-        pesterchum.app.quit()       # stop the trayicon from sticking around :/
+        #pesterchum.app.quit()       # stop the trayicon from sticking around :/
 
+        #from time import sleep
+        #sleep(5)
         # Just in case.
-        sys.exit()
+        sys.exit() # Actually, just gonna use this, 'cuz sockets are dumb :'3
 
     pcUpdate = QtCore.pyqtSignal('QString', 'QString')
     closeToTraySignal = QtCore.pyqtSignal()