import imaplib
import inspect
import logging
+import os
import pydoc
import re
import sys
+import time
import yaml
_LOGLEVELS = [logging.CRITICAL, logging.ERROR, logging.WARNING,
and set them in rule actions.
"""
- def __init__(self, config, loglevel=3):
+ def __init__(self, config, loglevel=3, password=None, update_logger=True):
"""
Create a Imapami object.
"port": None,
"ssl": True,
"login": None,
- "password": None,
+ "password": password,
"logfile": None,
"loglevel": 3,
"inbox": "INBOX",
self.variables = {}
self.logger = self._get_logger(loglevel)
self._load_config(config)
- self._update_logger()
+ if update_logger:
+ self._update_logger()
self.uidnext = {}
def _get_logger(self, loglevel):
password = self.config.get("password")
if password is None:
password = getpass.getpass()
+ self.config["password"] = password
server = self.config.get("server")
port = self.config.get("port")
if port is None:
parser.add_argument(
'-H', '--config-help', dest='config_help', action='store_true',
default=False, help='Show help about configuration file')
+ parser.add_argument(
+ '-r', '--refresh',
+ metavar='secs',
+ help='Call in a loop, with a pause of <secs> seconds.',
+ type=int)
parser.add_argument(
'-d', '--debug',
help='Console debug level, from 0 (no output) to 4 (debug).',
parser.print_help()
sys.exit(1)
- p = Imapami(open(args.config).read(), args.debug)
+ password = os.getenv("IMAPAMI_PASSWORD")
+ p = Imapami(open(args.config).read(), args.debug, password=password)
if args.check == True:
sys.stderr.write('Configuration parsing ok\n')
sys.exit(0)
- p.connect()
- p.get_uidnext()
- p.process_rules()
- p.close()
+ # XXX does not work :(
+ if args.refresh:
+ while True:
+ try:
+ p.connect()
+ p.get_uidnext()
+ p.process_rules()
+ p.close()
+ except:
+ p.logger.error("Exception during processing", exc_info=True)
+ time.sleep(args.refresh)
+ # XXX why needed?
+ p = Imapami(open(args.config).read(), args.debug,
+ password=p.config.get('password'), update_logger=False)
+ else:
+ p.connect()
+ p.get_uidnext()
+ p.process_rules()
+ p.close()
if __name__ == '__main__':
main()