Esempio n. 1
0
def get_messages_by_subscription(session, subscription):
    all_tags = list_tags()
    messages = {'all': set(), 'new': set()}
    messages_bytag = {}
    messages_read = session.user.get('readmsgs', set())

    # now occlude all private messages :)
    all_private = list_privmsgs(None)

    # this looks like perl code
    for tag_pattern in subscription:
        messages_bytag[tag_pattern] = collections.defaultdict(set)
        for tag_match in fnmatch.filter(all_tags, tag_pattern):
            msg_indicies = list_msgs(tags=(tag_match, ))
            messages['all'].update(msg_indicies - all_private)
            messages_bytag[tag_pattern]['all'].update(msg_indicies -
                                                      all_private)
        messages_bytag[tag_pattern]['new'] = (
            messages_bytag[tag_pattern]['all'] - messages_read)

    # and make a list of only our own
    messages['private'] = list_privmsgs(session.user.handle)

    # and calculate 'new' messages
    messages['new'] = (messages['all'] | messages['private']) - messages_read

    return messages, messages_bytag
Esempio n. 2
0
def get_messages_by_subscription(session, subscription):
    all_tags = list_tags()
    messages = {'all': set(), 'new': set()}
    messages_bytag = {}
    messages_read = session.user.get('readmsgs', set())

    # this looks like perl code
    for tag_pattern in subscription:
        messages_bytag[tag_pattern] = collections.defaultdict(set)
        for tag_match in fnmatch.filter(all_tags, tag_pattern):
            msg_indicies = list_msgs(tags=(tag_match,))
            messages['all'].update(msg_indicies)
            messages_bytag[tag_pattern]['all'].update(msg_indicies)
        messages_bytag[tag_pattern]['new'] = (
            messages_bytag[tag_pattern]['all'] - messages_read)

    # now occlude all private messages :)
    all_private = list_privmsgs(None)
    messages['new'] -= all_private
    messages['all'] -= all_private

    # and make a list of only our own
    messages['private'] = list_privmsgs(session.user.handle)

    # and calculate 'new' messages
    messages['new'] = (messages['all'] | messages['private']) - messages_read

    return messages, messages_bytag
Esempio n. 3
0
def do_describe_available_tags(term, colors):
    sorted_tags = sorted([(len(list_msgs(tags=(tag,))), tag) for tag in list_tags() or [u"public"]], reverse=True)
    decorated_tags = [
        colors["text"](tag) + colors["lowlight"]("({0})".format(num_msgs)) for num_msgs, tag in sorted_tags
    ]

    description = u"".join(
        (colors["highlight"](u"available tags"), ": ", colors["text"](u", ").join(decorated_tags), colors["text"](u"."))
    )
    return show_description(term, description, color=None)
Esempio n. 4
0
def do_describe_available_tags(term, colors):
    sorted_tags = sorted([(len(list_msgs(tags=(tag,))), tag)
                          for tag in list_tags() or [u'public']
                          ], reverse=True)
    decorated_tags = [
        colors['text'](tag) +
        colors['lowlight']('({0})'.format(num_msgs))
        for num_msgs, tag in sorted_tags]

    description = u''.join((
        colors['highlight'](u'available tags'), ': ',
        colors['text'](u', ').join(decorated_tags),
        colors['text'](u'.'),
    ))
    return show_description(term, description, color=None)
Esempio n. 5
0
def do_describe_available_tags(term, colors):
    sorted_tags = sorted([(len(list_msgs(tags=(tag, ))), tag)
                          for tag in list_tags() or [u'public']],
                         reverse=True)
    decorated_tags = [
        colors['text'](tag) + colors['lowlight']('({0})'.format(num_msgs))
        for num_msgs, tag in sorted_tags
    ]

    description = u''.join((
        colors['highlight'](u'available tags'),
        ': ',
        colors['text'](u', ').join(decorated_tags),
        colors['text'](u'.'),
    ))
    return show_description(term, description, color=None)
