Update __init__.py

This commit is contained in:
Swee 2024-10-19 19:43:48 -07:00
parent 670e503c6a
commit 5be89284ac

View file

@ -1,13 +1,17 @@
"""
IRC Parser for the SugarCaneIRC family.
"""
import socket import socket
import ssl as ssl_module
class message: # Message object class message: # Message object
def __init__(self, content:str, channel:str, nick:str): def __init__(self, content:str, channel:str, nick:str):
self.content = content self.content = content
self.channel = channel self.channel = channel
self.nick = nick self.nick = nick
class channel: # Channel object class channel: # Channel object
is_init = False is_init = False # If the channel's properties are initialized yet
topic = "" topic = "" # Channel topic
modes = "+nt" modes = "+nt" # Channel modes
def __init__(self, name:str): def __init__(self, name:str):
self.name = name self.name = name
def info_set(self, topic:str, modes:str): # Socket will automatically initialize the channel object 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 class IRCSession: # Actual IRC session
messages = None # Cached messages messages = None # Cached messages
connecting = False # Connection status 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 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 wsocket = None # Wrapped socket (if SSL is enabled)
self.server, self.port, self.nick, self.user = address,port,nick,user 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 def connect(self): # Attempt to connect
print("Connecting to " + self.server + ":" + str(self.port) + "...") 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 self.connecting = True
return False return False
def alive(self): # NOT FINISHED: To minimize exceptions, the client can ask the object if the socket connection is still alive. def alive(self): # NOT FINISHED: To minimize exceptions, the client can ask the object if the socket connection is still alive.