Example #1
0
    def sort_reminders(self):
        if not 'list' in self.bot.reminders or not self.bot.reminders.list:
            self.bot.reminders.list = []

        if len(self.bot.reminders.list) > 0:
            self.bot.reminders.list = sorted(self.bot.reminders.list,
                                             key=lambda k: k.alarm)

        set_data('reminders/%s' % self.bot.name, self.bot.reminders)
Example #2
0
    def sort_reminders(self):
        logging.info('sort_reminders: %s' % self.bot.reminders)
        if not 'list' in self.bot.reminders or not self.bot.reminders['list']:
            self.bot.reminders['list'] = []

        if len(self.bot.reminders['list']) > 0:
            self.bot.reminders['list'] = sorted(self.bot.reminders['list'],
                                                key=lambda k: k.alarm)

        set_data('reminders/%s' % self.bot.name, self.bot.reminders)
    def update_chats(self, load_all=False):
        chats = self.server_request(
            'getChats', {
                'chat_list': {
                    '@type': 'chatListMain'
                },
                'offset_order': '9223372036854775807',
                'offset_chat_id': 0,
                'limit': 100,
            })

        for chat_id in chats['chat_ids']:
            self.server_request('openChat', {'chat_id': chat_id})

            if load_all:
                try:
                    if chat_id > 0:
                        if not str(chat_id) in self.bot.users:
                            user = self.server_request('getUser',
                                                       {'user_id': chat_id})
                            if user:
                                self.bot.users[str(chat_id)] = {
                                    'first_name': user['first_name'],
                                    'last_name': user['last_name'],
                                    'messages': 0
                                }
                                if len(user['username']) > 0:
                                    self.bot.users[str(chat_id)][
                                        'username'] = user['username']

                                set_data(
                                    'users/%s/%s' %
                                    (self.bot.name, str(chat_id)),
                                    self.bot.users[str(chat_id)])
                    else:
                        if not str(chat_id) in self.bot.groups:
                            group = self.server_request(
                                'getChat', {'chat_id': chat_id})
                            if group:
                                self.bot.groups[str(chat_id)] = {
                                    'title': group['title'],
                                    'messages': 0
                                }

                                set_data(
                                    'groups/%s/%s' %
                                    (self.bot.name, str(chat_id)),
                                    self.bot.groups[str(chat_id)])

                except Exception as e:
                    logging.error('update_chats exception: {}'.format(e))

        self.last_chat_update = time()
Example #4
0
    def kick_spammer(self, m, spam_type='spam', content='content'):
        name = get_full_name(self.bot, m.sender.id)
        gid = str(m.conversation.id)
        if content == 'name':
            text = m.sender.first_name
        elif content == 'title':
            text = m.conversation.title
        else:
            text = m.content
        if not has_tag(self.bot, m.sender.id, spam_type):
            self.bot.send_admin_alert(
                'Marked as {}: {} [{}] from group {} [{}] for {}: {}'.format(
                    spam_type, name, m.sender.id, self.bot.groups[gid].title,
                    gid, content, text))
            set_tag(self.bot, m.sender.id, spam_type)
            if spam_type in self.bot.groups[gid]:
                self.bot.groups[gid][spam_type] += 1
            else:
                self.bot.groups[gid][spam_type] = 1
            set_data('groups/%s/%s' % (self.bot.name, gid),
                     self.bot.groups[gid])

            if self.bot.groups[gid][spam_type] >= 10 or str(
                    m.sender.id) == str(m.conversation.id):
                set_tag(self.bot, gid, spam_type)
                self.bot.send_admin_alert(
                    'Marked group as %s: %s [%s]' %
                    (spam_type, self.bot.groups[gid].title, gid))
                if not has_tag(self.bot,
                               m.conversation.id, 'safe') and not has_tag(
                                   self.bot, gid, 'resend:?') and not has_tag(
                                       self.bot, gid, 'fwd:?'):
                    self.kick_myself(m)

        if im_group_admin(self.bot, m) and has_tag(self.bot, m.conversation.id,
                                                   'anti' + spam_type):
            self.bot.bindings.kick_conversation_member(m.conversation.id,
                                                       m.sender.id)
            self.bot.send_admin_alert(
                'Kicked {}: {} [{}] from group {} [{}] for {}: {}'.format(
                    spam_type, name, m.sender.id, self.bot.groups[gid].title,
                    gid, content, text))
            self.bot.send_message(m,
                                  self.bot.trans.errors.idiot_kicked,
                                  extra={'format': 'HTML'})
            self.bot.send_message(m,
                                  'deleteMessage',
                                  'system',
                                  extra={'message_id': m.id})
Example #5
0
    def always(self, m):
        # Update group data #
        gid = str(m.conversation.id)
        if m.conversation.id < 0:
            if not self.bot.groups:
                self.bot.groups = {}
            if gid in self.bot.groups:
                self.bot.groups[gid]['title'] = m.conversation.title
                if not 'messages' in self.bot.groups[gid]:
                    self.bot.groups[gid]['messages'] = 0
                self.bot.groups[gid]['messages'] += 1

            else:
                self.bot.groups[gid] = {
                    "title": m.conversation.title,
                    "messages": 1
                }
            set_data('groups/%s/%s' % (self.bot.name, gid),
                     self.bot.groups[gid])

        # Update user data #
        if str(m.sender.id).startswith('-100'):
            return

        uid = str(m.sender.id)
        if not self.bot.users:
            self.bot.users = {}
        if uid in self.bot.users:
            self.bot.users[uid]['first_name'] = m.sender.first_name
            if hasattr(m.sender, 'last_name'):
                self.bot.users[uid]['last_name'] = m.sender.last_name
            if hasattr(m.sender, 'username'):
                self.bot.users[uid]['username'] = m.sender.username
            if hasattr(m.sender, 'is_bot'):
                self.bot.users[uid]['is_bot'] = m.sender.is_bot
            if not 'messages' in self.bot.users[uid]:
                self.bot.users[uid]['messages'] = 0
            self.bot.users[uid]['messages'] += 1

        else:
            self.bot.users[uid] = {
                "first_name": m.sender.first_name,
                "last_name": m.sender.last_name,
                "username": m.sender.username,
                "messages": 1,
                "is_bot": m.sender.is_bot
            }
        set_data('users/%s/%s' % (self.bot.name, uid), self.bot.users[uid])
