#!/usr/bin/python import sys sys.path.append('/usr/local/share/mantis-smtp') sys.path.append('/usr/share/mantis-smtp') import MantisSMTPServer import logging from optparse import OptionParser import simplejson as json parser = OptionParser() parser.add_option('-v', '--verbose', action="store_true", dest="verbose", help="Enable verbose mode") parser.add_option('-d', '--daemon', action="store_true", dest="daemon", help="Start in daemon mode") parser.add_option('--pid', action="store", type="string", dest="pidfile", help="The logfile path", default=None) parser.add_option('-c', '--configfile', action="store", type="string", dest="configfile", help="The logfile path", default=None) parser.add_option('-l', '--logfile', action="store", type="string", dest="logfile", help="The logfile path", default=None) parser.add_option('-H', '--address', action="store", type="string", dest="bind_address", help="The bind address", default="127.0.0.1") parser.add_option('-p', '--port', action="store", type="int", dest="port", help="The bind port", default=1025) (options, args) = parser.parse_args() logformat = '%(asctime)s - MantisMailServer - %(levelname)s - %(message)s' if options.verbose: loglevel = logging.DEBUG else: loglevel = logging.INFO if options.logfile: logging.basicConfig(filename=options.logfile,level=loglevel,format=logformat) else: logging.basicConfig(level=loglevel,format=logformat) server = MantisSMTPServer.MantisSMTPServer((options.bind_address, options.port), None) if options.daemon and options.pidfile is None: logging.error("Can't daemonize without pid file parameter (--pid)") sys.exit(3) if options.configfile: try: fd=open(options.configfile,'rb') config_text=fd.read() fd.close() except: logging.error('Error reading config file %s.' % options.configfile) sys.exit(1) try: config=json.loads(config_text) except: logging.error('Error parsing config file %s.' % options.configfile) sys.exit(2) try: for param in ['mail_from','ws_url','ws_user','ws_pwd','ws_default_category']: if param in config: server.set_option(param,config[param]) if 'projects' in config: for project in config['projects']: if 'project_name' not in config['projects'][project]: logging.warning('Project %s in config file does not have "project_name" parameter. Pass.' % project) continue if 'category' in config['projects'][project]: server.add_project(project,config['projects'][project]['project_name'],category=config['projects'][project]['category']) else: server.add_project(project,config['projects'][project]['project_name']) except: logging.error('Error loading config parameters from %s' % options.configfile) sys.exit(3) else: logging.warning('No config file supply. Running with default configuration...') try: if options.daemon: server.startDaemon(options.pidfile) else: server.start() except KeyboardInterrupt: server.stop()