From 5bcce3b474fecd04b4b164bb68e4493c2961c952 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 2 May 2019 11:53:38 -0400 Subject: [PATCH] Update,Restart Functions --- sopel_modules/SpiceBot_Channels/Channels.py | 92 ++--------------- sopel_modules/SpiceBot_Restart/Restart.py | 24 +++++ sopel_modules/SpiceBot_Restart/__init__.py | 6 ++ sopel_modules/SpiceBot_SBTools/__init__.py | 107 ++++++++++++++++++++ sopel_modules/SpiceBot_Update/Update.py | 42 ++++++++ sopel_modules/SpiceBot_Update/__init__.py | 6 ++ 6 files changed, 193 insertions(+), 84 deletions(-) create mode 100644 sopel_modules/SpiceBot_Restart/Restart.py create mode 100644 sopel_modules/SpiceBot_Restart/__init__.py create mode 100644 sopel_modules/SpiceBot_Update/Update.py create mode 100644 sopel_modules/SpiceBot_Update/__init__.py diff --git a/sopel_modules/SpiceBot_Channels/Channels.py b/sopel_modules/SpiceBot_Channels/Channels.py index 60bb0ce..9eef197 100644 --- a/sopel_modules/SpiceBot_Channels/Channels.py +++ b/sopel_modules/SpiceBot_Channels/Channels.py @@ -6,17 +6,17 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi import sopel.module from sopel.tools import stderr from sopel.config.types import StaticSection, ValidatedAttribute, ListAttribute -from sopel.module import OP, ADMIN, VOICE, OWNER, HALFOP -HOP = HALFOP import time -import re import spicemanip import sopel_modules.osd -from sopel_modules.SpiceBot_SBTools import sopel_triggerargs, inlist +from sopel_modules.SpiceBot_SBTools import ( + sopel_triggerargs, inlist, topic_compile, channel_privs, + join_all_channels, chanadmin_all_channels, channel_list_current + ) class SpiceBot_Channels_MainSection(StaticSection): @@ -70,23 +70,8 @@ def trigger_channel_list_initial(bot, trigger): foundchannelcount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) stderr("[SpiceBot_Channels] Channel listing finished! " + str(foundchannelcount) + " channel(s) found.") - # JOIN - if bot.config.SpiceBot_Channels.joinall: - for channel in bot.memory['SpiceBot_Channels']['channels'].keys(): - if channel not in bot.channels.keys() and channel not in bot.config.SpiceBot_Channels.chanignore: - bot.write(('JOIN', bot.nick, bot.memory['SpiceBot_Channels']['channels'][channel]['name'])) - if channel not in bot.channels.keys(): - bot.write(('SAJOIN', bot.nick, bot.memory['SpiceBot_Channels']['channels'][channel]['name'])) - - # Chan ADMIN +a - for channel in bot.channels.keys(): - if channel not in bot.config.SpiceBot_Channels.chanignore: - if bot.config.SpiceBot_Channels.operadmin: - if not bot.channels[channel].privileges[bot.nick] < ADMIN: - bot.write(('SAMODE', channel, "+a", bot.nick)) - channel_privs(bot, channel) - else: - bot.part(channel) + join_all_channels(bot) + chanadmin_all_channels(bot) if "*" in bot.memory['SpiceBot_Channels']['channels']: del bot.memory['SpiceBot_Channels']['channels']["*"] @@ -106,23 +91,9 @@ def trigger_channel_list_initial(bot, trigger): if len(newlist) and bot.config.SpiceBot_Channels.announcenew: bot.osd(["The Following channel(s) are new:", spicemanip.main(newlist, 'andlist')], bot.channels.keys()) - # JOIN - if bot.config.SpiceBot_Channels.joinall: - for channel in bot.memory['SpiceBot_Channels']['channels'].keys(): - if channel not in bot.channels.keys() and channel not in bot.config.SpiceBot_Channels.chanignore: - bot.write(('JOIN', bot.nick, bot.memory['SpiceBot_Channels']['channels'][channel]['name'])) - if channel not in bot.channels.keys(): - bot.write(('SAJOIN', bot.nick, bot.memory['SpiceBot_Channels']['channels'][channel]['name'])) + join_all_channels(bot) - # Chan ADMIN +a - for channel in bot.channels.keys(): - if channel not in bot.config.SpiceBot_Channels.chanignore: - if bot.config.SpiceBot_Channels.operadmin: - if not bot.channels[channel].privileges[bot.nick] < ADMIN: - bot.write(('SAMODE', channel, "+a", bot.nick)) - channel_privs(bot, channel) - else: - bot.part(channel) + chanadmin_all_channels(bot) if "*" in bot.memory['SpiceBot_Channels']['channels']: del bot.memory['SpiceBot_Channels']['channels']["*"] @@ -326,50 +297,3 @@ def nickname_comand_chanstats(bot, trigger): dispmsg.append("Channel users for " + str(channel) + " are: " + userslist) bot.osd(dispmsg, trigger.nick, 'notice') return - - -def channel_list_current(bot): - newlist = [item for item in bot.channels.keys() if item.lower() not in bot.memory['SpiceBot_Channels']['channels']] - for channel in newlist: - topic = bot.channels[channel].topic - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()] = dict() - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['name'] = str(channel) - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['topic'] = topic_compile(topic) - channel_privs(bot, channel) - - if "*" in bot.memory['SpiceBot_Channels']: - bot.memory['SpiceBot_Channels'].remove("*") - - -def topic_compile(topic): - actual_topic = re.compile(r'^\[\+[a-zA-Z]+\] (.*)') - topic = re.sub(actual_topic, r'\1', topic) - return topic - - -def channel_privs(bot, channel): - - if channel not in bot.memory['SpiceBot_Channels']['channels'].keys(): - topic = bot.channels[channel].topic - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()] = dict() - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['name'] = str(channel) - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['topic'] = topic_compile(topic) - - for chan in bot.memory['SpiceBot_Channels']['channels'].keys(): - for privtype in ['VOICE', 'HOP', 'OP', 'ADMIN', 'OWNER']: - if privtype not in bot.memory['SpiceBot_Channels']['channels'][str(chan).lower()].keys(): - bot.memory['SpiceBot_Channels']['channels'][str(chan).lower()][privtype] = [] - - for user in bot.channels[channel].privileges.keys(): - try: - privnum = bot.channels[channel].privileges[user] or 0 - except KeyError: - privnum = 0 - - for privtype in ['VOICE', 'HOP', 'OP', 'ADMIN', 'OWNER']: - if privnum == eval(privtype) or (privnum >= eval(privtype) and privtype == 'OWNER'): - if user not in bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype]: - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype].append(user) - else: - if user in bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype]: - bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype].remove(user) diff --git a/sopel_modules/SpiceBot_Restart/Restart.py b/sopel_modules/SpiceBot_Restart/Restart.py new file mode 100644 index 0000000..4ed0f09 --- /dev/null +++ b/sopel_modules/SpiceBot_Restart/Restart.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# coding=utf-8 +from __future__ import unicode_literals, absolute_import, print_function, division + +# sopel imports +import sopel.module +from sopel.tools import stderr +from sopel.config.types import StaticSection, ValidatedAttribute + +import sopel_modules.osd + +from sopel_modules.SpiceBot_SBTools import service_manip + + +@sopel.module.nickname_commands('restart') +def nickname_comand_chanstats(bot, trigger): + + if not trigger.admin: + bot.say("You are not authorized to perform this function.") + + stderr("Recieved Command to update.") + bot.osd("Received command from " + trigger.nick + " to restart. Be Back Soon!", bot.channels.keys()) + + service_manip(bot, bot.nick, 'restart') diff --git a/sopel_modules/SpiceBot_Restart/__init__.py b/sopel_modules/SpiceBot_Restart/__init__.py new file mode 100644 index 0000000..8bda139 --- /dev/null +++ b/sopel_modules/SpiceBot_Restart/__init__.py @@ -0,0 +1,6 @@ +# coding=utf-8 +"""Event Parsing from IRC +""" +from __future__ import unicode_literals, absolute_import, print_function, division + +from .Restart import * diff --git a/sopel_modules/SpiceBot_SBTools/__init__.py b/sopel_modules/SpiceBot_SBTools/__init__.py index f256903..71cc9cb 100644 --- a/sopel_modules/SpiceBot_SBTools/__init__.py +++ b/sopel_modules/SpiceBot_SBTools/__init__.py @@ -3,11 +3,20 @@ """ from __future__ import unicode_literals, absolute_import, print_function, division +from sopel.module import OP, ADMIN, VOICE, OWNER, HALFOP +from sopel.tools import stderr +HOP = HALFOP + import collections +import re +import os import spicemanip +"""Sopel Wrapping Tools""" + + def sopel_triggerargs(bot, trigger, command_type): triggerargs = [] @@ -23,6 +32,9 @@ def sopel_triggerargs(bot, trigger, command_type): return triggerargs +"""List Manipulation Functions""" + + def inlist(bot, searchterm, searchlist): # verify we are searching a list @@ -42,3 +54,98 @@ def inlist(bot, searchterm, searchlist): return True else: return False + + +"""Channel Functions""" + + +def topic_compile(topic): + actual_topic = re.compile(r'^\[\+[a-zA-Z]+\] (.*)') + topic = re.sub(actual_topic, r'\1', topic) + return topic + + +def channel_privs(bot, channel): + + if channel not in bot.memory['SpiceBot_Channels']['channels'].keys(): + topic = bot.channels[channel].topic + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()] = dict() + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['name'] = str(channel) + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['topic'] = topic_compile(topic) + + for chan in bot.memory['SpiceBot_Channels']['channels'].keys(): + for privtype in ['VOICE', 'HOP', 'OP', 'ADMIN', 'OWNER']: + if privtype not in bot.memory['SpiceBot_Channels']['channels'][str(chan).lower()].keys(): + bot.memory['SpiceBot_Channels']['channels'][str(chan).lower()][privtype] = [] + + for user in bot.channels[channel].privileges.keys(): + try: + privnum = bot.channels[channel].privileges[user] or 0 + except KeyError: + privnum = 0 + + for privtype in ['VOICE', 'HOP', 'OP', 'ADMIN', 'OWNER']: + if privnum == eval(privtype) or (privnum >= eval(privtype) and privtype == 'OWNER'): + if user not in bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype]: + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype].append(user) + else: + if user in bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype]: + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()][privtype].remove(user) + + +def join_all_channels(bot): + if bot.config.SpiceBot_Channels.joinall: + for channel in bot.memory['SpiceBot_Channels']['channels'].keys(): + if channel not in bot.channels.keys() and channel not in bot.config.SpiceBot_Channels.chanignore: + bot.write(('JOIN', bot.nick, bot.memory['SpiceBot_Channels']['channels'][channel]['name'])) + if channel not in bot.channels.keys(): + bot.write(('SAJOIN', bot.nick, bot.memory['SpiceBot_Channels']['channels'][channel]['name'])) + + +def chanadmin_all_channels(bot): + # Chan ADMIN +a + for channel in bot.channels.keys(): + if channel not in bot.config.SpiceBot_Channels.chanignore: + if bot.config.SpiceBot_Channels.operadmin: + if not bot.channels[channel].privileges[bot.nick] < ADMIN: + bot.write(('SAMODE', channel, "+a", bot.nick)) + channel_privs(bot, channel) + else: + bot.part(channel) + + +def channel_list_current(bot): + newlist = [item for item in bot.channels.keys() if item.lower() not in bot.memory['SpiceBot_Channels']['channels']] + for channel in newlist: + topic = bot.channels[channel].topic + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()] = dict() + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['name'] = str(channel) + bot.memory['SpiceBot_Channels']['channels'][str(channel).lower()]['topic'] = topic_compile(topic) + channel_privs(bot, channel) + + if "*" in bot.memory['SpiceBot_Channels']: + bot.memory['SpiceBot_Channels'].remove("*") + + +"""Environment Functions""" + + +def service_manip(bot, servicename, dowhat): + if str(dowhat) not in ["start", "stop", "restart"]: + return + try: + stderr(str(dowhat).title() + "ing " + str(servicename) + ".service.") + os.system("sudo service " + str(servicename) + " " + str(dowhat)) + except Exception as e: + stderr(str(dowhat).title() + "ing " + str(servicename) + ".service Failed: " + str(e)) + + +def spicebot_update(bot): + try: + stderr("Updating " + bot.nick + " from Github.") + for line in os.popen("pip3 install --upgrade --no-deps --force-reinstall git+" + + str(bot.config.SpiceBot_Update.gitrepo) + + "@" + str(bot.config.SpiceBot_Update.gitbranch)).read().split('\n'): + stderr(line) + except Exception as e: + stderr("Updating " + bot.nick + " from Github Failed:" + str(e)) diff --git a/sopel_modules/SpiceBot_Update/Update.py b/sopel_modules/SpiceBot_Update/Update.py new file mode 100644 index 0000000..479425b --- /dev/null +++ b/sopel_modules/SpiceBot_Update/Update.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# coding=utf-8 +from __future__ import unicode_literals, absolute_import, print_function, division + +# sopel imports +import sopel.module +from sopel.tools import stderr +from sopel.config.types import StaticSection, ValidatedAttribute + +import sopel_modules.osd + +from sopel_modules.SpiceBot_SBTools import service_manip, spicebot_update + + +class SpiceBot_Update_MainSection(StaticSection): + gitrepo = ValidatedAttribute('gitrepo', default="https://github.com/SpiceBot/SpiceBot") + gitbranch = ValidatedAttribute('gitrepo', default="master") + + +def configure(config): + config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) + config.SpiceBot_Update.configure_setting('gitrepo', 'SpiceBot_Update git repo to install') + config.SpiceBot_Update.configure_setting('gitbranch', 'SpiceBot_Update git branch to install') + + +def setup(bot): + stderr("[SpiceBot_Update] Initial Setup processing...") + bot.config.define_section("SpiceBot_Update", SpiceBot_Update_MainSection, validate=False) + + +@sopel.module.nickname_commands('update') +def nickname_comand_chanstats(bot, trigger): + + if not trigger.admin: + bot.say("You are not authorized to perform this function.") + + stderr("Recieved Command to update.") + bot.osd("Received command from " + trigger.nick + " to update from Github and restart. Be Back Soon!", bot.channels.keys()) + + spicebot_update(bot) + + service_manip(bot, bot.nick, 'restart') diff --git a/sopel_modules/SpiceBot_Update/__init__.py b/sopel_modules/SpiceBot_Update/__init__.py new file mode 100644 index 0000000..a2667e1 --- /dev/null +++ b/sopel_modules/SpiceBot_Update/__init__.py @@ -0,0 +1,6 @@ +# coding=utf-8 +"""Event Parsing from IRC +""" +from __future__ import unicode_literals, absolute_import, print_function, division + +from .Update import *