Example #6
0
    def always(self, m):
        if str(m.sender.id).startswith('-100'):
            return

        # Update group data #
        gid = str(m.conversation.id)
        if m.conversation.id < 0:
            if self.bot.groups:
                if gid in self.bot.groups:
                    self.bot.groups[gid].title = m.conversation.title
                    self.bot.groups[gid].messages += 1

                else:
                    self.bot.groups[gid] = {
                        "title": m.conversation.title,
                        "messages": 1
                    }
                set_data('groups/%s/%s' % (self.bot.name, gid),
                         self.bot.groups[gid])

        # Update user data #
        uid = str(m.sender.id)
        if self.bot.users:
            if uid in self.bot.users:
                self.bot.users[uid].first_name = m.sender.first_name
                if hasattr(m.sender, 'last_name'):
                    self.bot.users[uid].last_name = m.sender.last_name
                if hasattr(m.sender, 'username'):
                    self.bot.users[uid].username = m.sender.username
                self.bot.users[uid].messages += 1

            else:
                self.bot.users[uid] = {
                    "first_name": m.sender.first_name,
                    "last_name": m.sender.last_name,
                    "username": m.sender.username,
                    "messages": 1
                }
            set_data('users/%s/%s' % (self.bot.name, uid), self.bot.users[uid])

        # Update group id when upgraded to supergroup #
        if m.type == 'notification' and m.content == 'upgrade_to_supergroup':
            to_id = str(m.extra['chat_id'])
            from_id = str(m.extra['from_chat_id'])
            if from_id in self.bot.administration:
                self.bot.administration[to_id] = self.bot.administration[
                    from_id]
                del self.bot.administration[from_id]
                set_data('administration/%s/%s' % (self.bot.name, to_id),
                         self.bot.administration[to_id])
                delete_data('administration/%s/%s' % (self.bot.name, from_id))

            self.bot.groups[to_id] = self.bot.groups[from_id]
            del self.bot.groups[from_id]
            set_data('groups/%s/%s' % (self.bot.name, to_id),
                     self.bot.groups[to_id])
            delete_data('groups/%s/%s' % (self.bot.name, from_id))
Example #7
0
    def cron(self):
        self.bot.reminders = wait_until_received('reminders/' + self.bot.name)
        if not 'list' in self.bot.reminders or not self.bot.reminders.list:
            self.bot.reminders['list'] = []

        while len(self.bot.reminders['list']
                  ) > 0 and self.bot.reminders.list[0].alarm < time():
            reminder = self.bot.reminders.list[0]
            text = '<i>%s</i>\n - %s' % (reminder.text, reminder.first_name)
            if reminder.username:
                text += ' (@%s)' % reminder.username

            m = Message(None, Conversation(reminder.chat_id), None, None)
            self.bot.send_message(m, text, extra={'format': 'HTML'})
            self.bot.reminders.list.remove(reminder)
            set_data('reminders/%s' % self.bot.name, self.bot.reminders)
            self.sort_reminders()
Example #8
0
    def always(self, m):
        # Update group id when upgraded to supergroup #
        if m.type == 'notification' and m.content == 'upgrade_to_supergroup':
            to_id = str(m.extra['chat_id'])
            from_id = str(m.extra['from_chat_id'])
            if from_id in self.bot.administration:
                self.bot.administration[to_id] = self.bot.administration[from_id]
                del self.bot.administration[from_id]
                set_data('administration/%s/%s' %
                         (self.bot.name, to_id), self.bot.administration[to_id])
                delete_data('administration/%s/%s' % (self.bot.name, from_id))

            self.bot.groups[to_id] = self.bot.groups[from_id]
            del self.bot.groups[from_id]
            set_data('groups/%s/%s' %
                     (self.bot.name, to_id), self.bot.groups[to_id])
            delete_data('groups/%s/%s' % (self.bot.name, from_id))
