Debian Codegraph @

demo/src/main/java/mover.java

raw
1 /*
2 * Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0, which is available at
6 * http://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 import java.io.*;
12 import java.util.*;
13 import jakarta.mail.*;
14 import jakarta.mail.internet.*;
15
16 /* MOVE messages between mailboxes */
17
18 public class mover {
19
20 static String protocol = "imap";
21 static String host = null;
22 static String user = null;
23 static String password = null;
24 static String src = null;
25 static String dest = null;
26 static boolean expunge = false;
27 static String url = null;
28
29 public static void main(String argv[]) {
30 int start = 1; int end = -1;
31 int optind;
32
33 for (optind = 0; optind < argv.length; optind++) {
34 if (argv[optind].equals("-T")) { // protocol
35 protocol = argv[++optind];
36 } else if (argv[optind].equals("-H")) { // host
37 host = argv[++optind];
38 } else if (argv[optind].equals("-U")) { // user
39 user = argv[++optind];
40 } else if (argv[optind].equals("-P")) { // password
41 password = argv[++optind];
42 } else if (argv[optind].equals("-L")) {
43 url = argv[++optind];
44 } else if (argv[optind].equals("-s")) { // Source mbox
45 src = argv[++optind];
46 } else if (argv[optind].equals("-d")) { // Destination mbox
47 dest = argv[++optind];
48 } else if (argv[optind].equals("-x")) { // Expunge ?
49 expunge = true;
50 } else if (argv[optind].equals("--")) {
51 optind++;
52 break;
53 } else if (argv[optind].startsWith("-")) {
54 System.out.println(
55 "Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]");
56 System.out.println(
57 "\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]");
58 System.out.println(
59 "\t The -x option => EXPUNGE deleted messages");
60 System.out.println(
61 "\t msgnum1 => start of message-range; msgnum2 => end of message-range");
62 System.exit(1);
63 } else {
64 break;
65 }
66 }
67
68 if (optind < argv.length)
69 start = Integer.parseInt(argv[optind++]); // start msg
70
71 if (optind < argv.length)
72 end = Integer.parseInt(argv[optind++]); // end msg
73
74 try {
75 // Get a Properties object
76 Properties props = System.getProperties();
77
78 // Get a Session object
79 Session session = Session.getInstance(props, null);
80
81 // Get a Store object
82 Store store = null;
83 if (url != null) {
84 URLName urln = new URLName(url);
85 store = session.getStore(urln);
86 store.connect();
87 } else {
88 if (protocol != null)
89 store = session.getStore(protocol);
90 else
91 store = session.getStore();
92
93 // Connect
94 if (host != null || user != null || password != null)
95 store.connect(host, user, password);
96 else
97 store.connect();
98 }
99
100
101 // Open source Folder
102 Folder folder = store.getFolder(src);
103 if (folder == null || !folder.exists()) {
104 System.out.println("Invalid folder: " + src);
105 System.exit(1);
106 }
107
108 folder.open(Folder.READ_WRITE);
109
110 int count = folder.getMessageCount();
111 if (count == 0) { // No messages in the source folder
112 System.out.println(folder.getName() + " is empty");
113 // Close folder, store and return
114 folder.close(false);
115 store.close();
116 return;
117 }
118
119 // Open destination folder, create if reqd
120 Folder dfolder = store.getFolder(dest);
121 if (!dfolder.exists())
122 dfolder.create(Folder.HOLDS_MESSAGES);
123
124 if (end == -1)
125 end = count;
126
127 // Get the message objects to copy
128 Message[] msgs = folder.getMessages(start, end);
129 System.out.println("Moving " + msgs.length + " messages");
130
131 if (msgs.length != 0) {
132 folder.copyMessages(msgs, dfolder);
133 folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
134
135 // Dump out the Flags of the moved messages, to insure that
136 // all got deleted
137 for (int i = 0; i < msgs.length; i++) {
138 if (!msgs[i].isSet(Flags.Flag.DELETED))
139 System.out.println("Message # " + msgs[i] +
140 " not deleted");
141 }
142 }
143
144 // Close folders and store
145 folder.close(expunge);
146 store.close();
147
148 } catch (MessagingException mex) {
149 Exception ex = mex;
150 do {
151 System.out.println(ex.getMessage());
152 if (ex instanceof MessagingException)
153 ex = ((MessagingException)ex).getNextException();
154 else
155 ex = null;
156 } while (ex != null);
157 }
158 }
159 }