コード例 #1
0
ファイル: fomofetch.py プロジェクト: wmodes/fomophobia
def options_and_logging():
    """Handle command line options and start logging
    Args:
        None
    """
    from optparse import OptionParser
    parser = OptionParser(version="%%prog v%s" % __version__,
            usage="%prog [options] <argument> ...",
            description=__doc__.replace('\r\n', '\n').split('\n--snip--\n')[0])
    parser.add_option('-v', '--verbose', action="count", dest="verbose",
        default=2, help="Increase the verbosity. Use twice for extra effect")
    parser.add_option('-q', '--quiet', action="count", dest="quiet",
        default=0, help="Decrease the verbosity. Use twice for extra effect")
    #Reminder: %default can be used in help strings.
 
    # Allow pre-formatted descriptions
    parser.formatter.format_description = lambda description: description
 
    opts, args  = parser.parse_args()

    # Set up clean logging to stderr
    log_levels = [logging.CRITICAL, logging.ERROR, logging.WARNING,
                  logging.INFO, logging.DEBUG]
    opts.verbose = min(opts.verbose - opts.quiet, len(log_levels) - 1)
    opts.verbose = max(opts.verbose, 0)
    logging.basicConfig(level=log_levels[opts.verbose],
                        format='%(levelname)s: %(message)s')
コード例 #2
0
def jobParseArgs(args):
    parser = OptionParser()
    parser.add_option("-j", dest="jobid", type="str")
    (opts, args) = parser.parse_args(args)

    opts = parseTaskFile(opts.jobid)
    return opts
コード例 #3
0
ファイル: task.py プロジェクト: HalasNet/felix
def jobParseArgs (args):
  parser = OptionParser()
  parser.add_option("-j", dest="jobid", type="str")
  (opts, args) = parser.parse_args(args)

  opts = parseTaskFile(opts.jobid)
  return opts
コード例 #4
0
ファイル: ss.py プロジェクト: thecnology/silly-server
def parse_args():
    description='Silly Server for mocking real http servers'
    options = [
        {
            "dest": "root_dir",
            "required": False,
            "metavar": "/dir/somedir",
            "help": """Directory where your fake responses are waiting for me.
            If not provided - default response will be used everywhere.""",
            "type": str,
            "key": "-d",
        }, 
        {
            "dest": "port",
            "required": False,
            "metavar": "port",
            "help": "Port to listen on. Default is 8000.",
            "type": int,
            "key": "-p"
        }
    ]
    try:
        import argparse
        parser = argparse.ArgumentParser(description=description)
        for o in options:
            parser.add_argument(o["key"], dest=o["dest"], required=o["required"], metavar=o["metavar"],
                                help=o["help"], type=o["type"])
        return vars(parser.parse_args())
    except ImportError:
        import optparse
        parser = optparse.OptionParser(description=description)
        for o in options:
            parser.add_option(o["key"], dest=o["dest"], metavar=o["metavar"],
                              help=o["help"], type=o["type"])
        return vars(parser.parse_args()[0])
コード例 #5
0
ファイル: analyze.py プロジェクト: notnoop/junkmail
def parse_arguments():
    from optparse import OptionParser
    parser = OptionParser('usage: %prog [options] db-file')
    parser.add_option("-v", "--verbose", dest="verbose",
            default=False, help="verbose")
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error('db-file argument is required')

    return {'file': args[0], 'verbose':options.verbose}
コード例 #6
0
ファイル: downloadmail.py プロジェクト: notnoop/junkmail
def parse_arguments():
    from optparse import OptionParser
    import getpass
    parser = OptionParser('usage: %prog [options] gmail-username')
    parser.add_option("-f", "--file", dest="file", default="emails.sql",
            help="destination database file [default: %default]")
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error('email argument is required')
    password = getpass.getpass('Password: '******'email':args[0], 'password':password, 'file':options.file}
コード例 #7
0
ファイル: imap_client.py プロジェクト: jpierce42/firetower
def main():
    parser = OptionParser(usage='usage: firetower options args')
    parser.add_option(
        '-c', '--conf', action='store', dest='conf_path',
         help='Path to YAML configuration file.')
    (options, args) = parser.parse_args()
    conf = config.Config(options.conf_path)

    conn = imaplib.IMAP4_SSL(conf.imap_host)
    imap_user = conf.imap_user

    queue = Redis(host=conf.redis_host, port=conf.redis_port)

    processed = 0

    print "Enter email password"
    imap_password = getpass.getpass()

    conn.login(imap_user, imap_password)
    conn.select("Inbox")

    while True:
        _, msg_ids = conn.search(None, "(ALL)")
        msg_ids = msg_ids[0].split()
        if len(msg_ids) == 0:
            time.sleep(0.5)
            continue
        print "Processing %s messages" %len(msg_ids)

        _, msgs = conn.fetch(",".join(msg_ids), '(BODY[])')
        for msg in msgs:
            if not msg or msg == ")":
                continue
            msg_obj = email.message_from_string(msg[1])
            ft_dict = {}
            ft_dict['hostname'] = msg_obj['Received'].split(' ')[1] if msg_obj['Received'] else '????'
            ft_dict['sig'] = msg_obj.get_payload() or '????'
            ft_dict['date'] = msg_obj["Date"]
            ft_dict['programname'] = 'Maildir Util'

            # We don't huge signatures clogging the classification
            if len(ft_dict['sig']) < 10000 and isinstance(ft_dict['sig'], str):
                queue.push(conf.queue_key, json.dumps(ft_dict))
            processed += 1
            if not processed % 100:
                print "Processed: %s" %processed
        print "Processed: %s" %processed

        for msg_id in msg_ids:
            conn.store(msg_id, '+FLAGS', '\\Deleted')

        conn.expunge()
コード例 #8
0
def main(argv):
    parser = OptionParser()
    parser.add_option("-b",
                      "--base",
                      dest="base_url",
                      help="specify base Fossil URL",
                      metavar="URL")
    parser.add_option("-R",
                      "--repository",
                      dest="repo",
                      help="Fossil repository to add ticket to",
                      metavar="FILE")

    global options, args
    (options, args) = parser.parse_args()

    if not options.base_url:
        parser.error("Base URL not given.")
    elif not options.repo:
        parser.error("Repository path not given.")

    if len(argv) < 2:
        print("Usage: {0} repository".format(argv[0]))
        return False

    msg = parse_message(sys.stdin.read())

    if not msg:
        print("Unable to parse message.", file=sys.stderr)
        return False
    elif not msg["id"]:
        print("No Message-Id, ignoring.", file=sys.stderr)
        return False

    cache = Cache()
    if msg["id"] in cache:
        print("Message {0} already was processed.".format(msg["id"]),
              file=sys.stderr)
        return True

    uuid = handle_message(msg)

    cache[msg["id"]] = uuid
    cache.save()

    return True
コード例 #9
0
ファイル: phccis_bridge.py プロジェクト: coderunner4/icardea
def parse_options():
    parser = OptionParser(usage = "usage: %prog [options]")
    parser.set_defaults(port = LISTEN_PORT, timeout = CHK_TIMEOUT,
            pcc_endpoint = PCC_ENDPOINT, xds_endpoint = XDS_ENDPOINT)
    parser.add_option("-p", action="store", dest="port", type="int",
                      help='The TCP port to listen to for the HTTP interface. Default: %s'% LISTEN_PORT)
    parser.add_option("-x", action="store", dest="xds_endpoint", 
                      help='The server endpoint for sending the XDS (Provide & Register) messages. Default: %s'% XDS_ENDPOINT)
    parser.add_option("-c", action="store", dest="pcc_endpoint", 
                      help='The server endpoint for sending the PCC-9 messages. Default: %s'% PCC_ENDPOINT)
    parser.add_option("-t", action="store", dest="timeout", type="int",
                      help='Timeout in seconds to be used for the periodic check. Default: %s (i.e. %s minutes).  Minimum: 5 seconds.'%
                      (CHK_TIMEOUT, CHK_TIMEOUT/60))
    (options, args) = parser.parse_args()
    if options.timeout < 5:
        options.timeout = 5
    return options
コード例 #10
0
def parse_options():
    parser = OptionParser(usage="usage: %prog [options]")
    parser.set_defaults(port=LISTEN_PORT,
                        timeout=CHK_TIMEOUT,
                        pcc_endpoint=PCC_ENDPOINT,
                        xds_endpoint=XDS_ENDPOINT)
    parser.add_option(
        "-p",
        action="store",
        dest="port",
        type="int",
        help='The TCP port to listen to for the HTTP interface. Default: %s' %
        LISTEN_PORT)
    parser.add_option(
        "-x",
        action="store",
        dest="xds_endpoint",
        help=
        'The server endpoint for sending the XDS (Provide & Register) messages. Default: %s'
        % XDS_ENDPOINT)
    parser.add_option(
        "-c",
        action="store",
        dest="pcc_endpoint",
        help='The server endpoint for sending the PCC-9 messages. Default: %s'
        % PCC_ENDPOINT)
    parser.add_option(
        "-t",
        action="store",
        dest="timeout",
        type="int",
        help=
        'Timeout in seconds to be used for the periodic check. Default: %s (i.e. %s minutes).  Minimum: 5 seconds.'
        % (CHK_TIMEOUT, CHK_TIMEOUT / 60))
    (options, args) = parser.parse_args()
    if options.timeout < 5:
        options.timeout = 5
    return options
コード例 #11
0
import smtplib
from email import encoders, parser
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from optparse import OptionParser

# abs_file_path = "/Users/TriDo/Downloads/heart.jpg"
youremail = "*****@*****.**"
password = "******"

parser = OptionParser()
parser.add_option("-t", "--to", action="append", type="string", dest="toaddr")
parser.add_option("-c", "--cc", action="append", type="string", dest="tocc")
parser.add_option("-s",
                  "--subject",
                  action="store",
                  type="string",
                  dest="subject",
                  default="Sent from my Mac")
parser.add_option("-f", "--file", action="append", type="string", dest="file")
parser.add_option("-m",
                  "--message",
                  action="store",
                  type="string",
                  dest="message",
                  default="")
options, args = parser.parse_args()

