Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.io.*;
  33. import java.util.*;
  34. import javax.mail.*;
  35. import javax.mail.internet.*;
  36.  
  37. /* MOVE messages between mailboxes */
  38.  
  39. public class mover {
  40.  
  41.     static String protocol = "imap";
  42.     static String host = null;
  43.     static String user = null;
  44.     static String password = null;
  45.     static String src = null;
  46.     static String dest = null;
  47.     static boolean expunge = false;
  48.     static String url = null;
  49.  
  50.     public static void main(String argv[]) {
  51.         int start = 1; int end = -1;
  52.         int optind;
  53.  
  54.         for (optind = 0; optind < argv.length; optind++) {
  55.             if (argv[optind].equals("-T")) {        // protocol
  56.                 protocol = argv[++optind];
  57.             } else if (argv[optind].equals("-H")) { // host
  58.                 host = argv[++optind];
  59.             } else if (argv[optind].equals("-U")) { // user
  60.                 user = argv[++optind];
  61.             } else if (argv[optind].equals("-P")) { // password
  62.                 password = argv[++optind];
  63.             } else if (argv[optind].equals("-L")) {
  64.                 url = argv[++optind];
  65.             } else if (argv[optind].equals("-s")) { // Source mbox
  66.                 src = argv[++optind];
  67.             } else if (argv[optind].equals("-d")) { // Destination mbox
  68.                 dest = argv[++optind];
  69.             } else if (argv[optind].equals("-x")) { // Expunge ?
  70.                 expunge = true;
  71.             } else if (argv[optind].equals("--")) {
  72.                 optind++;
  73.                 break;
  74.             } else if (argv[optind].startsWith("-")) {
  75.                 System.out.println(
  76. "Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]");
  77.                 System.out.println(
  78. "\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]");
  79.                 System.out.println(
  80. "\t The -x option => EXPUNGE deleted messages");
  81.                 System.out.println(
  82. "\t msgnum1 => start of message-range; msgnum2 => end of message-range");
  83.                 System.exit(1);
  84.             } else {
  85.                 break;
  86.             }
  87.         }
  88.  
  89.         if (optind < argv.length)
  90.             start = Integer.parseInt(argv[optind++]); // start msg
  91.  
  92.         if (optind < argv.length)
  93.             end = Integer.parseInt(argv[optind++]);   // end msg
  94.  
  95.         try {
  96.             // Get a Properties object
  97.             Properties props = System.getProperties();
  98.  
  99.             // Get a Session object
  100.             Session session = Session.getInstance(props, null);
  101.  
  102.             // Get a Store object
  103.             Store store = null;
  104.             if (url != null) {
  105.                 URLName urln = new URLName(url);
  106.                 store = session.getStore(urln);
  107.                 store.connect();
  108.             } else {
  109.                 if (protocol != null)          
  110.                     store = session.getStore(protocol);
  111.                 else
  112.                     store = session.getStore();
  113.  
  114.                 // Connect
  115.                 if (host != null || user != null || password != null)
  116.                     store.connect(host, user, password);
  117.                 else
  118.                     store.connect();
  119.             }
  120.            
  121.  
  122.             // Open source Folder
  123.             Folder folder = store.getFolder(src);
  124.             if (folder == null || !folder.exists()) {
  125.                 System.out.println("Invalid folder: " + src);
  126.                 System.exit(1);
  127.             }
  128.  
  129.             folder.open(Folder.READ_WRITE);
  130.  
  131.             int count = folder.getMessageCount();
  132.             if (count == 0) { // No messages in the source folder
  133.                 System.out.println(folder.getName() + " is empty");
  134.                 // Close folder, store and return
  135.                 folder.close(false);
  136.                 store.close();
  137.                 return;
  138.             }
  139.  
  140.             // Open destination folder, create if reqd
  141.             Folder dfolder = store.getFolder(dest);
  142.             if (!dfolder.exists())
  143.                 dfolder.create(Folder.HOLDS_MESSAGES);
  144.  
  145.             if (end == -1)
  146.                 end = count;
  147.  
  148.             // Get the message objects to copy
  149.             Message[] msgs = folder.getMessages(start, end);
  150.             System.out.println("Moving " + msgs.length + " messages");
  151.  
  152.             if (msgs.length != 0) {
  153.                 folder.copyMessages(msgs, dfolder);
  154.                 folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
  155.  
  156.                 // Dump out the Flags of the moved messages, to insure that
  157.                 // all got deleted
  158.                 for (int i = 0; i < msgs.length; i++) {
  159.                     if (!msgs[i].isSet(Flags.Flag.DELETED))
  160.                         System.out.println("Message # " + msgs[i] +
  161.                                                 " not deleted");
  162.                 }
  163.             }
  164.            
  165.             // Close folders and store
  166.             folder.close(expunge);
  167.             store.close();
  168.  
  169.         } catch (MessagingException mex) {
  170.             Exception ex = mex;
  171.             do {
  172.                 System.out.println(ex.getMessage());
  173.                 if (ex instanceof MessagingException)
  174.                     ex = ((MessagingException)ex).getNextException();
  175.                 else
  176.                     ex = null;
  177.             } while (ex != null);
  178.         }
  179.     }
  180. }
  181.