<commit_before>from __future__ import absolute_import
from bot.pluginDespatch import Plugin
import re
import logging
import pytz
import time
from twisted.internet.task import LoopingCall
from datetime import datetime, timedelta
from django.utils import timezone
from django.conf import settings
from .models import DownVotes, Penalty, Probation, NickHistory
from logos.roomlib import get_room_option

logger = logging.getLogger(__name__)
logging.config.dictConfig(settings.LOGGING)

DOWN_VOTE_MINUTES = 5
DOWN_VOTES_REQUIRED = 1
PENALTY_TIME = 120

MIN_FLOOD_INTERVAL = 5  # In Seconds
FLOOD_THRESHHOLD = 3
FLOOD_PENALTY_TIME = 60*5 # Flood penalty in seconds

from bot.logos_decorators import irc_room_permission_required, \
    irc_network_permission_required

# decorator to ensure logos trigger function
# has ops in room and nick is in room
def check_ops(check_nick_in_room=False, use_current_room=False, me=False):
    def decorator(func):
        def func_wrapper(self, regex, chan, nick, **kwargs):
            if use_current_room:
                if chan[0] == "#":
                    room = chan
                else: # If in a private message window
                    room = None
            else:
                try:
                    room = regex.group('room')   
                except IndexError:
                    room = None
                    
            if me:
                this_nick = nick
            else:
                this_nick = regex.group('nick').lower()
                
            my_nick = self.get_nickname()
            
            if room: # not private message or no current room
                if room.lower() not in self.get_rooms():
                    self.notice(nick, 'I am not in room {}'.format(room))
                    return
                    
                my_ops = self.get_op_status(my_nick, room)
                if my_ops is None or my_ops not in "~@&":
                    self.notice(nick, 'I do not have ops in room {}'.format(room))
                    return
                    
                if check_nick_in_room and this_nick not in self.get_room_nicks(room):
                    self.notice(nick, 'user {} is not in room'.format(this_nick))
                else:
                    return func(self, regex, [chan], nick, **kwargs)
            else: # is private message
                rooms = self.get_rooms_for_nick(nick)
                opped_rooms = []
                for room in rooms:
                    # make sure the room management plugin is enabled
                    # in all rooms we are acting in
                    if self.is_plugin_enabled(room):
                        my_ops = self.get_op_status(my_nick, room)
                        if my_ops and my_ops in "~@&": # make sure I have ops
                            # Make sure nick is in room action is requested
                            if nick in self.get_room_nicks(room):
                                opped_rooms.append(room)
                if not opped_rooms:
                    self.notice(nick, 'I do not have ops in any enabled rooms you are in')
                    return
                return func(self, regex, opped_rooms, nick, **kwargs)
        
        return func_wrapper
    return decorator

# To test this plugin remember to type the following
# commands on IRC where the bot can see them:
# !activste plugin room_manage
# !enable plugin #myroom room_manage
# Change #myroom to be whatever room you are in.