server = smtplib.SMTP('smtp.gmail.com', 587)
コード例 #12
0
            'signature': self.signer.generate_signature(msg.as_string())
        }
        if self.fix_sender:
            values['fix_sender'] = 'true'
        data = urllib.urlencode([(k, v) for k, v in values.items()])
        status, errmsg = self.connection.make_request(data)

        if status != 204 and not self.fail_silently:
            raise MessageSendingFailure(errmsg)


if __name__ == '__main__':
    """mail -s [space-separated to-addresses] to-address
       and the message on stdin"""
    parser = optparse.OptionParser()
    parser.add_option("-s", dest="subject", help="subject of message")
    parser.add_option(
        "--fix-sender",
        action="store_true",
        dest="fix_sender",
        help=
        "If sender is not authorized, replace From with an authorized sender")
    options, to_addresses = parser.parse_args()
    if to_addresses:
        msg = email.message.Message()
        msg['From'] = os.environ['USER']
        msg['To'] = ",".join(to_addresses)  # escaping necessary?
        msg['Subject'] = options.subject
        msg.set_payload(sys.stdin.read())
    else:
        # We're expecting a whole message on stdin:
コード例 #13
0
ファイル: niget.py プロジェクト: skunkwerks/netinf
def py_niget():
    """
    @brief Command line program to perform a NetInf 'get' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  niget.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%prog [-q] [-l] [-d] [-m|-v] [-f <pathname of content file>] <ni name>\n" + \
            "<ni name> must include location (netloc) from which to retrieve object."
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="File to hold retrieved content. Defaults to hash code in current directory if not present")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-s", "--server", dest="server",
                      type="string",
                      help="hostname:port of server to send the NetInf GET to")
    parser.add_option("-l", "--lax", default=False,
                      action="store_true", dest="lax",
                      help="Store returned content even if digest doesn't validate")
    parser.add_option("-m", "--metadata", default=False,
                      action="store_true", dest="metadata",
                      help="Output returned metadata as JSON string")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="view",
                      help="Pretty print returned metadata.")
    parser.add_option("-d", "--dump", default=False,
                      action="store_true", dest="dump",
                      help="Dump raw HTTP response to stdout.")

    (options, args) = parser.parse_args()

    # Check command line options - -q, -f, -l, -m, -v and -d are optional, <ni name> is mandatory
    if len(args) != 1:
        parser.error("URL <ni name> not specified.")
        sys.exit(-1)
    verbose = not options.quiet

    # Create NIname instance for supplied URL and validate it
    ni_url = NIname(args[0])

    # Must be a complete ni: URL with non-empty params field
    rv = ni_url.validate_ni_url(has_params = True)
    if (rv != ni_errs.niSUCCESS):
        if verbose:
            print("Error: %s is not a complete, valid ni scheme URL: %s" % (ni_url.get_url(), ni_errs_txt[rv]))
        sys.exit(-2)

    # Generate file name for output if not specified
    if (options.file_name == None):
        options.file_name = ni_url.get_digest()
        
    # Generate NetInf form access URL
    if (options.server != None):
        server = options.server
    else:
        server = ni_url.get_netloc()

    http_url = "http://%s/netinfproto/get" % server
    """
    if (http_url == None):
        if verbose:
            print("Error: Unable to generate http: transformed URL for ni URL %s" % ni_urlparse.get_url())
        sys.exit(-3)
    """
    
    # Set up HTTP form data for get request
    form_data = urllib.urlencode({ "URI":   ni_url.get_url(),
                                   "msgid": random.randint(1, 32000),
                                   "ext":   "" })

    # Send POST request to destination server
    try:
        http_object = urllib2.urlopen(http_url, form_data)
    except Exception, e:
        if verbose:
            print("Error: Unable to access http URL %s: %s" % (http_url, str(e)))
        sys.exit(-4)
コード例 #14
0
ファイル: gcontacts.py プロジェクト: vicgc/bin
def main():
    """ Main routine.
    Args:
        None.
    Returns:
        None.
    """

    usage = "%prog [options] [keyword]" + \
            "\nVersion: {ver}".format(ver=__version__)
    parser = OptionParser(usage=usage)

    parser.add_option("-a", "--account", dest="account",
        help="The gmail account email address.")
    parser.add_option("-c", "--create", dest="create",
        help="Create contact from email.")
    parser.add_option("-e", "--edit", dest="edit", action="store_true",
        help="Edit contacts.")
    parser.add_option("-f", "--full-help", dest="full_help",
        action="store_true",
        help="Print full help and exit. Full help includes examples/notes.")
    parser.add_option('-m', '--mode', dest='mode',
        choices=('abook', 'email', 'long', 'short'), default='long',
        help="Mode. One of: 'abook', 'email', 'long', or 'short'. \
            Default: 'long'")
    parser.add_option('-r', '--include-header', dest='print_status',
        action='store_true', default=False,
        help="Include a status header line. (Required for mutt)")
    parser.add_option('-s', '--sort', dest='sort',
        choices=('email', 'name'), default='email',
        help="Field to sort by. One of 'email' or 'name'. Default 'email'.")
    parser.add_option('-v', '--verbose',
        action='store_const', const=1, dest='verbose',
        help='Print messages to stdout.',)
    parser.add_option('--vv', action='store_const', const=2,
        dest='verbose', help='More verbose.')

    (options, args) = parser.parse_args()

    if options.verbose > 0:
        if options.verbose == 1:
            LOG.setLevel(logging.INFO)
        else:
            LOG.setLevel(logging.DEBUG)

    if options.full_help:
        parser.print_help()
        print
        print usage_full()
        exit(0)

    keyword = None
    if len(args) > 0:
        keyword = args[0]

    if options.account:
        email_addr = options.account
    else:
        email_addr = get_email_address()
    if not email_addr:
        msg = "Unable to determine google email account to login with."
        print >> sys.stderr, msg
        quit(1)

    password = get_password(email_addr)

    LOG.debug("email: {email} password: {pw}".format(email=email_addr,
        pw=password))

    LOG.debug("Creating google contacts service.")
    gd_client = gdata.contacts.service.ContactsService()

    LOG.debug("Logging in.")
    gd_client.email = email_addr
    gd_client.password = password
    gd_client.source = 'dm-contacts-1'
    gd_client.ProgrammaticLogin()

    if options.edit:
        # All details are required for edit. Force long mode.
        options.mode = 'long'

    contact_set = Contacts(gd_client=gd_client)

    if options.create:
        contact_set.create_contact_from_file(options.create)
    else:
        contact_set.get()
        contact_set.filter(keyword)
        if options.edit:
            contact_set.edit()
            contact_set.update()
        else:
            contact_set.print_contacts(mode=options.mode, sort_by=options.sort,
                print_status=options.print_status)
コード例 #15
0
ファイル: mailfv.py プロジェクト: robert3005/scripts
            raise Error, "received unparsable response."

    def createDirectoryIfNotExistent(self, directory):
        if not os.path.exists(directory):
            self.logger.info('Creating directory {0}'.format(directory))
            os.makedirs(directory)

    def logout(self):
        self.logger.debug('Closing connection to imap.google.com')
        self.M.close()
        self.M.logout()

if __name__ == '__main__':

    parser = OptionParser()
    parser.add_option("-c", "--config", default='/root/.mailfv', dest="filename", help="location of config file", metavar="CONFIG")
    options, args = parser.parse_args()
    retCode = 0

    waitDaemon = threading.Event()
    waitDaemon.clear()
    retCode = Daemonize.createDaemon(022, '/', 1024, waitDaemon)
    waitDaemon.wait()

    def handler(signum, frame):
        gmail.exit()
        gmail.join()
        sys.exit(retCode)

    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)
コード例 #16
0
ファイル: nipub.py プロジェクト: skunkwerks/netinf
def py_nipub():
    """
    @brief Command line program to perform a NetInf 'publish' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nipub.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%%prog %s\n       %%prog %s\n%s\n       %%prog %s\n       %%prog %s\n%s\n%s" % \
            ("[-q] [-e] [-j|-v|-w|-p] -f <pathname of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}",
             "[-q] [-e] [-j|-v|-w|-p] [-f <pathname of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}",
             "          -- publish file via NI URI over HTTP",
             "[-q] [-e] [-j|-v|-w|-p] -u <HTTP URI of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}",
             "[-q] [-e] [-j|-v|-w|-p] [-u <HTTP URI of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}",
             "          -- publish web content via NI URI over HTTP",
             "Send response as HTML document (-w), plain text (-p), or JSON (-v or -j)\n"
             "Unless -q is specified, the response is sent to standard output.\n"
             "For a JSON response, it can either be output as a 'raw' JSON string (-j) or pretty printed (-v).\n"
             "If none of  -j, -v, -w or -p are specified, a raw JSON response will be requested.")
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="Pathname for local file to be published.")
    parser.add_option("-u", "--uri", dest="http_name",
                      type="string",
                      help="HTTP URL for content to be published.")
    parser.add_option("-d", "--digest", dest="hash_alg",
                      type="string",
                      help="Digest algorithm to be used to hash content "
                           "and create NI URI. Defaults to sha-256.")
    parser.add_option("-n", "--name", dest="ni_name",
                      type="string",
                      help="Complete ni name. If specified with a file or "
                           "HTTP URL, the digest generated from the content "
                           "will be checked against th digest in the name.")
    parser.add_option("-e", "--ext", dest="ext",
                      type="string",
                      help="A JSON encoded object to be sent as the 'ext' "
                           "parameter for the Publish message.")
    parser.add_option("-l", "--loc", dest="locs", action="append",
                      type="string",
                      help="An FQDN where NI might be retrieved. Maybe be "
                           "zero to two if -n is present and has a non-empty netloc. "
                           "Otherwise must be one or two. HTTP is sent to first "
                           "loc if no authority in -n.")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-j", "--json", default=False,
                      action="store_true", dest="json_raw",
                      help="Request response as JSON string and output raw JSON "
                           "string returned on stdout.")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="json_pretty",
                      help="Request response as JSON string and pretty print "
                           "JSON string returned on stdout.")
    parser.add_option("-w", "--web", default=False,
                      action="store_true", dest="html",
                      help="Request response as HTML document and output HTML "
                           "returned on stdout.")
    parser.add_option("-p", "--plain", default=False,
                      action="store_true", dest="plain",
                      help="Request response as plain text document and output text "
                           "returned on stdout.")


    (options, args) = parser.parse_args()

    # Check command line options:
    # Arguments -q, -e, -w, -p, -j and -v are optional; there must be one of a -n with an authority in it or at least one -l.
    # Either -d or -n must be specified.
    # If -d is specified, there must be either a -f or a -u but not both at once.
    # If -n is specified, one of -f or -u may be specified. No leftover arguments allowed.
    # Specifying more than one of -w, -p, -j and -v is inappropriate.
    if len(args) != 0:
        parser.error("Unrecognized arguments %s supplied." % str(args))
        sys.exit(-1)
    if ((options.locs is not None) and (len(options.locs) > 2)):
        parser.error("Initial version only supports two locators (-l/--loc).")
        sys.exit(-1)
    if ((options.ni_name == None) and (options.locs == None)):
        parser.error("Must specify a locator (-l/--loc) or a name (-n/--name) with a netloc component to define where to send the request.")
        sys.exit(-1)
    if ((options.hash_alg != None) and (options.ni_name != None)):
        parser.error("Cannot specify both digest algorithm to be used (-d) and complete ni name with algorithm and digest (-n).")
        sys.exit(-1)
    if ((options.hash_alg == None) and (options.ni_name == None)):
        parser.error("Must specify either digest algorithm to be used (-d) or complete ni name with algorithm and digest (-n).")
        sys.exit(-1)
    if ((((options.ni_name == None) and (options.file_name == None) and (options.http_name == None))) or
        ((options.file_name != None) and (options.http_name != None))):
        parser.error("Exactly one of -f/--file and -u/--uri must be specified with -d and optionally with -n.")
        sys.exit(-1)
    fc = 0
    for flag in [options.json_raw, options.json_pretty, options.html, options.plain]:
        if flag:
            fc += 1
    if fc > 1:
        parser.error("Should specify at most one response type argument out of -j, -v, -w and -p.")
        sys.exit(-1)

    file_name = None
    
    # **** -u is not implemented yet
    if options.http_name != None:
        target = options.http_name
        print "Web name as source(-u/--uri option) not yet implemented. Exiting"
        sys.exit(-2)

    if options.file_name != None:
        target = options.file_name
        file_name = options.file_name
        full_put = True
    else:
        target = None
        full_put = False
    debug("full_put: %s" %full_put)

    verbose = not options.quiet

    #  If we have a full ni name (-n option) given..
    if options.ni_name is not None:
        # Check the validity of the ni name
        try:
            ni_name = NIname(options.ni_name)
        except Exception, e:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name)
            sys.exit(-3)
        rv = ni_name.validate_ni_url()
        if rv != ni_errs.niSUCCESS:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" % options.ni_name)
            sys.exit(-3)

        # Extract the scheme and hash algorithm from the name
        scheme = ni_name.get_scheme()
        hash_alg = ni_name.get_alg_name()

        # If the ni name has a netloc in it then that is where to send; if not must have a loc
        nl = ni_name.get_netloc()
        if ((nl == "") and (options.locs == None)):
            print("Error: name (-n/--name) mist have a netloc if no locator options given,")
            sys.exit(-4)
        if nl != "":
            destination = nl
            authority = nl
        else:
            destination = options.locs[0]
            authority = ""
