Add configuration file support

This commit is contained in:
Jordyn 2026-03-14 03:42:04 -05:00
commit 4fab129a1e
2 changed files with 29 additions and 7 deletions

17
main.py
View file

@ -7,15 +7,16 @@ import subprocess
import re import re
import time import time
import queue import queue
import config
class Server(): class Server():
# Setup the IRC related things such as user details and prefixes # Setup the IRC related things such as user details and prefixes
# This function also sets up the threading events and queues # This function also sets up the threading events and queues
def __init__(self, realname, nickname, channel, command_prefix = "$!", bot_prefix = "$$", opper_nicknames = []): def __init__(self, realname, nickname, channels, command_prefix = "$!", bot_prefix = "$$", opper_nicknames = []):
print(f"[SERVER/MAINTHREAD] Using Server() on Python3 PID {os.getpid()}") print(f"[SERVER/MAINTHREAD] Using Server() on Python3 PID {os.getpid()}")
self.realname = realname self.realname = realname
self.nickname = nickname self.nickname = nickname
self.channel = channel self.channels = channels
self.opper_nicknames = opper_nicknames self.opper_nicknames = opper_nicknames
# Prefix for commands to execute # Prefix for commands to execute
@ -35,7 +36,7 @@ class Server():
self._msg_time = 0 self._msg_time = 0
self._msg_count = 0 self._msg_count = 0
print(f"[SERVER/MAINTHREAD] Using nickname {nickname} on {channel}!") print(f"[SERVER/MAINTHREAD] Using nickname {nickname}!")
# This function handles the socket bring up # This function handles the socket bring up
# Sets socket paramaters, and starts the send/recv threads # Sets socket paramaters, and starts the send/recv threads
@ -145,7 +146,10 @@ class Server():
def _send_userreg(self): def _send_userreg(self):
self._msg_q.put(f"USER {self.realname} * * :{self.nickname}\r\n".encode()) self._msg_q.put(f"USER {self.realname} * * :{self.nickname}\r\n".encode())
self._msg_q.put(f"NICK {self.nickname}\r\n".encode()) self._msg_q.put(f"NICK {self.nickname}\r\n".encode())
self._msg_q.put(f"JOIN {self.channel}\r\n".encode())
for channel in self.channels:
print(f"[RECVTHREAD] Joining channel {channel}!")
self._msg_q.put(f"JOIN {channel}\r\n".encode())
# Used to strip ASCII control characters (such as terminal escape codes) # Used to strip ASCII control characters (such as terminal escape codes)
# from text. Mainly to avoid unknown command errors from the IRC server. # from text. Mainly to avoid unknown command errors from the IRC server.
@ -318,9 +322,8 @@ class Server():
self._msg_q.put(f"PRIVMSG {target_channel} :CMD {cmd} exited with returncode {proc.returncode}\r\n".encode()) self._msg_q.put(f"PRIVMSG {target_channel} :CMD {cmd} exited with returncode {proc.returncode}\r\n".encode())
if __name__ == "__main__": if __name__ == "__main__":
serv = Server("username", "nickname", "##channel", opper_nicknames = ["opperhere"]) serv = Server(**config.user, **config.bot)
serv.connect("IP", 6667, sock_recvbuf = 8192) serv.connect(**config.server)
# Allow for the user to send raw IRC messages # Allow for the user to send raw IRC messages
# These bypass the send queue # These bypass the send queue

19
skel_config.py Normal file
View file

@ -0,0 +1,19 @@
user = {
"realname": "setme",
"nickname": "setme",
"channels": ["##join", "##these", "##channels"]
}
server = {
"ip": "irc.serv.net",
"port": 6667,
"sock_timeout": 60,
"sock_sendbuf": 512,
"sock_recvbuf": 512
}
bot = {
"command_prefix": "$!",
"bot_prefix": "$$",
"opper_nicknames": ["nicknamehere"]
}