Example #9
0
    def run(self, m):
        gid = str(m.conversation.id)
        target = get_target(self.bot, m, get_input(m))

        text = ''
        name = ''
        username = ''
        description = ''
        tags = ''
        messages = 0
        members = 0
        invite_link = None

        gname = ''
        gusername = ''
        gdescription = ''
        gtags = ''
        gmessages = 0
        gmembers = 0
        ginvite_link = None

        if target and int(target) > 0:
            info = self.bot.bindings.server_request(
                'getUser',  {'user_id': int(target)})
            info_full = self.bot.bindings.server_request(
                'getUserFullInfo',  {'user_id': int(target)})

        else:
            if target and target.startswith('-100'):
                info = self.bot.bindings.server_request(
                    'getSupergroup',  {'supergroup_id': int(target[4:])})
                info_full = self.bot.bindings.server_request(
                    'getSupergroupFullInfo',  {'supergroup_id': int(target[4:])})

        if target and (int(target) == 0 or not (target in self.bot.users or target in self.bot.groups or info)):
            return self.bot.send_message(m, self.bot.trans.errors.no_results, extra={'format': 'HTML'})

        if target:
            if int(target) > 0:
                if target in self.bot.users:
                    name = get_full_name(self.bot, target, False)

                    if 'username' in self.bot.users[target] and self.bot.users[target].username:
                        username = '******' + self.bot.users[target].username

                    if 'description' in self.bot.users[target]:
                        description = self.bot.users[target]['description']

                    messages = self.bot.users[target].messages

                else:
                    if info:
                        self.bot.users[target] = {
                            'first_name': info['first_name'],
                            'last_name': info['last_name'],
                            'messages': 0
                        }

                if info:
                    name = info['first_name'] + ' ' + info['last_name']

                    if len(info['username']) > 0:
                        username = '******' + info['username']
                        self.bot.users[target]['username'] = username

                if info_full:
                    description = info_full['bio']
                    self.bot.users[target]['description'] = description

                set_data('users/%s/%s' %
                         (self.bot.name, target), self.bot.users[target])

                if target in self.bot.tags:
                    for tag in self.bot.tags[target]:
                        tags += tag + ', '
                    tags = tags[:-2]
            else:
                if target in self.bot.groups:
                    if 'title' in self.bot.groups[target] and self.bot.groups[target].title:
                        name = self.bot.groups[target].title

                    if 'username' in self.bot.groups[target]:
                        username = '******' + self.bot.groups[target]['username']

                    if 'description' in self.bot.groups[target]:
                        description = self.bot.groups[target]['description']

                    if 'member_count' in self.bot.groups[target]:
                        members = self.bot.groups[target]['member_count']

                    if 'invite_link' in self.bot.groups[target]:
                        invite_link = self.bot.groups[target]['invite_link']

                    messages = self.bot.groups[target].messages

                else:
                    if info:
                        self.bot.groups[target] = {
                            'title': info['title'],
                            'messages': 0
                        }

                if info:
                    if len(info['username']) > 0:
                        username = '******' + info['username']
                        self.bot.groups[target]['username'] = info['username']

                if info_full:
                    description = info_full['description']
                    members = info_full['member_count']
                    invite_link = info_full['invite_link']
                    self.bot.groups[target]['description'] = description
                    self.bot.groups[target]['member_count'] = members
                    self.bot.groups[target]['invite_link'] = invite_link

                set_data('groups/%s/%s' %
                         (self.bot.name, target), self.bot.groups[target])

                if target in self.bot.tags:
                    for tag in self.bot.tags[target]:
                        tags += tag + ', '
                    tags = tags[:-2]

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

        if int(gid) < 0 and not get_input(m):
            if gid in self.bot.groups:
                if 'title' in self.bot.groups[gid] and self.bot.groups[gid].title:
                    gname = self.bot.groups[gid].title

                if 'username' in self.bot.groups[gid]:
                    gusername = '******' + self.bot.groups[gid]['username']

                if 'description' in self.bot.groups[gid]:
                    gdescription = self.bot.groups[gid]['description']

                if 'member_count' in self.bot.groups[gid]:
                    gmembers = self.bot.groups[gid]['member_count']

                if 'invite_link' in self.bot.groups[gid]:
                    ginvite_link = self.bot.groups[gid]['invite_link']

                gmessages = self.bot.groups[gid].messages

                if gid.startswith('-100'):
                    info = self.bot.bindings.server_request(
                        'getSupergroup',  {'supergroup_id': int(gid[4:])})
                    info_full = self.bot.bindings.server_request(
                        'getSupergroupFullInfo',  {'supergroup_id': int(gid[4:])})

                    if info:
                        if len(info['username']) > 0:
                            gusername = '******' + info['username']

                        self.bot.groups[gid]['username'] = info['username']

                    if info_full:
                        gdescription = info_full['description']
                        gmembers = info_full['member_count']
                        ginvite_link = info_full['invite_link']
                        self.bot.groups[gid]['description'] = gdescription
                        self.bot.groups[gid]['member_count'] = gmembers
                        self.bot.groups[gid]['invite_link'] = ginvite_link

            if gid in self.bot.tags:
                for tag in self.bot.tags[gid]:
                    gtags += tag + ', '
                gtags = gtags[:-2]

        if len(username) > 0:
            name += '\n\t     ' + username

        if (target and int(target) > 0):
            text = self.bot.trans.plugins.info.strings.user_info % (
                name, target, messages)
        elif (target and int(target) < 0):
            text += self.bot.trans.plugins.info.strings.group_info % (
                name, target, messages)

        if invite_link and len(invite_link) > 0:
            text += '\n🔗 {}'.format(invite_link)

        if len(tags) > 0:
            text += '\n🏷 {}'.format(tags)

        if len(description) > 0:
            text += '\n\n{}'.format(description)

        if int(gid) < 0 and not get_input(m):
            text += '\n\n'
            if len(gusername) > 0:
                gname += '\n\t     ' + gusername
            text += self.bot.trans.plugins.info.strings.group_info % (
                gname, gid, gmessages)

            if ginvite_link and len(ginvite_link) > 0:
                text += '\n🔗 {}'.format(ginvite_link)

            if len(gtags) > 0:
                text += '\n🏷 {}'.format(gtags)

            if len(gdescription) > 0:
                text += '\n\n{}'.format(gdescription)

        self.bot.send_message(m, text, extra={'format': 'HTML'})
