Esempio n. 1
0
File: bot.py Progetto: Felfa/ifarres
    def on_message_receive(self, msg):
        try:
            triggered = False

            if msg.content == None:
                return

            step = get_step(self, msg.conversation.id)

            if step:
                for plugin in self.plugins:
                    if get_plugin_name(plugin) == step['plugin'] and hasattr(
                            plugin, 'steps'):
                        if msg.content.startswith('/next'):
                            plugin.steps(msg, -1)
                            cancel_steps(self, msg.conversation.id)

                        if msg.content.startswith('/done'):
                            plugin.steps(msg, 0)
                            cancel_steps(self, msg.conversation.id)

                        else:
                            plugin.steps(msg, step['step'])

            else:
                for plugin in self.plugins:
                    # Always do this action for every message. #
                    if hasattr(plugin, 'always'):
                        plugin.always(msg)

                    # If no query show help #
                    if msg.type == 'inline_query':
                        if msg.content == '':
                            msg.content = '/help'

                    # Check if any command of a plugin matches. #
                    for command in plugin.commands:
                        if 'command' in command:
                            if self.check_trigger(command['command'], msg,
                                                  plugin):
                                break

                        if 'friendly' in command:
                            if self.check_trigger(command['friendly'], msg,
                                                  plugin):
                                break

                        if 'shortcut' in command:
                            if len(command['shortcut']) < 3:
                                shortcut = command['shortcut'] + ' '
                            else:
                                shortcut = command['shortcut']

                            if self.check_trigger(shortcut, msg, plugin):
                                break

        except Exception as e:
            logging.exception(traceback.format_exc())
            self.send_alert(traceback.format_exc())
Esempio n. 2
0
    def steps(self, m, step):
        if step == -1:
            self.bot.send_message(m, 'ok... :c', extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)
        if step == 0:
            self.bot.send_message(m, 'fine!', extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)

        elif step == 1:
            self.bot.send_message(m,
                                  'ok, now send me your nudes or /cancel',
                                  extra={'format': 'HTML'})
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 2)

        elif step == 2:
            self.bot.send_message(m, '( ͡° ͜ʖ ͡°)', extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)
Esempio n. 3
0
    def on_message_receive(self, msg):
        try:
            ignore_message = False
            if msg.content == None or (msg.type != 'inline_query'
                                       and msg.date < time() - 60 * 5):
                return

            # if msg.sender.id != self.config['owner'] and not is_trusted(self, msg.sender.id, msg) and (has_tag(self, msg.conversation.id, 'spam') or has_tag(self, msg.sender.id, 'spam')):
            #     ignore_message = True
            #     self.send_message(msg, self.trans.errors.spammer_detected, extra={'format': 'HTML'})

            if msg.sender.id != self.config['owner'] and not is_trusted(
                    self, msg.sender.id,
                    msg) and (has_tag(self, msg.conversation.id, 'muted')
                              or has_tag(self, msg.sender.id, 'muted')):
                ignore_message = True

            step = get_step(self, msg.conversation.id)

            if step:
                if not ignore_message:
                    for plugin in self.plugins:
                        if get_plugin_name(plugin) == step.plugin and hasattr(
                                plugin, 'steps'):
                            if msg.content.startswith('/cancel'):
                                plugin.steps(msg, -1)
                                cancel_steps(self, msg.conversation.id)

                            if msg.content.startswith('/done'):
                                plugin.steps(msg, 0)
                                cancel_steps(self, msg.conversation.id)

                            else:
                                plugin.steps(msg, step['step'])

            else:
                for plugin in self.plugins:
                    # Always do this action for every message. #
                    if hasattr(plugin, 'always'):
                        plugin.always(msg)

                    # If no query show help #
                    if msg.type == 'inline_query' and not ignore_message:
                        if msg.content == '':
                            msg.content = 'help'

                    if hasattr(plugin, 'commands') and not ignore_message:
                        # Check if any command of a plugin matches. #
                        for command in plugin.commands:
                            if 'parameters' not in command:
                                command['parameters'] = None

                            if 'command' in command:
                                if self.check_trigger(command['command'],
                                                      command['parameters'],
                                                      msg, plugin):
                                    break
                                if 'keep_default' in command and command[
                                        'keep_default']:
                                    if self.check_trigger(
                                            command['command'],
                                            command['parameters'], msg, plugin,
                                            False, True):
                                        break

                            if 'friendly' in command and not has_tag(
                                    self, msg.sender.id, 'noreplies'
                            ) and not has_tag(
                                    self, msg.conversation.id, 'noreplies'
                            ) and msg.conversation.id != self.config.alerts_conversation_id and msg.conversation.id != self.config.admin_conversation_id:
                                if self.check_trigger(command['friendly'],
                                                      command['parameters'],
                                                      msg, plugin, True):
                                    break

                            if 'shortcut' in command:
                                if self.check_trigger(command['shortcut'],
                                                      command['parameters'],
                                                      msg, plugin):
                                    break
                                if 'keep_default' in command and command[
                                        'keep_default']:
                                    if self.check_trigger(
                                            command['shortcut'],
                                            command['parameters'], msg, plugin,
                                            False, True):
                                        break

        except KeyboardInterrupt:
            pass

        except Exception as e:
            catch_exception(e, self)
