Merge pull request #87 from SpiceBot/development

Development
This commit is contained in:
Deathbybandaid 2019-07-01 10:32:56 -04:00 committed by GitHub
commit c9f601608b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 284 additions and 82 deletions

View File

@ -26,7 +26,7 @@ class BotChannels():
"""This Logs all channels known to the server"""
def __init__(self):
self.setup_channels()
self.who_reqs = {}
self.lock = threading.Lock()
self.channel_lock = False
self.channels = []
@ -91,6 +91,20 @@ class BotChannels():
topic = re.sub(actual_topic, r'\1', topic)
return topic
def add_channel(self, channel):
channel = str(channel).lower()
self.lock.acquire()
if channel not in self.channels:
self.channels.append(channel)
self.lock.release()
def remove_channel(self, channel):
channel = str(channel).lower()
self.lock.acquire()
if channel in self.channels:
self.channels.remove(channel)
self.lock.release()
def bot_part_empty(self, bot):
"""Don't stay in empty channels"""
ignorepartlist = []
@ -103,8 +117,7 @@ class BotChannels():
self.lock.acquire()
del self.dict['list'][channel.lower()]
self.lock.release()
if channel.lower() in self.channels:
self.channels.remove(channel.lower())
self.remove_channel(channel)
def join_all_channels(self, bot):
if botconfig.SpiceBot_Channels.joinall:
@ -116,13 +129,13 @@ class BotChannels():
bot.write(('SAJOIN', bot.nick, self.dict['list'][channel]['name']))
if channel in list(bot.channels.keys()):
if channel.lower() not in self.channels:
self.channels.append(channel.lower())
self.add_channel(channel)
def chanadmin_all_channels(self, bot):
# Chan ADMIN +a
for channel in list(bot.channels.keys()):
if channel.startswith("#"):
self.add_channel(channel)
if channel not in botconfig.SpiceBot_Channels.chanignore:
if botconfig.SpiceBot_Channels.operadmin:
if not bot.channels[channel].privileges[bot.nick] < sopel.module.ADMIN:
@ -131,8 +144,7 @@ class BotChannels():
bot.part(channel)
if channel not in list(bot.channels.keys()):
if channel.lower() in self.channels:
self.channels.remove(channel.lower())
self.remove_channel(channel)
def whois_ident(self, nick):
nick = Identifier(nick)
@ -140,31 +152,36 @@ class BotChannels():
return nick_id
def add_to_channel(self, channel, nick, nick_id=None):
channel = str(channel)
channel = str(channel).lower()
self.lock.acquire()
if not nick_id:
nick_id = self.whois_ident(nick)
if channel.lower() not in list(self.dict['list'].keys()):
self.dict['list'][channel.lower()] = dict()
if 'users' not in self.dict['list'][channel.lower()]:
self.dict['list'][channel.lower()]['users'] = []
if nick_id not in self.dict['list'][channel.lower()]['users']:
if int(nick_id) not in self.dict['list'][channel.lower()]['users']:
self.dict['list'][channel.lower()]['users'].append(int(nick_id))
if channel.lower() not in self.channels:
self.channels.append(channel.lower())
self.lock.release()
def remove_from_channel(self, channel, nick, nick_id=None):
channel = str(channel)
channel = str(channel).lower()
self.lock.acquire()
if not nick_id:
nick_id = self.whois_ident(nick)
if channel.lower() not in list(self.dict['list'].keys()):
self.dict['list'][channel.lower()] = dict()
if 'users' not in self.dict['list'][channel.lower()]:
self.dict['list'][channel.lower()]['users'] = []
if nick_id in self.dict['list'][channel.lower()]['users']:
self.dict['list'][channel.lower()]['users'].remove(nick_id)
if int(nick_id) in self.dict['list'][channel.lower()]['users']:
self.dict['list'][channel.lower()]['users'].remove(int(nick_id))
self.lock.release()
def remove_all_from_channel(self, channel):
channel = str(channel)
channel = str(channel).lower()
self.lock.acquire()
if channel.lower() not in list(self.dict['list'].keys()):
self.dict['list'][channel.lower()] = dict()
if 'users' not in self.dict['list'][channel.lower()]:
@ -172,6 +189,7 @@ class BotChannels():
self.dict['list'][channel.lower()]['users'] = []
if channel.lower() in self.channels:
self.channels.remove(channel.lower())
self.lock.release()
def channel_scan(self, bot):
for channel in list(bot.channels.keys()):
@ -236,6 +254,38 @@ class BotChannels():
def mode(self, bot, trigger):
return
def rpl_names(self, bot, trigger):
"""Handle NAMES response, happens when joining to channels."""
names = trigger.split()
# TODO specific to one channel type. See issue 281.
channels = re.search(r'(#\S*)', trigger.raw)
if not channels:
return
channel = Identifier(channels.group(1))
self.add_channel(channel)
mapping = {'+': sopel.module.VOICE,
'%': sopel.module.HALFOP,
'@': sopel.module.OP,
'&': sopel.module.ADMIN,
'~': sopel.module.OWNER}
for name in names:
nick = Identifier(name.lstrip(''.join(mapping.keys())))
self.add_to_channel(channel, nick)
def rpl_who(self, bot, trigger):
if len(trigger.args) < 2 or trigger.args[1] not in self.who_reqs:
# Ignored, some module probably called WHO
return
if len(trigger.args) != 8:
return
_, _, channel, user, host, nick, status, account = trigger.args
nick = Identifier(nick)
channel = Identifier(channel)
self.add_to_channel(channel, nick)
def check_channel_bot(self, channel, allchan=False):
if str(channel).lower() in self.channels:
return True