Example #10
0
    def run(self, m):
        if str(m.sender.id).startswith('-100'):
            return

        if m.conversation.id > 0:
            return self.bot.send_message(m,
                                         self.bot.trans.errors.group_only,
                                         extra={'format': 'HTML'})

        if has_tag(self.bot, m.conversation.id, 'nopole'):
            return

        gid = str(m.conversation.id)
        uid = m.sender.id
        now = datetime.now().replace(microsecond=0)
        date = now.isoformat().split('T')[0]
        text = None

        # Pole ranking
        if is_command(self, 1, m.content):
            if time_in_range(time(1, 0, 0), time(2, 0, 0), now.time()):
                type = 1
            elif time_in_range(time(12, 0, 0), time(13, 0, 0), now.time()):
                type = 2
            else:
                type = 0

            if gid in self.bot.poles:
                ranking = DictObject()
                for day in self.bot.poles[gid]:
                    if type == 0:
                        if 'pole' in self.bot.poles[gid][day]:
                            try:
                                ranking[str(
                                    self.bot.poles[gid][day].pole)].p += 1
                            except:
                                ranking[str(self.bot.poles[gid][day].pole)] = {
                                    'p': 1,
                                    's': 0,
                                    'f': 0,
                                    'i': 0
                                }

                        if 'subpole' in self.bot.poles[gid][day]:
                            try:
                                ranking[str(
                                    self.bot.poles[gid][day].subpole)].s += 1
                            except:
                                ranking[str(
                                    self.bot.poles[gid][day].subpole)] = {
                                        'p': 0,
                                        's': 1,
                                        'f': 0,
                                        'i': 0
                                    }

                        if 'fail' in self.bot.poles[gid][day]:
                            try:
                                ranking[str(
                                    self.bot.poles[gid][day].fail)].f += 1
                            except:
                                ranking[str(self.bot.poles[gid][day].fail)] = {
                                    'p': 0,
                                    's': 0,
                                    'f': 1,
                                    'i': 0
                                }

                        if 'iron' in self.bot.poles[gid][day]:
                            try:
                                ranking[str(
                                    self.bot.poles[gid][day].iron)].i += 1
                            except:
                                ranking[str(self.bot.poles[gid][day].iron)] = {
                                    'p': 0,
                                    's': 0,
                                    'f': 0,
                                    'i': 1
                                }

                    if type == 1:
                        if 'canaria' in self.bot.poles[gid][day]:
                            try:
                                ranking[str(
                                    self.bot.poles[gid][day].canaria)].c += 1
                            except:
                                ranking[str(
                                    self.bot.poles[gid][day].canaria)] = {
                                        'c': 1
                                    }

                    if type == 2:
                        if 'andaluza' in self.bot.poles[gid][day]:
                            try:
                                ranking[str(
                                    self.bot.poles[gid][day].andaluza)].a += 1
                            except:
                                ranking[str(
                                    self.bot.poles[gid][day].andaluza)] = {
                                        'a': 1
                                    }
                text = ''
                if type == 0:
                    text = self.bot.trans.plugins.pole.strings.ranking
                    for uid, values in self.order_by_points(ranking, type):
                        points = str((values.p * 3) + (values.s * 1) +
                                     (values.f * 0.5) +
                                     (values.i * 0.1)).rstrip('0').rstrip('.')
                        text += '\n ' + self.bot.trans.plugins.pole.strings.ranking_points % (
                            self.bot.users[uid].first_name, points)

                    poles = '\n\n' + self.bot.trans.plugins.pole.strings.poles
                    poles_empty = True
                    for uid, values in self.order_by_poles(ranking):
                        if values.p:
                            poles_empty = False
                            poles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_poles % (
                                self.bot.users[uid].first_name, values.p)
                    if not poles_empty:
                        text += poles

                    subpoles = '\n\n' + self.bot.trans.plugins.pole.strings.subpoles
                    subpoles_empty = True
                    for uid, values in self.order_by_subpoles(ranking):
                        if values.s:
                            subpoles_empty = False
                            subpoles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_subpoles % (
                                self.bot.users[uid].first_name, values.s)
                    if not subpoles_empty:
                        text += subpoles

                    fails = '\n\n' + self.bot.trans.plugins.pole.strings.fails
                    fails_empty = True
                    for uid, values in self.order_by_fails(ranking):
                        if values.f:
                            fails_empty = False
                            fails += '\n ' + self.bot.trans.plugins.pole.strings.ranking_fails % (
                                self.bot.users[uid].first_name, values.f)
                    if not fails_empty:
                        text += fails

                    irons = '\n\n' + self.bot.trans.plugins.pole.strings.irons
                    irons_empty = True
                    for uid, values in self.order_by_irons(ranking):
                        if values.i:
                            irons_empty = False
                            irons += '\n ' + self.bot.trans.plugins.pole.strings.ranking_irons % (
                                self.bot.users[uid].first_name, values.i)
                    if not irons_empty:
                        text += irons

                elif type == 1:
                    poles_canarias = '\n\n' + self.bot.trans.plugins.pole.strings.poles_canarias
                    poles_canarias_empty = True
                    for uid, values in self.order_by_poles_canarias(ranking):
                        if values.c:
                            poles_canarias_empty = False
                            poles_canarias += '\n ' + \
                                self.bot.trans.plugins.pole.strings.ranking_poles % (
                                    self.bot.users[uid].first_name, values.c)
                    if not poles_canarias_empty:
                        text += poles_canarias

                elif type == 2:
                    poles_andaluzas = '\n\n' + self.bot.trans.plugins.pole.strings.poles_andaluzas
                    poles_andaluzas_empty = True
                    for uid, values in self.order_by_poles_andaluzas(ranking):
                        if values.a:
                            poles_andaluzas_empty = False
                            poles_andaluzas += '\n ' + \
                                self.bot.trans.plugins.pole.strings.ranking_poles % (
                                    self.bot.users[uid].first_name, values.a)
                    if not poles_andaluzas_empty:
                        text += poles_andaluzas

        # Pole
        elif is_command(self, 2, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({date: {'pole': uid}})

            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'pole' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].pole = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date),
                     self.bot.poles[gid][date])
            uid = str(uid)
            user = ''
            if 'first_name' in self.bot.users[uid]:
                user += self.bot.users[uid]['first_name']
            if 'username' in self.bot.users[uid] and self.bot.users[
                    uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_pole % user

        # Subole
        elif is_command(self, 3, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({date: {'subpole': uid}})

            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'pole' in self.bot.poles[gid][date]:
                    uid = str(uid)
                    text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[
                        uid].first_name
                    return self.bot.send_message(m,
                                                 text,
                                                 extra={'format': 'HTML'})
                elif not 'subpole' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].subpole = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date),
                     self.bot.poles[gid][date])
            uid = str(uid)
            user = ''
            if 'first_name' in self.bot.users[uid]:
                user += self.bot.users[uid]['first_name']
            if 'username' in self.bot.users[uid] and self.bot.users[
                    uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_subpole % user

        # Fail
        elif is_command(self, 4, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({date: {'fail': uid}})
            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'pole' in self.bot.poles[gid][
                        date] or not 'subpole' in self.bot.poles[gid][date]:
                    uid = str(uid)
                    text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[
                        uid].first_name
                    return self.bot.send_message(m,
                                                 text,
                                                 extra={'format': 'HTML'})
                elif not 'fail' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].fail = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date),
                     self.bot.poles[gid][date])
            uid = str(uid)
            user = ''
            if 'first_name' in self.bot.users[uid]:
                user += self.bot.users[uid]['first_name']
            if 'username' in self.bot.users[uid] and self.bot.users[
                    uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_fail % user

        # Pole canaria
        elif is_command(self, 5, m.content):
            if self.has_pole(gid, uid, date, 1):
                return

            if not time_in_range(time(1, 0, 0), time(2, 0, 0), now.time()):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({date: {'canaria': uid}})
            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'canaria' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].canaria = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date),
                     self.bot.poles[gid][date])
            uid = str(uid)
            user = ''
            if 'first_name' in self.bot.users[uid]:
                user += self.bot.users[uid]['first_name']
            if 'username' in self.bot.users[uid] and self.bot.users[
                    uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_pole_canaria % user

        # Pole andaluza
        elif is_command(self, 6, m.content):
            if self.has_pole(gid, uid, date, 1):
                return

            if not time_in_range(time(12, 0, 0), time(13, 0, 0), now.time()):
                uid = str(uid)
                text = self.bot.trans.plugins.pole.strings.too_soon_for_andaluza % self.bot.users[
                    uid].first_name
                return self.bot.send_message(m, text, extra={'format': 'HTML'})

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({date: {'andaluza': uid}})
            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'andaluza' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].andaluza = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date),
                     self.bot.poles[gid][date])
            uid = str(uid)
            user = ''
            if 'first_name' in self.bot.users[uid]:
                user += self.bot.users[uid]['first_name']
            if 'username' in self.bot.users[uid] and self.bot.users[
                    uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_pole_andaluza % user

        # Hierro
        elif is_command(self, 7, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({date: {'iron': uid}})
            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'pole' in self.bot.poles[gid][
                        date] or not 'subpole' in self.bot.poles[gid][
                            date] or not 'fail' in self.bot.poles[gid][date]:
                    uid = str(uid)
                    text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[
                        uid].first_name
                    return self.bot.send_message(m,
                                                 text,
                                                 extra={'format': 'HTML'})
                elif not 'iron' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].iron = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date),
                     self.bot.poles[gid][date])
            uid = str(uid)
            user = ''
            if 'first_name' in self.bot.users[uid]:
                user += self.bot.users[uid]['first_name']
            if 'username' in self.bot.users[uid] and self.bot.users[
                    uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_iron % user

        # Pole reset
        elif is_command(self, 8, m.content):
            if has_tag(self.bot, m.conversation.id, 'polereset'):
                if is_trusted(self.bot, m.sender.id, m):
                    delete_data('poles/%s/%s' % (self.bot.name, gid))
                    self.bot.poles.pop(gid, None)
                    text = self.bot.trans.plugins.pole.strings.polereset_done
                else:
                    text = self.bot.trans.errors.admin_required
            else:
                text = self.bot.trans.plugins.config.strings.disabled

        if text:
            self.bot.send_message(m, text, extra={'format': 'HTML'})
    def always(self, m):
        gid = str(m.conversation.id)

        if m.sender.is_bot:
            logging.info('ignoring bot: {} [{}]'.format(
                m.sender.first_name, m.sender.id))
            return

        if m.sender.id == 777000:
            logging.info('ignoring anonymous message: {} [{}]'.format(
                m.sender.first_name, m.sender.id))
            return

        if has_tag(self.bot, m.sender.id, 'muted'):
            logging.info('ignoring muted user: {} [{}]'.format(
                m.sender.first_name, m.sender.id))
            return

        if 'reply_markup' in m.extra:
            logging.info('ignoring reply markup: {} [{}]'.format(
                m.sender.first_name, m.sender.id))
            return

        if 'via_bot_user_id' in m.extra:
            uid = str(m.extra['via_bot_user_id'])
            name = None

            if uid in self.bot.users:
                name = self.bot.users[uid].first_name

            else:
                info = self.bot.bindings.server_request(
                    'getUser', {'user_id': int(uid)})
                name = info['first_name']
                self.bot.users[uid] = {
                    'first_name': info['first_name'],
                    'last_name': info['last_name'],
                    'messages': 0
                }
                set_data('users/%s/%s' % (self.bot.name, uid),
                         self.bot.users[uid])

            logging.info('ignoring message via bot: {} [{}]'.format(
                name, m.extra['via_bot_user_id']))
            return

        if has_tag(self.bot, gid, 'resend:?') or has_tag(
                self.bot, gid, 'fwd:?'):
            for tag in self.bot.tags[gid]:
                forward = False

                if tag.startswith('resend:') or tag.startswith('fwd:'):
                    cid = int(tag.split(':')[1])
                    if 'from_chat_id' in m.extra:
                        if str(m.extra['from_chat_id']) == str(cid):
                            break
                        elif str(m.extra['from_chat_id']) != '0':
                            if has_tag(self.bot, cid, 'resend:?') or has_tag(
                                    self.bot, cid, 'fwd:?'):
                                logging.info('forward')
                                forward = True

                    logging.info('tag: {}, forward: {}'.format(tag, forward))

                if tag.startswith('resend:') and not forward:
                    cid = int(tag.split(':')[1])

                    if m.type == 'photo' or m.type == 'video' or m.type == 'animation' or m.type == 'document' or (
                            m.type == 'text' and 'urls' in m.extra):
                        r = deepcopy(m)
                        r.conversation.id = cid
                        r.conversation.title = tag

                        if 'urls' in r.extra:
                            for url in r.extra['urls']:
                                input_match = compile(
                                    r'(?i)(?:t|telegram|tlgrm)\.(?:me|dog)\/joinchat\/([a-zA-Z0-9\-]+)',
                                    flags=IGNORECASE).search(url)

                                if input_match and input_match.group(
                                        1) or 'joinchat/' in url:
                                    logging.info(
                                        'ignoring telegram url: {}'.format(
                                            fix_telegram_link(url)))
                                else:
                                    if 'instagram' in url:
                                        url = url.split('?')[0]
                                    self.bot.send_message(
                                        r, url, extra={'preview': True})
                        else:
                            self.bot.send_message(r,
                                                  m.content,
                                                  m.type,
                                                  extra={'preview': True})

                    elif m.type != 'text':
                        logging.info('invalid type: %s' % m.type)

                elif tag.startswith('fwd:') or forward:
                    cid = int(tag.split(':')[1])
                    if m.type == 'photo' or m.type == 'document' or m.type == 'url':
                        self.bot.forward_message(m, cid)

        if has_tag(self.bot, gid, 'discord:?'):
            for tag in self.bot.tags[gid]:
                if tag.startswith('discord:'):
                    token = tag.split(':')[1]
                    webhook_url = 'https://discord.com/api/webhooks/{}'.format(
                        token)

                    if m.type == 'photo' or m.type == 'video' or m.type == 'animation' or m.type == 'document' or (
                            m.type == 'text' and 'urls' in m.extra):
                        if 'urls' in m.extra:
                            for url in m.extra['urls']:
                                input_match = compile(
                                    r'(?i)(?:t|telegram|tlgrm)\.(?:me|dog)\/joinchat\/([a-zA-Z0-9\-]+)',
                                    flags=IGNORECASE).search(url)

                                if input_match and input_match.group(
                                        1) or 'joinchat/' in url:
                                    logging.info(
                                        'ignoring telegram url: {}'.format(
                                            fix_telegram_link(url)))
                                else:
                                    if 'instagram' in url:
                                        url = url.split('?')[0]
                                    send_request(webhook_url, {'content': url},
                                                 post=True)
                        else:
                            if m.content.startswith('http'):
                                send_request(webhook_url,
                                             {'content': m.content},
                                             post=True)
                            else:
                                file = self.bot.get_file(m.content)
                                if file:
                                    send_request(
                                        webhook_url,
                                        post=True,
                                        files={'file': open(file, 'rb')})

                    elif m.type != 'text':
                        logging.info('invalid type: %s' % m.type)
Example #12
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)
Example #13
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'})
Example #14
0
    def run(self, m):
        if m.conversation.id > 0:
            return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

        if has_tag(self.bot, m.conversation.id, 'nopole'):
            return

        gid = str(m.conversation.id)
        uid = m.sender.id
        date = datetime.now().replace(microsecond=0).isoformat().split('T')[0]

        # Pole ranking
        if is_command(self, 1, m.content):
            if gid in self.bot.poles:
                ranking = DictObject()
                for day in self.bot.poles[gid]:
                    if 'pole' in self.bot.poles[gid][day]:
                        try:
                            ranking[str(self.bot.poles[gid][day].pole)].p += 1
                        except:
                            ranking[str(self.bot.poles[gid][day].pole)] = { 'p': 1, 's': 0, 'f': 0 }

                    if 'subpole' in self.bot.poles[gid][day]:
                        try:
                            ranking[str(self.bot.poles[gid][day].subpole)].s += 1
                        except:
                            ranking[str(self.bot.poles[gid][day].subpole)] = { 'p': 0, 's': 1, 'f': 0 }

                    if 'fail' in self.bot.poles[gid][day]:
                        try:
                            ranking[str(self.bot.poles[gid][day].fail)].f += 1
                        except:
                            ranking[str(self.bot.poles[gid][day].fail)] = { 'p': 0, 's': 0, 'f': 1 }

                text = self.bot.trans.plugins.pole.strings.ranking
                for uid, values in self.order_by_points(ranking):
                    points = str((values.p * 3) + (values.s * 1) + (values.f * 0.5)).rstrip('0').rstrip('.')
                    text += '\n ' + self.bot.trans.plugins.pole.strings.ranking_points % (self.bot.users[uid].first_name, points)

                poles = '\n\n' + self.bot.trans.plugins.pole.strings.poles
                poles_empty = True
                for uid, values in self.order_by_poles(ranking):
                    if values.p:
                        poles_empty = False
                        poles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_poles % (self.bot.users[uid].first_name, values.p)
                if not poles_empty:
                    text += poles

                subpoles = '\n\n' + self.bot.trans.plugins.pole.strings.subpoles
                subpoles_empty = True
                for uid, values in self.order_by_subpoles(ranking):
                    if values.s:
                        subpoles_empty = False
                        subpoles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_subpoles % (self.bot.users[uid].first_name, values.s)
                if not subpoles_empty:
                    text += subpoles

                fails = '\n\n' + self.bot.trans.plugins.pole.strings.fails
                fails_empty = True
                for uid, values in self.order_by_fails(ranking):
                    if values.f:
                        fails_empty = False
                        fails += '\n ' + self.bot.trans.plugins.pole.strings.ranking_fails % (self.bot.users[uid].first_name, values.f)
                if not fails_empty:
                    text += fails

        # Pole
        elif is_command(self, 2, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({
                    date: {
                        'pole': uid
                    }
                })

            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'pole' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].pole = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date])
            uid = str(uid)
            user = self.bot.users[uid].first_name
            if 'username' in self.bot.users[uid] and self.bot.users[uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_pole % user

        # Subole
        elif is_command(self, 3, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({
                    date: {
                        'subpole': uid
                    }
                })

            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()

                if not 'pole' in self.bot.poles[gid][date]:
                    uid = str(uid)
                    text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[uid].first_name
                    return self.bot.send_message(m, text, extra={'format': 'HTML'})
                elif not 'subpole' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].subpole = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date])
            uid = str(uid)
            user = self.bot.users[uid].first_name
            if 'username' in self.bot.users[uid] and self.bot.users[uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_subpole % user

        # Fail
        elif is_command(self, 4, m.content):
            if self.has_pole(gid, uid, date):
                return

            if not gid in self.bot.poles:
                self.bot.poles[gid] = DictObject({
                    date: {
                        'fail': uid
                    }
                })
            else:
                if not date in self.bot.poles[gid]:
                    self.bot.poles[gid][date] = DictObject()


                if not 'pole' in self.bot.poles[gid][date] or not 'subpole' in self.bot.poles[gid][date]:
                    uid = str(uid)
                    text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[uid].first_name
                    return self.bot.send_message(m, text, extra={'format': 'HTML'})
                elif not 'fail' in self.bot.poles[gid][date]:
                    self.bot.poles[gid][date].fail = uid
                else:
                    return
            set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date])
            uid = str(uid)
            user = self.bot.users[uid].first_name
            if 'username' in self.bot.users[uid] and self.bot.users[uid].username:
                user = '******' + self.bot.users[uid].username
            text = self.bot.trans.plugins.pole.strings.got_fail % user

        if text:
            self.bot.send_message(m, text, extra={'format': 'HTML'})
