KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mover


1 /*
2  * @(#)mover.java 1.9 05/11/17
3  *
4  * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.io.*;
40 import java.util.*;
41 import javax.mail.*;
42 import javax.mail.internet.*;
43
44 /* MOVE messages between mailboxes */
45
46 public class mover {
47
48     static String JavaDoc protocol = "imap";
49     static String JavaDoc host = null;
50     static String JavaDoc user = null;
51     static String JavaDoc password = null;
52     static String JavaDoc src = null;
53     static String JavaDoc dest = null;
54     static boolean expunge = false;
55     static String JavaDoc url = null;
56
57     public static void main(String JavaDoc argv[]) {
58     int start = 1; int end = -1;
59     int optind;
60
61     for (optind = 0; optind < argv.length; optind++) {
62         if (argv[optind].equals("-T")) { // protocol
63
protocol = argv[++optind];
64         } else if (argv[optind].equals("-H")) { // host
65
host = argv[++optind];
66         } else if (argv[optind].equals("-U")) { // user
67
user = argv[++optind];
68         } else if (argv[optind].equals("-P")) { // password
69
password = argv[++optind];
70         } else if (argv[optind].equals("-L")) {
71         url = argv[++optind];
72         } else if (argv[optind].equals("-s")) { // Source mbox
73
src = argv[++optind];
74         } else if (argv[optind].equals("-d")) { // Destination mbox
75
dest = argv[++optind];
76         } else if (argv[optind].equals("-x")) { // Expunge ?
77
expunge = true;
78         } else if (argv[optind].equals("--")) {
79         optind++;
80         break;
81         } else if (argv[optind].startsWith("-")) {
82         System.out.println(
83 "Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]");
84         System.out.println(
85 "\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]");
86         System.out.println(
87 "\t The -x option => EXPUNGE deleted messages");
88         System.out.println(
89 "\t msgnum1 => start of message-range; msgnum2 => end of message-range");
90         System.exit(1);
91         } else {
92         break;
93         }
94     }
95
96     if (optind < argv.length)
97         start = Integer.parseInt(argv[optind++]); // start msg
98

99     if (optind < argv.length)
100         end = Integer.parseInt(argv[optind++]); // end msg
101

102     try {
103         // Get a Properties object
104
Properties props = System.getProperties();
105
106         // Get a Session object
107
Session session = Session.getInstance(props, null);
108
109         // Get a Store object
110
Store store = null;
111         if (url != null) {
112         URLName urln = new URLName(url);
113         store = session.getStore(urln);
114         store.connect();
115         } else {
116         if (protocol != null)
117             store = session.getStore(protocol);
118         else
119             store = session.getStore();
120
121         // Connect
122
if (host != null || user != null || password != null)
123             store.connect(host, user, password);
124         else
125             store.connect();
126         }
127         
128
129         // Open source Folder
130
Folder folder = store.getFolder(src);
131         if (folder == null || !folder.exists()) {
132             System.out.println("Invalid folder: " + folder.getName());
133             System.exit(1);
134         }
135
136         folder.open(Folder.READ_WRITE);
137
138         int count = folder.getMessageCount();
139         if (count == 0) { // No messages in the source folder
140
System.out.println(folder.getName() + " is empty");
141         // Close folder, store and return
142
folder.close(false);
143         store.close();
144         return;
145         }
146
147         // Open destination folder, create if reqd
148
Folder dfolder = store.getFolder(dest);
149         if (!dfolder.exists())
150         dfolder.create(Folder.HOLDS_MESSAGES);
151
152         if (end == -1)
153         end = count;
154
155         // Get the message objects to copy
156
Message[] msgs = folder.getMessages(start, end);
157         System.out.println("Moving " + msgs.length + " messages");
158
159         if (msgs.length != 0) {
160         folder.copyMessages(msgs, dfolder);
161         folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
162
163         // Dump out the Flags of the moved messages, to insure that
164
// all got deleted
165
for (int i = 0; i < msgs.length; i++) {
166             if (!msgs[i].isSet(Flags.Flag.DELETED))
167             System.out.println("Message # " + msgs[i] +
168                         " not deleted");
169         }
170         }
171         
172         // Close folders and store
173
folder.close(expunge);
174         store.close();
175
176     } catch (MessagingException mex) {
177         Exception JavaDoc ex = mex;
178         do {
179         System.out.println(ex.getMessage());
180         if (ex instanceof MessagingException)
181             ex = ((MessagingException)ex).getNextException();
182         else
183             ex = null;
184         } while (ex != null);
185     }
186     }
187 }
188
Popular Tags