class RoomManagementPlugin(Plugin):
    # Uncomment the line below to load this plugin.  Also if
    # you are using this as a starting point for your own plugin
    # remember to change 'sample' to be a unique identifier for your plugin,
    # and 'My Bot Plugin' to a short description for your plugin.
    plugin = ('rm', 'Room Management Plugin')
    
    def __init__(self, *args, **kwargs):
        # Change the line below to match the name of the class
        # you change this plugin to.
        super(RoomManagementPlugin, self).__init__(*args, **kwargs)
        self.repeater = LoopingCall(self.repeat)
        self.repeater.start(30, now = True)
        
        self.commands = ((r'nicks', self.nicks, 'show nicks in room'),
                         (r'kick (?P<room>#\S+) (?P<nick>\S+)', self.kick_nick, 'kick nick from room'),
                         (r'ban (?P<room>#\S+) (?P<nick>\S+)', self.ban_nick, 'ban (mute) nick in room'),
                         (r'remove penalty (?P<room>#\S+) (?P<nick>\S+)', self.remove_penalty, 'remove room penalty'),
                         # (r'down vote (?P<nick>\S+)', self.down_vote, 'Down vote a nick'),
                         # (r'dv (?P<nick>\S+)', self.down_vote, 'Down vote a nick'),
                         
                         (r'timer', self.timer, 'demonstrates the timer in a plugin'),
                         # (r'mute (?P<room>#\S+) (?P<nick>\S+)', self.mute, 'normal user despatch'),
                         # (r'mute (?P<nick>\S+)', self.mute, 'normal user despatch'),
                         (r'op me', self.op_me, 'gives ops'),
                         (r'deop me', self.deop_me, 'removes ops'),
                         (r'kick me', self.kick_me, 'kicks you off channel')
                         )
        self.antiflood = {}
    
    def get_hostmask(self, nick):
        hostmask = self.get_host(nick).split('@')[1]
        if hostmask:
            host_mask = '*!*@'+hostmask
        else:
            host_mask = nick+'!*@*'
        return host_mask
        
    def repeat(self):
        for penalty in Penalty.objects.filter(network = self.network):
            if timezone.now() > penalty.end_time:
                self.mode( penalty.room, False, "b", mask = penalty.nick_mask)
                penalty.delete()

    
    def privmsg(self, user, channel, message):
        # Anti-flood checks
        if self.is_plugin_enabled(channel) and message[0] != get_room_option(self.network, channel, 'activation'):

            my_nick = self.get_nickname()
            
            my_ops = self.get_op_status(my_nick, channel)
            if my_ops is not None and my_ops in "~@&%":
                nick, user_mask = user.split('!')
                nick = nick.lower()
                user_mask = '*!'+user_mask
                logger.debug("{} has user_mask {}".format(nick, user_mask))
                timestamp = time.time()<commit_msg>Fix bug in room manage plugin checking wrongly for ops in private window<commit_after>from __future__ import absolute_import
from bot.pluginDespatch import Plugin
import re
import logging
import pytz
import time
from twisted.internet.task import LoopingCall
from datetime import datetime, timedelta
from django.utils import timezone
from django.conf import settings
from .models import DownVotes, Penalty, Probation, NickHistory
from logos.roomlib import get_room_option

logger = logging.getLogger(__name__)
logging.config.dictConfig(settings.LOGGING)

DOWN_VOTE_MINUTES = 5
DOWN_VOTES_REQUIRED = 1
PENALTY_TIME = 120

MIN_FLOOD_INTERVAL = 5  # In Seconds
FLOOD_THRESHHOLD = 3
FLOOD_PENALTY_TIME = 60*5 # Flood penalty in seconds

from bot.logos_decorators import irc_room_permission_required, \
    irc_network_permission_required

# decorator to ensure logos trigger function
# has ops in room and nick is in room
def check_ops(check_nick_in_room=False, use_current_room=False, me=False):
    def decorator(func):
        def func_wrapper(self, regex, chan, nick, **kwargs):
            if use_current_room:
                if chan[0] == "#":
                    room = chan
                else: # If in a private message window
                    room = None
            else:
                try:
                    room = regex.group('room')   
                except IndexError:
                    room = None
                    
            if me:
                this_nick = nick
            else:
                this_nick = regex.group('nick').lower()
                
            my_nick = self.get_nickname()
            
            if room: # not private message or no current room
                if room.lower() not in self.get_rooms():
                    self.notice(nick, 'I am not in room {}'.format(room))
                    return
                    
                my_ops = self.get_op_status(my_nick, room)
                if my_ops is None or my_ops not in "~@&":
                    self.notice(nick, 'I do not have ops in room {}'.format(room))
                    return
                    
                if check_nick_in_room and this_nick not in self.get_room_nicks(room):
                    self.notice(nick, 'user {} is not in room'.format(this_nick))
                else:
                    return func(self, regex, [chan], nick, **kwargs)
            else: # is private message
                rooms = self.get_rooms_for_nick(nick)
                opped_rooms = []
                for room in rooms:
                    # make sure the room management plugin is enabled
                    # in all rooms we are acting in
                    if self.is_plugin_enabled(room):
                        my_ops = self.get_op_status(my_nick, room)
                        if my_ops and my_ops in "~@&": # make sure I have ops
                            # Make sure nick is in room action is requested
                            if nick in self.get_room_nicks(room):
                                opped_rooms.append(room)
                if not opped_rooms:
                    self.notice(nick, 'I do not have ops in any enabled rooms you are in')
                    return
                return func(self, regex, opped_rooms, nick, **kwargs)
        
        return func_wrapper
    return decorator

