1
0
Fork 0

Update irc_bot.py

This commit is contained in:
Eric Lay 2025-05-11 17:56:15 -05:00
parent 3326176482
commit 48979cb1f6
1 changed files with 66 additions and 11 deletions

View File

@ -3,12 +3,13 @@ import threading
import time import time
import re import re
import requests import requests
import ssl
from auth_db import auth_db from auth_db import auth_db
from utils import strip_bbcode, parse_username from utils import strip_bbcode, parse_username
from config import ( from config import (
SERVER, PORT, BOTNICK, API_ENDPOINT, API_TOKEN, VERIFY_ENDPOINT, SERVER, PORT, BOTNICK, NICKSERV_USER, NICKSERV_PASS, API_ENDPOINT, API_TOKEN, VERIFY_ENDPOINT,
LOBBY_CHANNEL, MAIN_CHANNEL, ADMIN_CHANNEL, STAFF_CHANNELS, LOBBY_CHANNEL, MAIN_CHANNEL, ADMIN_CHANNEL, STAFF_CHANNELS,
RECONNECT_DELAY, GROUP_NAMES, STAFF_GROUP_IDS, ADMIN_GROUP_IDS RECONNECT_DELAY, GROUP_NAMES, STAFF_GROUP_IDS, ADMIN_GROUP_IDS, OPER_USER, OPER_PASS
) )
class IRCBot: class IRCBot:
@ -23,7 +24,8 @@ class IRCBot:
attempt = 0 attempt = 0
while True: while True:
try: try:
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) raw_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.irc = ssl.wrap_socket(raw_sock)
self.irc.connect((SERVER, PORT)) self.irc.connect((SERVER, PORT))
self.irc.send(f"NICK {self.nickname}\r\n".encode("utf-8")) self.irc.send(f"NICK {self.nickname}\r\n".encode("utf-8"))
self.irc.send(f"USER {self.nickname} 0 * :{self.nickname}\r\n".encode("utf-8")) self.irc.send(f"USER {self.nickname} 0 * :{self.nickname}\r\n".encode("utf-8"))
@ -34,18 +36,45 @@ class IRCBot:
if not resp: if not resp:
raise ConnectionError("No data received.") raise ConnectionError("No data received.")
for line in resp.decode(errors="ignore").split("\r\n"): for line in resp.decode(errors="ignore").split("\r\n"):
if not line:
continue
print(f"[IRC RAW] {line}")
if line.startswith("PING"): if line.startswith("PING"):
self.irc.send(f"PONG {line.split()[1]}\r\n".encode("utf-8")) self.irc.send(f"PONG {line.split()[1]}\r\n".encode("utf-8"))
elif " 001 " in line: elif " 001 " in line:
print(f"[+] Connected as {self.nickname}") print(f"[+] Connected as {self.nickname}")
# Identify with NickServ
self.irc.send(f"PRIVMSG NickServ :IDENTIFY {NICKSERV_USER} {NICKSERV_PASS}\r\n".encode("utf-8"))
time.sleep(2) # give time for identify to settle
# Send OPER command
self.irc.send(f"OPER {OPER_USER} {OPER_PASS}\r\n".encode("utf-8"))
print("[IRC CMD] Sent OPER")
# Optionally ghost the previous nick if not using SASL
if self.nickname != BOTNICK:
print("[*] Reclaiming original nickname")
self.irc.send(f"PRIVMSG NickServ :GHOST {BOTNICK} {NICKSERV_PASS}\r\n".encode("utf-8"))
time.sleep(1)
self.irc.send(f"NICK {BOTNICK}\r\n".encode("utf-8"))
self.nickname = BOTNICK
time.sleep(1)
self.irc.send(f"PRIVMSG NickServ :IDENTIFY {NICKSERV_USER} {NICKSERV_PASS}\r\n".encode("utf-8"))
time.sleep(1)
self.irc.send(f"JOIN {LOBBY_CHANNEL}\r\n".encode("utf-8")) self.irc.send(f"JOIN {LOBBY_CHANNEL}\r\n".encode("utf-8"))
return return
elif " 433 " in line: elif " 433 " in line:
# Nickname is already in use
print(f"[!] Nick {self.nickname} already in use, trying alternate...")
attempt += 1 attempt += 1
self.nickname += "_" self.nickname = f"{BOTNICK}_{attempt}"
self.irc.close() self.irc.send(f"NICK {self.nickname}\r\n".encode("utf-8"))
time.sleep(2) break # Break inner loop to restart connection flow with new nick
break
except Exception as e: except Exception as e:
print(f"[-] IRC connect error: {e}") print(f"[-] IRC connect error: {e}")
time.sleep(RECONNECT_DELAY) time.sleep(RECONNECT_DELAY)
@ -65,8 +94,11 @@ class IRCBot:
if not data: if not data:
raise ConnectionError("Disconnected.") raise ConnectionError("Disconnected.")
for line in data.strip().split("\r\n"): for line in data.strip().split("\r\n"):
print(f"[IRC RAW] {line}") # DEBUG FOR NOW
if line.startswith("PING"): if line.startswith("PING"):
self.irc.send(f"PONG {line.split()[1]}\r\n".encode("utf-8")) self.irc.send(f"PONG {line.split()[1]}\r\n".encode("utf-8"))
elif "You are now an IRC Operator" in line or "oper" in line.lower():
print("[+] Oper login succeeded")
elif "PRIVMSG" in line: elif "PRIVMSG" in line:
self.handle_message(line) self.handle_message(line)
elif " PART " in line or " QUIT " in line: elif " PART " in line or " QUIT " in line:
@ -102,9 +134,13 @@ class IRCBot:
# Relay public message if user is verified # Relay public message if user is verified
if not is_private: if not is_private:
user_data = auth_db.get_verified_user(user) user_data = auth_db.get_verified_user(user)
print(f"[DEBUG] Lookup for user '{user}' returned: {user_data}")
if user_data: if user_data:
site_user, irc_key, _ = user_data site_user, irc_key, _ = user_data
print(f"[DEBUG] Sending message from {site_user} with token {irc_key}: {msg}")
self.api_call(site_user, msg, irc_key) self.api_call(site_user, msg, irc_key)
else:
print(f"[DEBUG] No verified user found for: {user}")
# Handle verification messages from users # Handle verification messages from users
def verify_user(self, nick, msg): def verify_user(self, nick, msg):
@ -134,14 +170,18 @@ class IRCBot:
group_name = GROUP_NAMES.get(group_id, "Unknown") group_name = GROUP_NAMES.get(group_id, "Unknown")
auth_db.verify_user(nick, site_user, irc_key, group_id) auth_db.verify_user(nick, site_user, irc_key, group_id)
print(f"[DEBUG] Verification success: {site_user} ({group_name}) - group_id={group_id}")
self.send_notice(nick, f"Verified as {site_user} ({group_name})") self.send_notice(nick, f"Verified as {site_user} ({group_name})")
time.sleep(1) # Add delay before sending SAJOIN
# Join main lobby # Join main lobby
print(f"[DEBUG] Sending SAJOIN for {nick} to {MAIN_CHANNEL}")
self.send_raw_command(f"SAJOIN {nick} {MAIN_CHANNEL}") self.send_raw_command(f"SAJOIN {nick} {MAIN_CHANNEL}")
# Join staff chats if in staff group # Join staff chats if in staff group
if group_id in STAFF_GROUP_IDS: if group_id in STAFF_GROUP_IDS:
for channel in STAFF_CHANNELS: for channel in STAFF_CHANNELS:
print(f"[DEBUG] Sending SAJOIN for {nick} to {channel}")
self.send_raw_command(f"SAJOIN {nick} {channel}") self.send_raw_command(f"SAJOIN {nick} {channel}")
# Join admin room if in admin group # Join admin room if in admin group
@ -181,12 +221,27 @@ class IRCBot:
# API call ChatBridgeController # API call ChatBridgeController
def api_call(self, user, msg, token): def api_call(self, user, msg, irc_key):
try: try:
requests.post(API_ENDPOINT, headers={ payload = {
"username": user,
"irc_key": irc_key,
"text": msg,
"source": "irc"
}
headers = {
"Authorization": f"Bearer {API_TOKEN}", "Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json" "Content-Type": "application/json",
}, json={"username": user, "text": msg, "token": token}) "Accept": "application/json"
}
print(f"[DEBUG] Posting to API: {API_ENDPOINT}")
print(f"[DEBUG] Headers: {headers}")
print(f"[DEBUG] Payload: {payload}")
resp = requests.post(API_ENDPOINT, headers=headers, json=payload)
print(f"[DEBUG] API response status: {resp.status_code}")
print(f"[DEBUG] API response body: {resp.text}")
except Exception as e: except Exception as e:
print(f"[-] API call failed: {e}") print(f"[-] API call failed: {e}")