コード例 #17
0
ファイル: mailfv.py プロジェクト: robert3005/scripts
        if not os.path.exists(directory):
            self.logger.info('Creating directory {0}'.format(directory))
            os.makedirs(directory)

    def logout(self):
        self.logger.debug('Closing connection to imap.google.com')
        self.M.close()
        self.M.logout()


if __name__ == '__main__':

    parser = OptionParser()
    parser.add_option("-c",
                      "--config",
                      default='/root/.mailfv',
                      dest="filename",
                      help="location of config file",
                      metavar="CONFIG")
    options, args = parser.parse_args()
    retCode = 0

    waitDaemon = threading.Event()
    waitDaemon.clear()
    retCode = Daemonize.createDaemon(022, '/', 1024, waitDaemon)
    waitDaemon.wait()

    def handler(signum, frame):
        gmail.exit()
        gmail.join()
        sys.exit(retCode)
コード例 #18
0
ファイル: mockmail.py プロジェクト: phihag/mockmail
def main():
    parser = OptionParser()
    parser.add_option(
        '-c', '--config', dest='configfile', metavar='FILE',
        help='JSON configuration file to load')
    parser.add_option(
        '-d', '--daemonize', action='store_const', const=True, dest='daemonize', default=None,
        help='Run mockmail in the background. Overwrites configuration')
    parser.add_option(
        '-i', '--interactive', action='store_const', const=True, dest='daemonize', default=None,
        help='Run mockmail in the foreground. Overwrites configuration')
    parser.add_option(
        '--resourcedir', dest='resourcedir', metavar='DIR',
        help='Load resources and templates from this directrory')
    parser.add_option(
        '--pidfile', dest='pidfile', default=None,
        help='Set pidfile to use. Overwrites configuration')
    parser.add_option(
        '--ctl-status', action='store_const', dest='ctl', const='status', default=None,
        help='Check whether mockmail service is running.')
    parser.add_option(
        '--ctl-start',  action='store_const', dest='ctl', const='start',  default=None,
        help='Start mockmail service.')
    parser.add_option(
        '--ctl-stop',   action='store_const', dest='ctl', const='stop',   default=None,
        help='Stop mockmail  service.')
    parser.add_option(
        '--quiet-ctl', action='store_true', dest='quiet_ctl', default=False,
        help='Do not print announcement in --ctl-* operations.')
    parser.add_option(
        '--dumpconfig', action='store_true', dest='dumpconfig',
        help='Do not run mockmail, but dump the effective configuration')
    parser.add_option(
        '--version', action='store_true', dest='dumpversion',
        help='Do not run mockmail, but output the version')
    parser.add_option(
        '--check-resourcedir', action='store_true', dest='check_resourcedir',
        help='Do not run mockmail, but check that the resource directory is set correctly')
    opts, args = parser.parse_args()

    if len(args) != 0:
        parser.error('Did not expect any arguments. Use -c to specify a configuration file.')

    if opts.dumpversion:
        print(__version__)
        return

    config = {
        'smtpaddr': '',     # IP address to bind the SMTP port on. The default allows anyone to send you emails.
        'smtpport': 2525,     # SMTP port number. On unixoid systems, you will need superuser privileges to bind to a port < 1024
        'httpaddr': '',       # IP address to bind the web interface on. The default allows anyone to see your mail.
        'httpport': 2580,     # Port to bind the web interface on. You may want to configure your webserver on port 80 to proxy the connection.
        'chroot': None,       # Specify the directory to chroot into.
        'chroot_mkdir': False,  # Automatically create the chroot directory if it doesn't exist, and chroot is set.
        'dropuser': None,     # User account (name or uid) to drop to, None to not drop privileges
        'dropgroup': None,    # User group (name or gid) to drop into. By default, this is the primary group of the user.
        'static_dev': False,  # Read static files on demand. Good for development (a reload will update the file), but should not be set in production
        'daemonize': False,   # Whether mockmail should go into the background after having started
        'pidfile': None,      # File to write the process ID of mockmail to (relative to the chroot)
        'resourcedir': None,  # Directory to load templates and resources
        'workarounds': True,  # Work around platform bugs
        'static_cache_secs': 0,  # Cache duration for static files
        'smtp_grace_period': None,  # Set to a number to wait that long to open a port
    }
    if opts.configfile:
        with open(opts.configfile, 'r') as cfgf:
            config.update(json.load(cfgf))
    if opts.daemonize is not None:
        config['daemonize'] = opts.daemonize
    if opts.pidfile is not None:
        config['pidfile'] = opts.pidfile
    if opts.resourcedir is not None:
        config['resourcedir'] = opts.resourcedir

    if opts.dumpconfig:
        json.dump(config, sys.stdout, indent=4)
        sys.stdout.write('\n')
        return

    if config['resourcedir'] is None:
        config['resourcedir'] = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'share', 'mockmail'))

    if opts.check_resourcedir:
        print('Loading resources from ' + os.path.abspath(config['resourcedir']) + ' ...')

        _readIds(
            _TEMPLATES,
            lambda fid: os.path.join(config['resourcedir'], 'templates', fid + '.mustache'),
            mapContent=lambda content: content.decode('UTF-8'),
            ondemand=False)
        _readIds(
            _STATIC_FILES,
            lambda fid: os.path.join(config['resourcedir'], 'static', fid),
            ondemand=False)
        sys.exit(0)

    ctl_print = (lambda s: 0) if opts.quiet_ctl else sys.stdout.write
    if opts.ctl == 'status':
        pid = _getPid(_effectivePidfile(config))
        if pid:
            ctl_print('mockmail is running.\n')
            sys.exit(0)
        else:
            ctl_print('mockmail is NOT running.\n')
            sys.exit(3)
    elif opts.ctl == 'start':
        pid = _getPid(_effectivePidfile(config))
        ctl_print('Starting Test MTA: mockmail')
        if pid:
            sys.stdout.write(' (pid ' + str(pid) + ') already running.\n')
            sys.exit(0)
        else:
            config['daemonize'] = True
            mockmail(config)
            ctl_print('.\n')
            return
    elif opts.ctl == 'stop':
        pidfn = _effectivePidfile(config)
        pid = _getPid(pidfn)
        ctl_print('Stopping Test MTA: mockmail ...')
        if pid:
            os.kill(pid, signal.SIGTERM)
            try:
                os.unlink(pidfn)
            except OSError:
                pass
        ctl_print('.\n')
        sys.exit(0)

    if config['pidfile']:
        pid = _getPid(_effectivePidfile(config))
        if pid:
            raise Exception(
                'mockmail is already running (pid %s), read from %s' %
                (pid, _effectivePidfile(config)))

    mockmail(config)
