Merge pull request #4 from SpiceBot/development

Development
This commit is contained in:
Deathbybandaid 2019-05-02 11:59:15 -04:00 committed by GitHub
commit 7061fa0ca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 193 additions and 84 deletions

View File

@ -6,17 +6,17 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi
import sopel.module import sopel.module
from sopel.tools import stderr from sopel.tools import stderr
from sopel.config.types import StaticSection, ValidatedAttribute, ListAttribute from sopel.config.types import StaticSection, ValidatedAttribute, ListAttribute
from sopel.module import OP, ADMIN, VOICE, OWNER, HALFOP
HOP = HALFOP
import time import time
import re
import spicemanip import spicemanip
import sopel_modules.osd 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): class SpiceBot_Channels_MainSection(StaticSection):
@ -70,23 +70,8 @@ def trigger_channel_list_initial(bot, trigger):
foundchannelcount = len(bot.memory['SpiceBot_Channels']['channels'].keys()) foundchannelcount = len(bot.memory['SpiceBot_Channels']['channels'].keys())
stderr("[SpiceBot_Channels] Channel listing finished! " + str(foundchannelcount) + " channel(s) found.") stderr("[SpiceBot_Channels] Channel listing finished! " + str(foundchannelcount) + " channel(s) found.")
# JOIN join_all_channels(bot)
if bot.config.SpiceBot_Channels.joinall: chanadmin_all_channels(bot)
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)
if "*" in bot.memory['SpiceBot_Channels']['channels']: if "*" in bot.memory['SpiceBot_Channels']['channels']:
del 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: if len(newlist) and bot.config.SpiceBot_Channels.announcenew:
bot.osd(["The Following channel(s) are new:", spicemanip.main(newlist, 'andlist')], bot.channels.keys()) bot.osd(["The Following channel(s) are new:", spicemanip.main(newlist, 'andlist')], bot.channels.keys())
# JOIN 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']))
# Chan ADMIN +a chanadmin_all_channels(bot)
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)
if "*" in bot.memory['SpiceBot_Channels']['channels']: if "*" in bot.memory['SpiceBot_Channels']['channels']:
del 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) dispmsg.append("Channel users for " + str(channel) + " are: " + userslist)
bot.osd(dispmsg, trigger.nick, 'notice') bot.osd(dispmsg, trigger.nick, 'notice')
return 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)

View File

@ -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')

View File

@ -0,0 +1,6 @@
# coding=utf-8
"""Event Parsing from IRC
"""
from __future__ import unicode_literals, absolute_import, print_function, division
from .Restart import *

View File

@ -3,11 +3,20 @@
""" """
from __future__ import unicode_literals, absolute_import, print_function, division 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 collections
import re
import os
import spicemanip import spicemanip
"""Sopel Wrapping Tools"""
def sopel_triggerargs(bot, trigger, command_type): def sopel_triggerargs(bot, trigger, command_type):
triggerargs = [] triggerargs = []
@ -23,6 +32,9 @@ def sopel_triggerargs(bot, trigger, command_type):
return triggerargs return triggerargs
"""List Manipulation Functions"""
def inlist(bot, searchterm, searchlist): def inlist(bot, searchterm, searchlist):
# verify we are searching a list # verify we are searching a list
@ -42,3 +54,98 @@ def inlist(bot, searchterm, searchlist):
return True return True
else: else:
return False 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))

View File

@ -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')

View File

@ -0,0 +1,6 @@
# coding=utf-8
"""Event Parsing from IRC
"""
from __future__ import unicode_literals, absolute_import, print_function, division
from .Update import *