Esempio n. 4
0
 def run(self, m):
     self.bot.send_message(m, 'send me your name', extra={'format': 'HTML'})
     set_step(self.bot, m.conversation.id, get_plugin_name(self), 1)
Esempio n. 5
0
    def steps(self, m, step):
        gid = str(m.conversation.id)

        if step == -1:
            self.bot.send_message(
                m,
                self.bot.trans.plugins.administration.strings.cancel %
                m.conversation.title,
                extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)

        if step == 0:
            self.bot.send_message(
                m,
                self.bot.trans.plugins.administration.strings.done %
                m.conversation.title,
                extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)

        elif step == 1:
            if self.bot.trans.plugins.administration.strings.yes.lower(
            ) in m.content.lower():
                set_step(self.bot, m.conversation.id, get_plugin_name(self), 2)
                if not m.content.startswith(
                        '/cancel') and not m.content.startswith('/done'):
                    self.bot.send_message(
                        m,
                        self.bot.trans.plugins.administration.strings.ask_link
                        % m.conversation.title,
                        extra={
                            'format': 'HTML',
                            'force_reply': True
                        })

            else:
                cancel_steps(self.bot, m.conversation.id)
                self.bot.send_message(m,
                                      self.bot.trans.errors.unknown,
                                      extra={'format': 'HTML'})

        elif step == 2:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 3)
            if not m.content.startswith(
                    '/cancel') and not m.content.startswith('/done'):
                self.bot.administration[gid].link = m.content
                set_data('administration/%s/%s/link' % (self.bot.name, gid),
                         self.bot.administration[gid].link)
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_alias %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 3:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 4)
            if not m.content.startswith(
                    '/cancel') and not m.content.startswith('/done'):
                self.bot.administration[gid].alias = m.content.lower()
                set_data('administration/%s/%s/alias' % (self.bot.name, gid),
                         self.bot.administration[gid].alias)
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_rules %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 4:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 5)
            if not m.content.startswith(
                    '/cancel') and not m.content.startswith('/done'):
                self.bot.administration[gid].rules = m.content.split('\n')
                set_data('administration/%s/%s/rules' % (self.bot.name, gid),
                         self.bot.administration[gid].rules)
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_motd %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 5:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 6)
            if not m.content.startswith(
                    '/cancel') and not m.content.startswith('/done'):
                self.bot.administration[gid].motd = m.content
                set_data('administration/%s/%s/motd' % (self.bot.name, gid),
                         self.bot.administration[gid].motd)
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_public %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 6:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), -1)
            if not m.content.startswith(
                    '/cancel') and not m.content.startswith('/done'):
                if self.bot.trans.plugins.administration.strings.yes.lower(
                ) in m.content.lower():
                    self.bot.administration[gid].public = True
                else:
                    self.bot.administration[gid].public = False
                set_data('administration/%s/%s/public' % (self.bot.name, gid),
                         self.bot.administration[gid].public)