Esempio n. 6
0
def msg_filter(msgs):
    """
        filter all matching messages. userland implementation
        of private/public messaging by using the 'tags' database.
        'new', or unread messages are marked by idx matching
        session.user['readmsgs'] when read. Finally, implement 'group'
        tagging, so that users of group 'impure' are allowed to read
        messages tagged by 'impure', regardless of recipient or 'public'.

        returns (msgs), (new). new contains redundant msgs
    """
    # pylint: disable=R0914,R0912,R0915
    #         Too many local variables
    #         Too many branches
    #         Too many statements
    from x84.bbs import list_msgs, echo, getsession, getterminal, get_msg, Ansi
    session, term = getsession(), getterminal()
    public_msgs = list_msgs(('public',))
    addressed_to = 0
    addressed_grp = 0
    filtered = 0
    deleted = 0
    private = 0
    public = 0
    new = set()
    echo(u' Processing ' + term.reverse_yellow('..'))
    for msg_id in msgs.copy():
        if msg_id in public_msgs:
            # can always ready msgs tagged with 'public'
            public += 1
        else:
            private += 1
        msg = get_msg(msg_id)
        if msg.recipient == session.user.handle:
            addressed_to += 1
        else:
            # a system of my own, by creating groups
            # with the same as tagged messages, you may
            # create private or intra-group messages.
            tag_matches_group = False
            for tag in msg.tags:
                if tag in session.user.groups:
                    tag_matches_group = True
                    break
            if tag_matches_group:
                addressed_grp += 1
            elif msg_id not in public_msgs:
                # denied to read this message
                if FILTER_PRIVATE:
                    msgs.remove(msg_id)
                    filtered += 1
                    continue
            elif msg_id in DELETED:
                msgs.remove(msg_id)
                deleted += 1
        if msg_id not in ALREADY_READ:
            new.add(msg_id)

    if 0 == len(msgs):
        echo(u'\r\n\r\nNo messages (%s filtered).' % (filtered,))
    else:
        txt_out = list()
        if addressed_to > 0:
            txt_out.append('%s addressed to you' % (
                term.bold_yellow(str(addressed_to)),))
        if addressed_grp > 0:
            txt_out.append('%s addressed by group' % (
                term.bold_yellow(str(addressed_grp)),))
        if filtered > 0:
            txt_out.append('%s filtered' % (
                term.bold_yellow(str(filtered)),))
        if deleted > 0:
            txt_out.append('%s deleted' % (
                term.bold_yellow(str(deleted)),))
        if public > 0:
            txt_out.append('%s public' % (
                term.bold_yellow(str(public)),))
        if private > 0:
            txt_out.append('%s private' % (
                term.bold_yellow(str(private)),))
        if len(new) > 0:
            txt_out.append('%s new' % (
                term.bold_yellow(str(len(new),)),))
        if 0 != len(txt_out):
            echo(u'\r\n\r\n' + Ansi(
                u', '.join(txt_out) + u'.').wrap(term.width - 2))
    return msgs, new
