예제 #1
0
def cmd_url_ban(bot, user, text, command, parameter):
    global log

    if bot.is_admin(user):
        if parameter:
            bot.mumble.users[text.actor].send_text_message(
                util.url_ban(util.get_url_from_input(parameter)))

            id = item_id_generators['url'](url=parameter)
            var.cache.free_and_delete(id)
            var.playlist.remove_by_id(id)
        else:
            if var.playlist.current_item() and var.playlist.current_item(
            ).type == 'url':
                item = var.playlist.current_item().item()
                bot.mumble.users[text.actor].send_text_message(
                    util.url_ban(util.get_url_from_input(item.url)))
                var.cache.free_and_delete(item.id)
                var.playlist.remove_by_id(item.id)
            else:
                bot.send_msg(
                    constants.strings('bad_parameter', command=command))
    else:
        bot.mumble.users[text.actor].send_text_message(
            constants.strings('not_admin'))
    return
예제 #2
0
def cmd_play_radio(bot, user, text, command, parameter):
    global log

    if not parameter:
        all_radio = var.config.items('radio')
        msg = tr('preconfigurated_radio')
        for i in all_radio:
            comment = ""
            if len(i[1].split(maxsplit=1)) == 2:
                comment = " - " + i[1].split(maxsplit=1)[1]
            msg += "<br />" + i[0] + comment
        bot.send_msg(msg, text)
    else:
        if var.config.has_option('radio', parameter):
            parameter = var.config.get('radio', parameter)
            parameter = parameter.split()[0]
        url = util.get_url_from_input(parameter)
        if url:
            music_wrapper = get_cached_wrapper_from_scrap(type='radio',
                                                          url=url,
                                                          user=user)

            var.playlist.append(music_wrapper)
            log.info("cmd: add to playlist: " +
                     music_wrapper.format_debug_string())
            send_item_added_message(bot, music_wrapper,
                                    len(var.playlist) - 1, text)
        else:
            bot.send_msg(tr('bad_url'), text)
예제 #3
0
def cmd_url_unban(bot, user, text, command, parameter):
    global log

    if bot.is_admin(user):
        if parameter:
            bot.mumble.users[text.actor].send_text_message(util.url_unban(util.get_url_from_input(parameter)))
    else:
        bot.mumble.users[text.actor].send_text_message(constants.strings('not_admin'))
    return
예제 #4
0
def cmd_play_url(bot, user, text, command, parameter):
    global log

    url = util.get_url_from_input(parameter)
    if url:
        music_wrapper = get_cached_wrapper_from_scrap(type='url', url=url, user=user)
        var.playlist.append(music_wrapper)

        log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
        send_item_added_message(bot, music_wrapper, len(var.playlist) - 1, text)
        if len(var.playlist) == 2:
            # If I am the second item on the playlist. (I am the next one!)
            bot.async_download_next()
    else:
        bot.send_msg(tr('bad_parameter', command=command), text)
예제 #5
0
def cmd_play_playlist(bot, user, text, command, parameter):
    global log

    offset = 0  # if you want to start the playlist at a specific index
    try:
        offset = int(parameter.split(" ")[-1])
    except ValueError:
        pass

    url = util.get_url_from_input(parameter)
    log.debug(f"cmd: fetching media info from playlist url {url}")
    items = get_playlist_info(url=url, start_index=offset, user=user)
    if len(items) > 0:
        items = var.playlist.extend(list(map(lambda item: get_cached_wrapper_from_scrap(**item), items)))
        for music in items:
            log.info("cmd: add to playlist: " + music.format_debug_string())
    else:
        bot.send_msg(tr("playlist_fetching_failed"), text)