Esempio n. 6
0
    def run(self, m):
        input = get_input(m)
        uid = str(m.sender.id)
        gid = str(m.conversation.id)

        # List all administration commands. #
        if is_command(self, 1, m.content):
            text = self.bot.trans.plugins.administration.strings.commands
            for command in self.commands:
                # Adds the command and parameters#
                if command.command == '^new_chat_member$':
                    text += '\n • ' + \
                        self.bot.trans.plugins.administration.strings.new_chat_member
                else:
                    text += '\n • ' + \
                        command.command.replace('/', self.bot.config.prefix)

                if 'parameters' in command and command.parameters:
                    for parameter in command.parameters:
                        name, required = list(parameter.items())[0]
                        # Bold for required parameters, and italic for optional #
                        if required:
                            text += ' <b>&lt;%s&gt;</b>' % name
                        else:
                            text += ' [%s]' % name

                if 'description' in command:
                    text += '\n   <i>%s</i>' % command.description
                else:
                    text += '\n   <i>?¿</i>'

            return self.bot.send_message(m, text, extra={'format': 'HTML'})

        # List all groups. #
        if is_command(self, 2, m.content):
            text = self.bot.trans.plugins.administration.strings.groups
            if len(self.bot.administration) > 0:
                for gid, attr in self.bot.administration.items():
                    if 'public' in self.bot.administration[
                            gid] and self.bot.administration[gid].public:
                        if 'link' in self.bot.administration[
                                gid] and self.bot.config.bindings != 'telegram-cli':
                            text += '\n • <a href="%s">%s</a>' % (
                                self.bot.administration[gid].link,
                                self.bot.groups[gid].title)
                        else:
                            text += '\n • %s' % self.bot.groups[gid].title

                        if self.bot.administration[gid].alias:
                            text += ' /<b>%s</b>/' % attr.alias

            else:
                text = self.bot.trans.plugins.administration.strings.no_groups

            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Join a group. #
        elif is_command(self, 3, m.content):
            if not input:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            for id in self.bot.administration:
                if (input in self.bot.administration
                        or compile('^' + input.lower() + '$').search(
                            self.bot.administration[id].alias.lower())
                        or compile('^' + input.lower() + '$').search(
                            self.bot.groups[id].title.lower())):
                    gid_to_join = id
                    break

            if not gid_to_join in self.bot.administration:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.not_added %
                    m.conversation.title,
                    extra={'format': 'HTML'})

            text = '<b>%s</b>' % self.bot.groups[gid_to_join].title
            if self.bot.administration[gid_to_join].motd:
                text += '\n<i>%s</i>' % self.bot.administration[
                    gid_to_join].motd

            text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules
            i = 1
            if 'rules' in self.bot.administration[gid_to_join]:
                for rule in self.bot.administration[gid_to_join].rules:
                    text += '\n %s. <i>%s</i>' % (i, rule)
                    i += 1
            else:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules

            # if self.bot.administration[gid_to_join].public:
            #     text += '\n\n%s' % self.bot.trans.plugins.administration.strings.public_group
            # else:
            #     text += '\n\n%s' % self.bot.trans.plugins.administration.strings.private_group

            if not self.bot.administration[gid_to_join].link:
                text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink
            else:
                text += '\n\n<a href="%s">%s</a>' % (
                    self.bot.administration[gid_to_join].link,
                    self.bot.trans.plugins.administration.strings.join)
            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Information about a group. #
        elif is_command(self, 4, m.content) or is_command(self, 9, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})
            if not gid in self.bot.administration:
                if is_command(self, 4, m.content):
                    return self.bot.send_message(
                        m,
                        self.bot.trans.plugins.administration.strings.not_added
                        % m.conversation.title,
                        extra={'format': 'HTML'})
                elif is_command(self, 9, m.content):
                    return

            text = '<b>%s</b>' % self.bot.groups[gid].title
            if self.bot.administration[gid].motd:
                text += '\n<i>%s</i>' % self.bot.administration[gid].motd

            text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules
            i = 1
            for rule in self.bot.administration[gid].rules:
                text += '\n %s. <i>%s</i>' % (i, rule)
                i += 1

            if not self.bot.administration[gid].rules:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules

            # if self.bot.administration[gid].public:
            #     text += '\n\n%s' % self.bot.trans.plugins.administration.strings.public_group
            # else:
            #     text += '\n\n%s' % self.bot.trans.plugins.administration.strings.private_group

            if is_command(self, 4, m.content):
                if not self.bot.administration[gid].link:
                    text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink
                else:
                    text += '\n\n<a href="%s">%s</a>' % (
                        self.bot.trans.plugins.administration.strings.join,
                        self.bot.administration[gid].link)
            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Rules of a group. #
        elif is_command(self, 5, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})
            if not gid in self.bot.administration:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.not_added %
                    m.conversation.title,
                    extra={'format': 'HTML'})

            if input and is_int(input):
                try:
                    i = int(input)
                    text = '%s. <i>%s</i>' % (
                        i, self.bot.administration[gid].rules[i - 1])
                except:
                    text = self.bot.trans.plugins.administration.strings.notfound

            else:
                text = self.bot.trans.plugins.administration.strings.rules
                i = 1
                for rule in self.bot.administration[gid].rules:
                    text += '\n %s. <i>%s</i>' % (i, rule)
                    i += 1

            if not self.bot.administration[gid].rules:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Kicks a user. #
        elif is_command(self, 6, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.permission_required,
                    extra={'format': 'HTML'})

            if not input and not m.reply:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            if m.reply:
                target = m.reply.sender.id
            elif is_int(input):
                target = input
            else:
                target = m.sender.id

            res = self.bot.kick_user(m, target)
            self.bot.unban_user(m, target)
            if res is None:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.admin_required,
                    extra={'format': 'HTML'})
            elif not res:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.failed,
                                             extra={'format': 'HTML'})
            else:
                return self.bot.send_message(
                    m,
                    '<pre>An enemy has been slain.</pre>',
                    extra={'format': 'HTML'})

            return self.bot.send_message(m,
                                         self.bot.trans.errors.unknown,
                                         extra={'format': 'HTML'})

        # Bans a user. #
        elif is_command(self, 7, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.permission_required,
                    extra={'format': 'HTML'})

            if not input:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            return self.bot.send_message(m,
                                         self.bot.trans.errors.unknown,
                                         extra={'format': 'HTML'})

        # Configures a group. #
        elif is_command(self, 8, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.permission_required,
                    extra={'format': 'HTML'})

            if not input:
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_to_add %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })
                if not gid in self.bot.administration:
                    self.bot.administration[gid] = {
                        'alias': None,
                        'description': None,
                        'link': None,
                        'rules': [],
                        'public': False
                    }
                    set_data('administration/%s/%s' % (self.bot.name, gid),
                             self.bot.administration[gid])
                set_step(self.bot, m.conversation.id, get_plugin_name(self), 1)
            else:
                if first_word(input) == 'add':
                    if not gid in self.bot.administration:
                        self.bot.administration[gid] = {
                            'alias': None,
                            'description': None,
                            'link': None,
                            'rules': [],
                            'public': False
                        }
                        set_data('administration/%s/%s' % (self.bot.name, gid),
                                 self.bot.administration[gid])
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.added
                            % m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            already_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'remove':
                    if gid in self.bot.administration:
                        del self.bot.administration[gid]
                        delete_data('administration/%s/%s' %
                                    (self.bot.name, gid))
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            removed % m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'alias':
                    if gid in self.bot.administration:
                        self.bot.administration[
                            gid].alias = all_but_first_word(input).lower()
                        set_data(
                            'administration/%s/%s/alias' %
                            (self.bot.name, gid),
                            self.bot.administration[gid].alias)
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'motd':
                    if gid in self.bot.administration:
                        self.bot.administration[gid].motd = all_but_first_word(
                            input)
                        set_data(
                            'administration/%s/%s/motd' % (self.bot.name, gid),
                            self.bot.administration[gid].motd)
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'link':
                    if gid in self.bot.administration:
                        self.bot.administration[gid].link = all_but_first_word(
                            input)
                        set_data(
                            'administration/%s/%s/link' % (self.bot.name, gid),
                            self.bot.administration[gid].link)
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'rules':
                    if gid in self.bot.administration:
                        self.bot.administration[
                            gid].rules = all_but_first_word(
                                all_but_first_word(input).split('\n')[0:])
                        set_data(
                            'administration/%s/%s/rules' %
                            (self.bot.name, gid),
                            self.bot.administration[gid].rules)
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'rule':
                    if gid in self.bot.administration:
                        try:
                            i = int(first_word(all_but_first_word(input))) - 1
                            if i > len(self.bot.administration[gid].rules):
                                i = len(self.bot.administration[gid].rules)
                            elif i < 1:
                                i = 0
                        except:
                            return self.bot.send_message(
                                m,
                                self.bot.trans.errors.unknown,
                                extra={'format': 'HTML'})
                        self.bot.administration[gid].rules.insert(
                            i, all_but_first_word(all_but_first_word(input)))
                        set_data(
                            'administration/%s/%s/rules' %
                            (self.bot.name, gid),
                            self.bot.administration[gid].rules)
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'public':
                    if gid in self.bot.administration:
                        if self.bot.trans.plugins.administration.strings.yes.lower(
                        ) in all_but_first_word(input).lower():
                            self.bot.administration[gid].public = True
                        else:
                            self.bot.administration[gid].public = False
                        set_data(
                            'administration/%s/%s/public' %
                            (self.bot.name, gid),
                            self.bot.administration[gid].public)
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                else:
                    return self.bot.send_message(
                        m,
                        self.bot.trans.errors.no_results,
                        extra={'format': 'HTML'})