View File

@ -35,6 +35,8 @@ class BotEvents(object):
def __init__(self):
self.lock = threading.Lock()
self.RPL_WELCOME = '001' # This is a defined IRC event
self.RPL_NAMREPLY = '353' # This is a defined IRC event
self.RPL_WHOREPLY = '352' # This is a defined IRC event
self.BOT_UPTIME = time.time()
self.BOT_WELCOME = '1001'
self.BOT_READY = '1002'

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals, absolute_import, division, print_function
"""A way to track users"""
import sopel
from sopel.tools import Identifier
from .Database import db as botdb
@ -9,21 +10,28 @@ from .Tools import is_number, inlist, similar, array_arrangesort
import spicemanip
import threading
import re
# TODO timestamp for new .seen
class BotUsers():
def __init__(self):
self.lock = threading.Lock()
self.dict = {
"all": botdb.get_bot_value('users') or {},
"online": [],
"offline": [],
"away": [], # TODO
"current": {},
}
"""during setup, all users from database are offline until marked online"""
for user_id in list(self.dict["all"].keys()):
self.lock.acquire()
self.dict["offline"].append(int(user_id))
self.lock.release()
def __getattr__(self, name):
''' will only get called for undefined attributes '''
@ -34,18 +42,21 @@ class BotUsers():
raise Exception('User dict does not contain a function or key ' + str(name.lower()))
def ID(self, nickinput):
self.lock.acquire()
if is_number(nickinput):
nick_id = nickinput
if nick_id in list(self.dict["current"].keys()):
nick = self.dict["current"][nick_id]["nick"]
elif nick_id in list(self.dict["all"].keys()) and len(self.dict["all"][nick_id]):
nick = self.dict["all"][nick_id][0]
elif nick_id in list(self.dict["all"].keys()) and len(self.dict["all"][int(nick_id)]):
nick = self.dict["all"][int(nick_id)][0]
else:
raise Exception('ID ' + str(nickinput) + ' does not appear to be associated with a nick')
self.lock.release()
return nick
else:
nick_id = self.whois_ident(nickinput)
return nick_id
self.lock.release()
return int(nick_id)
def whois_ident(self, nick, usercreate=True):
nick = Identifier(nick)
@ -62,23 +73,27 @@ class BotUsers():
botdb.set_bot_value('users', self.dict["all"])
def add_to_all(self, nick, nick_id=None):
self.lock.acquire()
if not nick_id:
nick_id = self.ID(nick)
# add to all if not there
if nick_id not in list(self.dict["all"].keys()):
self.dict["all"][nick_id] = []
if int(nick_id) not in list(self.dict["all"].keys()):
self.dict["all"][int(nick_id)] = []
# add nick alias as index 0
if nick in self.dict["all"][nick_id]:
self.dict["all"][nick_id].remove(nick)
self.dict["all"][nick_id].insert(0, nick)
if nick in self.dict["all"][int(nick_id)]:
self.dict["all"][int(nick_id)].remove(nick)
self.dict["all"][int(nick_id)].insert(0, nick)
self.lock.release()
self.save_user_db()
def add_to_current(self, nick, nick_id=None):
self.lock.acquire()
if not nick_id:
nick_id = self.whois_ident(nick)
# add to current if not there
if nick_id not in list(self.dict["current"].keys()):
self.dict["current"][nick_id] = {"channels": [], "nick": nick}
if int(nick_id) not in list(self.dict["current"].keys()):
self.dict["current"][int(nick_id)] = {"channels": [], "nick": nick}
self.lock.release()
def channel_scan(self, bot):
for channel in list(bot.channels.keys()):
@ -90,15 +105,44 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(user, nick_id)
# set current nick
self.dict["current"][nick_id]["nick"] = user
self.mark_current_nick(user, nick_id)
# add joined channel to nick list
if str(channel).lower() not in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].append(str(channel).lower())
self.add_channel(channel, nick_id)
# mark user as online
if nick_id not in self.dict["online"]:
self.dict["online"].append(int(nick_id))
if nick_id in self.dict["offline"]:
self.dict["offline"].remove(nick_id)
self.mark_user_online(nick_id)
def mark_current_nick(self, nick, nick_id):
self.lock.acquire()
self.dict["current"][int(nick_id)]["nick"] = nick
self.lock.release()
def add_channel(self, channel, nick_id):
self.lock.acquire()
if str(channel).lower() not in self.dict["current"][int(nick_id)]["channels"]:
self.dict["current"][int(nick_id)]["channels"].append(str(channel).lower())
self.lock.release()
def remove_channel(self, channel, nick_id):
self.lock.acquire()
if str(channel).lower() in self.dict["current"][int(nick_id)]["channels"]:
self.dict["current"][int(nick_id)]["channels"].remove(str(channel).lower())
self.lock.release()
def mark_user_online(self, nick_id):
self.lock.acquire()
if int(nick_id) not in self.dict["online"]:
self.dict["online"].append(int(nick_id))
if int(nick_id) in self.dict["offline"]:
self.dict["offline"].remove(int(nick_id))
self.lock.release()
def mark_user_offline(self, nick_id):
self.lock.acquire()
if int(nick_id) in self.dict["online"]:
self.dict["online"].remove(int(nick_id))
if int(nick_id) not in self.dict["offline"]:
self.dict["offline"].append(int(nick_id))
self.lock.release()
def join(self, bot, trigger):
if trigger.nick == bot.nick:
@ -110,15 +154,11 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(user, nick_id)
# set current nick
self.dict["current"][nick_id]["nick"] = user
self.mark_current_nick(user, nick_id)
# add joined channel to nick list
if str(trigger.sender).lower() not in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].append(str(trigger.sender).lower())
self.add_channel(trigger.sender, nick_id)
# mark user as online
if nick_id not in self.dict["online"]:
self.dict["online"].append(int(nick_id))
if nick_id in self.dict["offline"]:
self.dict["offline"].remove(nick_id)
self.mark_user_online(nick_id)
return
# Identify
nick_id = self.whois_ident(trigger.nick)
@ -127,15 +167,27 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id)
# set current nick
self.dict["current"][nick_id]["nick"] = trigger.nick
self.mark_current_nick(trigger.nick, nick_id)
# add joined channel to nick list
if str(trigger.sender).lower() not in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].append(str(trigger.sender).lower())
self.add_channel(trigger.sender, nick_id)
# mark user as online
if nick_id not in self.dict["online"]:
self.dict["online"].append(int(nick_id))
if nick_id in self.dict["offline"]:
self.dict["offline"].remove(nick_id)
self.mark_user_online(nick_id)
def chat(self, bot, trigger):
if trigger.nick == bot.nick:
return
# Identify
nick_id = self.whois_ident(trigger.nick)
# Verify nick is in the all list
self.add_to_all(trigger.nick, nick_id)
# Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id)
# set current nick
self.mark_current_nick(trigger.nick, nick_id)
# add joined channel to nick list
self.add_channel(trigger.sender, nick_id)
# mark user as online
self.mark_user_online(nick_id)
def quit(self, bot, trigger):
if trigger.nick == bot.nick:
@ -147,25 +199,16 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id)
# empty nicks channel list
if str(trigger.sender).lower() in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].remove(str(trigger.sender).lower())
self.dict["current"][nick_id]["channels"] = []
self.remove_channel(trigger.sender, nick_id)
# mark user as offline
if nick_id in self.dict["online"]:
self.dict["online"].remove(nick_id)
if nick_id not in self.dict["offline"]:
self.dict["offline"].append(int(nick_id))
self.mark_user_offline(nick_id)
def part(self, bot, trigger):
if trigger.nick == bot.nick:
for nick_id in list(self.dict["current"].keys()):
if str(trigger.sender).lower() in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].remove(str(trigger.sender).lower())
self.remove_channel(trigger.sender, nick_id)
# mark offline
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.dict["online"].remove(nick_id)
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.dict["offline"].append(int(nick_id))
self.mark_user_offline(nick_id)
return
# Identify
nick_id = self.whois_ident(trigger.nick)
@ -174,25 +217,17 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id)
# remove channel from nick list
if str(trigger.sender).lower() in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].remove(str(trigger.sender).lower())
self.remove_channel(trigger.sender, nick_id)
# mark offline
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.dict["online"].remove(nick_id)
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.dict["offline"].append(int(nick_id))
self.mark_user_offline(nick_id)
def kick(self, bot, trigger):
targetnick = Identifier(str(trigger.args[1]))
if targetnick == bot.nick:
for nick_id in list(self.dict["current"].keys()):
if str(trigger.sender).lower() in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].remove(str(trigger.sender).lower())
self.remove_channel(trigger.sender, nick_id)
# mark offline
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.dict["online"].remove(nick_id)
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.dict["offline"].append(int(nick_id))
self.mark_user_offline(nick_id)
return
# Identify
nick_id = self.whois_ident(targetnick)
@ -201,13 +236,9 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(targetnick, nick_id)
# remove channel from nick list
if str(trigger.sender).lower() in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].remove(str(trigger.sender).lower())
self.remove_channel(trigger.sender, nick_id)
# mark offline
if not len(self.dict["current"][nick_id]["channels"]) and nick_id in self.dict["online"]:
self.dict["online"].remove(nick_id)
if not len(self.dict["current"][nick_id]["channels"]) and nick_id not in self.dict["offline"]:
self.dict["offline"].append(int(nick_id))
self.mark_user_offline(nick_id)
def nick(self, bot, trigger):
newnick = Identifier(trigger)
@ -223,19 +254,92 @@ class BotUsers():
# Verify nick is in the all list
self.add_to_current(newnick, nick_id)
# set current nick
self.dict["current"][nick_id]["nick"] = newnick
self.mark_current_nick(newnick, nick_id)
# add joined channel to nick list
if str(trigger.sender).lower() not in self.dict["current"][nick_id]["channels"]:
self.dict["current"][nick_id]["channels"].append(str(trigger.sender).lower())
self.add_channel(trigger.sender, nick_id)
# mark user as online
if nick_id not in self.dict["online"]:
self.dict["online"].append(int(nick_id))
if nick_id in self.dict["offline"]:
self.dict["offline"].remove(nick_id)
self.mark_user_online(nick_id)
def mode(self, bot, trigger):
return
def rpl_names(self, bot, trigger):
"""Handle NAMES response, happens when joining to channels."""
names = trigger.split()
# TODO specific to one channel type. See issue 281.
channels = re.search(r'(#\S*)', trigger.raw)
if not channels:
return
channel = Identifier(channels.group(1))
mapping = {'+': sopel.module.VOICE,
'%': sopel.module.HALFOP,
'@': sopel.module.OP,
'&': sopel.module.ADMIN,
'~': sopel.module.OWNER}
for name in names:
nick = Identifier(name.lstrip(''.join(mapping.keys())))
# Identify
nick_id = self.whois_ident(nick)
# Verify nick is in the all list
self.add_to_all(nick, nick_id)
# Verify nick is in the all list
self.add_to_current(nick, nick_id)
# set current nick
self.mark_current_nick(nick, nick_id)
# add joined channel to nick list
self.add_channel(channel, nick_id)
# mark user as online
self.mark_user_online(nick_id)
def rpl_who(self, bot, trigger):
if len(trigger.args) < 2 or trigger.args[1] not in self.who_reqs:
# Ignored, some module probably called WHO
return
if len(trigger.args) != 8:
return
_, _, channel, user, host, nick, status, account = trigger.args
nick = Identifier(nick)
channel = Identifier(channel)
# Identify
nick_id = self.whois_ident(nick)
# Verify nick is in the all list
self.add_to_all(nick, nick_id)
# Verify nick is in the all list
self.add_to_current(nick, nick_id)
# set current nick
self.mark_current_nick(nick, nick_id)
# add joined channel to nick list
self.add_channel(channel, nick_id)
# mark user as online
self.mark_user_online(nick_id)
def account(self, bot, trigger):
# Identify
nick_id = self.whois_ident(trigger.nick)
# Verify nick is in the all list
self.add_to_all(trigger.nick, nick_id)
# Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id)
# set current nick
self.mark_current_nick(trigger.nick, nick_id)
# mark user as online
self.mark_user_online(nick_id)
def track_notify(self, bot, trigger):
# Identify
nick_id = self.whois_ident(trigger.nick)
# Verify nick is in the all list
self.add_to_all(trigger.nick, nick_id)
# Verify nick is in the all list
self.add_to_current(trigger.nick, nick_id)
# set current nick
self.mark_current_nick(trigger.nick, nick_id)
# mark user as online
self.mark_user_online(nick_id)
def nick_actual(self, nick, altlist=None):
nick_id = self.whois_ident(nick)
nick_actual = self.ID(nick_id)