예제 #6
0
    def message_received(self, text):
        raw_message = text.message.strip()
        message = re.sub(r'<.*?>', '', raw_message)
        user = self.mumble.users[text.actor]['name']

        if var.config.getboolean('commands', 'split_username_at_space'):
            # in can you use https://github.com/Natenom/mumblemoderator-module-collection/tree/master/os-suffixes ,
            # you want to split the username
            user = user.split()[0]

        command_symbols = var.config.get('commands', 'command_symbol')
        match = re.match(fr'^[{re.escape(command_symbols)}](?P<command>\S+)(?:\s(?P<argument>.*))?', message)
        if match:
            command = match.group("command").lower()
            argument = match.group("argument") or ""

            if not command:
                return

            self.log.info(f'bot: received command "{command}" with arguments "{argument}" from {user}')

            # Anti stupid guy function
            if not self.is_admin(user) and not var.config.getboolean('bot', 'allow_private_message') and text.session:
                self.mumble.users[text.actor].send_text_message(
                    tr('pm_not_allowed'))
                return

            for i in var.db.items("user_ban"):
                if user.lower() == i[0]:
                    self.mumble.users[text.actor].send_text_message(
                        tr('user_ban'))
                    return

            if not self.is_admin(user) and argument:
                input_url = util.get_url_from_input(argument)
                if input_url and var.db.has_option('url_ban', input_url):
                    self.mumble.users[text.actor].send_text_message(
                        tr('url_ban'))
                    return

            command_exc = ""
            try:
                if command in self.cmd_handle:
                    command_exc = command
                else:
                    # try partial match
                    cmds = self.cmd_handle.keys()
                    matches = []
                    for cmd in cmds:
                        if cmd.startswith(command) and self.cmd_handle[cmd]['partial_match']:
                            matches.append(cmd)

                    if len(matches) == 1:
                        self.log.info("bot: {:s} matches {:s}".format(command, matches[0]))
                        command_exc = matches[0]

                    elif len(matches) > 1:
                        self.mumble.users[text.actor].send_text_message(
                            tr('which_command', commands="<br>".join(matches)))
                        return
                    else:
                        self.mumble.users[text.actor].send_text_message(
                            tr('bad_command', command=command))
                        return

                if self.cmd_handle[command_exc]['admin'] and not self.is_admin(user):
                    self.mumble.users[text.actor].send_text_message(tr('not_admin'))
                    return

                if not self.cmd_handle[command_exc]['access_outside_channel'] \
                        and not self.is_admin(user) \
                        and not var.config.getboolean('bot', 'allow_other_channel_message') \
                        and self.mumble.users[text.actor]['channel_id'] != self.mumble.users.myself['channel_id']:
                    self.mumble.users[text.actor].send_text_message(
                        tr('not_in_my_channel'))
                    return

                self.cmd_handle[command_exc]['handle'](self, user, text, command_exc, argument)
            except:
                error_traceback = traceback.format_exc()
                error = error_traceback.rstrip().split("\n")[-1]
                self.log.error(f"bot: command {command_exc} failed with error: {error_traceback}\n")
                self.send_msg(tr('error_executing_command', command=command_exc, error=error), text)
예제 #7
0
    def message_received(self, text):
        message = text.message.strip()
        user = self.mumble.users[text.actor]['name']

        if var.config.getboolean('commands', 'split_username_at_space'):
            # in can you use https://github.com/Natenom/mumblemoderator-module-collection/tree/master/os-suffixes ,
            # you want to split the username
            user = user.split()[0]

        if message[0] in var.config.get('commands', 'command_symbol'):
            # remove the symbol from the message
            message = message[1:].split(' ', 1)

            # use the first word as a command, the others one as  parameters
            if len(message) > 0:
                command = message[0].lower()
                parameter = ''
                if len(message) > 1:
                    parameter = message[1].rstrip()
            else:
                return

            self.log.info('bot: received command ' + command + ' - ' +
                          parameter + ' by ' + user)

            # Anti stupid guy function
            if not self.is_admin(user) and not var.config.getboolean(
                    'bot', 'allow_private_message') and text.session:
                self.mumble.users[text.actor].send_text_message(
                    constants.strings('pm_not_allowed'))
                return

            for i in var.db.items("user_ban"):
                if user.lower() == i[0]:
                    self.mumble.users[text.actor].send_text_message(
                        constants.strings('user_ban'))
                    return

            if not self.is_admin(user) and parameter:
                input_url = util.get_url_from_input(parameter)
                if input_url:
                    for i in var.db.items("url_ban"):
                        if input_url == i[0]:
                            self.mumble.users[text.actor].send_text_message(
                                constants.strings('url_ban'))
                            return

            command_exc = ""
            try:
                if command in self.cmd_handle:
                    command_exc = command
                else:
                    # try partial match
                    cmds = self.cmd_handle.keys()
                    matches = []
                    for cmd in cmds:
                        if cmd.startswith(command) and self.cmd_handle[cmd][
                                'partial_match']:
                            matches.append(cmd)

                    if len(matches) == 1:
                        self.log.info("bot: {:s} matches {:s}".format(
                            command, matches[0]))
                        command_exc = matches[0]

                    elif len(matches) > 1:
                        self.mumble.users[text.actor].send_text_message(
                            constants.strings('which_command',
                                              commands="<br>".join(matches)))
                        return
                    else:
                        self.mumble.users[text.actor].send_text_message(
                            constants.strings('bad_command', command=command))
                        return

                if self.cmd_handle[command_exc]['admin'] and not self.is_admin(
                        user):
                    self.mumble.users[text.actor].send_text_message(
                        constants.strings('not_admin'))
                    return

                if not self.cmd_handle[command_exc]['access_outside_channel'] \
                        and not self.is_admin(user) \
                        and not var.config.getboolean('bot', 'allow_other_channel_message') \
                        and self.mumble.users[text.actor]['channel_id'] != self.mumble.users.myself['channel_id']:
                    self.mumble.users[text.actor].send_text_message(
                        constants.strings('not_in_my_channel'))
                    return

                self.cmd_handle[command_exc]['handle'](self, user, text,
                                                       command_exc, parameter)
            except:
                error_traceback = traceback.format_exc()
                error = error_traceback.rstrip().split("\n")[-1]
                self.log.error(
                    f"bot: command {command_exc} failed with error: {error_traceback}\n"
                )
                self.send_msg(
                    constants.strings('error_executing_command',
                                      command=command_exc,
                                      error=error), text)
예제 #8
0
def cmd_url_unban(bot, user, text, command, parameter):
    global log

    if parameter:
        bot.mumble.users[text.actor].send_text_message(
            util.url_unban(util.get_url_from_input(parameter)))