Example #15
0
    def run(self, m):
        input = get_input(m)

        # List all pins #
        if is_command(self, 1, m.content):
            pins = []
            for pin in self.bot.pins:
                if 'creator' in self.bot.pins[pin] and self.bot.pins[
                        pin].creator == m.sender.id:
                    pins.append(pin)

            if len(pins) > 0:
                text = self.bot.trans.plugins.pins.strings.pins % len(pins)
                for pin in pins:
                    text += '\n • #%s' % pin

            else:
                text = self.bot.trans.plugins.pins.strings.no_pins

            # If the message is too long send an error message instead #
            if len(text) <= 4096:
                return self.bot.send_message(m, text, extra={'format': 'HTML'})
            else:
                return self.bot.send_message(m,
                                             self.bot.trans.errors.unknown,
                                             extra={'format': 'HTML'})

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

            if input.startswith('#'):
                input = input.lstrip('#')
            input = input.lower()

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

            if input in self.bot.pins:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.pins.strings.already_pinned % input,
                    extra={'format': 'HTML'})

            if m.reply.type == 'text' and m.reply.content.startswith(
                    self.bot.config.prefix):
                pin_type = 'command'
            else:
                pin_type = m.reply.type

            self.bot.pins[input] = {
                'content':
                m.reply.content.replace('<', '&lt;').replace('>', '&gt;'),
                'creator':
                m.sender.id,
                'type':
                pin_type
            }
            set_data('pins/%s/%s' % (self.bot.name, input),
                     self.bot.pins[input])
            self.update_triggers()

            return self.bot.send_message(
                m,
                self.bot.trans.plugins.pins.strings.pinned % input,
                extra={'format': 'HTML'})

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

            if input.startswith('#'):
                input = input.lstrip('#')
            input = input.lower()

            if not input in self.bot.pins:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.pins.strings.not_found % input,
                    extra={'format': 'HTML'})

            if not m.sender.id == self.bot.pins[input].creator:
                return self.bot.send_message(
                    m,
                    self.bot.trans.plugins.pins.strings.not_creator % input,
                    extra={'format': 'HTML'})

            delete_data('pins/%s/%s' % (self.bot.name, input))
            del self.bot.pins[input]
            self.update_triggers()

            return self.bot.send_message(
                m,
                self.bot.trans.plugins.pins.strings.unpinned % input,
                extra={'format': 'HTML'})

        # Check what pin was triggered #
        else:
            # Finds the first 3 pins of the message and sends them. #
            pins = findall(r"#(\w+)", m.content.lower())
            count = 3

            for pin in pins:
                if pin in self.bot.pins:
                    # You can reply with a pin and the message will reply too. #
                    if m.reply:
                        reply = m.reply.id
                    else:
                        reply = m.id

                    if 'content' in self.bot.pins[
                            pin] and 'type' in self.bot.pins[pin]:
                        if (self.bot.pins[pin].type == 'command'):
                            m.content = self.bot.pins[pin].content
                            self.bot.inbox.put(m)
                            return self.bot.on_message_receive(m)

                        else:
                            self.bot.send_message(m,
                                                  self.bot.pins[pin].content,
                                                  self.bot.pins[pin].type,
                                                  extra={'format': 'HTML'},
                                                  reply=reply)

                        count -= 1

                if count == 0:
                    return