コード例 #19
0
ファイル: imapcopy.py プロジェクト: vitush/mailtools
def get_arguments():

    from optparse import OptionParser
    parser = OptionParser(usage="%prog options ")
    parser.add_option("-f","--config",    dest='config_file',      help='Config File ')
    parser.add_option("-s","--src-server",dest='src_server',help='Source IMAP server')
    parser.add_option("-S","--dst-server",dest='dst_server',help='Destination IMAP server')
    parser.add_option("-p","--src-port",  dest='src_port',  help='Source IMAP server port', type='int')
    parser.add_option("-P","--dst-port",  dest='dst_port',  help='Destination IMAP server port', type='int')
    parser.add_option("-u","--src-user",  dest='src_user',  help='Source IMAP user')
    parser.add_option("-U","--dst-user",  dest='dst_user',  help='Destination IMAP user')
    parser.add_option("-w","--src-password",  dest='src_password',  help='Source IMAP password',type="string")
    parser.add_option("-W","--dst-password",  dest='dst_password',  help='Destination IMAP password',type="string")
    parser.add_option("-x", "--src-ssl",   dest='src_ssl',   action="store_true", help='Use SSL')
    parser.add_option("-X", "--dst-ssl",   dest='dst_ssl',   action="store_true", help='Use SSL')
    parser.add_option("-d","--start-date", dest='start_date',help='Start from date YYYYMMDD ')
    parser.add_option("-D","--end-date"  , dest='end_date',  help='End date YYYYMMDD')
    parser.add_option("-v", "--verbose",   dest="verbose",   action="store_true", help="Verbose mode")
    parser.add_option("-c", "--checksum", dest="use_checksum", action="store_true", 
                        help="Use a checksum of several mail headers, instead of the Message-ID")
    parser.add_option("-m", "--checksum-with-id", dest="use_id_in_checksum", action="store_true", 
                        help="Include the Message-ID (if any) in the -c checksum.")
    parser.add_option("-l", "--list", dest="just_list", action="store_true", 
                                            help="Just list mailboxes on Source Server")

    parser.set_defaults(verbose=False, ssl=False, dry_run=False, just_list=False,src_port=143,dst_port=143)
    (options, args) = parser.parse_args()
  
    config = ConfigParser.ConfigParser()    
    #Load Data from config file
    if options.config_file:
        config.read(options.config_file)
        for a in config.items("IMAPCopy"):
            setattr(options, a[0],a[1])
              
    if (not options.start_date) or (not options.end_date):
            sys.stderr.write("\nError: Must specify Start and End dates. \n")
            parser.print_help()
            sys.exit(1)
         
    
    if options.just_list:
        if (not options.src_server) or (not options.src_user) :
            sys.stderr.write("\nError: Must specify src-server, src-users, src-passwords.\n")
            parser.print_help()
            sys.exit(1)
    else:
        if (not options.src_server) or (not options.dst_server) or (not options.src_user) or (not options.dst_user):
            sys.stderr.write("\nError: Must specify servers, users, passwords .\n\n")
            parser.print_help()
            sys.exit(1)


    return (options, args)
