Update __init__.py

This commit is contained in:
Swee 2024-10-19 21:29:14 -07:00
parent a29c2b05ac
commit 4abb5d0b40

View file

@ -4,6 +4,7 @@ IRC Parser for the SugarCaneIRC family.
import socket import socket
import ssl as ssl_module import ssl as ssl_module
import threading import threading
__version__ = "_TEST_"
class systemMessage: # System message object class systemMessage: # System message object
chan = None chan = None
def __init__(self, content:str, user:str, typ:str, mention:bool, **kwargs): def __init__(self, content:str, user:str, typ:str, mention:bool, **kwargs):
@ -29,7 +30,7 @@ class IRCSession: # Actual IRC session
messages = [] # Cached messages messages = [] # Cached messages
raw_text = "" # Cached RAW data raw_text = "" # Cached RAW data
channels = [] # Cached channels channels = [] # Cached channels
connecting = False # Connection status connected = False # Connection status
is_ssl = False # Wether the connection uses TLS/SSL 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. ssl_accept_invalid = False # If SSL is enabled, do not fail to connect if the certificate is invalid.
socket = socket.socket() # Socket socket = socket.socket() # Socket
@ -54,10 +55,21 @@ class IRCSession: # Actual IRC session
self.send("USER " + self.user + " " + self.user + " " + self.nick + " :SugarCaneIRC user\n") self.send("USER " + self.user + " " + self.user + " " + self.nick + " :SugarCaneIRC user\n")
self.send(f"NICK {self.nick}\n") self.send(f"NICK {self.nick}\n")
def send(self, content:str): # Attempt to send raw data to the socket def send(self, content:str): # Attempt to send raw data to the socket
if content[len(content)-1] != "\n":
content+="\n"
if self.ssl: if self.ssl:
self.wsocket.send(bytes(content,"UTF-8")) self.wsocket.send(bytes(content,"UTF-8"))
else: else:
self.socket.send(bytes(content,"UTF-8")) self.socket.send(bytes(content,"UTF-8"))
def quit(self, message="SugarCaneParseIRC version " + __version__):
self.send(f"QUIT {message}\n")
self.close()
def close(self):
if self.ssl:
self.wsocket.close()
else:
self.socket.close()
self.connected = False
def get(self): # Attempt to get the raw data and parse it. def get(self): # Attempt to get the raw data and parse it.
# The code is copied from sweeBotIRC btw # The code is copied from sweeBotIRC btw
if self.ssl: if self.ssl:
@ -68,8 +80,8 @@ class IRCSession: # Actual IRC session
self.parseall() self.parseall()
print(r) print(r)
if r.find("PING") != -1: if r.find("PING") != -1:
self.irc_socket.send( self.send(
bytes("PONG " + r.split()[1] + "\r\n", "UTF-8") "PONG " + r.split()[1] + "\r\n"
) )
def parseall(self): # Parse all of the fetched raw data, in a thread. def parseall(self): # Parse all of the fetched raw data, in a thread.
threading.Thread(target=self.parse, kwargs={"content": self.raw_text}) threading.Thread(target=self.parse, kwargs={"content": self.raw_text})