| 1 |
/* |
| 2 |
* Copyright (c) 1997, 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.util.*; |
| 12 |
import java.text.*; |
| 13 |
import jakarta.mail.*; |
| 14 |
|
| 15 |
/** |
| 16 |
* |
| 17 |
* Split mail folders according to date of messages. |
| 18 |
* |
| 19 |
* @author Bill Shannon |
| 20 |
*/ |
| 21 |
|
| 22 |
public class foldersplit { |
| 23 |
|
| 24 |
static String protocol; |
| 25 |
static String host = null; |
| 26 |
static String user = null; |
| 27 |
static String password = null; |
| 28 |
static String dst = null; |
| 29 |
static String url = null; |
| 30 |
static int port = -1; |
| 31 |
static boolean verbose = false; |
| 32 |
static boolean debug = false; |
| 33 |
static boolean nop = false; |
| 34 |
static SimpleDateFormat df = new SimpleDateFormat("yyyy.MM"); |
| 35 |
|
| 36 |
public static void main(String argv[]) { |
| 37 |
int optind; |
| 38 |
|
| 39 |
for (optind = 0; optind < argv.length; optind++) { |
| 40 |
if (argv[optind].equals("-T")) { |
| 41 |
protocol = argv[++optind]; |
| 42 |
} else if (argv[optind].equals("-H")) { |
| 43 |
host = argv[++optind]; |
| 44 |
} else if (argv[optind].equals("-U")) { |
| 45 |
user = argv[++optind]; |
| 46 |
} else if (argv[optind].equals("-P")) { |
| 47 |
password = argv[++optind]; |
| 48 |
} else if (argv[optind].equals("-d")) { |
| 49 |
dst = argv[++optind]; |
| 50 |
} else if (argv[optind].equals("-v")) { |
| 51 |
verbose = true; |
| 52 |
} else if (argv[optind].equals("-n")) { |
| 53 |
nop = true; |
| 54 |
} else if (argv[optind].equals("-D")) { |
| 55 |
debug = true; |
| 56 |
} else if (argv[optind].equals("-L")) { |
| 57 |
url = argv[++optind]; |
| 58 |
} else if (argv[optind].equals("-p")) { |
| 59 |
port = Integer.parseInt(argv[++optind]); |
| 60 |
} else if (argv[optind].equals("--")) { |
| 61 |
optind++; |
| 62 |
break; |
| 63 |
} else if (argv[optind].startsWith("-")) { |
| 64 |
System.out.println( |
| 65 |
"Usage: foldersplit [-L url] [-T protocol] [-H host] [-p port] [-U user]"); |
| 66 |
System.out.println( |
| 67 |
"\t[-P password] [-v] [-D] folder ..."); |
| 68 |
System.exit(1); |
| 69 |
} else { |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
try { |
| 75 |
// Get a Properties object |
| 76 |
Properties props = System.getProperties(); |
| 77 |
|
| 78 |
// Get a Session object |
| 79 |
Session session = Session.getDefaultInstance(props, null); |
| 80 |
session.setDebug(debug); |
| 81 |
|
| 82 |
// Get a Store object |
| 83 |
Store store = null; |
| 84 |
if (url != null) { |
| 85 |
URLName urln = new URLName(url); |
| 86 |
store = session.getStore(urln); |
| 87 |
store.connect(); |
| 88 |
} else { |
| 89 |
if (protocol != null) |
| 90 |
store = session.getStore(protocol); |
| 91 |
else |
| 92 |
store = session.getStore(); |
| 93 |
|
| 94 |
// Connect |
| 95 |
if (host != null || user != null || password != null) |
| 96 |
store.connect(host, port, user, password); |
| 97 |
else |
| 98 |
store.connect(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
// Open the Folder |
| 103 |
|
| 104 |
Folder deffolder = store.getDefaultFolder(); |
| 105 |
if (deffolder == null) { |
| 106 |
System.out.println("No default folder"); |
| 107 |
System.exit(1); |
| 108 |
} |
| 109 |
Folder dstfolder; |
| 110 |
if (dst == null) { |
| 111 |
dstfolder = deffolder; |
| 112 |
} else { |
| 113 |
dstfolder = deffolder.getFolder(dst); |
| 114 |
if (dstfolder == null) { |
| 115 |
System.out.println("No destination folder"); |
| 116 |
System.exit(1); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
Folder outf = null; |
| 121 |
for (; optind < argv.length; optind++) { |
| 122 |
Folder folder = deffolder.getFolder(argv[optind]); |
| 123 |
if (folder == null) { |
| 124 |
System.out.println("Invalid folder: " + argv[optind]); |
| 125 |
continue; |
| 126 |
} |
| 127 |
System.out.println("Folder: " + folder.getFullName()); |
| 128 |
|
| 129 |
folder.open(Folder.READ_ONLY); |
| 130 |
int totalMessages = folder.getMessageCount(); |
| 131 |
|
| 132 |
if (totalMessages == 0) { |
| 133 |
System.out.println("Empty folder"); |
| 134 |
folder.close(false); |
| 135 |
continue; |
| 136 |
} |
| 137 |
|
| 138 |
if (verbose) { |
| 139 |
int newMessages = folder.getNewMessageCount(); |
| 140 |
System.out.println("Total messages = " + totalMessages); |
| 141 |
System.out.println("New messages = " + newMessages); |
| 142 |
System.out.println("-------------------------------"); |
| 143 |
} |
| 144 |
|
| 145 |
// Attributes & Flags for all messages .. |
| 146 |
Message[] msgs = folder.getMessages(); |
| 147 |
|
| 148 |
// Use a suitable FetchProfile |
| 149 |
FetchProfile fp = new FetchProfile(); |
| 150 |
fp.add(FetchProfile.Item.ENVELOPE); |
| 151 |
fp.add(FetchProfile.Item.FLAGS); |
| 152 |
folder.fetch(msgs, fp); |
| 153 |
|
| 154 |
for (int i = 0; i < msgs.length; i++) { |
| 155 |
if (verbose) { |
| 156 |
System.out.println("--------------------------"); |
| 157 |
System.out.println("MESSAGE #" + (i + 1) + ":"); |
| 158 |
} |
| 159 |
Date d = msgs[i].getReceivedDate(); |
| 160 |
if (d != null) { |
| 161 |
String n = df.format(d); |
| 162 |
if (verbose) |
| 163 |
System.out.println(n); |
| 164 |
if (outf == null || !n.equals(outf.getName())) { |
| 165 |
outf = dstfolder.getFolder(n); |
| 166 |
if (!outf.exists()) { |
| 167 |
System.out.println("Creating: " + |
| 168 |
outf.getFullName()); |
| 169 |
if (!nop) |
| 170 |
outf.create(Folder.HOLDS_MESSAGES); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
if (!nop) |
| 175 |
outf.appendMessages(new Message[] { msgs[i] }); |
| 176 |
} |
| 177 |
folder.close(false); |
| 178 |
} |
| 179 |
|
| 180 |
store.close(); |
| 181 |
} catch (Exception ex) { |
| 182 |
System.out.println("Oops, got exception! " + ex.getMessage()); |
| 183 |
ex.printStackTrace(); |
| 184 |
} |
| 185 |
System.exit(1); |
| 186 |
} |
| 187 |
} |