Esempio n. 7
0
    def steps(self, m, step):
        gid = str(m.conversation.id)

        if step == -1:
            self.bot.send_message(
                m,
                self.bot.trans.plugins.administration.strings.cancel %
                m.conversation.title,
                extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)

        if step == 0:
            self.bot.send_message(
                m,
                self.bot.trans.plugins.administration.strings.done %
                m.conversation.title,
                extra={'format': 'HTML'})
            cancel_steps(self.bot, m.conversation.id)

        elif step == 1:
            if self.bot.trans.plugins.administration.strings.yes.lower(
            ) in m.content.lower():
                set_step(self.bot, m.conversation.id, get_plugin_name(self), 2)
                if not m.content.startswith(
                        '/next') and not m.content.startswith('/done'):
                    self.bot.send_message(
                        m,
                        self.bot.trans.plugins.administration.strings.ask_link
                        % m.conversation.title,
                        extra={
                            'format': 'HTML',
                            'force_reply': True
                        })

            else:
                cancel_steps(self.bot, m.conversation.id)
                self.bot.send_message(m,
                                      self.bot.trans.errors.unknown,
                                      extra={'format': 'HTML'})

        elif step == 2:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 3)
            if not m.content.startswith('/next') and not m.content.startswith(
                    '/done'):
                self.administration[gid]['link'] = m.content
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_alias %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 3:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 4)
            if not m.content.startswith('/next') and not m.content.startswith(
                    '/done'):
                self.administration[gid]['alias'] = m.content.lower()
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_rules %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 4:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), 5)
            if not m.content.startswith('/next') and not m.content.startswith(
                    '/done'):
                self.administration[gid]['rules'] = m.content.split('\n')
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_motd %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })

        elif step == 5:
            set_step(self.bot, m.conversation.id, get_plugin_name(self), -1)
            if not m.content.startswith('/next') and not m.content.startswith(
                    '/done'):
                self.administration[gid]['motd'] = m.content
