From 005c531ecf2dbdb7372972170ed3f29402e0c8a7 Mon Sep 17 00:00:00 2001 From: Georgios Atheridis Date: Wed, 23 Mar 2022 10:27:03 +0200 Subject: [PATCH] added account disabler --- aptbot/__main__.py | 87 ++++++++++++++++++++++++++++++++-------------- aptbot/args.py | 7 ++++ aptbot/bot.py | 67 ++++++++++++++++++----------------- 3 files changed, 103 insertions(+), 58 deletions(-) diff --git a/aptbot/__main__.py b/aptbot/__main__.py index 972927a..98132a3 100644 --- a/aptbot/__main__.py +++ b/aptbot/__main__.py @@ -11,6 +11,7 @@ from threading import Thread from dotenv import load_dotenv from types import ModuleType +load_dotenv() if "XDG_CONFIG_HOME" in os.environ: CONFIG_HOME = os.environ["XDG_CONFIG_HOME"] @@ -22,8 +23,6 @@ else: CONFIG_PATH = os.path.join(CONFIG_HOME, f"aptbot") -load_dotenv() - PORT = 26538 LOCALHOST = "127.0.0.1" @@ -34,29 +33,30 @@ def loop(bot: aptbot.bot.Bot, modules: dict[str, ModuleType]): messages = bot.receive_messages() for message in messages: if message.channel: - method = Thread( - target=modules[message.channel].main, - args=(bot, message, ) - ) - method.daemon = True - method.start() - print(message) - time.sleep(0.001) + if message.command: + print( + f"#{message.channel} ({message.command.value}) | {message.nick}: {message.value}") + try: + method = Thread( + target=modules[message.channel].main, + args=(bot, message, ) + ) + except KeyError: + pass + else: + method.daemon = True + method.start() + time.sleep(0.01) def update_modules(modules: dict[str, ModuleType]): modules.clear() - # modules = {} - channels = os.listdir(CONFIG_PATH) + channels = filter(lambda x: not x.startswith('.'), os.listdir(CONFIG_PATH)) + channels = list(channels) + # print(channels) for channel in channels: account_path = os.path.join(CONFIG_PATH, f"{channel}") - module_path = os.path.join( - account_path, f"message_interpreter.py") - spec = importlib.util.spec_from_file_location( - "message_interpreter", - module_path, - ) - account_path = os.path.join(CONFIG_PATH, f"{channel}") + sys.path.append(account_path) module_path = os.path.join( account_path, f"message_interpreter.py") spec = importlib.util.spec_from_file_location( @@ -72,12 +72,14 @@ def update_modules(modules: dict[str, ModuleType]): print(f"Problem Loading Module: {e}") else: modules[channel] = foo + sys.path.remove(account_path) def initialize(bot: aptbot.bot.Bot): channels = os.listdir(CONFIG_PATH) for channel in channels: - bot.join_channel(channel) + if not channel.startswith('.'): + bot.join_channel(channel) def listener(): @@ -117,6 +119,8 @@ def listener(): sys.exit() elif "UPDATE" in command: update_modules(modules) + elif "PART" in command: + bot.leave_channel(channel) time.sleep(1) @@ -132,14 +136,26 @@ def send(func,): def add_account(s: socket.socket, acc: str): account_path = os.path.join(CONFIG_PATH, f"{acc}") + hidden_account_path = os.path.join(CONFIG_PATH, f".{acc}") + try: + os.rename(hidden_account_path, account_path) + except FileNotFoundError: + pass os.makedirs(account_path, exist_ok=True) - f = open(os.path.join(account_path, "message_interpreter.py"), "a") - f.write("""from aptbot.bot import Bot, Message, Commands + # print(os.listdir(".")) + # shutil.copy("message_interpreter.py", account_path) + try: + f = open(os.path.join(account_path, "message_interpreter.py"), "r") + except FileNotFoundError: + f = open(os.path.join(account_path, "message_interpreter.py"), "a") + f.write("""from aptbot.bot import Bot, Message, Commands def main(bot, message: Message): pass""") - f.close() + f.close() + else: + f.close() command = "JOIN" channel = acc @@ -149,8 +165,8 @@ def main(bot, message: Message): def send_msg(s: socket.socket, msg: str): command = "SEND" - channel = "skgyorugo" - msg = msg + channel = msg.split(' ')[0] + msg = msg[len(channel) + 1:] s.send(bytes(f"{command}==={channel}==={msg}", "utf-8")) @@ -161,6 +177,20 @@ def disable(s: socket.socket): s.send(bytes(f"{command}==={channel}==={msg}", "utf-8")) +def disable_account(s: socket.socket, acc: str): + account_path = os.path.join(CONFIG_PATH, f"{acc}") + hidden_account_path = os.path.join(CONFIG_PATH, f".{acc}") + try: + os.rename(account_path, hidden_account_path) + except FileNotFoundError: + print(f"Account {acc} is already disabled.") + + command = "PART" + channel = "" + msg = "" + s.send(bytes(f"{command}==={channel}==={msg}", "utf-8")) + + def update(s: socket.socket): command = "UPDATE" channel = "" @@ -174,10 +204,15 @@ def main(): listener() s = socket.socket() - s.connect((LOCALHOST, PORT)) + try: + s.connect((LOCALHOST, PORT)) + except ConnectionRefusedError: + pass if argsv.add_account: add_account(s, argsv.add_account) + if argsv.disable_account: + disable_account(s, argsv.disable_account) if argsv.send_message: send_msg(s, argsv.send_message) if argsv.disable: diff --git a/aptbot/args.py b/aptbot/args.py index 2aba280..6f9801b 100644 --- a/aptbot/args.py +++ b/aptbot/args.py @@ -14,6 +14,13 @@ def parse_arguments() -> argparse.Namespace: help=f"Add an account to connect with the bot" ) + arg_parser.add_argument( + "-d", + "--disable-account", + type=str, + help=f"Disable an account from the bot" + ) + arg_parser.add_argument( "-s", "--send-message", diff --git a/aptbot/bot.py b/aptbot/bot.py index dd5b615..95f962d 100644 --- a/aptbot/bot.py +++ b/aptbot/bot.py @@ -1,8 +1,7 @@ import socket import time -import sys from enum import Enum -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import Optional @@ -23,17 +22,17 @@ class Commands(Enum): @dataclass class Message: - tags: dict[str, str] - nick: str - command: Optional[Commands] - channel: str - value: str + tags: dict[str, str] = field(default_factory=dict) + nick: str = "" + command: Optional[Commands] = None + channel: str = "" + value: str = "" class Bot: def __init__(self, nick: str, oauth_token: str, client_id: str): self.irc = socket.socket() - self.server = "irc.twitch.tv" + self.server = "irc.chat.twitch.tv" self.port = 6667 self.nick = nick self.oauth_token = oauth_token @@ -41,11 +40,13 @@ class Bot: self.connected_channels = [] def send_command(self, command: str): - print(f"< {command}") + if "PASS" not in command: + print(f"< {command}") self.irc.send((command + "\r\n").encode()) def connect(self): self.irc.connect((self.server, self.port)) + time.sleep(3) self.send_command(f"PASS oauth:{self.oauth_token}") self.send_command(f"NICK {self.nick}") self.send_command(f"CAP REQ :twitch.tv/membership") @@ -65,11 +66,12 @@ class Bot: self.connected_channels.remove(channel) def send_privmsg(self, channel: str, text: str): + print(f"#{channel} ({Commands.PRIVMSG.value}) | {self.nick}: {text}") self.send_command(f"{Commands.PRIVMSG.value} #{channel} :{text}") @staticmethod def parse_message(received_msg: str) -> Message: - message = Message({}, "", None, "", "") + message = Message() value_start = received_msg.find( ':', @@ -105,33 +107,34 @@ class Bot: def _handle_message(self, received_msg: str) -> Message: if received_msg == "PING :tmi.twitch.tv": self.send_command("PONG :tmi.twitch.tv") - return Message({}, "", None, "", "") + return Message() elif not received_msg: - return Message({}, "", None, "", "") + return Message() return Bot.parse_message(received_msg) def receive_messages(self) -> list[Message]: messages = [] - i = 0 - while i < 5: - if i: - try: - self.connect() - self.join_channels(self.connected_channels) - except OSError as e: - print(f"Connection failed {e}") - try: - received_msgs = self.irc.recv(2048).decode() - except ConnectionResetError as e: - print(f"There was an error connecting with error {e}") - print("Trying to connect again") - time.sleep(2**i+1) - i += 1 - else: - print("broke") - break - else: - sys.exit(1) + # i = 0 + # while i < 5: + # if i: + # try: + # self.connect() + # self.join_channels(self.connected_channels) + # except OSError as e: + # print(f"Connection failed {e}") + # try: + # received_msgs = self.irc.recv(2048).decode() + # except ConnectionResetError as e: + # print(f"There was an error connecting with error {e}") + # print("Trying to connect again") + # time.sleep(2**i+1) + # i += 1 + # else: + # print("broke") + # break + # else: + # sys.exit(1) + received_msgs = self.irc.recv(2048).decode() for received_msgs in received_msgs.split("\r\n"): messages.append(self._handle_message(received_msgs)) return messages -- 2.30.2