コード例 #20
0
ファイル: nipubdir.py プロジェクト: skunkwerks/netinf
def py_nipubdir():
    """
    @brief Command line program to perform a NetInf 'publish' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nipub.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    

    # Options parsing and verification stuff
    usage = "%%prog -d <pathname of content directory> -n <FQDN of netinf node> [-a <hash alg>] [-m NN] [-c count]"

    parser = OptionParser(usage)
    
    parser.add_option("-d", "--dir", dest="dir_name",
                      type="string",
                      help="Pathname for directory to be published.")
    parser.add_option("-a", "--alg", dest="hash_alg", default="sha-256",
                      type="string",
                      help="Hash algorithm to be used for NI URIs. Defaults to sha-256.")
    parser.add_option("-n", "--node", dest="host",
                      type="string",
                      help="The FQDN where I'll send PUBLISH messages.")
    parser.add_option("-m", "--multiprocess", dest="mprocs", default=1,
                      type="int",
                      help="The number of client processes to use in a pool (default 1)")
    parser.add_option("-c", "--count", dest="count", default=0,
                      type="int",
                      help="The number of files to publish (default: all)")

    (options, args) = parser.parse_args()

    # Check command line options:
    # Arguments -h is optional, all others needed


    # Specifying more than one of -w, -p, -j and -v is inappropriate.
    if len(args) != 0:
        parser.error("Unrecognized arguments %s supplied." % str(args))
        sys.exit(-1)
    if options.dir_name == None:
        parser.error("You must supply a directory name with -d")
        sys.exit(-1)
    if options.host == None: 
        parser.error("You must supply a host name with -n")
        sys.exit(-1)

    nilog("Starting nipubdir,dir,%s,to,%s,alg,%s,processes,%d,count,%d" 
            % (options.dir_name,options.host,options.hash_alg,options.mprocs,options.count))

    # loop over all files below directory and putone() for each we find
    count,goodlist,badlist=pubdirs(options.dir_name,options.hash_alg,options.host,options.mprocs,options.count)

    # print goodlist
    # print badlist

    nilog("Finished nipubdir,dir,%s,to,%s,alg,%s,processes,%d,count,%d" 
        % (options.dir_name,options.host,options.hash_alg,options.mprocs,count))

    sys.exit(0)
コード例 #21
0
ファイル: imap_client.py プロジェクト: schmichael/firetower
def main():
    parser = OptionParser(usage="usage: firetower options args")
    parser.add_option("-c", "--conf", action="store", dest="conf_path", help="Path to YAML configuration file.")
    parser.add_option(
        "-d", dest="clear_firstrun", action="store_true", default=False, help="Clear backed up emails on first run"
    )
    (options, args) = parser.parse_args()
    conf = config.Config(options.conf_path)

    conn = imaplib.IMAP4_SSL(conf.imap_host)
    imap_user = conf.imap_user

    queue = redis_util.get_redis_conn(host=conf.redis_host, port=conf.redis_port, redis_db=conf.redis_db)

    processed = 0

    print "Enter email password"
    imap_password = getpass.getpass()

    conn.login(imap_user, imap_password)
    conn.select("Inbox")

    first_run = True

    while True:
        _, msg_ids = conn.search(None, "(ALL)")
        msg_ids = msg_ids[0].split()
        if len(msg_ids) == 0:
            time.sleep(0.5)
            if first_run:
                first_run = False
            continue
        print "Processing %s messages" % len(msg_ids)

        id_batches = []
        # split the ids into batches of 1000
        count = 0
        split = 1000
        while count < len(msg_ids):
            id_batches.append(msg_ids[count : count + split])
            count += split

        for id_batch in id_batches:
            if options.clear_firstrun and first_run:
                first_run = False
                break

            _, msgs = conn.fetch(",".join(id_batch), "(BODY[])")
            for msg in msgs:
                if not msg or msg == ")":
                    continue
                msg_obj = email.message_from_string(msg[1])
                ft_dict = {}
                ft_dict["hostname"] = msg_obj["Received"].split(" ")[1] if msg_obj["Received"] else "????"
                ft_dict["sig"] = msg_obj.get_payload() or "????"
                ft_dict["date"] = msg_obj["Date"]
                ft_dict["programname"] = "Maildir Util"

                # We don't huge signatures clogging the classification
                if len(ft_dict["sig"]) < 10000 and isinstance(ft_dict["sig"], str):
                    queue.lpush(conf.queue_key, json.dumps(ft_dict))
                processed += 1
                if not processed % 100:
                    print "Processed: %s" % processed

        for msg_id in msg_ids:
            conn.store(msg_id, "+FLAGS", "\\Deleted")
        print "Processed: %s" % processed

        conn.expunge()
コード例 #22
0
ファイル: send.py プロジェクト: xmw/studeval
#!/usr/bin/python2
# -*- coding: utf-8 -*-

import mailbox, optparse, smtplib, sys
import email.mime.text, email.parser


parser = optparse.OptionParser()
parser.add_option('-s', '--server', dest='host', default='huygens.fs.lmu.de',
    help='smtp relay server hostname', metavar='STRING')
parser.add_option('-p', '--port', dest='port', default='465',
    help='smtp relay port', 	metavar='STRING')
parser.add_option('-t', '--type', dest='type', default='ssl',
    help='smtp connection type', metavar='plain|ssl|tls')
parser.add_option('-U', '--username', dest='username', default='webermi',
    help='optional smtp username', metavar='STRING')
parser.add_option('-P', '--password', dest='password',
    help='optional smtp password, leave blank for prompt', metavar='STRING')
parser.add_option('-m', '--maildir', dest='maildir', default='mail',
    help='mailbox directory', metavar='DIR')

(options, args) = parser.parse_args()

if not options.host or not options.maildir or not options.type in ('plain', 'ssl', 'tls'):
    parser.print_help()
    sys.exit(1)

if options.username and not options.password:
    import getpass
    options.password = getpass.getpass('Enter password for %s on %s: ' % (options.username, options.host))
コード例 #23
0
ファイル: mockmail.py プロジェクト: ziozzang/mockmail
def main():
    parser = OptionParser()
    parser.add_option('-c',
                      '--config',
                      dest='configfile',
                      metavar='FILE',
                      help='JSON configuration file to load')
    parser.add_option(
        '-d',
        '--daemonize',
        action='store_const',
        const=True,
        dest='daemonize',
        default=None,
        help='Run mockmail in the background. Overwrites configuration')
    parser.add_option(
        '-i',
        '--interactive',
        action='store_const',
        const=False,
        dest='daemonize',
        default=None,
        help='Run mockmail in the foreground. Overwrites configuration')
    parser.add_option('--resourcedir',
                      dest='resourcedir',
                      metavar='DIR',
                      help='Load resources and templates from this directrory')
    parser.add_option('--pidfile',
                      dest='pidfile',
                      default=None,
                      help='Set pidfile to use. Overwrites configuration')
    parser.add_option('--ctl-status',
                      action='store_const',
                      dest='ctl',
                      const='status',
                      default=None,
                      help='Check whether mockmail service is running.')
    parser.add_option('--ctl-start',
                      action='store_const',
                      dest='ctl',
                      const='start',
                      default=None,
                      help='Start mockmail service.')
    parser.add_option('--ctl-stop',
                      action='store_const',
                      dest='ctl',
                      const='stop',
                      default=None,
                      help='Stop mockmail  service.')
    parser.add_option('--quiet-ctl',
                      action='store_true',
                      dest='quiet_ctl',
                      default=False,
                      help='Do not print announcement in --ctl-* operations.')
    parser.add_option(
        '--dumpconfig',
        action='store_true',
        dest='dumpconfig',
        help='Do not run mockmail, but dump the effective configuration')
    parser.add_option('--version',
                      action='store_true',
                      dest='dumpversion',
                      help='Do not run mockmail, but output the version')
    parser.add_option(
        '--check-resourcedir',
        action='store_true',
        dest='check_resourcedir',
        help=
        'Do not run mockmail, but check that the resource directory is set correctly'
    )
    opts, args = parser.parse_args()

    if len(args) != 0:
        parser.error(
            'Did not expect any arguments. Use -c to specify a configuration file.'
        )

    if opts.dumpversion:
        print(__version__)
        return

    config = {
        'smtpaddr':
        '',  # IP address to bind the SMTP port on. The default allows anyone to send you emails.
        'smtpport':
        2525,  # SMTP port number. On unixoid systems, you will need superuser privileges to bind to a port < 1024
        'httpaddr':
        '',  # IP address to bind the web interface on. The default allows anyone to see your mail.
        'httpport':
        2580,  # Port to bind the web interface on. You may want to configure your webserver on port 80 to proxy the connection.
        'chroot': None,  # Specify the directory to chroot into.
        'chroot_mkdir':
        False,  # Automatically create the chroot directory if it doesn't exist, and chroot is set.
        'dropuser':
        None,  # User account (name or uid) to drop to, None to not drop privileges
        'dropgroup':
        None,  # User group (name or gid) to drop into. By default, this is the primary group of the user.
        'static_dev':
        False,  # Read static files on demand. Good for development (a reload will update the file), but should not be set in production
        'daemonize':
        False,  # Whether mockmail should go into the background after having started
        'pidfile':
        None,  # File to write the process ID of mockmail to (relative to the chroot)
        'resourcedir': None,  # Directory to load templates and resources
        'workarounds': True,  # Work around platform bugs
        'static_cache_secs': 0,  # Cache duration for static files
        'smtp_grace_period':
        None,  # Set to a number to wait that long to open a port
    }
    if opts.configfile:
        with open(opts.configfile, 'r') as cfgf:
            config.update(json.load(cfgf))
    if opts.daemonize is not None:
        config['daemonize'] = opts.daemonize
    if opts.pidfile is not None:
        config['pidfile'] = opts.pidfile
    if opts.resourcedir is not None:
        config['resourcedir'] = opts.resourcedir

    if opts.dumpconfig:
        json.dump(config, sys.stdout, indent=4)
        sys.stdout.write('\n')
        return

    if config['resourcedir'] is None:
        config['resourcedir'] = os.path.normpath(
            os.path.join(os.path.dirname(__file__), '..', 'share', 'mockmail'))

    if opts.check_resourcedir:
        print('Loading resources from ' +
              os.path.abspath(config['resourcedir']) + ' ...')

        _readIds(_TEMPLATES,
                 lambda fid: os.path.join(config['resourcedir'], 'templates',
                                          fid + '.mustache'),
                 mapContent=lambda content: content.decode('UTF-8'),
                 ondemand=False)
        _readIds(
            _STATIC_FILES,
            lambda fid: os.path.join(config['resourcedir'], 'static', fid),
            ondemand=False)
        sys.exit(0)

    ctl_print = (lambda s: 0) if opts.quiet_ctl else sys.stdout.write
    if opts.ctl == 'status':
        pid = _getPid(_effectivePidfile(config))
        if pid:
            ctl_print('mockmail is running.\n')
            sys.exit(0)
        else:
            ctl_print('mockmail is NOT running.\n')
            sys.exit(3)
    elif opts.ctl == 'start':
        pid = _getPid(_effectivePidfile(config))
        ctl_print('Starting Test MTA: mockmail')
        if pid:
            sys.stdout.write(' (pid ' + str(pid) + ') already running.\n')
            sys.exit(0)
        else:
            config['daemonize'] = True
            mockmail(config)
            ctl_print('.\n')
            return
    elif opts.ctl == 'stop':
        pidfn = _effectivePidfile(config)
        pid = _getPid(pidfn)
        ctl_print('Stopping Test MTA: mockmail ...')
        if pid:
            os.kill(pid, signal.SIGTERM)
            try:
                os.unlink(pidfn)
            except OSError:
                pass
        ctl_print('.\n')
        sys.exit(0)

    if config['pidfile']:
        pid = _getPid(_effectivePidfile(config))
        if pid:
            raise Exception(
                'mockmail is already running (pid %s), read from %s' %
                (pid, _effectivePidfile(config)))

    mockmail(config)
コード例 #24
0
def main():
    """Parse command-line arguments and invoke the RemoveAttachments program class."""
    parser = OptionParser()
    parser.add_option("-s", "--server", help="IMAP4 server")
    parser.add_option("--port", help="IMAP4 server port")
    parser.add_option("--ssl", help="Use SSL connectivity", action="store_true")
    parser.add_option("-u", "--username", help="IMAP4 username")
    parser.add_option("-p", "--password", help="IMAP4 password")
    parser.add_option("--only-mailbox", help="Specify a single mailbox to process (default: all, recursive)")
    parser.add_option("--couchdb-server", help="CouchDB server address")
    parser.add_option("--couchdb-db", help="CouchDB database name (default: attachments)")
    parser.add_option("--before-date",
                      help="Only check messages sent before this date (format YYYY-MM-DD)")
    parser.add_option("--min-size", default=5000,
                      help="Ignore mails smaller than this size, in kB [%default]")
    parser.add_option("-r", "--remove", help="Remove attachments after processing", action="store_true")
    parser.add_option("--eat-more-attachments", help="Use looser criteria for detecting attachments",
                      action="store_true")
    parser.add_option("--gmail", help="Enable Gmail quirks mode (see README) (default off)",
                      action="store_true")
    parser.add_option("-v", "--verbose", help="Log debug messages", action="store_true")
    options = parser.parse_args()[0]

    if options.verbose:
        logging.getLogger().setLevel(logging.DEBUG)

    if options.before_date is not None:
        before_date = parse_date(options.before_date)
    else:
        before_date = None

    if options.min_size is not None:
        try:
            min_size = int(options.min_size)
        except ValueError:
            die("--min-size requires integer argument")
    else:
        min_size = 0

    if options.port is not None:
        try:
            port = int(options.port)
        except ValueError:
            die("--port requires integer argument")
    else:
        if options.ssl:
            port = 993
        else:
            port = 143

    try:
        RemoveAttachments(options.server, port, options.ssl, options.username, options.password,
                          options.only_mailbox, options.couchdb_server, options.couchdb_db, options.remove,
                          options.eat_more_attachments, options.gmail, min_size, before_date).run()
    except RemoveAttachmentsException, e:
        logging.error(e)
        sys.exit(1)
コード例 #25
0
ファイル: email-collector.py プロジェクト: clixx-io/clixx.io
        """hello Event handler
        Fired once in 5 seconds.
        """
        run()

    def started(self, component):
        """started Event handler
        """
        # Timer(seconds, event, persist=False)
        Timer(20, Event.create("checkmessages"), persist=True).register(self)

if __name__ == "__main__":

    usage = "usage: %prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option("-o", "--once", dest="once",action="store_true",
                      help="Run just one time")

    (options, args) = parser.parse_args()

    txtfiles = []

    logger.info("Program startup")    

    try:

        run()

        if options.once:
            sys.exit(0)

        logger.info("Program startup")
コード例 #26
0
ファイル: gyb.py プロジェクト: PaulCapestany/got-your-back
def SetupOptionParser():
  def get_action_labels(option, opt, value, parser):
    value = []
    for arg in parser.rargs:
      # stop on --foo like options
      if arg[:2] == "--" and len(arg) > 2:
        break
      # stop on -a
      if arg[:1] == "-" and len(arg) > 1:
        break
      value.append(arg.lower())

    del parser.rargs[:len(value)]
    if 'all' in value or 'all mail' in value:
      value = []
    parser.values.action_labels = value
    # opt is like '--backup'
    parser.values.action = opt[2:]

  # Usage message is the module's docstring.
  parser = OptionParser(usage=__doc__)
  parser.add_option('-e', '--email',
    dest='email',
    help='Full email address of user to backup')
  parser.add_option('-a', '--action',
    type='choice',
    choices=['backup','restore','estimate', 'reindex'],
    dest='action',
    default='backup',
    help='Optional: Action to perform. backup, restore or estimate.')
  parser.add_option('--action-labels', help=SUPPRESS_HELP)
  parser.add_option('--backup', 
    action='callback', 
    callback=get_action_labels,
    help='Sets the ''backup'' action and takes an optional list of labels to backup.')
  parser.add_option('--estimate', 
    action='callback', 
    callback=get_action_labels,
    help='Sets the ''estimate'' action and takes an optional list of labels to estimate.')
  parser.add_option('--restore', 
    action='callback', 
    callback=get_action_labels,
    help='Sets the ''restore'' action and takes an optional list of labels to restore.')
  parser.add_option('--resume', 
    action='store_true', 
    default=False,
    help='With ''restore'', resume an interrupted restore.')
  parser.add_option('--reindex', 
    dest='action',
    action='store_const',
    const='reindex',
    help=SUPPRESS_HELP)
  parser.add_option('-f', '--folder',
    dest='folder',
    help='Optional: Folder to use for backup or restore. Default is ./gmail-backup/',
    default='XXXuse-email-addessXXX')
  parser.add_option('-s', '--search',
    dest='gmail_search',
    default='',
    help='Optional: Gmail search to perform, matching messages are backed up. Text like *7d* will be replaced by the date 7 days ago. For example, -s "after:*3d*" would search for "after:%s".' % (datetime.datetime.now() - datetime.timedelta(3)).strftime('%Y/%m/%d'))
  parser.add_option('-v', '--version',
    action='store_true',
    dest='version',
    help='just print GYB Version and then quit')
  parser.add_option('-d', '--debug',
    action='store_true',
    dest='debug',
    help='Turn on verbose debugging and connection information (for troubleshooting purposes only)')
  parser.add_option('-l', '--label-restored',
    dest='label_restored',
    help='Optional: Used on restore only. If specified, all restored messages will receive this label. For example, -l "3-21-11 Restore" will label all uploaded messages with that label.')
  parser.add_option('-t', '--two-legged',
    dest='two_legged',
    help='Google Apps Business and Education accounts only. Use administrator two legged OAuth to authenticate as end user.')
  parser.add_option('-C', '--compress',
    dest='compress',
    action='count',
    default=1,
    help='Optional: enable network compression')
  parser.add_option('-c', '--no-compress',
    dest='compress',
    action='store_const',
    const=0,
    help='Optional: disable network compression')
  parser.add_option('-F', '--fast-incremental',
    dest='refresh',
    action='store_false',
    default=True,
    help='Optional: skips refreshing labels for existing message')
  parser.add_option('-B', '--batch-size',
    dest='batch_size',
    type='int',
    default=100,
    help='Optional: Sets the number of messages to include batch when backing up.')
  return parser
コード例 #27
0
ファイル: imap_client.py プロジェクト: gmcquillan/firetower
def main():
    parser = OptionParser(usage='usage: firetower options args')
    parser.add_option(
        '-c', '--conf', action='store', dest='conf_path',
         help='Path to YAML configuration file.',
    )
    parser.add_option(
        '-d', dest='clear_firstrun', action="store_true", default=False,
        help='Clear backed up emails on first run'
    )
    parser.add_option(
        '-p', dest='imap_password', action="store_true", default=False,
        help='IMAP password.'
    )
    (options, args) = parser.parse_args()
    conf = config.Config(options.conf_path)

    conn = imaplib.IMAP4_SSL(conf.imap_host)
    imap_user = conf.imap_user
    imap_password = options.user_pass

    queue = redis_util.get_redis_conn(
        host=conf.redis_host, port=conf.redis_port, redis_db=conf.redis_db)

    processed = 0

    if not imap_password:
        print "Enter email password"
        imap_password = getpass.getpass()

    conn.login(imap_user, imap_password)
    conn.select("Inbox")

    first_run = True

    while True:
        _, msg_ids = conn.search(None, "(ALL)")
        msg_ids = msg_ids[0].split()
        if len(msg_ids) == 0:
            time.sleep(0.5)
            if first_run:
                first_run = False
            continue
        print "Processing %s messages" %len(msg_ids)

        id_batches = []
        # split the ids into batches of 1000
        count = 0
        split = 1000
        while count < len(msg_ids):
            id_batches.append(msg_ids[count:count+split])
            count += split

        for id_batch in id_batches:
            if options.clear_firstrun and first_run:
                first_run = False
                break

            _, msgs = conn.fetch(",".join(id_batch), '(BODY[])')
            for msg in msgs:
                if not msg or msg == ")":
                    continue
                msg_obj = email.message_from_string(msg[1])
                ft_dict = {}
                ft_dict['hostname'] = msg_obj['Received'].split(' ')[1] if msg_obj['Received'] else '????'
                ft_dict['sig'] = msg_obj.get_payload() or '????'
                ft_dict['date'] = msg_obj["Date"]
                ft_dict['programname'] = 'Maildir Util'

                # We don't huge signatures clogging the classification
                if len(ft_dict['sig']) < 10000 and isinstance(ft_dict['sig'], str):
                    queue.lpush(conf.queue_key, json.dumps(ft_dict))
                processed += 1
                if not processed % 100:
                    print "Processed: %s" %processed

        for msg_id in msg_ids:
            conn.store(msg_id, '+FLAGS', '\\Deleted')
        print "Processed: %s" %processed



        conn.expunge()
コード例 #28
0
ファイル: pymm.py プロジェクト: bwesterb/pymm
 def parse_cmdline(self):
     parser = optparse.OptionParser()
     parser.add_option("-f", "--filter-address", dest="filter_address", help="Use EMAIL as filter", metavar="EMAIL")
     parser.add_option("-L", "--log-to", dest="logTo", default="/var/log/pymm", help="Log to FILE", metavar="FILE")
     parser.add_option("-p", "--pre-domain", dest="pre_domain", help="Use DOMAIN as pre domain", metavar="DOMAIN")
     parser.add_option(
         "-P", "--post-address", dest="post_address", help="Use EMAIL as post address", metavar="EMAIL"
     )
     parser.add_option(
         "-t", "--target-domain", dest="target_domain", help="Use DOMAIN as target domain", metavar="DOMAIN"
     )
     parser.add_option(
         "-x", "--header-key", dest="header_key", default="X-37-For", help="Use KEY as header key", metavar="KEY"
     )
     return parser.parse_args()
コード例 #29
0
ファイル: gyb.py プロジェクト: ericsubach/got-your-back
def SetupOptionParser():
  # Usage message is the module's docstring.
  parser = OptionParser(usage=__doc__ % getGYBVersion(), add_help_option=False)
  parser.add_option('--email',
    dest='email',
    help='Full email address of user or group to act against')
  action_choices = ['backup','restore', 'restore-group', 'count', 'purge', 'estimate', 'reindex', 'import-eml']
  parser.add_option('--action',
    type='choice',
    choices=action_choices,
    dest='action',
    default='backup',
    help='Action to perform - %s. Default is backup.' % ', '.join(action_choices))
  parser.add_option('--search',
    dest='gmail_search',
    default='in:anywhere',
    help='Optional: On backup, estimate, count and purge, Gmail search to scope operation against')
  parser.add_option('--local-folder',
    dest='local_folder',
    help='Optional: On backup, restore, estimate, local folder to use. Default is GYB-GMail-Backup-<email>',
    default='XXXuse-email-addressXXX')
  parser.add_option('--eml-folder',
    dest='eml_folder',
    help='Folder to search recursively for EML files to import. The messages are imported onto the web server and are not stored locally. The "Inbox" label is automatically applied. An additional label may be specified with "--label-eml".')
  parser.add_option('--label-eml',
    dest='label_eml',
    help='Optional: On import of EML files, all messages will additionally receive this label. For example, "--label-eml gyb-eml" will label all uploaded messages with a gyb-eml label.')
  parser.add_option('--use-imap-folder',
    dest='use_folder',
    help='Optional: IMAP folder to act against. Default is "All Mail" label. You can run "--use_folder [Gmail]/Chats" to backup chat.')
  parser.add_option('--label-restored',
    dest='label_restored',
    help='Optional: On restore, all messages will additionally receive this label. For example, "--label_restored gyb-restored" will label all uploaded messages with a gyb-restored label.')
  parser.add_option('--service-account',
    dest='service_account',
    help='Google Apps Business and Education only. Use OAuth 2.0 Service Account to authenticate.')
  parser.add_option('--use-admin',
    dest='use_admin',
    help='Optional. On restore-group, authenticate as this admin user.')
  parser.add_option('--batch-size',
    dest='batch_size',
    type='int',
    default=100,
    help='Optional: On backup, sets the number of messages to batch download.')
  parser.add_option('--noresume', 
    action='store_true', 
    default=False,
    help='Optional: On restores, start from beginning. Default is to resume where last restore left off.')
  parser.add_option('--fast-incremental',
    dest='refresh',
    action='store_false',
    default=True,
    help='Optional: On backup, skips refreshing labels for existing message')
  parser.add_option('--debug',
    action='store_true',
    dest='debug',
    help='Turn on verbose debugging and connection information (troubleshooting)')
  parser.add_option('--version',
    action='store_true',
    dest='version',
    help='print GYB version and quit')
  parser.add_option('--help',
    action='help',
    help='Display this message.')
  return parser
コード例 #30
0
ファイル: imapdedup.py プロジェクト: alex-tools/IMAPdedup
def get_arguments():
    # Get arguments and create link to server
    from optparse import OptionParser
    parser = OptionParser(usage="%prog [options] <mailboxname> [<mailboxname> ...]")
    parser.add_option("-s", "--server",dest='server',help='IMAP server')
    parser.add_option("-p", "--port",  dest='port',  help='IMAP server port', type='int')
    parser.add_option("-x", "--ssl",   dest='ssl',   action="store_true", help='Use SSL')
    parser.add_option("-u", "--user",  dest='user',  help='IMAP user name')
    parser.add_option("-w", "--password", dest='password',  help='IMAP password (Will prompt if not specified)')
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="Verbose mode")
    parser.add_option("-n", "--dry-run", dest="dry_run", action="store_true", 
                        help="Don't actually do anything, just report what would be done")
    parser.add_option("-c", "--checksum", dest="use_checksum", action="store_true", 
                        help="Use a checksum of several mail headers, instead of the Message-ID")
    parser.add_option("-m", "--checksum-with-id", dest="use_id_in_checksum", action="store_true", 
                        help="Include the Message-ID (if any) in the -c checksum.")
    parser.add_option("-l", "--list", dest="just_list", action="store_true", 
                                            help="Just list mailboxes")

    parser.set_defaults(verbose=False, ssl=False, dry_run=False, just_list=False)
    (options, args) = parser.parse_args()
    if (not options.server) or (not options.user):
        sys.stderr.write("\nError: Must specify server, user, password and at least one mailbox.\n\n")
        parser.print_help()
        sys.exit(1)
    if not options.password:
        options.password = getpass.getpass()

    return (options, args)
コード例 #31
0
ファイル: imapdedup.py プロジェクト: zfcuk/IMAPdedup
def get_arguments():
    # Get arguments and create link to server
    from optparse import OptionParser
    parser = OptionParser(
        usage="%prog [options] <mailboxname> [<mailboxname> ...]")
    parser.add_option("-s", "--server", dest='server', help='IMAP server')
    parser.add_option("-p",
                      "--port",
                      dest='port',
                      help='IMAP server port',
                      type='int')
    parser.add_option("-x",
                      "--ssl",
                      dest='ssl',
                      action="store_true",
                      help='Use SSL')
    parser.add_option("-u", "--user", dest='user', help='IMAP user name')
    parser.add_option("-w",
                      "--password",
                      dest='password',
                      help='IMAP password (Will prompt if not specified)')
    parser.add_option("-v",
                      "--verbose",
                      dest="verbose",
                      action="store_true",
                      help="Verbose mode")
    parser.add_option(
        "-n",
        "--dry-run",
        dest="dry_run",
        action="store_true",
        help="Don't actually do anything, just report what would be done")
    parser.add_option(
        "-c",
        "--checksum",
        dest="use_checksum",
        action="store_true",
        help="Use a checksum of several mail headers, instead of the Message-ID"
    )
    parser.add_option(
        "-m",
        "--checksum-with-id",
        dest="use_id_in_checksum",
        action="store_true",
        help="Include the Message-ID (if any) in the -c checksum.")
    parser.add_option("-l",
                      "--list",
                      dest="just_list",
                      action="store_true",
                      help="Just list mailboxes")

    parser.set_defaults(verbose=False,
                        ssl=False,
                        dry_run=False,
                        just_list=False)
    (options, args) = parser.parse_args()
    if (not options.server) or (not options.user):
        sys.stderr.write(
            "\nError: Must specify server, user, password and at least one mailbox.\n\n"
        )
        parser.print_help()
        sys.exit(1)
    if not options.password:
        options.password = getpass.getpass()

    return (options, args)
コード例 #32
0
def get_arguments(args):
    # Get arguments and create link to server
    from optparse import OptionParser
    parser = OptionParser(
        usage="%prog [options] <mailboxname> [<mailboxname> ...]")
    parser.add_option("-P",
                      "--process",
                      dest='process',
                      help='IMAP process to access mailboxes')
    parser.add_option("-s", "--server", dest='server', help='IMAP server')
    parser.add_option("-p",
                      "--port",
                      dest='port',
                      help='IMAP server port',
                      type='int')
    parser.add_option("-x",
                      "--ssl",
                      dest='ssl',
                      action="store_true",
                      help='Use SSL')
    parser.add_option("-u", "--user", dest='user', help='IMAP user name')
    parser.add_option("-w",
                      "--password",
                      dest='password',
                      help='IMAP password (Will prompt if not specified)')
    parser.add_option("-v",
                      "--verbose",
                      dest="verbose",
                      action="store_true",
                      help="Verbose mode")
    parser.add_option("--trace",
                      dest="trace",
                      action="store_true",
                      help="Enable IMAP protocol tracing")
    parser.add_option(
        "-n",
        "--dry-run",
        dest="dry_run",
        action="store_true",
        help="Don't actually do anything, just report what would be done")
    parser.add_option(
        "-c",
        "--checksum",
        dest="use_checksum",
        action="store_true",
        help="Use a checksum of several mail headers, instead of the Message-ID"
    )
    parser.add_option(
        "-m",
        "--checksum-with-id",
        dest="use_id_in_checksum",
        action="store_true",
        help="Include the Message-ID (if any) in the -c checksum.")
    parser.add_option(
        "",
        "--no-close",
        dest='no_close',
        action="store_true",
        help=
        'Do not "close" mailbox when done. Some servers will purge deleted messages on a close command.'
    )
    parser.add_option("-l",
                      "--list",
                      dest="just_list",
                      action="store_true",
                      help="Just list mailboxes")

    parser.set_defaults(verbose=False,
                        ssl=False,
                        dry_run=False,
                        no_close=False,
                        just_list=False,
                        trace=False)
    (options, mboxes) = parser.parse_args(args)
    if ((not options.server) or (not options.user)) and not options.process:
        sys.stderr.write(
            "\nError: Must specify server, user, and at least one mailbox.\n\n"
        )
        parser.print_help()
        sys.exit(1)
    if not options.password and not options.process:
        # Read from IMAPDEDUP_PASSWORD env variable, or prompt for one.
        options.password = os.getenv("IMAPDEDUP_PASSWORD") or getpass.getpass()

    if options.use_id_in_checksum and not options.use_checksum:
        sys.stderr.write("\nError: If you use -m you must also use -c.\n")
        sys.exit(1)

    return (options, mboxes)
コード例 #33
0
ファイル: nigetalt.py プロジェクト: skunkwerks/netinf
def py_nigetalt():
    """
    @brief Command line program to perform a NetInf 'get' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nigetalt.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%prog [-q] [-l] [-d] [-m|-v] [-f <pathname of content file>] [-w <locator>] <ni name>\n" + \
            "Either <ni name> must include location (netloc) from which to retrieve object, or\n" + \
            "a locator must be given with the -w/--whence option.\n" + \
            "The locator may be prefixed with an HTTP ('http://')or DTN ('dtn://') URI scheme identifier.\n" + \
            "If no scheme identifier is given then HTTP is assumed.  The DTN scheme does not accept ports."
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="File to hold retrieved content. Defaults to hash code in current directory if not present")
    parser.add_option("-w", "--whence", dest="loc",
                      type="string", default=None,
                      help="Locator to which to send NetInf GET request.  May be prefixed with http:// or dtn://")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-l", "--lax", default=False,
                      action="store_true", dest="lax",
                      help="Store returned content even if digest doesn't validate")
    parser.add_option("-m", "--metadata", default=False,
                      action="store_true", dest="metadata",
                      help="Output returned metadata as JSON string")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="view",
                      help="Pretty print returned metadata.")

    (options, args) = parser.parse_args()

    # Check command line options - -q, -f, -l, -m, and -v are optional,
    # <ni name> is mandatory
    # -w is optional if <ni name> contains a netloc
    if len(args) != 1:
        parser.error("URL <ni name> not specified.")
        sys.exit(-1)
    verbose = not options.quiet

    # Create NIname instance for supplied URL and validate it
    ni_url = NIname(args[0])

    # Must be a complete ni: URL with non-empty params field
    rv = ni_url.validate_ni_url(has_params = True)
    if (rv != ni_errs.niSUCCESS):
        if verbose:
            print("Error: %s is not a complete, valid ni scheme URL: %s" % (ni_url.get_url(), ni_errs_txt[rv]))
        sys.exit(-2)

    # Generate file name for output if not specified
    if (options.file_name == None):
        options.file_name = ni_url.get_digest()

    # Decide Convergence Layer to use and locator to access
    netloc = ni_url.get_netloc()
    cl = HTTP_SCHEME
    if netloc == "":
        # Must have -w option
        if options.loc is None:
            if verbose:
                print("Error: Must provide a locator either in ni URI or via -w/--whence")
            sys.exit(-3)
        loc = options.loc.lower()
    elif options.loc is not None:
        if verbose:
            print("Warning: -w/--whence locator option overrides netloc in ni URI")
        loc = options.loc.lower()
    else:
        loc = netloc.lower()

    # See if URI scheme was specified
    if loc.startswith(HTTP_SCHEME):
        loc = loc[len(HTTP_SCHEME):]
    elif loc.startswith(DTN_SCHEME):
        loc = loc[len(DTN_SCHEME):]
        cl = DTN_SCHEME
    else:
        ssep = loc.find("://")
        if ssep != -1:
            if verbose:
                print("Error: Convergence Layer for scheme %s is not supported - use dtn or http" %
                      loc[:ssep])
            sys.exit(-4)
        # Default assume HTTP

    # Action the GET according to CL selected
    if cl == HTTP_SCHEME:
        json_report, got_content, faulty = get_via_http(ni_url, loc,
                                                        options.file_name,
                                                        verbose, options.lax)
    else:
        json_report, got_content, faulty = get_via_dtn(ni_url, loc,
                                                       options.file_name,
                                                       verbose, options.lax)

    if options.view:
        print("Returned metadata for %s:" % args[0])
        print json.dumps(json_report, indent = 4)
    elif options.metadata:
        print json.dumps(json_report, separators=(",", ":"))

    if not got_content:
        rv = 1
    elif faulty:
        rv = 2
    else:
        rv = 0

    if verbose and got_content:
        if not faulty:
            print("Content successfully retrieved and placed in file %s." %
                  options.file_name)
        else:
            print("Content retrieved and placed in file %s but digest didn't verify." %
                  options.file_name)
    elif verbose:
        print("Only metadata retrieved")

    sys.exit(rv)
