diff --git a/build/lib/BotpySE/BackgroundTask.py b/build/lib/BotpySE/BackgroundTask.py deleted file mode 100644 index 30391c2..0000000 --- a/build/lib/BotpySE/BackgroundTask.py +++ /dev/null @@ -1,30 +0,0 @@ -# -# BackgroundTask.py -# Botpy -# -# Created by Ashish Ahuja on 4th September 2017. -# -# - -import threading - -class BackgroundTask: - def __init__(self, function_callback, interval=30): - self.function_callback = function_callback - self.interval = interval - self.stop_event = threading.Event() - - def call_function(self): - while not self.stop_event.wait(self.interval): - self.function_callback() - - def start_task(self): - self.stop_event.clear() - background_thread = threading.Thread(target=self.call_function) - background_thread.start() - - def stop_task(self): - self.stop_event.set() - - def is_alive(self): - return not self.stop_event.is_set() diff --git a/build/lib/BotpySE/BackgroundTaskManager.py b/build/lib/BotpySE/BackgroundTaskManager.py deleted file mode 100644 index 45cd6a4..0000000 --- a/build/lib/BotpySE/BackgroundTaskManager.py +++ /dev/null @@ -1,26 +0,0 @@ -# -# BackgroundTaskManager.py -# Botpy -# -# Created by Ashish Ahuja on 4th September 2017. -# -# - -class BackgroundTaskManager: - def __init__(self, background_tasks): - self.tasks = background_tasks - - def add_background_task(self, background_task): - self.tasks.append(background_task) - - def start_tasks(self): - for each_task in self.tasks: - each_task.start_task() - - def stop_tasks(self): - for each_task in self.tasks: - each_task.stop_task() - - def restart_tasks(self): - self.stop_tasks() - self.start_tasks() diff --git a/build/lib/BotpySE/Bot.py b/build/lib/BotpySE/Bot.py deleted file mode 100644 index 331ffb4..0000000 --- a/build/lib/BotpySE/Bot.py +++ /dev/null @@ -1,170 +0,0 @@ -# -# Bot.py -# Botpy -# -# Created by Ashish Ahuja on 1st August 2017. -# -# - -from .CommandManager import * -from .BackgroundTaskManager import * -from .BackgroundTask import * -from . import ChatRoom -from . import ChatUser -from . import Utilities - -import os -import json -import jsonpickle as jp - -class Bot(ce.client.Client): - def __init__(self, bot_name, commands, room_ids, background_tasks=[], host='stackexchange.com', email=None, password=None): - super().__init__(host, email, password) - - background_tasks.append(BackgroundTask(self._shutdown_check, interval=5)) - background_tasks.append(BackgroundTask(self._reboot_check, interval=5)) - background_tasks.append(BackgroundTask(self._save_users, interval=60)) - - self.name = bot_name - self.is_alive = False - self._ids = room_ids - self.commands = commands - self._command_manager = CommandManager(commands) - self.save_directory = os.path.expanduser("~") + "/" + "." + self.name.lower() + "/" - self._users = list() - self._rooms = list() - - background_tasks.append(BackgroundTask(self._command_manager.cleanup_finished_commands, interval=3)) - self._background_task_manager = BackgroundTaskManager(background_tasks) - - def join_rooms(self): - for each_id in self._ids: - #self._rooms.setdefault(each_id, self.get_room(each_id)) - self.get_room(each_id) - - for each_room in self._rooms: - each_room.join() - - def leave_rooms(self): - for each_room in self._rooms: - each_room.leave() - - def watch_rooms(self): - for each_room in self._rooms: - each_room.watch(self._handle_event) - - def add_privilege_type(self, privilege_level, privilege_name): - for each_room in self._rooms: - each_room.add_privilege_type(privilege_level, privilege_name) - - def start_bot(self): - self.is_alive = True - self.join_rooms() - self._load_users() - self.watch_rooms() - self._background_task_manager.start_tasks() - - def stop_bot(self): - self._background_task_manager.stop_tasks() - self._save_users() - self.leave_rooms() - self.is_alive = False - - def _shutdown_check(self): - if Utilities.should_shutdown: - self.stop_bot() - - def _reboot_check(self): - if Utilities.should_reboot: - Utilities.should_reboot = False - self.stop_bot() - self.start_bot() - self.add_essential_background_tasks() - - def _handle_event(self, event, _): - if isinstance(event, ce.events.MessagePosted): - message = event - short_name = '@' + self.name[:3] - - try: - print("(%s) %s (id: %d): %s" % (message.room.name, message.user.name, message.user.id, message.content)) - except UnicodeEncodeError as unicode_err: - print("Unicode encode error occurred: " + str(unicode_err)) - - try: - content_split = message.content.lower().split() - except AttributeError: - print("Attribute error occurred.") - return - - if content_split[0].startswith(short_name.lower()): - self._command_manager.handle_command(message) - elif isinstance(event, ce.events.UserEntered): - event.room.add_user(event.user) - - def _save_users(self): - for room in self._rooms: - filename = self._convert_to_save_filename(room.id) - try: - save_list = list() - for user in room._users: - save_list.append({'id': user.id, '_privilege_type': user._privilege_type}) - - with open(filename, "w") as file_handle: - json.dump(jp.encode(save_list), file_handle) - except IOError as ioerr: - print("IOError occurred: ") - print(str(ioerr)) - except pickle.PickleError as perr: - print("Pickling error occurred: ") - print(str(perr)) - - def _load_users(self): - for room in self._rooms: - filename = self._convert_to_save_filename(room.id) - try: - with open(filename, "r") as file_handle: - file_users = jp.decode(json.load(file_handle)) - for user in file_users: - room._users = [x for x in room._users if x.id != user['id']] - #room._users.append(self._set_dict(ChatUser.ChatUser, user['id'], user)) - room._users.append(self.get_user(user['id'], _privilege_type=user['_privilege_type'])) - except IOError as ioerr: - print("IOError occurred: ") - print(str(ioerr)) - except pickle.PickleError as perr: - print("Pickling error occurred: ") - print(str(perr)) - - def _convert_to_save_filename(self, id): - return self.name.replace(' ', '_') + '_room_' + str(id) + '_data' - - def get_room(self, room_id, **attrs_to_set): - return self._get_and_set_deduplicated_list( - ChatRoom.ChatRoom, room_id, self._rooms, attrs_to_set) - - def get_user(self, user_id, **attrs_to_set): - return self._get_and_set_deduplicated_list( - ChatUser.ChatUser, user_id, self._users, attrs_to_set) - - def _get_and_set_deduplicated_list(self, cls, id, instances, attrs): - instance = None - for each in instances: - if each.id == id: - instance = each - break - if instance is None: - instance = cls(id, self) - instances.append(instance) - - for key, value in attrs.items(): - setattr(instance, key, value) - return instance - - def _set_dict(self, cls, id, attrs): - instance = cls(id, self) - - for key, value in attrs.items(): - setattr(instance, key, value) - - return instance diff --git a/build/lib/BotpySE/ChatRoom.py b/build/lib/BotpySE/ChatRoom.py deleted file mode 100644 index 51a3c68..0000000 --- a/build/lib/BotpySE/ChatRoom.py +++ /dev/null @@ -1,87 +0,0 @@ -# -# ChatRoom.py -# Botpy -# -# Created by Ashish Ahuja on 12th September 2017. -# -# - -import chatexchange as ce -import os -import weakref - -from . import ChatUser -from . import PrivilegeType -from . import Utilities - -class ChatRoom(ce.rooms.Room): - def __init__(self, room_id, client, save_directory=None): - super().__init__(room_id, client) - - if save_directory is None: - save_directory = os.getcwd() - - self._filename = save_directory + "room_" + str(self.id) + "_name_" + self.name.replace(" ", "_") + "_users" - self._privilege_types = list() - self._users = list() - - self._users.extend(self.get_pingable_users()) - - def join(self): - print("Joined room '" + self.name + "' with room id " + str(self.id) + ".") - return self._client._join_room(self.id) - - def leave(self): - return self._client._leave_room(self.id) - - def add_privilege_type(self, privilege_level, privilege_name): - self._privilege_types.append(PrivilegeType.PrivilegeType(privilege_level, privilege_name)) - - def remove_privilege_type(self, privilege_name): - for each_type in self._privilege_types: - if each_type.name == privilege_name: - self._privilege_types.remove(each_type) - - def is_user_privileged(self, user_id, required_level): - for each_user in self._users: - if each_user.id == user_id: - return each_user.is_privileged(required_level) - return False - - def get_privilege_type_by_level(self, privilege_level): - for each_type in self._privilege_types: - if each_type.level == privilege_level: - return each_type - - return None - - def get_privilege_type_by_name(self, name): - for each_type in self._privilege_types: - if each_type.name == name: - return each_type - - return None - - def get_users(self): - return self._users - - def change_privilege_level(self, user_id, privilege_type=None): - if not isinstance(privilege_type, PrivilegeType.PrivilegeType) and privilege_type is not None: - raise TypeError('privilege_user: Expected privilege type of type "PrivilegeType.PrivilegeType"') - return False - - for each_user in self._users: - if each_user.id == user_id: - each_user.change_privilege_level(privilege_type) - return True - return False - - def add_user(self, user): - if not isinstance(user, ChatUser.ChatUser): - raise TypeError('add_user: Expected user of type "ChatUser.ChatUser"') - return - - for each_user in self._users: - if each_user.id == user.id: - return - self._users.append(user) diff --git a/build/lib/BotpySE/ChatUser.py b/build/lib/BotpySE/ChatUser.py deleted file mode 100644 index 3414e55..0000000 --- a/build/lib/BotpySE/ChatUser.py +++ /dev/null @@ -1,30 +0,0 @@ -# -# ChatUser.py -# Botpy -# -# Created by Ashish Ahuja on 7th March 2018. -# -# - -import chatexchange as ce -from . import PrivilegeType - -class ChatUser(ce.users.User): - def __init__(self, id, client, privilege_type=None): - super().__init__(id, client) - self._privilege_type = privilege_type - - def is_privileged(self, privilege_req): - if self._privilege_type: - if self._privilege_type.level >= privilege_req: - return True - return False - - def change_privilege_level(self, privilege_level): - if not isinstance(privilege_level, PrivilegeType.PrivilegeType) and privilege_level is not None: - raise TypeError('ChatUser.change_privilege_level: privilege_level is not of type "PrivilegeType.PrivilegeType"') - - self._privilege_type = privilege_level - - def get_privilege_type(self): - return self._privilege_type diff --git a/build/lib/BotpySE/Chatcommunicate.py b/build/lib/BotpySE/Chatcommunicate.py deleted file mode 100644 index e83e440..0000000 --- a/build/lib/BotpySE/Chatcommunicate.py +++ /dev/null @@ -1,36 +0,0 @@ -# -# Chatcommunicate.py -# Botpy -# -# Created by Ashish Ahuja on 2nd August 2017. -# -# - -import chatexchange as ce -import threading - -class Chatcommunicate: - def __init__(self, bot_name, command_manager): - self.bot_name = bot_name - self.command_manager = command_manager - self.short_name = '@' + bot_name[:3] - - def handle_event(self, event, _): - if isinstance(event, ce.events.MessagePosted): - message = event - - try: - print("(%s) %s (id: %d): %s" % (message.room.name, message.user.name, message.user.id, message.content)) - except UnicodeEncodeError as unicode_err: - print("Unicode encode error occurred: " + str(unicode_err)) - - try: - content_split = message.content.lower().split() - except AttributeError: - print("Attribute error occurred.") - return - - if content_split[0].startswith(self.short_name.lower()): - self.command_manager.handle_command(message) - elif isinstance(event, ce.events.UserEntered): - # A user has entered the room. diff --git a/build/lib/BotpySE/Client.py b/build/lib/BotpySE/Client.py deleted file mode 100644 index 59d3eb2..0000000 --- a/build/lib/BotpySE/Client.py +++ /dev/null @@ -1,24 +0,0 @@ -# -# Client.py -# Botpy -# -# Created by Ashish Ahuja on 7th March 2018. -# -# - -import chatexchange as ce - -from . import ChatRoom -from . import ChatUser - -class Client(ce.client.Client): - def __init__(self, host='stackexchange.com', email=None, password=None): - super().__init__(host, email, password) - - def get_room(self, room_id, **attrs_to_set): - return self._get_and_set_deduplicated( - ChatRoom.ChatRoom, room_id, self._rooms, attrs_to_set) - - def get_user(self, user_id, **attrs_to_set): - return self._get_and_set_deduplicated( - ChatUser.ChatUser, user_id, self._users, attrs_to_set) diff --git a/build/lib/BotpySE/Command.py b/build/lib/BotpySE/Command.py deleted file mode 100644 index ada36a1..0000000 --- a/build/lib/BotpySE/Command.py +++ /dev/null @@ -1,33 +0,0 @@ -# -# Command.py -# Botpy -# -# Created by Ashish Ahuja on 1st August 2017 -# - -import chatexchange as ce - -class Command: - def __init__(self, command_manager, message, arguments, usage_index=0): - self.command_manager = command_manager - self.message = message - self.arguments = arguments - self.usage_index = usage_index - - def usage(): - raise NotImplementedError - - def privileges(self): - return 0 - - #Whether the command has completed execution. - finished = False - - def reply(self, text, length_check=True): - self.message.message.reply(text, length_check=length_check) - - def post(self, text, length_check=True): - self.message.room.send_message(text, length_check=length_check) - - def run(self): - raise NotImplementedError diff --git a/build/lib/BotpySE/CommandAlive.py b/build/lib/BotpySE/CommandAlive.py deleted file mode 100644 index 33728fe..0000000 --- a/build/lib/BotpySE/CommandAlive.py +++ /dev/null @@ -1,20 +0,0 @@ -# -# CommandAlive.py -# Botpy -# -# Created by Ashish Ahuja. -# -# - -from .Command import * - -class CommandAlive(Command): - @staticmethod - def usage(): - return ["alive", "status"] - - def privileges(self): - return 1 - - def run(self): - self.reply("Yes") diff --git a/build/lib/BotpySE/CommandAmiprivileged.py b/build/lib/BotpySE/CommandAmiprivileged.py deleted file mode 100644 index 130fac6..0000000 --- a/build/lib/BotpySE/CommandAmiprivileged.py +++ /dev/null @@ -1,20 +0,0 @@ -# -# CommandAmiprivileged.py -# Botpy -# -# Created by Ashish Ahuja on 29th September 2017. -# -# - -from .Command import * - -class CommandAmiprivileged(Command): - def usage(): - return ["amiprivileged", "doihaveprivs", "privileges"] - - def run(self): - user_privs = self.message.user.get_privilege_type() - if user_privs is None: - self.reply("You do not have any privileges.") - else: - self.reply("You have the privilege: " + user_privs.name) diff --git a/build/lib/BotpySE/CommandListPrivilegedUsers.py b/build/lib/BotpySE/CommandListPrivilegedUsers.py deleted file mode 100644 index 5256825..0000000 --- a/build/lib/BotpySE/CommandListPrivilegedUsers.py +++ /dev/null @@ -1,25 +0,0 @@ -# -# CommandListPrivilegedUsers.py -# Botpy -# -# Created by Ashish Ahuja on 1st October 2017. -# -# - -from .Command import * -import tabulate - -class CommandListPrivilegedUsers(Command): - def usage(): - return ["membership", "privileged", "listprivileged"] - - def run(self): - privilege_list = list() - - for each_user in self.message.room.get_users(): - if each_user.get_privilege_type() is not None: - privilege_list.append([each_user.id, each_user.get_privilege_type().name]) - - table = tabulate.tabulate(privilege_list, headers=["User ID", "Privilege level"], tablefmt="orgtbl") - - self.post(" " + table.replace("\n", "\n ")) diff --git a/build/lib/BotpySE/CommandListRunningCommands.py b/build/lib/BotpySE/CommandListRunningCommands.py deleted file mode 100644 index 57c4355..0000000 --- a/build/lib/BotpySE/CommandListRunningCommands.py +++ /dev/null @@ -1,26 +0,0 @@ -# -# CommandListRunningCommands.py -# Botpy -# -# Created by Ashish Ahuja on 6th September 2017. -# -# - -from .Command import * -import tabulate -import re - -class CommandListRunningCommands(Command): - @staticmethod - def usage(): - return ["running commands", "rc"] - - def run(self): - command_list = list() - - for each_command, _ in self.command_manager.running_commands: - command_list.append([each_command.message.user.name, each_command.usage()[each_command.usage_index]]) - - table = tabulate.tabulate(command_list, headers=["User", "Command"], tablefmt="orgtbl") - - self.post(" " + re.sub('\n', '\n ', table)) diff --git a/build/lib/BotpySE/CommandManager.py b/build/lib/BotpySE/CommandManager.py deleted file mode 100644 index 32bf49f..0000000 --- a/build/lib/BotpySE/CommandManager.py +++ /dev/null @@ -1,76 +0,0 @@ -# -# CommandManager.py -# Botpy -# -# Created by Ashish Ahuja on 4th September 2017. -# -# - -import chatexchange as ce -import threading - -class CommandManager: - def __init__(self, commands): - self.commands = commands - self.running_commands = [] - - def run_command(self, command): - if command.privileges() == 0: - command_thread = threading.Thread(target=command.run) - self.running_commands.append([command, command_thread]) - command_thread.start() - return - - if command.message.room.is_user_privileged(command.message.user.id, command.privileges()): - command_thread = threading.Thread(target=command.run) - self.running_commands.append([command, command_thread]) - command_thread.start() - return - - command.reply("You do not have sufficient privileges to run this command.") - - def handle_command(self, message): - try: - message_content = message.content.lower().split() - del message_content[0] - except AttributeError: - return - - for command in self.commands: - command_usage = command.usage() - - usage_index = -1 - - for usage in command_usage: - usage_index += 1 - usage_components = usage.split() - args = [] - match = True - last_index = min(len(usage_components), len(message_content)) - - for i in range(last_index): - content_component = message_content[i] - usage_component = usage_components[i] - - if usage_component == '*': - args.append(content_component) - elif usage_component == '...': - #Everything else is arguments - temp_index = i - while temp_index < len(message_content): - args.append(message_content[temp_index]) - temp_index += 1 - elif content_component != usage_component: - match = False - - min_count = (len(usage_components) - 1) if (usage_components[-1] == '...') else len(usage_components) - if len(message_content) < min_count: - match = False - if match: - self.run_command(command(self, message, args, usage_index)) - return - - def cleanup_finished_commands(self): - for command, command_thread in self.running_commands: - if not command_thread.isAlive(): - self.running_commands.remove([command, command_thread]) diff --git a/build/lib/BotpySE/CommandPrivilegeUser.py b/build/lib/BotpySE/CommandPrivilegeUser.py deleted file mode 100644 index 0d69378..0000000 --- a/build/lib/BotpySE/CommandPrivilegeUser.py +++ /dev/null @@ -1,33 +0,0 @@ -# -# CommandPrivilegeUser.py -# Botpy -# -# Created by Ashish Ahuja on 17th September 2017. -# -# - -from .Command import * - -class CommandPrivilegeUser(Command): - def usage(): - return ["privilege * *", "addpriv * *"] - - def privileges(self): - return 0 - - def run(self): - user_id = int(self.arguments[0]) - privilege_name = self.arguments[1] - - privilege_type = self.message.room.get_privilege_type_by_name(privilege_name) - - if privilege_type == None: - self.reply("Please give a valid privilege type") - return - - if self.message.room.is_user_privileged(user_id, privilege_type.level): - self.reply("The user specified already has the required privileges.") - return - - self.message.room.change_privilege_level(user_id, privilege_type) - self.reply("The user specified has been given the privileges.") diff --git a/build/lib/BotpySE/CommandReboot.py b/build/lib/BotpySE/CommandReboot.py deleted file mode 100644 index 507dbea..0000000 --- a/build/lib/BotpySE/CommandReboot.py +++ /dev/null @@ -1,19 +0,0 @@ -# -# CommandReboot.py -# Botpy -# -# Created by Ashish Ahuja on 5th October 2017. -# -# - -from .Command import * -from . import Utilities - -class CommandReboot(Command): - @staticmethod - def usage(): - return ['reboot', 'restart'] - - def run(self): - self.reply("Rebooting...") - Utilities.should_reboot = True diff --git a/build/lib/BotpySE/CommandStop.py b/build/lib/BotpySE/CommandStop.py deleted file mode 100644 index f291bda..0000000 --- a/build/lib/BotpySE/CommandStop.py +++ /dev/null @@ -1,19 +0,0 @@ -# -# CommandStop.py -# Botpy -# -# Created by Ashish Ahuja on 5th September 2017. -# -# - -from .Command import * -from . import Utilities - -class CommandStop(Command): - @staticmethod - def usage(): - return ['stop', 'shutdown'] - - def run(self): - self.reply("Shutting down...") - Utilities.should_shutdown = True diff --git a/build/lib/BotpySE/CommandUnprivilegeUser.py b/build/lib/BotpySE/CommandUnprivilegeUser.py deleted file mode 100644 index 3b069c5..0000000 --- a/build/lib/BotpySE/CommandUnprivilegeUser.py +++ /dev/null @@ -1,23 +0,0 @@ -# -# CommandUnprivilegeUser.py -# Botpy -# -# Created by Ashish Ahuja on 28th September 2017. -# -# - -from .Command import * - -class CommandUnprivilegeUser(Command): - @staticmethod - def usage(): - return ["unprivilege *", "unprivilege user *", "remove privileges *"] - - def privileges(self): - return 1 - - def run(self): - user_id = int(self.arguments[0]) - - self.message.room.change_privilege_level(user_id) - self.reply("The user specified has been stripped of all privileges.") diff --git a/build/lib/BotpySE/EssentialBackgroundTasks.py b/build/lib/BotpySE/EssentialBackgroundTasks.py deleted file mode 100644 index b418bce..0000000 --- a/build/lib/BotpySE/EssentialBackgroundTasks.py +++ /dev/null @@ -1,7 +0,0 @@ -# -# EssentialBackgroundTasks.py -# Botpy -# -# Created by Ashish Ahuja on 4th September 2017. -# -# diff --git a/build/lib/BotpySE/PrivilegeType.py b/build/lib/BotpySE/PrivilegeType.py deleted file mode 100644 index a231eb7..0000000 --- a/build/lib/BotpySE/PrivilegeType.py +++ /dev/null @@ -1,15 +0,0 @@ -# -# PrivilegeType.py -# Botpy -# -# Created by Ashish Ahuja on 12th September 2017. -# -# - -class PrivilegeType: - def __init__(self, privilege_level, privilege_name): - self.level = privilege_level - self.name = privilege_name - - def change_privilege_level(self, new_level): - self.level = new_level diff --git a/build/lib/BotpySE/PrivilegedChatUser.py b/build/lib/BotpySE/PrivilegedChatUser.py deleted file mode 100644 index 3e4bcb3..0000000 --- a/build/lib/BotpySE/PrivilegedChatUser.py +++ /dev/null @@ -1,27 +0,0 @@ -# -# PrivilegedChatUser.py -# Botpy -# -# Created by Ashish Ahuja on 12th September 2017. -# -# - -class PrivilegedChatUser: - def __init__(self, user_id, room_id, privilege_type): - self.user_id = user_id - self.room_id = room_id - self.level = privilege_type.level - self.privilege_type = privilege_type - self.block = False - - def block_user(self): - self.block = True - - def unblock_user(self): - self.block = False - - def is_privileged(self, privilege_required): - if self.privilege_level >= privilege_required and not self.block: - return True - - return False diff --git a/build/lib/BotpySE/Reporter.py b/build/lib/BotpySE/Reporter.py deleted file mode 100644 index 6fbf448..0000000 --- a/build/lib/BotpySE/Reporter.py +++ /dev/null @@ -1,57 +0,0 @@ -# -# Reporter.py -# Botpy -# -# Created by Ashish Ahuja on 5th October 2017. -# -# - -class Reporter: - def __init__(self, bot_name, error_rooms, report_rooms, bot_link): - self.bot_name = bot_name - self.error_rooms = error_rooms - self.report_rooms = report_rooms - self.bot_link = bot_link - self.reports = list() - self.pending_reports = list() - - def get_start_link(self): - return "\[ [" + self.bot_name + "](" + self.bot_link + ") ]" - - def report(self, message, error_rooms=False): - room_list = self.report_rooms - - if error_rooms: - room_list.extend(self.error_rooms) - - for each_room in room_list: - report = Report(self.get_start_link() + ' ' + message, each_room) - report.report() - self.reports.append(report) - self.pending_reports.append(report) - - def check_message_for_report(self, message): - print(message) - print('yo') - if message.startswith(' [