Esempio n. 7
0
def main(autoscan_tags=None):
    """ Main procedure. """
    # pylint: disable=W0603,R0912
    #         Using the global statement
    #         Too many branches
    from x84.bbs import getsession, getterminal, echo, getch
    from x84.bbs import list_msgs
    session, term = getsession(), getterminal()
    session.activity = 'autoscan msgs'
    echo(banner())
    global ALREADY_READ, SEARCH_TAGS, DELETED
    if autoscan_tags is not None:
        SEARCH_TAGS = autoscan_tags
        echo(u''.join((
            term.bold_black('[ '),
            term.yellow('AUtOSCAN'),
            term.bold_black(' ]'), u'\r\n')))
    else:
        SEARCH_TAGS = set(['public'])
        # also throw in user groups, maybe the top 3 .. ?
        SEARCH_TAGS.update(session.user.groups)
        SEARCH_TAGS = prompt_tags(SEARCH_TAGS)
        # user escape
        if SEARCH_TAGS is None:
            return

    echo(u'\r\n\r\n%s%s ' % (
        term.bold_yellow('SCANNiNG'),
        term.bold_black(':'),))
    echo(u','.join([term.red(tag) for tag in SEARCH_TAGS]
                   if 0 != len(SEARCH_TAGS) else ['<All>', ]))

    if (SEARCH_TAGS != session.user.get('autoscan', None)):
        echo(u'\r\n\r\nSave tag list as autoscan on login [yn] ?\b\b')
        while True:
            inp = getch()
            if inp in (u'q', 'Q', unichr(27), u'n', u'N'):
                break
            elif inp in (u'y', u'Y'):
                session.user['autoscan'] = SEARCH_TAGS
                break

    # retrieve all matching messages,
    all_msgs = list_msgs(SEARCH_TAGS)
    echo(u'\r\n\r\n%s messages.' % (term.yellow_reverse(str(len(all_msgs),))))
    if 0 == len(all_msgs):
        getch(0.5)
        return

    # filter messages public/private/group-tag/new
    ALREADY_READ = session.user.get('readmsgs', set())
    DELETED = session.user.get('trash', set())
    msgs, new = msg_filter(all_msgs)
    if 0 == len(msgs) and 0 == len(new):
        getch(0.5)
        return

    # prompt read 'a'll, 'n'ew, or 'q'uit
    echo(u'\r\n  REAd [%s]ll %d%s message%s [qa%s] ?\b\b' % (
        term.yellow_underline(u'a'),
        len(msgs), (
        u' or %d [%s]EW ' % (
        len(new), term.yellow_underline(u'n'),)
            if new else u''),
        u's' if 1 != len(msgs) else u'',
        u'n' if new else u'',))
    while True:
        inp = getch()
        if inp in (u'q', 'Q', unichr(27)):
            return
        elif inp in (u'n', u'N') and len(new):
            # read only new messages
            msgs = new
            break
        elif inp in (u'a', u'A'):
            break

    # read target messages
    read_messages(msgs, new)
Esempio n. 8
0
def msg_filter(msgs):
    """
        filter all matching messages. userland implementation
        of private/public messaging by using the 'tags' database.
        'new', or unread messages are marked by idx matching
        session.user['readmsgs'] when read. Finally, implement 'group'
        tagging, so that users of group 'impure' are allowed to read
        messages tagged by 'impure', regardless of recipient or 'public'.

        returns (msgs), (new). new contains redundant msgs
    """
    # pylint: disable=R0914,R0912,R0915
    #         Too many local variables
    #         Too many branches
    #         Too many statements
    from x84.bbs import list_msgs, echo, getsession, getterminal, get_msg
    session, term = getsession(), getterminal()
    public_msgs = list_msgs(('public', ))
    addressed_to = 0
    addressed_grp = 0
    filtered = 0
    deleted = 0
    private = 0
    public = 0
    new = set()
    echo(u' Processing ' + term.reverse_yellow('..'))
    for msg_id in msgs.copy():
        if msg_id in public_msgs:
            # can always ready msgs tagged with 'public'
            public += 1
        else:
            private += 1
        msg = get_msg(msg_id)
        if msg.recipient == session.user.handle:
            addressed_to += 1
        else:
            # a system of my own, by creating groups
            # with the same as tagged messages, you may
            # create private or intra-group messages.
            tag_matches_group = False
            for tag in msg.tags:
                if tag in session.user.groups:
                    tag_matches_group = True
                    break
            if tag_matches_group:
                addressed_grp += 1
            elif msg_id not in public_msgs:
                # denied to read this message
                if FILTER_PRIVATE:
                    msgs.remove(msg_id)
                    filtered += 1
                    continue
            elif msg_id in DELETED:
                msgs.remove(msg_id)
                deleted += 1
        if msg_id not in ALREADY_READ:
            new.add(msg_id)

    if 0 == len(msgs):
        echo(u'\r\n\r\nNo messages (%s filtered).' % (filtered, ))
    else:
        txt_out = list()
        if addressed_to > 0:
            txt_out.append('%s addressed to you' %
                           (term.bold_yellow(str(addressed_to)), ))
        if addressed_grp > 0:
            txt_out.append('%s addressed by group' %
                           (term.bold_yellow(str(addressed_grp)), ))
        if filtered > 0:
            txt_out.append('%s filtered' % (term.bold_yellow(str(filtered)), ))
        if deleted > 0:
            txt_out.append('%s deleted' % (term.bold_yellow(str(deleted)), ))
        if public > 0:
            txt_out.append('%s public' % (term.bold_yellow(str(public)), ))
        if private > 0:
            txt_out.append('%s private' % (term.bold_yellow(str(private)), ))
        if len(new) > 0:
            txt_out.append('%s new' % (term.bold_yellow(str(len(new), )), ))
        if 0 != len(txt_out):
            echo(u'\r\n\r\n' + u'\r\n'.join(
                term.wrap(u', '.join(txt_out) + u'.', (term.width - 2))))
    return msgs, new