コード例 #34
0
ファイル: gmail.py プロジェクト: Zabrane/appengine-mailer
        data = urllib.urlencode([(k, v.encode('utf-8')) for k, v in values.items()])
        status, errmsg = self.connection.make_request(data)

        if status != 204:
            if not self.fail_silently:
                raise MessageSendingFailure(errmsg)
            else:
                return False
        return True


if __name__ == '__main__':
    """mail -s [space-separated to-addresses] to-address
       and the message on stdin"""
    parser = optparse.OptionParser()
    parser.add_option("-s", dest="subject", help="subject of message")
    parser.add_option("--fix-sender", action="store_true", dest="fix_sender",
                      help="If sender is not authorized, replace From with an authorized sender")
    options, to_addresses = parser.parse_args()
    if to_addresses:
        msg = email.message.Message()
        msg['From'] = os.environ['USER']
        msg['To'] = ",".join(to_addresses) # escaping necessary?
        msg['Subject'] = options.subject
        msg.set_payload(sys.stdin.read())
    else:
        # We're expecting a whole message on stdin:
        msg = email.parser.Parser().parse(sys.stdin)
        recipient = os.environ.get('RECIPIENT')
        if recipient:
            msg['To'] = recipient
コード例 #35
0
def main():
    global monitor
    parser = OptionParser(version='%prog v' + __version__)
    parser.add_option('-c', '--config', default='config.ini',
                      help='Location of config file (default: %default)', 
                      metavar='FILE')
    parser.add_option('-v', '--verbose', action="store_true", dest="verbose",
                      default=False, help='Print out extra info')
    parser.add_option('-q', '--quiet', action="store_true", dest="quiet",
                      default=False, help="Don't print anything to screen")
    (options, args) = parser.parse_args()
    
    config = ConfigParser()
    config.read(options.config)
    #username = config.get('jabberbot', 'username')
    #password = config.get('jabberbot', 'password')
    #adminjid = config.get('jabberbot', 'adminjid')
    imap_server = config.get('mail', 'server')
    imap_ssl = config.getboolean('mail', 'ssl')
    imap_user = config.get('mail', 'user')
    imap_password = config.get('mail', 'password')

    # configure logging
    LEVELS = {'debug': logging.DEBUG,
              'info': logging.INFO,
              'warning': logging.WARNING,
              'error': logging.ERROR,
              'critical': logging.CRITICAL}
    level = LEVELS.get(config.get('logging', 'level'), logging.NOTSET)
    # file logging
    logging.basicConfig(level=level,
                        format="%(asctime)s: %(name)s: %(levelname)s: %(message)s",
                        filename=config.get('logging', 'file'),
                        filemode='a')
    # log to console
    if not options.quiet:
        consolelog = logging.StreamHandler()
        if options.verbose:
            consolelog.setLevel(logging.DEBUG)
        else:
            # don't think I even have any critical level alerts right now
            consolelog.setLevel(logging.INFO)
        consolelog.setFormatter(logging.Formatter("%(levelname)-8s: %(message)s"))
        logging.getLogger('').addHandler(consolelog)
    logmain = logging.getLogger('main')

    monitor = ImapMonitor(server=imap_server, ssl=imap_ssl, user=imap_user, 
                          password=imap_password)

    signal.signal(signal.SIGTERM, sigterm_handler)
    while True:
        try:
            monitor.run_forever()
        except imaplib.IMAP4.abort, e:
            # server error, need to start over
            logmain.warning("Server error: %s " % e)
            logmain.info("reconnecting in 30 seconds")
            time.sleep(30)
            monitor.__init__(server=imap_server, ssl=imap_ssl, user=imap_user, 
                             password=imap_password)
        except KeyboardInterrupt, e:
            # Ctrl-c
            logmain.info('Received ctrl-c or SIGINT, cleaning up')
            monitor.cleanup()
            # not raising since everything has been handled
            sys.exit(0)
