2024-10-19 19:43:48 -07:00
|
|
|
"""
|
|
|
|
IRC Parser for the SugarCaneIRC family.
|
|
|
|
"""
|
2024-10-19 19:25:37 -07:00
|
|
|
import socket
|
2024-10-19 19:43:48 -07:00
|
|
|
import ssl as ssl_module
|
2024-10-19 19:25:37 -07:00
|
|
|
class message: # Message object
|
2024-10-19 19:46:42 -07:00
|
|
|
def __init__(self, content:str, chan:str, nick:str):
|
2024-10-19 19:25:37 -07:00
|
|
|
self.content = content
|
2024-10-19 19:46:42 -07:00
|
|
|
self.channel = chan
|
2024-10-19 19:25:37 -07:00
|
|
|
self.nick = nick
|
|
|
|
class channel: # Channel object
|
2024-10-19 19:43:48 -07:00
|
|
|
is_init = False # If the channel's properties are initialized yet
|
|
|
|
topic = "" # Channel topic
|
|
|
|
modes = "+nt" # Channel modes
|
2024-10-19 19:25:37 -07:00
|
|
|
def __init__(self, name:str):
|
|
|
|
self.name = name
|
|
|
|
def info_set(self, topic:str, modes:str): # Socket will automatically initialize the channel object
|
|
|
|
self.is_init = True
|
|
|
|
self.topic, self.modes = topic, modes
|
|
|
|
class IRCSession: # Actual IRC session
|
2024-10-19 19:27:56 -07:00
|
|
|
messages = None # Cached messages
|
|
|
|
connecting = False # Connection status
|
2024-10-19 19:43:48 -07:00
|
|
|
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.
|
2024-10-19 19:27:56 -07:00
|
|
|
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Socket
|
2024-10-19 19:43:48 -07:00
|
|
|
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:
|
|
|
|
if ssl_igninvalid:
|
|
|
|
self.context = ssl_module._create_unverified_context()
|
2024-10-19 19:45:35 -07:00
|
|
|
self.wsocket = self.context.wrap_socket(self.socket, server_hostname=address)
|
2024-10-19 19:25:37 -07:00
|
|
|
def connect(self): # Attempt to connect
|
2024-10-19 19:27:56 -07:00
|
|
|
print("Connecting to " + self.server + ":" + str(self.port) + "...")
|
2024-10-19 19:43:48 -07:00
|
|
|
if self.ssl:
|
|
|
|
self.wsocket.connect((self.server, self.port))
|
|
|
|
else:
|
|
|
|
self.socket.connect((self.server, self.port))
|
2024-10-19 19:25:37 -07:00
|
|
|
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.
|
|
|
|
return False
|
2024-10-19 19:25:46 -07:00
|
|
|
def whois(self, nick:str): # NOT FINISHED: Try to /whois the user, will return a user() object or None.
|
2024-10-19 19:25:37 -07:00
|
|
|
return None
|