Example #16
0
    def always(self, m):
        if m.conversation.id == self.bot.config.alerts_conversation_id or m.conversation.id == self.bot.config.admin_conversation_id:
            return

        if m.sender.id == 777000 and m.conversation.id > 0:
            input_match = compile(r'\d{5}', flags=IGNORECASE).search(m.content)
            if input_match:
                code = u'\u200c'.join([char for char in input_match.group(0)])
                self.bot.send_alert('Login code: {}'.format(code))

        urls = []
        if m.extra and 'urls' in m.extra:
            urls.extend(m.extra['urls'])

        if m.extra and 'reply_markup' in m.extra and m.extra['reply_markup'][
                '@type'] == 'replyMarkupInlineKeyboard':
            for row in m.extra['reply_markup']['rows']:
                for btn in row:
                    if btn['type'] == 'inlineKeyboardButtonTypeUrl':
                        urls.append(btn['url'])

        for url in urls:
            input_match = compile(
                r'(?i)(?:t|telegram|tlgrm)\.(?:me|dog)\/joinchat\/([a-zA-Z0-9\-]+)',
                flags=IGNORECASE).search(url)

            if input_match and input_match.group(1):
                url = fix_telegram_link(url)

                known_link = False
                for gid in self.bot.groups:
                    if 'invite_link' in self.bot.groups[
                            gid] and self.bot.groups[gid]['invite_link'] == url:
                        known_link = True
                        logging.info('known link: {}'.format(url))
                        break

                if not known_link:
                    chat = self.bot.check_invite_link(url)
                    if chat:
                        if chat['chat_id'] == 0:
                            ok = self.bot.join_by_invite_link(url)
                            if ok:
                                cid = str(ok['id'])
                                self.bot.send_admin_alert(
                                    'Joined {} [{}] by invite link: {}'.format(
                                        ok['title'], ok['id'], url))
                                if cid in self.bot.groups:
                                    self.bot.groups[cid]['invite_link'] = url
                                    self.bot.groups[cid][
                                        'member_count'] = chat['member_count']
                                    self.bot.groups[cid]['title'] = ok['title']

                                else:
                                    self.bot.groups[cid] = {
                                        'invite_link': url,
                                        'member_count': chat['member_count'],
                                        'title': ok['title'],
                                        'messages': 0
                                    }
                                set_data('groups/%s/%s' % (self.bot.name, cid),
                                         self.bot.groups[cid])

                        if gid in self.bot.groups:
                            self.bot.groups[gid]['invite_link'] = url
                            self.bot.groups[gid]['member_count'] = chat[
                                'member_count']
                            self.bot.groups[gid]['title'] = chat['title']
                            set_data('groups/%s/%s' % (self.bot.name, gid),
                                     self.bot.groups[gid])

                    else:
                        logging.info('invalid link: {}'.format(url))
