#!/opt/datacelco/bin/python
# -*- coding: UTF-8 -*-
'''
    SIPCheck is a tool that parse the file /var/log/asterisk/messages
    looking for error and warnings messages from unauthorized access or
    trying calls from anonymous users and add their ip to the firewall
    and warns to sinologic server to add for global list of banned ip.
'''

import logging
import argparse
import time
import sys

from sipcheck.sipcheck import SIPCheck

if __name__ == '__main__':
    logger = logging.getLogger('sipcheck')
    try:
        parser = argparse.ArgumentParser(description='SIPCheck')
        parser.add_argument('-c', action="store", dest="config_file", default="/etc/sipcheck.conf")
        try:
            with open("/etc/sipcheck.conf"):
                pass
        except IOError:
            if len(sys.argv)==1:
                parser.print_help()
                sys.exit(1)
        cargs = parser.parse_args()
        server = SIPCheck(cargs.config_file)
        server.start()

        while True:
            time.sleep(1)

    except KeyboardInterrupt:
        server.quit()
        server.join()
        logger.debug("Keyboard stopped detected...")
    finally:
        logger.info("SIPCheck Stopped")
