#!/usr/bin/env python3
import argparse
import asyncio
import logging
import os
import sys

import requests
import setproctitle

import infernoshout


@asyncio.coroutine
def recv_loop(shoutbox):
    while True:
        yield from asyncio.sleep(5)
        try:
            shoutbox.update()
            logging.info("%d active users" % shoutbox.active_users)
            for i in shoutbox.buf.pop_all():
                print(i, flush=True)
        except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) as e:
            logging.warn("connection error: %s" % e)


def handle_input(shoutbox):
    i = sys.stdin.readline().rstrip("\r\n")
    if not i:  # EOF
        exit()

    shoutbox.send(i)


def main():
    logging.basicConfig(level=logging.INFO)

    os.environ["SPT_NOENV"] = "true"
    setproctitle.setproctitle(sys.argv[0])

    parser = argparse.ArgumentParser(description="Command line feed for Inferno Shoutbox")
    parser.add_argument("-b", "--backlog", action="store_true", help="Display the backlog after connecting")
    parser.add_argument("-n", "--no-input", action="store_true", help="Disable sending messages via stdin")
    parser.add_argument("-A", "--disable-anus", action="store_true", help="Don't use the \"openanus\" feature (last activity timestamp, broken on some forums)")
    parser.add_argument("--user-agent", action="store", default=None)
    parser.add_argument("url", help="Base URL of the forum")
    parser.add_argument("cookies", help="Cookies in the standard Cookie header format (RFC 6265, section 4.1.1)")
    args = parser.parse_args()

    s = infernoshout.Shoutbox(args.url, infernoshout.utils.dict_from_cookie_str(args.cookies), use_anus=not args.disable_anus, ua=args.user_agent)

    s.update()
    if args.backlog:
        for i in s.buf.pop_all():
            print(i, flush=True)
    else:
        s.buf.pop_all()

    loop = asyncio.get_event_loop()

    if not args.no_input:
        loop.add_reader(sys.stdin, lambda: handle_input(s))

    loop.run_until_complete(recv_loop(s))

if __name__ == "__main__":
    main()
