#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# File: dropemail
#
# Copyright (c) InQuant GmbH
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

__author__    = """Hans-Peter Locher<hans-peter.locher@inquant.de>"""
__docformat__ = 'plaintext'
__revision__  = "$Revision: 36831 $"
__version__   = '$Revision: 1.7 $'[11:-2]


import sys
import xmlrpclib
import optparse

################################################################
def main():
    """ use script to drop a mail to a plone instance's inbox folder, 
        remember to specify username and password in the url. 
    """
    # setup options
    option_parser = optparse.OptionParser(
            usage="""usage:%prog[options]"""
    )

    option_parser.add_option( "-u", "--url", dest="url",
            default=None,
            help="url of the plone instance's inbox, don't forget username and password, example: http://usr:pwd@host/plonesite/inbox" )

    option_parser.add_option( "-f", "--file", dest="mailfile",
            default=None,
            help="mailfile, if omitted uses stdin" )

    (options, args) = option_parser.parse_args()

    if not options.url:
        option_parser.print_help()
        print >>sys.stderr, '\nERROR: please specify a url'
        return 1
    if options.url:
        url = options.url

    mailfile = sys.stdin
    if options.mailfile:
        mailfile = file(options.mailfile, "r")
   
    #append viewname to url:
    if url.endswith("/"):
        url += "xmlrpcview"
    else:
        url += "/xmlrpcview"

    try:
        # connect to server and drop email
        server = xmlrpclib.Server(url)
        server.drop(mailfile.read())
    except Exception, e:
        raise e

################################################################
if __name__ == "__main__":
    main()

# vim: set ft=python ts=4 sw=4 expandtab :
