示例#1
0
async def _del_string(event: NewMessage.Event, db: ArangoDB) -> MDTeXDocument:
    """Add a string to the Collection of its type"""
    msg: Message = event.message
    args = msg.raw_text.split()[2:]
    _, args = parsers.parse_arguments(' '.join(args))
    string_type = args[0]
    strings = args[1:]
    removed_items = []
    for string in strings:
        hex_type = AUTOBAHN_TYPES.get(string_type)
        collection = db.ab_collection_map.get(hex_type)
        if hex_type is None or collection is None:
            continue

        if hex_type == '0x3':
            link_creator, chat_id, random_part = await helpers.resolve_invite_link(
                string)
            string = chat_id

        existing_one: Document = collection.fetchFirstExample(
            {'string': string})
        if existing_one:
            existing_one[0].delete()
            removed_items.append(string)

    return MDTeXDocument(
        Section(Bold('Deleted Items:'),
                SubSection(Bold(string_type), *removed_items)))
示例#2
0
async def _add_string(event: NewMessage.Event, db: ArangoDB) -> MDTeXDocument:
    """Add a string to the Collection of its type"""
    msg: Message = event.message
    args = msg.raw_text.split()[2:]
    _, args = parsers.parse_arguments(' '.join(args))
    string_type = args[0]
    strings = args[1:]
    added_items = []
    for string in strings:
        hex_type = AUTOBAHN_TYPES.get(string_type)
        collection = db.ab_collection_map.get(hex_type)
        if hex_type is None or collection is None:
            continue
        if hex_type == '0x3':
            link_creator, chat_id, random_part = await helpers.resolve_invite_link(
                string)
            string = chat_id
        elif hex_type == '0x4':
            string = await helpers.resolve_url(string)

        existing_one = collection.fetchByExample({'string': string},
                                                 batchSize=1)
        if not existing_one:
            collection.add_string(string)
            added_items.append(Code(string))
    return MDTeXDocument(
        Section(Bold('Added Items:'),
                SubSection(Bold(string_type), *added_items)))
示例#3
0
async def _query_banlist(event: NewMessage.Event,
                         db: ArangoDB) -> MDTeXDocument:
    msg: Message = event.message
    args = msg.raw_text.split()[2:]
    keyword_args, args = parsers.parse_arguments(' '.join(args))
    reason = keyword_args.get('reason')
    users = []
    if args:
        users = db.query(
            'For doc in BanList '
            'FILTER doc._key in @ids '
            'RETURN doc',
            bind_vars={'ids': args})
        query_results = [
            KeyValueItem(Code(user['id']), user['reason']) for user in users
        ] or [Italic('None')]
    if reason is not None:
        result = db.query(
            'FOR doc IN BanList '
            'FILTER doc.reason LIKE @reason '
            'COLLECT WITH COUNT INTO length '
            'RETURN length',
            bind_vars={'reason': reason})
        query_results = [KeyValueItem(Bold('Count'), Code(result))]
    return MDTeXDocument(Section(Bold('Query Results'), *query_results))
示例#4
0
async def _query_string(event: NewMessage.Event,
                        db: ArangoDB) -> MDTeXDocument:
    """Add a string to the Collection of its type"""
    msg: Message = event.message
    args = msg.raw_text.split()[2:]
    keyword_args, args = parsers.parse_arguments(' '.join(args))
    if 'types' in args:
        return MDTeXDocument(
            Section(
                Bold('Types'), *[
                    KeyValueItem(Bold(name), Code(code))
                    for name, code in AUTOBAHN_TYPES.items()
                ]))
    string_type = keyword_args.get('type')
    code = keyword_args.get('code')
    code_range = keyword_args.get('range')
    hex_type = None
    collection = None
    if string_type is not None:
        hex_type = AUTOBAHN_TYPES.get(string_type)
        collection = db.ab_collection_map[hex_type]
    if code is None and code_range is None:
        all_strings = collection.fetchAll()
        if not len(all_strings) > 100:
            items = [
                KeyValueItem(Bold(f'0x{doc["_key"]}'.rjust(5)),
                             Code(doc['string'])) for doc in all_strings
            ]
        else:
            items = [Pre(', '.join([doc['string'] for doc in all_strings]))]
        return MDTeXDocument(
            Section(Bold(f'Strings for {string_type}[{hex_type}]'), *items))

    elif hex_type is not None and code is not None:
        db_key = code.split('x')[-1]
        string = collection.fetchDocument(db_key).getStore()['string']
        return MDTeXDocument(
            Section(Bold(f'String for {string_type}[{code}]'), Code(string)))

    elif hex_type is not None and code_range is not None:
        start, stop = [int(c.split('x')[-1]) for c in code_range.split('-')]
        keys = [str(i) for i in range(start, stop + 1)]
        documents = db.query(
            f'FOR doc IN @@collection '
            'FILTER doc._key in @keys '
            'RETURN doc',
            bind_vars={
                '@collection': collection.name,
                'keys': keys
            })
        items = [
            KeyValueItem(Bold(f'0x{doc["_key"]}'.rjust(5)),
                         Code(doc['string'])) for doc in documents
        ]
        return MDTeXDocument(
            Section(Bold(f'Strings for {string_type}[{hex_type}]'), *items))
示例#5
0
async def get_args(
        event: NewMessage.Event) -> Tuple[Dict[str, str], List[str]]:
    """Get arguments from a event

    Args:
        event: The event

    Returns:
        Parsed arguments as returned by parser.parse_arguments()
    """
    _args = event.message.raw_text.split()[1:]
    return parsers.parse_arguments(' '.join(_args))
示例#6
0
async def _add_tags(event: NewMessage.Event, db: ArangoDB):
    """Add tags to chat.

    Args:
        event: The event of the command
        db: The database object

    Returns: A string with the action taken.
    """
    msg: Message = event.message
    args = msg.raw_text.split()[2:]
    chat_document = db.groups[event.chat_id]
    db_named_tags: Dict = chat_document['named_tags'].getStore()
    db_tags: List = chat_document['tags']
    named_tags, tags = parsers.parse_arguments(' '.join(args))
    for k, v in named_tags.items():
        db_named_tags[k] = v
    for _tag in tags:
        if _tag not in db_tags:
            db_tags.append(_tag)
    chat_document['named_tags'] = db_named_tags
    chat_document['tags'] = db_tags
    chat_document.save()
示例#7
0
async def user_info(event: NewMessage.Event) -> None:
    """Show information about a user.

    Args:
        event: The event of the command

    Returns: None

    """
    chat: Channel = await event.get_chat()
    client: KantekClient = event.client
    msg: Message = event.message
    _args = msg.raw_text.split()[1:]
    keyword_args, args = parsers.parse_arguments(' '.join(_args))
    response = ''
    if not args and msg.is_reply:
        response = await _info_from_reply(event, **keyword_args)
    elif args or 'search' in keyword_args:
        response = await _info_from_arguments(event, **keyword_args)
    if response:
        await client.respond(event, response)

    tlog.info('Ran `tag` in `%s`. Response: %s', chat.title, response)