Esempio n. 9
0
def main(autoscan_tags=None):
    """ Main procedure. """
    # pylint: disable=W0603,R0912
    #         Using the global statement
    #         Too many branches
    from x84.bbs import getsession, getterminal, echo, getch
    from x84.bbs import list_msgs
    session, term = getsession(), getterminal()
    session.activity = 'autoscan msgs'
    echo(banner())
    global ALREADY_READ, SEARCH_TAGS, DELETED
    if autoscan_tags is not None:
        SEARCH_TAGS = autoscan_tags
        echo(u''.join((term.bold_black('[ '), term.yellow('AUtOSCAN'),
                       term.bold_black(' ]'), u'\r\n')))
    else:
        SEARCH_TAGS = set(['public'])
        # also throw in user groups, maybe the top 3 .. ?
        SEARCH_TAGS.update(session.user.groups)
        SEARCH_TAGS = prompt_tags(SEARCH_TAGS)
        # user escape
        if SEARCH_TAGS is None:
            return

    echo(u'\r\n\r\n%s%s ' % (
        term.bold_yellow('SCANNiNG'),
        term.bold_black(':'),
    ))
    echo(u','.join([term.red(tag)
                    for tag in SEARCH_TAGS] if 0 != len(SEARCH_TAGS) else [
                        '<All>',
                    ]))

    if (SEARCH_TAGS != session.user.get('autoscan', None)):
        echo(u'\r\n\r\nSave tag list as autoscan on login [yn] ?\b\b')
        while True:
            inp = getch()
            if inp in (u'q', 'Q', unichr(27), u'n', u'N'):
                break
            elif inp in (u'y', u'Y'):
                session.user['autoscan'] = SEARCH_TAGS
                break

    # retrieve all matching messages,
    all_msgs = list_msgs(SEARCH_TAGS)
    echo(u'\r\n\r\n%s messages.' % (term.yellow_reverse(str(len(all_msgs), ))))
    if 0 == len(all_msgs):
        getch(0.5)
        return

    # filter messages public/private/group-tag/new
    ALREADY_READ = session.user.get('readmsgs', set())
    DELETED = session.user.get('trash', set())
    msgs, new = msg_filter(all_msgs)
    if 0 == len(msgs) and 0 == len(new):
        getch(0.5)
        return

    # prompt read 'a'll, 'n'ew, or 'q'uit
    echo(u'\r\n  REAd [%s]ll %d%s message%s [qa%s] ?\b\b' % (
        term.yellow_underline(u'a'),
        len(msgs),
        (u' or %d [%s]EW ' % (
            len(new),
            term.yellow_underline(u'n'),
        ) if new else u''),
        u's' if 1 != len(msgs) else u'',
        u'n' if new else u'',
    ))
    while True:
        inp = getch()
        if inp in (u'q', 'Q', unichr(27)):
            return
        elif inp in (u'n', u'N') and len(new):
            # read only new messages
            msgs = new
            break
        elif inp in (u'a', u'A'):
            break

    # read target messages
    read_messages(msgs, new)