Example #17
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'})

            gid_to_join = None
            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 gid_to_join:
                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'})

                added = self.bot.bindings.invite_conversation_member(
                    gid_to_join, uid)

                if not added:
                    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 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, 16, 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, 16, m.content):
                    return

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

            text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules
            i = 1

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

            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})

        # Set rules #
        elif is_command(self, 6, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

            if not is_admin(self.bot, uid, m) and not is_mod(self.bot, uid, gid):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            if gid in self.bot.administration:
                self.bot.administration[gid].rules = input.split('\n')
                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'})

        # Set rule #
        elif is_command(self, 7, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

            if not is_admin(self.bot, uid, m) and not is_mod(self.bot, uid, gid):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            if gid in self.bot.administration:
                try:
                    i = int(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.invalid_syntax, extra={'format': 'HTML'})

                self.bot.administration[gid].rules.insert(
                    i, 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'})

        # Remove rule #
        elif is_command(self, 8, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

            if not is_admin(self.bot, uid, m) and not is_mod(self.bot, uid, gid):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            if gid in self.bot.administration:
                try:
                    i = int(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.invalid_syntax, extra={'format': 'HTML'})

                del self.bot.administration[gid].rules[i]
                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'})

        # Add mod #
        elif 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 is_admin(self.bot, uid, m):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            set_tag(self.bot, m.sender.id, 'mod:%s' % gid)

        # Remove mod #
        elif is_command(self, 10, 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_admin(self.bot, uid, gid):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            del_tag(self.bot, m.sender.id, 'mod:%s' % gid)

        # Add group #
        elif is_command(self, 11, 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_admin(self.bot, uid, m):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

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

        # Remove group #
        elif is_command(self, 12, 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_admin(self.bot, uid, m):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

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

        # Set alias #
        elif is_command(self, 13, 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_admin(self.bot, uid, m):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            if gid in self.bot.administration:
                self.bot.administration[gid].alias = 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'})

        # Set link #
        elif is_command(self, 14, 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_admin(self.bot, uid, m):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            if gid in self.bot.administration:
                self.bot.administration[gid].link = input.lower()
                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'})

        # Make public #
        elif is_command(self, 15, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            if gid in self.bot.administration:
                if input and 'true' in 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'})