Esempio n. 8
0
    def run(self, m):
        input = get_input(m)
        uid = str(m.sender.id)
        gid = str(m.conversation.id)

        # List all administration commands. #
        if is_command(self, 1, m.content):
            text = self.bot.trans.plugins.administration.strings.commands
            for command in self.commands:
                # Adds the command and parameters#
                if command['command'] == '^new_chat_member$':
                    text += '\n • ' + self.bot.trans.plugins.administration.strings.new_chat_member
                else:
                    text += '\n • ' + command['command'].replace(
                        '/', self.bot.config.prefix)

                if 'parameters' in command:
                    for parameter in command['parameters']:
                        name, required = list(parameter.items())[0]
                        # Bold for required parameters, and italic for optional #
                        if required:
                            text += ' <b>&lt;%s&gt;</b>' % name
                        else:
                            text += ' [%s]' % name

                if 'description' in command:
                    text += '\n   <i>%s</i>' % command['description']
                else:
                    text += '\n   <i>?¿</i>'

            return self.bot.send_message(m, text, extra={'format': 'HTML'})

        # List all groups. #
        if is_command(self, 2, m.content):
            text = self.bot.trans.plugins.administration.strings.groups
            i = 1
            for gid, attr in self.administration.items():
                text += '\n %s. %s (%s)' % (i, self.groups[gid]['title'],
                                            attr['alias'])
                i += 1
            return self.bot.send_message(m, text, extra={'format': 'HTML'})

        # Send message to another group. #
        if is_command(self, 10, m.content):
            input = get_input(m, ignore_reply=False)
            if not input:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            id_backup = m.conversation.id
            group_number = int(findall(r"(\w+)", m.content)[1])
            msg_offset = len(str(group_number)) + 1
            text = input[msg_offset:]

            if text and is_int(group_number):
                m.conversation.id = 0

                for gid in self.administration:
                    if group_number == 1:
                        m.conversation.id = int(gid)
                    group_number -= 1

                if m.conversation.id != id_backup and m.conversation.id != 0:
                    text = self.bot.trans.plugins.administration.strings.message_received % m.conversation.title + text
                    return self.bot.send_message(m,
                                                 text,
                                                 extra={'format': 'HTML'})
                    #return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.sent, extra={'format': 'HTML'})
                elif m.conversation.id == id_backup:
                    return self.bot.send_message(
                        m,
                        self.bot.trans.plugins.administration.strings.no_echo,
                        extra={'format': 'HTML'})
                else:
                    m.conversation.id = id_backup
                    return self.bot.send_message(
                        m,
                        self.bot.trans.errors.send_error,
                        extra={'format': 'HTML'})
            else:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.
                    invalid_syntax,
                    extra={'format': 'HTML'})

        # Join a group. #
        elif is_command(self, 3, m.content):
            if not input:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            for id in self.administration:
                if (input in self.administration
                        or input in self.administration[id]['alias']
                        or input in self.groups[id]['title']):
                    gid_to_join = id
                    break

            if not gid_to_join in self.administration:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.not_added %
                    m.conversation.title,
                    extra={'format': 'HTML'})

            text = '<b>%s</b>\n<i>%s</i>\n\n%s' % (
                self.groups[gid_to_join]['title'],
                self.administration[gid_to_join]['description'],
                self.bot.trans.plugins.administration.strings.rules)
            i = 1
            for rule in self.administration[gid_to_join]['rules']:
                text += '\n %s. <i>%s</i>' % (i, rule)
                i += 1
            if not self.administration[gid_to_join]['rules']:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            if not self.administration[gid_to_join]['link']:
                text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink
            else:
                text += '\n\n<a href="%s">%s</a>' % (
                    self.administration[gid_to_join]['link'],
                    self.bot.trans.plugins.administration.strings.join)
            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Information about a group. #
        elif is_command(self, 4, m.content) or is_command(self, 9, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})
            if not gid in self.administration:
                if is_command(self, 4, m.content):
                    return self.bot.send_message(
                        m,
                        self.bot.trans.plugins.administration.strings.not_added
                        % m.conversation.title,
                        extra={'format': 'HTML'})
                elif is_command(self, 9, m.content):
                    return

            text = '<b>%s</b>\n<i>%s</i>\n\n%s' % (
                self.groups[gid]['title'],
                self.administration[gid]['description'],
                self.bot.trans.plugins.administration.strings.rules)
            i = 1
            for rule in self.administration[gid]['rules']:
                text += '\n %s. <i>%s</i>' % (i, rule)
                i += 1

            if not self.administration[gid]['rules']:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            if is_command(self, 4, m.content):
                if not self.administration[gid]['link']:
                    text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink
                else:
                    text += '\n\n<a href="%s">%s</a>' % (
                        self.bot.trans.plugins.administration.strings.join.
                        self.administration[gid]['link'])
            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Rules of a group. #
        elif is_command(self, 5, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})
            if not gid in self.administration:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.not_added %
                    m.conversation.title,
                    extra={'format': 'HTML'})

            if input and is_int(input):
                try:
                    i = int(input)
                    text = '%s. <i>%s</i>' % (
                        i, self.administration[gid]['rules'][i - 1])
                except:
                    text = self.bot.trans.plugins.administration.strings.notfound

            else:
                text = self.bot.trans.plugins.administration.strings.rules
                i = 1
                for rule in self.administration[gid]['rules']:
                    text += '\n %s. <i>%s</i>' % (i, rule)
                    i += 1

            if not self.administration[gid]['rules']:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            return self.bot.send_message(m,
                                         text,
                                         extra={
                                             'format': 'HTML',
                                             'preview': False
                                         })

        # Kicks a user. #
        elif is_command(self, 6, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.permission_required,
                    extra={'format': 'HTML'})

            if not input and not m.reply:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            if m.reply:
                target = m.reply.sender.id
            elif input:
                target = input
            else:
                target = m.sender.id

            res = self.bot.kick_user(m, target)
            self.bot.unban_user(m, target)
            if res is None:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.admin_required,
                    extra={'format': 'HTML'})
            elif not res:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.failed,
                                             extra={'format': 'HTML'})
            else:
                return self.bot.send_message(
                    m,
                    '<pre>An enemy has been slain.</pre>',
                    extra={'format': 'HTML'})

            return self.bot.send_message(m,
                                         self.bot.trans.errors.unknown,
                                         extra={'format': 'HTML'})

        # Bans a user. #
        elif is_command(self, 7, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.permission_required,
                    extra={'format': 'HTML'})

            if not input:
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.missing_parameter,
                    extra={'format': 'HTML'})

            return self.bot.send_message(m,
                                         self.bot.trans.errors.unknown,
                                         extra={'format': 'HTML'})

        # Configures a group. #
        elif is_command(self, 8, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.group_only,
                                             extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(
                    m,
                    self.bot.trans.errors.permission_required,
                    extra={'format': 'HTML'})

            if not input:
                self.bot.send_message(
                    m,
                    self.bot.trans.plugins.administration.strings.ask_to_add %
                    m.conversation.title,
                    extra={
                        'format': 'HTML',
                        'force_reply': True
                    })
                if not gid in self.administration:
                    self.administration[gid] = {
                        'alias': None,
                        'description': None,
                        'link': None,
                        'rules': []
                    }
                    self.administration.store_database()
                set_step(self.bot, m.conversation.id, get_plugin_name(self), 1)
            else:
                if first_word(input) == 'add':
                    if not gid in self.administration:
                        self.administration[gid] = {
                            'alias': None,
                            'description': None,
                            'link': None,
                            'rules': []
                        }
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.added
                            % m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            already_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'remove':
                    if gid in self.administration:
                        del (self.administration[gid])
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            removed % m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'alias':
                    if gid in self.administration:
                        self.administration[gid]['alias'] = all_but_first_word(
                            input)
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'motd':
                    if gid in self.administration:
                        self.administration[gid][
                            'description'] = all_but_first_word(input)
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'link':
                    if gid in self.administration:
                        self.administration[gid]['link'] = all_but_first_word(
                            input)
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'rules':
                    if gid in self.administration:
                        self.administration[gid]['rules'] = all_but_first_word(
                            input).split('\n')[0:]
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                elif first_word(input) == 'rule':
                    if gid in self.administration:
                        try:
                            i = int(first_word(all_but_first_word(input))) - 1
                            if i > len(self.administration[gid]['rules']):
                                i = len(self.administration[gid]['rules'])
                            elif i < 1:
                                i = 0
                        except:
                            return self.bot.send_message(
                                m,
                                self.bot.trans.errors.unknown,
                                extra={'format': 'HTML'})
                        self.administration[gid]['rules'].insert(
                            i, all_but_first_word(all_but_first_word(input)))
                        self.administration.store_database()
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.set %
                            m.conversation.title,
                            extra={'format': 'HTML'})
                    else:
                        return self.bot.send_message(
                            m,
                            self.bot.trans.plugins.administration.strings.
                            not_added % m.conversation.title,
                            extra={'format': 'HTML'})

                else:
                    return self.bot.send_message(
                        m,
                        self.bot.trans.errors.no_results,
                        extra={'format': 'HTML'})