Esempio n. 10
0
File: feeds.py Progetto: hick/x84
def main(autoscan_tags=None):
    """ Main procedure. """
    # pylint: disable=W0603,R0912
    #         Using the global statement
    #         Too many branches
    from x84.bbs import getsession, getterminal, echo, getch
    from x84.bbs import list_msgs
    session, term = getsession(), getterminal()
    session.activity = 'autoscan msgs'

    ### 尝试
    session.log.info("Hick3")
    ### 获取所有 tag
    tagdb = DBProxy('tags')
    all_tags = sorted(tagdb.items())
    session.log.info(all_tags)
    ### 尝试直接调出显示的 message ,第二个参数应该是标识为未读的
    msg = new = [1, 2, 3, 4,5 ,6, 7, 8,9]
    read_messages(msg, new)
    return

    ### 首先是显示提示输入的 tag 的标签
    echo(banner())
    global ALREADY_READ, SEARCH_TAGS, DELETED
    if autoscan_tags is not None:
        SEARCH_TAGS = autoscan_tags
        echo(u''.join((
            term.bold_black('[ '),
            term.yellow('AUtOSCAN'),
            term.bold_black(' ]'), u'\r\n')))
    ### 默认就往这里了, 提示输入 tag
    else:
        # 默认 tag 为 public
        SEARCH_TAGS = set(['hick3'])
        # also throw in user groups, maybe the top 3 .. ?
        SEARCH_TAGS.update(session.user.groups)
        SEARCH_TAGS = prompt_tags(SEARCH_TAGS)
        # user escape
        if SEARCH_TAGS is None:
            return

    echo(u'\r\n\r\n%s%s ' % (
        term.bold_yellow('SCANNiNG'),
        term.bold_black(':'),))
    echo(u','.join([term.red(tag) for tag in SEARCH_TAGS]
                   if 0 != len(SEARCH_TAGS) else ['<All>', ]))

    ### 直到有选择 tags , 保存到 session.user 中
    if (SEARCH_TAGS != session.user.get('autoscan', None)):
        echo(u'\r\n\r\nSave tag list as autoscan on login [yn] ?\b\b')
        while True:
            inp = getch()
            if inp in (u'q', 'Q', unichr(27), u'n', u'N'):
                break
            elif inp in (u'y', u'Y'):
                session.user['autoscan'] = SEARCH_TAGS
                break

    # retrieve all matching messages,:  list_msgs 根据 tags 获得所有记录,看情形这个信息量大了有问题哈
    all_msgs = list_msgs(SEARCH_TAGS)
    echo(u'\r\n\r\n%s messages.' % (term.yellow_reverse(str(len(all_msgs),))))
    if 0 == len(all_msgs):
        getch(0.5)
        return

    # filter messages public/private/group-tag/new
    ### 分 tag 和是否未读,删除等统计
    ALREADY_READ = session.user.get('readmsgs', set())
    DELETED = session.user.get('trash', set())
    msgs, new = msg_filter(all_msgs)
    if 0 == len(msgs) and 0 == len(new):
        getch(0.5)
        return

    # prompt read 'a'll, 'n'ew, or 'q'uit
    echo(u'\r\n  REAd [%s]ll %d%s message%s [qa%s] ?\b\b' % (
        term.yellow_underline(u'a'),
        len(msgs), (
        u' or %d [%s]EW ' % (
        len(new), term.yellow_underline(u'n'),)
            if new else u''),
        u's' if 1 != len(msgs) else u'',
        u'n' if new else u'',))
    while True:
        inp = getch()
        if inp in (u'q', 'Q', unichr(27)):
            return
        elif inp in (u'n', u'N') and len(new):
            # read only new messages
            msgs = new
            break
        elif inp in (u'a', u'A'):
            break

    # 根君上面的用户选择,读取消息, 某次记录 log  msgs 和 new 都是帖子 id 的 set 
    # read target messages
    # session.log.info(msgs)
    # session.log.info(new)
    read_messages(msgs, new)
