From 5be89284ac1ab787c54f32b2c9f67b1deba64b6a Mon Sep 17 00:00:00 2001
From: Swee <sweeistaken@gmail.com>
Date: Sat, 19 Oct 2024 19:43:48 -0700
Subject: [PATCH] Update __init__.py

---
 scparseirc/__init__.py | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/scparseirc/__init__.py b/scparseirc/__init__.py
index 8e95421..1edc84b 100644
--- a/scparseirc/__init__.py
+++ b/scparseirc/__init__.py
@@ -1,13 +1,17 @@
+"""
+IRC Parser for the SugarCaneIRC family.
+"""
 import socket
+import ssl as ssl_module
 class message: # Message object
     def __init__(self, content:str, channel:str, nick:str):
         self.content = content
         self.channel = channel
         self.nick = nick
 class channel: # Channel object
-    is_init = False
-    topic = ""
-    modes = "+nt"
+    is_init = False # If the channel's properties are initialized yet
+    topic = "" # Channel topic
+    modes = "+nt" # Channel modes
     def __init__(self, name:str):
         self.name = name
     def info_set(self, topic:str, modes:str): # Socket will automatically initialize the channel object
@@ -16,12 +20,23 @@ class channel: # Channel object
 class IRCSession: # Actual IRC session
     messages = None # Cached messages
     connecting = False # Connection status
+    is_ssl = False # Wether the connection uses TLS/SSL
+    ssl_accept_invalid = False # If SSL is enabled, do not fail to connect if the certificate is invalid.
     socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Socket
-    def __init__(self, address:str, port:int, nick:str, user:str, *args, **kwargs): # Contains the configuration
-        self.server, self.port, self.nick, self.user = address,port,nick,user
+    wsocket = None # Wrapped socket (if SSL is enabled)
+    context = ssl_module.create_default_context()
+    def __init__(self, address:str, port:int, nick:str, user:str, ssl:bool, ssl_igninvalid:bool, *args, **kwargs): # Contains the configuration
+        self.server, self.port, self.nick, self.user, self.ssl, self.ssl_accept_invalid = address,port,nick,user,ssl,ssl_igninvalid
+        if ssl:
+            self.wsocket = self.context.wrap_socket(self.socket, server_hostname=address)
+            if ssl_igninvalid:
+                self.context = ssl_module._create_unverified_context()
     def connect(self): # Attempt to connect
         print("Connecting to " + self.server + ":" + str(self.port) + "...")
-        self.socket.connect((self.server, self.port))
+        if self.ssl:
+            self.wsocket.connect((self.server, self.port))
+        else:
+            self.socket.connect((self.server, self.port))
         self.connecting = True
         return False
     def alive(self): # NOT FINISHED: To minimize exceptions, the client can ask the object if the socket connection is still alive.