コード例 #36
0
ファイル: imapdedup.py プロジェクト: LLuke/IMAPdedup
def get_arguments(args):
    # Get arguments and create link to server
    from optparse import OptionParser
    parser = OptionParser(usage="%prog [options] <mailboxname> [<mailboxname> ...]")
    parser.add_option("-P", "--process",dest='process', help='IMAP process to access mailboxes')
    parser.add_option("-s", "--server",dest='server',help='IMAP server')
    parser.add_option("-p", "--port",  dest='port',  help='IMAP server port', type='int')
    parser.add_option("-x", "--ssl",   dest='ssl',   action="store_true", help='Use SSL')
    parser.add_option("-u", "--user",  dest='user',  help='IMAP user name')
    parser.add_option("-w", "--password", dest='password',  help='IMAP password (Will prompt if not specified)')
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="Verbose mode")
    parser.add_option("-n", "--dry-run", dest="dry_run", action="store_true",
                        help="Don't actually do anything, just report what would be done")
    parser.add_option("-c", "--checksum", dest="use_checksum", action="store_true",
                        help="Use a checksum of several mail headers, instead of the Message-ID")
    parser.add_option("-m", "--checksum-with-id", dest="use_id_in_checksum", action="store_true",
                        help="Include the Message-ID (if any) in the -c checksum.")
    parser.add_option("",   "--no-close",  dest='no_close', action="store_true",
                        help='Do not "close" mailbox when done. Some servers will purge deleted messages on a close command.')
    parser.add_option("-l", "--list", dest="just_list", action="store_true",
                                            help="Just list mailboxes")

    parser.set_defaults(verbose=False, ssl=False, dry_run=False, no_close=False, just_list=False)
    (options, mboxes) = parser.parse_args(args)
    if ((not options.server) or (not options.user)) and not options.process:
        sys.stderr.write("\nError: Must specify server, user, and at least one mailbox.\n\n")
        parser.print_help()
        sys.exit(1)
    if not options.password and not options.process:
        # Read from IMAPDEDUP_PASSWORD env variable, or prompt for one.
        options.password = os.getenv("IMAPDEDUP_PASSWORD") or getpass.getpass()

    if options.use_id_in_checksum and not options.use_checksum:
        sys.stderr.write("\nError: If you use -m you must also use -c.\n")
        sys.exit(1)

    return (options, mboxes)
