001package votorola.s.line; // Copyright 2007, 2011, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.util.*;
004import javax.mail.*;
005import javax.mail.internet.*;
006import votorola.a.*;
007import votorola.a.response.line.*;
008import votorola.s.mail.*;
009import votorola.g.lang.*;
010import votorola.g.logging.*;
011import votorola.g.option.*;
012
013
014/** Main class of the executable <code>vodeliver</code> - a test tool to deliver
015  * email messages to a mailbox.
016  *
017  *     @see <a href='../../../../../s/manual.xht#line-vodeliver' target='_top'>vodeliver</a>
018  */
019public @ThreadSafe final class VODeliver
020{
021
022    private VODeliver() {}
023
024
025
026    /** Runs the tool from the command line.
027      *
028      *     @param argv command line argument array
029      */
030    public static void main( String[] argv )
031    {
032        LoggerX.i(VODeliver.class).info( "vodeliver is running with arguments " + Arrays.toString( argv ));
033        final Map<String,Option> optionMap = CommandLine.compileBaseOptions();
034        final int aFirstNonOption = GetoptX.parse( "vodeliver", argv, optionMap );
035        if( optionMap.get("help").hasOccured() )
036        {
037            System.out.print
038            (
039              "Usage: cat MESSAGE-FILE | vodeliver [maildir:/INBOX-PATH | INBOX-URL]\n" +
040              "  or   cat MESSAGE-FILE | vodeliver # to voface-mail inbox\n" +
041              "Deliver an email test message to an inbox.  See also your:\n" +
042              "http://reluk.ca/project/votorola/_/javadoc/votorola/s/mail/VOFaceMail.html#inboxStoreURLName()\n"
043            );
044            System.exit( 0 );
045        }
046
047        try
048        {
049            String inboxURL = null; // final after init
050            {
051                final int addressCount = argv.length - aFirstNonOption;
052                if( addressCount > 1 )
053                {
054                    System.err.println( "vodeliver: too many arguments" );
055                    System.err.println( GetoptX.createHelpPrompt( "vodeliver" ));
056                    System.exit( 1 );
057                }
058                else if( addressCount == 1 ) inboxURL = argv[aFirstNonOption];
059                else
060                {
061                    final VoteServer voteServer = new VoteServer( System.getProperty( "user.name" ));
062                    final VOFaceMail.ConstructionContext cc = VOFaceMail.ConstructionContext.configure(
063                      voteServer, VOFaceMail.compileConfigurationScript( voteServer ));
064                    inboxURL = cc.getInboxStoreURLName();
065                }
066            }
067
068            final Session session = Session.getDefaultInstance( new Properties() );
069            final Store store = session.getStore( new URLName( inboxURL ));
070            store.connect();
071            try
072            {
073             // final Folder folder = StoreX.ensureDefaultFolder( store );
074             /// for Maildir, that now gives the 'cur' directory.  This gives 'new':
075                final Folder folder = store.getDefaultFolder();
076                folder.open( Folder.READ_WRITE );
077
078                final MimeMessage message = new MimeMessage( session, System.in );
079                folder.appendMessages( new Message[]{ message });
080            }
081            finally{ store.close(); }
082        }
083        catch( RuntimeException x ) { throw x; }
084        catch( Exception x ) // ScriptException, MessagingException
085        {
086            x.printStackTrace( System.err );
087            System.exit( 1 );
088        }
089    }
090
091
092
093}