| 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 jakarta.mail.*; |
| 12 |
import jakarta.mail.internet.*; |
| 13 |
|
| 14 |
/* |
| 15 |
* Copy folder hierarchies between different Stores. This is a useful |
| 16 |
* utility to populate new (and possibly empty) mail stores. Specify |
| 17 |
* both the source and destination folders as URLs. |
| 18 |
* |
| 19 |
* @author John Mani |
| 20 |
* @author Bill Shannon |
| 21 |
*/ |
| 22 |
|
| 23 |
public class populate { |
| 24 |
|
| 25 |
static boolean force = false; |
| 26 |
static boolean skipSpecial = false; |
| 27 |
static boolean clear = false; |
| 28 |
static boolean dontPreserveFlags = false; |
| 29 |
static boolean warn = false; |
| 30 |
|
| 31 |
public static void main(String argv[]) { |
| 32 |
String srcURL = null; |
| 33 |
String dstURL = null; |
| 34 |
boolean debug = false; |
| 35 |
|
| 36 |
int optind; |
| 37 |
|
| 38 |
for (optind = 0; optind < argv.length; optind++) { |
| 39 |
if (argv[optind].equals("-s")) { |
| 40 |
srcURL = argv[++optind]; |
| 41 |
} else if (argv[optind].equals("-d")) { |
| 42 |
dstURL = argv[++optind]; |
| 43 |
} else if (argv[optind].equals("-D")) { |
| 44 |
debug = true; |
| 45 |
} else if (argv[optind].equals("-f")) { |
| 46 |
force = true; |
| 47 |
} else if (argv[optind].equals("-S")) { |
| 48 |
skipSpecial = true; |
| 49 |
} else if (argv[optind].equals("-c")) { |
| 50 |
clear = true; |
| 51 |
} else if (argv[optind].equals("-P")) { |
| 52 |
dontPreserveFlags = true; |
| 53 |
} else if (argv[optind].equals("-W")) { |
| 54 |
warn = true; |
| 55 |
} else if (argv[optind].equals("--")) { |
| 56 |
optind++; |
| 57 |
break; |
| 58 |
} else if (argv[optind].startsWith("-")) { |
| 59 |
printUsage(); |
| 60 |
System.exit(1); |
| 61 |
} else { |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
try { |
| 67 |
|
| 68 |
if (srcURL == null || dstURL == null) { |
| 69 |
printUsage(); |
| 70 |
System.exit(1); |
| 71 |
} |
| 72 |
|
| 73 |
Session session = Session.getInstance( |
| 74 |
System.getProperties(), null); |
| 75 |
session.setDebug(debug); |
| 76 |
|
| 77 |
// Get source folder |
| 78 |
URLName srcURLName = new URLName(srcURL); |
| 79 |
Folder srcFolder; |
| 80 |
// Check if the source URL has a folder specified. If |
| 81 |
// not, we use the default folder |
| 82 |
if (srcURLName.getFile() == null) { |
| 83 |
Store s = session.getStore(srcURLName); |
| 84 |
s.connect(); |
| 85 |
srcFolder = s.getDefaultFolder(); |
| 86 |
} else { |
| 87 |
srcFolder = session.getFolder(new URLName(srcURL)); |
| 88 |
if (!srcFolder.exists()) { |
| 89 |
System.out.println("source folder does not exist"); |
| 90 |
srcFolder.getStore().close(); |
| 91 |
System.exit(1); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Set up destination folder |
| 96 |
URLName dstURLName = new URLName(dstURL); |
| 97 |
Folder dstFolder; |
| 98 |
// Check if the destination URL has a folder specified. If |
| 99 |
// not, we use the source folder name |
| 100 |
if (dstURLName.getFile() == null) { |
| 101 |
Store s = session.getStore(dstURLName); |
| 102 |
s.connect(); |
| 103 |
dstFolder = s.getFolder(srcFolder.getName()); |
| 104 |
} else |
| 105 |
dstFolder = session.getFolder(dstURLName); |
| 106 |
|
| 107 |
if (clear && dstFolder.exists()) { |
| 108 |
if (!dstFolder.delete(true)) { |
| 109 |
System.out.println("couldn't delete " + |
| 110 |
dstFolder.getFullName()); |
| 111 |
return; |
| 112 |
} |
| 113 |
} |
| 114 |
copy(srcFolder, dstFolder); |
| 115 |
|
| 116 |
// Close the respective stores. |
| 117 |
srcFolder.getStore().close(); |
| 118 |
dstFolder.getStore().close(); |
| 119 |
|
| 120 |
} catch (MessagingException mex) { |
| 121 |
System.out.println(mex.getMessage()); |
| 122 |
mex.printStackTrace(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
private static void copy(Folder src, Folder dst) |
| 127 |
throws MessagingException { |
| 128 |
System.out.println("Populating " + dst.getFullName()); |
| 129 |
|
| 130 |
Folder ddst = dst; |
| 131 |
Folder[] srcFolders = null; |
| 132 |
if ((src.getType() & Folder.HOLDS_FOLDERS) != 0) |
| 133 |
srcFolders = src.list(); |
| 134 |
boolean srcHasChildren = srcFolders != null && srcFolders.length > 0; |
| 135 |
|
| 136 |
if (!dst.exists()) { |
| 137 |
// Create it. |
| 138 |
boolean dstHoldsOnlyFolders = false; |
| 139 |
if (!dst.create(src.getType())) { |
| 140 |
// might not be able to create a folder that holds both |
| 141 |
if (!dst.create(srcHasChildren ? Folder.HOLDS_FOLDERS : |
| 142 |
Folder.HOLDS_MESSAGES)) { |
| 143 |
// might only be able to create one type (Gmail) |
| 144 |
if (!dst.create(Folder.HOLDS_MESSAGES)) { |
| 145 |
System.out.println("couldn't create " + |
| 146 |
dst.getFullName()); |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
dstHoldsOnlyFolders = srcHasChildren; |
| 151 |
} |
| 152 |
|
| 153 |
// Copy over any messges from src to dst |
| 154 |
if ((src.getType() & Folder.HOLDS_MESSAGES) != 0) { |
| 155 |
src.open(Folder.READ_ONLY); |
| 156 |
if (dstHoldsOnlyFolders) { |
| 157 |
if (src.getMessageCount() > 0) { |
| 158 |
System.out.println("Unable to copy messages from " + |
| 159 |
src.getFullName() + " to " + dst.getFullName() + |
| 160 |
" because destination holds only folders"); |
| 161 |
} |
| 162 |
} else |
| 163 |
copyMessages(src, dst); |
| 164 |
src.close(false); |
| 165 |
} |
| 166 |
} else { |
| 167 |
System.out.println(dst.getFullName() + " already exists"); |
| 168 |
// Copy over any messges from src to dst |
| 169 |
if (force && (src.getType() & Folder.HOLDS_MESSAGES) != 0) { |
| 170 |
src.open(Folder.READ_ONLY); |
| 171 |
copyMessages(src, dst); |
| 172 |
src.close(false); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// Copy over subfolders |
| 177 |
if (srcHasChildren) { |
| 178 |
for (int i = 0; i < srcFolders.length; i++) { |
| 179 |
// skip special directories? |
| 180 |
if (skipSpecial) { |
| 181 |
if (srcFolders[i].getName().equals("SCCS") || |
| 182 |
srcFolders[i].getName().equals("Drafts") || |
| 183 |
srcFolders[i].getName().equals("Trash") || |
| 184 |
srcFolders[i].getName().equals("Shared Folders")) |
| 185 |
continue; |
| 186 |
} |
| 187 |
copy(srcFolders[i], dst.getFolder(srcFolders[i].getName())); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Copy message from src to dst. If dontPreserveFlags is set |
| 194 |
* we first copy the messages to memory, clear all the flags, |
| 195 |
* and then copy to the destination. |
| 196 |
*/ |
| 197 |
private static void copyMessages(Folder src, Folder dst) |
| 198 |
throws MessagingException { |
| 199 |
Message[] msgs = src.getMessages(); |
| 200 |
if (dontPreserveFlags) { |
| 201 |
for (int i = 0; i < msgs.length; i++) { |
| 202 |
MimeMessage m = new MimeMessage((MimeMessage)msgs[i]); |
| 203 |
m.setFlags(m.getFlags(), false); |
| 204 |
msgs[i] = m; |
| 205 |
} |
| 206 |
} |
| 207 |
if (warn) { |
| 208 |
// have to copy messages one at a time |
| 209 |
for (int i = 0; i < msgs.length; i++) { |
| 210 |
try { |
| 211 |
src.copyMessages(new Message[] { msgs[i] }, dst); |
| 212 |
} catch (MessagingException mex) { |
| 213 |
System.out.println("WARNING: Copy of message " + (i + 1) + |
| 214 |
" from " + src.getFullName() + |
| 215 |
" to " + dst.getFullName() + |
| 216 |
" failed: " + mex.toString()); |
| 217 |
} |
| 218 |
} |
| 219 |
} else |
| 220 |
src.copyMessages(msgs, dst); |
| 221 |
} |
| 222 |
|
| 223 |
private static void printUsage() { |
| 224 |
System.out.println("populate [-D] [-f] [-S] [-c] " + |
| 225 |
"-s source_url -d dest_url"); |
| 226 |
System.out.println("URLs are of the form: " + |
| 227 |
"protocol://username:password@hostname/foldername"); |
| 228 |
System.out.println("The destination URL does not need a foldername," + |
| 229 |
" in which case, the source foldername is used"); |
| 230 |
} |
| 231 |
} |