Esempio n. 11
0
def main(autoscan_tags=None):
    """ Main procedure. """
    # pylint: disable=W0603,R0912
    #         Using the global statement
    #         Too many branches
    from x84.bbs import getsession, getterminal, echo, getch
    from x84.bbs import list_msgs
    session, term = getsession(), getterminal()
    session.activity = 'autoscan msgs'

    ### 尝试
    session.log.info("Hick3")
    ### 获取所有 tag
    tagdb = DBProxy('tags')
    all_tags = sorted(tagdb.items())
    session.log.info(all_tags)
    ### 尝试直接调出显示的 message ,第二个参数应该是标识为未读的
    msg = new = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    read_messages(msg, new)
    return

    ### 首先是显示提示输入的 tag 的标签
    echo(banner())
    global ALREADY_READ, SEARCH_TAGS, DELETED
    if autoscan_tags is not None:
        SEARCH_TAGS = autoscan_tags
        echo(u''.join((term.bold_black('[ '), term.yellow('AUtOSCAN'),
                       term.bold_black(' ]'), u'\r\n')))
    ### 默认就往这里了, 提示输入 tag
    else:
        # 默认 tag 为 public
        SEARCH_TAGS = set(['hick3'])
        # also throw in user groups, maybe the top 3 .. ?
        SEARCH_TAGS.update(session.user.groups)
        SEARCH_TAGS = prompt_tags(SEARCH_TAGS)
        # user escape
        if SEARCH_TAGS is None:
            return

    echo(u'\r\n\r\n%s%s ' % (
        term.bold_yellow('SCANNiNG'),
        term.bold_black(':'),
    ))
    echo(u','.join([term.red(tag)
                    for tag in SEARCH_TAGS] if 0 != len(SEARCH_TAGS) else [
                        '<All>',
                    ]))

    ### 直到有选择 tags , 保存到 session.user 中
    if (SEARCH_TAGS != session.user.get('autoscan', None)):
        echo(u'\r\n\r\nSave tag list as autoscan on login [yn] ?\b\b')
        while True:
            inp = getch()
            if inp in (u'q', 'Q', unichr(27), u'n', u'N'):
                break
            elif inp in (u'y', u'Y'):
                session.user['autoscan'] = SEARCH_TAGS
                break

    # retrieve all matching messages,:  list_msgs 根据 tags 获得所有记录,看情形这个信息量大了有问题哈
    all_msgs = list_msgs(SEARCH_TAGS)
    echo(u'\r\n\r\n%s messages.' % (term.yellow_reverse(str(len(all_msgs), ))))
    if 0 == len(all_msgs):
        getch(0.5)
        return

    # filter messages public/private/group-tag/new
    ### 分 tag 和是否未读,删除等统计
    ALREADY_READ = session.user.get('readmsgs', set())
    DELETED = session.user.get('trash', set())
    msgs, new = msg_filter(all_msgs)
    if 0 == len(msgs) and 0 == len(new):
        getch(0.5)
        return

    # prompt read 'a'll, 'n'ew, or 'q'uit
    echo(u'\r\n  REAd [%s]ll %d%s message%s [qa%s] ?\b\b' % (
        term.yellow_underline(u'a'),
        len(msgs),
        (u' or %d [%s]EW ' % (
            len(new),
            term.yellow_underline(u'n'),
        ) if new else u''),
        u's' if 1 != len(msgs) else u'',
        u'n' if new else u'',
    ))
    while True:
        inp = getch()
        if inp in (u'q', 'Q', unichr(27)):
            return
        elif inp in (u'n', u'N') and len(new):
            # read only new messages
            msgs = new
            break
        elif inp in (u'a', u'A'):
            break

    # 根君上面的用户选择,读取消息, 某次记录 log  msgs 和 new 都是帖子 id 的 set
    # read target messages
    # session.log.info(msgs)
    # session.log.info(new)
    read_messages(msgs, new)