コード例 #37
0
ファイル: nipubalt.py プロジェクト: skunkwerks/netinf
def py_nipubalt():
    """
    @brief Command line program to perform a NetInf 'publish' operation using http
    @brief convergence layer.
    
    Uses NIproc global instance of NI operations class

    Run:
    
    >  nipubalt.py --help

    to see usage and options.

    Exit code is 0 for success, 1 if HTTP returned something except 200,
    and negative for local errors.
    """
    
    # Options parsing and verification stuff
    usage = "%%prog %s\n       %%prog %s\n%s\n%s" % \
            ("[-q] [-e] [-j|-v|-w|-p] -f <pathname of content file> -d <digest alg> [-l <FQDN - locator>]{1,2}",
             "[-q] [-e] [-j|-v|-w|-p] [-f <pathname of content file>] -n <ni name> [-l <FQDN - locator>]{0,2}",
             "          -- publish file via NI URI over HTTP and/or DTN",
             "At least one locator must be given either as part of the -n option or via a -l option.\n"
             "Locators given with -l options can optionally be prefixed with the HTTP scheme (http://) or \n"
             "the DTN scheme (dtn://).  If a -l option is given, this is used to determine the initial\n"
             "publication destination and the convergence layer used will be HTPP unless the -l option\n"
             "explicitly gives the DTN scheme prefix.  If there are no -l options but the -n option has\n"
             "a netloc compnent (FQDN or IP address with optional port) the this will be used with the\n"
             "HTTP convergence layer\n"
             "The response will be sent as HTML document (-w), plain text (-p), or JSON (-v or -j)\n"
             "Unless -q is specified, the response is sent to standard output.\n"
             "For a JSON response, it can either be output as a 'raw' JSON string (-j) or pretty printed (-v).\n"
             "If none of  -j, -v, -w or -p are specified, a raw JSON response will be requested.")
    parser = OptionParser(usage)
    
    parser.add_option("-f", "--file", dest="file_name",
                      type="string",
                      help="Pathname for local file to be published.")
    parser.add_option("-d", "--digest", dest="hash_alg",
                      type="string",
                      help="Digest algorithm to be used to hash content "
                           "and create NI URI. Defaults to sha-256.")
    parser.add_option("-n", "--name", dest="ni_name",
                      type="string",
                      help="Complete ni name. If specified with a file or "
                           "HTTP URL, the digest generated from the content "
                           "will be checked against th digest in the name.")
    parser.add_option("-e", "--ext", dest="ext",
                      type="string",
                      help="A JSON encoded object to be sent as the 'ext' "
                           "parameter for the Publish message.")
    parser.add_option("-l", "--loc", dest="locs", action="append",
                      type="string",
                      help="A locator where NI might be retrieved. Maybe be "
                           "zero to two if -n is present and has a non-empty netloc. "
                           "Otherwise must be one or two. HTTP or DTN is sent to first "
                           "loc if present. Otherwise sent to netloc (authority) in -n."
                           "NOTE: this precedence differs from earlier versions of nipub.")
    parser.add_option("-q", "--quiet", default=False,
                      action="store_true", dest="quiet",
                      help="Suppress textual output")
    parser.add_option("-j", "--json", default=False,
                      action="store_true", dest="json_raw",
                      help="Request response as JSON string and output raw JSON "
                           "string returned on stdout.")
    parser.add_option("-v", "--view", default=False,
                      action="store_true", dest="json_pretty",
                      help="Request response as JSON string and pretty print "
                           "JSON string returned on stdout.")
    parser.add_option("-w", "--web", default=False,
                      action="store_true", dest="html",
                      help="Request response as HTML document and output HTML "
                           "returned on stdout.")
    parser.add_option("-p", "--plain", default=False,
                      action="store_true", dest="plain",
                      help="Request response as plain text document and output text "
                           "returned on stdout.")


    (options, args) = parser.parse_args()

    # Check command line options:
    # Arguments -q, -e, -w, -p, -j and -v are optional; there must be one of a -n with an authority in it or at least one -l.
    # If -n option is specified then there must not be a -d.
    # If -d is specified, there must be a -f.
    # If -n is specified, -f may be specified - otherwise only metadata is published. No leftover arguments allowed.
    # Specifying more than one of -w, -p, -j and -v is inappropriate.
    if len(args) != 0:
        parser.error("Unrecognized arguments %s supplied." % str(args))
        sys.exit(-1)
    if ((options.locs is not None) and (len(options.locs) > 2)):
        parser.error("Initial version only supports two locators (-l/--loc).")
        sys.exit(-1)
    if ((options.ni_name == None) and (options.locs == None)):
        parser.error("Must specify a locator (-l/--loc) or a name (-n/--name) with a netloc component to define where to send the request.")
        sys.exit(-1)
    if ((options.hash_alg != None) and (options.ni_name != None)):
        parser.error("Cannot specify both digest algorithm to be used (-d) and complete ni name with algorithm and digest (-n).")
        sys.exit(-1)
    fc = 0
    for flag in [options.json_raw, options.json_pretty, options.html, options.plain]:
        if flag:
            fc += 1
    if fc > 1:
        parser.error("Should specify at most one response type argument out of -j, -v, -w and -p.")
        sys.exit(-1)

    file_name = None
    
    if options.file_name != None:
        file_name = os.path.abspath(options.file_name)
        # Check the file is readable
        if not os.access(file_name, os.R_OK):
            if verbose:
                print("File to be published %s is not readable" % file_name)
            sys.exit(1)
        full_put = True
    else:
        full_put = False
    debug("full_put: %s" %full_put)

    verbose = not options.quiet

    if ((options.locs is not None) and (len(options.locs) > 2)):
        if verbose:
            print "Warning: only first two -l/--loc locators will be published"

    #  If we have a full ni name (-n option) given..
    if options.ni_name is not None:
        # Check the validity of the ni name
        try:
            ni_name = NIname(options.ni_name)
        except Exception, e:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" %
                      options.ni_name)
            sys.exit(-3)
        rv = ni_name.validate_ni_url()
        if rv != ni_errs.niSUCCESS:
            if verbose:
                print("Error: value of -n/--name option '%s' is not a valid ni name" %
                      options.ni_name)
            sys.exit(-3)

        # Extract the scheme and hash algorithm from the name
        scheme = ni_name.get_scheme()
        hash_alg = ni_name.get_alg_name()

        # If there is a -l option, that is where the request is sent.
        nl = ni_name.get_netloc()
        if ((options.locs is None) and (nl == "")) :
            print("Error: name (-n/--name) must have a netloc if no locator options given,")
            sys.exit(-4)
        # NOTE: The following logic ie reversed from earlier versions so that 
        # can force use of DTN convergence layer with a -l option.
        if nl == "":
            # Already checked this exists
            destination = options.locs[0]
        else:
            destination = nl
        authority = nl