View File

@ -41,6 +41,7 @@ def nickname_comand_update(bot, trigger, botcom):
SpiceBot.spicebot_reload(bot, 'SpiceBot_Update', quitmessage)
@SpiceBot.prerun('nickname')
@sopel.module.nickname_commands('restart')
def nickname_comand_restart(bot, trigger, botcom):

View File

@ -139,3 +139,17 @@ def channels_users_mode(bot, trigger):
def bot_channelscan(bot, trigger):
while True:
SpiceBot.channels.channel_scan(bot)
@sopel.module.event(SpiceBot.events.RPL_NAMREPLY)
@sopel.module.rule('(.*)')
def handle_names(bot, trigger):
SpiceBot.channels.rpl_names(bot, trigger)
@sopel.module.event(SpiceBot.events.RPL_WHOREPLY)
@sopel.module.rule('.*')
@sopel.module.priority('high')
@sopel.module.unblockable
def recv_who(bot, trigger):
SpiceBot.channels.rpl_who(bot, trigger)

View File

@ -49,3 +49,34 @@ def users_mode(bot, trigger):
@sopel.module.rule('.*')
def bot_channelscan_users(bot, trigger):
SpiceBot.users.channel_scan(bot)
@sopel.module.rule('.*')
def bot_users_chat(bot, trigger):
SpiceBot.users.chat(bot, trigger)
@sopel.module.event(SpiceBot.events.RPL_NAMREPLY)
@sopel.module.rule('(.*)')
def handle_names(bot, trigger):
SpiceBot.users.rpl_names(bot, trigger)
@sopel.module.event(SpiceBot.events.RPL_WHOREPLY)
@sopel.module.rule('.*')
@sopel.module.priority('high')
@sopel.module.unblockable
def recv_who(bot, trigger):
SpiceBot.users.rpl_who(bot, trigger)
@sopel.module.event('ACCOUNT')
@sopel.module.rule('.*')
def account_notify(bot, trigger):
SpiceBot.users.account(bot, trigger)
@sopel.module.event('AWAY')
@sopel.module.rule('.*')
def track_notify(bot, trigger):
SpiceBot.users.track_notify(bot, trigger)