Esempio n. 9
0
    def on_message_receive(self, msg):
        try:
            if msg.content == None or (msg.type != 'inline_query'
                                       and msg.date < time() - 60):
                return

            if msg.sender.id != self.config['owner'] and (
                    has_tag(self, msg.conversation.id, 'muted')
                    or has_tag(self, msg.sender.id, 'muted')):
                return

            step = get_step(self, msg.conversation.id)

            if step:
                for plugin in self.plugins:
                    if get_plugin_name(plugin) == step.plugin and hasattr(
                            plugin, 'steps'):
                        if msg.content.startswith('/cancel'):
                            plugin.steps(msg, -1)
                            cancel_steps(self, msg.conversation.id)

                        if msg.content.startswith('/done'):
                            plugin.steps(msg, 0)
                            cancel_steps(self, msg.conversation.id)

                        else:
                            plugin.steps(msg, step['step'])

            else:
                for plugin in self.plugins:
                    # Always do this action for every message. #
                    if hasattr(plugin, 'always'):
                        plugin.always(msg)

                    # If no query show help #
                    if msg.type == 'inline_query':
                        if msg.content == '':
                            msg.content = 'help'

                    # Check if any command of a plugin matches. #
                    for command in plugin.commands:
                        if 'parameters' not in command:
                            command['parameters'] = None

                        if 'command' in command:
                            if self.check_trigger(command['command'],
                                                  command['parameters'], msg,
                                                  plugin):
                                break

                        if 'friendly' in command:
                            if self.check_trigger(command['friendly'],
                                                  command['parameters'], msg,
                                                  plugin, True):
                                break

                        if 'shortcut' in command:
                            if self.check_trigger(command['shortcut'],
                                                  command['parameters'], msg,
                                                  plugin):
                                break

        except KeyboardInterrupt:
            pass

        except Exception as e:
            catch_exception(e, self)