# To test this plugin remember to type the following
# commands on IRC where the bot can see them:
# !activste plugin room_manage
# !enable plugin #myroom room_manage
# Change #myroom to be whatever room you are in.

class RoomManagementPlugin(Plugin):
    # Uncomment the line below to load this plugin.  Also if
    # you are using this as a starting point for your own plugin
    # remember to change 'sample' to be a unique identifier for your plugin,
    # and 'My Bot Plugin' to a short description for your plugin.
    plugin = ('rm', 'Room Management Plugin')
    
    def __init__(self, *args, **kwargs):
        # Change the line below to match the name of the class
        # you change this plugin to.
        super(RoomManagementPlugin, self).__init__(*args, **kwargs)
        self.repeater = LoopingCall(self.repeat)
        self.repeater.start(30, now = True)
        
        self.commands = ((r'nicks', self.nicks, 'show nicks in room'),
                         (r'kick (?P<room>#\S+) (?P<nick>\S+)', self.kick_nick, 'kick nick from room'),
                         (r'ban (?P<room>#\S+) (?P<nick>\S+)', self.ban_nick, 'ban (mute) nick in room'),
                         (r'remove penalty (?P<room>#\S+) (?P<nick>\S+)', self.remove_penalty, 'remove room penalty'),
                         # (r'down vote (?P<nick>\S+)', self.down_vote, 'Down vote a nick'),
                         # (r'dv (?P<nick>\S+)', self.down_vote, 'Down vote a nick'),
                         
                         (r'timer', self.timer, 'demonstrates the timer in a plugin'),
                         # (r'mute (?P<room>#\S+) (?P<nick>\S+)', self.mute, 'normal user despatch'),
                         # (r'mute (?P<nick>\S+)', self.mute, 'normal user despatch'),
                         (r'op me', self.op_me, 'gives ops'),
                         (r'deop me', self.deop_me, 'removes ops'),
                         (r'kick me', self.kick_me, 'kicks you off channel')
                         )
        self.antiflood = {}
    
    def get_hostmask(self, nick):
        hostmask = self.get_host(nick).split('@')[1]
        if hostmask:
            host_mask = '*!*@'+hostmask
        else:
            host_mask = nick+'!*@*'
        return host_mask
        
    def repeat(self):
        for penalty in Penalty.objects.filter(network = self.network):
            if timezone.now() > penalty.end_time:
                self.mode( penalty.room, False, "b", mask = penalty.nick_mask)
                penalty.delete()

    
    def privmsg(self, user, channel, message):
        # Anti-flood checks
        if self.is_plugin_enabled(channel) and channel[0] == '#' and message[0] != get_room_option(self.network, channel, 'activation'):

            my_nick = self.get_nickname()
            
            my_ops = self.get_op_status(my_nick, channel)
            if my_ops is not None and my_ops in "~@&%":
                nick, user_mask = user.split('!')
                nick = nick.lower()
                user_mask = '*!'+user_mask
                logger.debug("{} has user_mask {}".format(nick, user_mask))
                timestamp = time.time()
{'fraction_non_alphanumeric': 0.098351775903428, 'fraction_numerical': 0.002398823802522634, 'mean_word_length': 1.7633098139833225, 'pattern_counts': {'":': 8, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4593899', 'n_tokens_mistral': 3746, 'n_tokens_neox': 3536, 'n_words': 1105}