| 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.io.*; |
| 12 |
import jakarta.mail.*; |
| 13 |
import jakarta.mail.internet.*; |
| 14 |
|
| 15 |
/* |
| 16 |
* Copy folder hierarchies between files and a Store. This is a useful |
| 17 |
* utility to populate new (and possibly empty) mail stores. Specify |
| 18 |
* the source as a directory name and the destination folders as a URL. |
| 19 |
* |
| 20 |
* @author John Mani |
| 21 |
* @author Bill Shannon |
| 22 |
*/ |
| 23 |
|
| 24 |
public class fpopulate { |
| 25 |
|
| 26 |
static boolean force = false; |
| 27 |
static boolean skipSCCS = false; |
| 28 |
static boolean clear = false; |
| 29 |
|
| 30 |
static Session session; |
| 31 |
|
| 32 |
public static void main(String argv[]) { |
| 33 |
String srcdir = null; |
| 34 |
String dstURL = null; |
| 35 |
boolean debug = false; |
| 36 |
|
| 37 |
int optind; |
| 38 |
|
| 39 |
for (optind = 0; optind < argv.length; optind++) { |
| 40 |
if (argv[optind].equals("-s")) { |
| 41 |
srcdir = argv[++optind]; |
| 42 |
} else if (argv[optind].equals("-d")) { |
| 43 |
dstURL = argv[++optind]; |
| 44 |
} else if (argv[optind].equals("-D")) { |
| 45 |
debug = true; |
| 46 |
} else if (argv[optind].equals("-f")) { |
| 47 |
force = true; |
| 48 |
} else if (argv[optind].equals("-S")) { |
| 49 |
skipSCCS = true; |
| 50 |
} else if (argv[optind].equals("-c")) { |
| 51 |
clear = true; |
| 52 |
} else if (argv[optind].equals("--")) { |
| 53 |
optind++; |
| 54 |
break; |
| 55 |
} else if (argv[optind].startsWith("-")) { |
| 56 |
printUsage(); |
| 57 |
System.exit(1); |
| 58 |
} else { |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
try { |
| 64 |
|
| 65 |
if (srcdir == null || dstURL == null) { |
| 66 |
printUsage(); |
| 67 |
System.exit(1); |
| 68 |
} |
| 69 |
|
| 70 |
session = Session.getDefaultInstance( |
| 71 |
System.getProperties(), null); |
| 72 |
session.setDebug(debug); |
| 73 |
|
| 74 |
// Get source folder |
| 75 |
File srcFolder = new File(srcdir); |
| 76 |
if (!srcFolder.exists()) { |
| 77 |
System.out.println("source folder does not exist"); |
| 78 |
System.exit(1); |
| 79 |
} |
| 80 |
|
| 81 |
// Set up destination folder |
| 82 |
URLName dstURLName = new URLName(dstURL); |
| 83 |
Folder dstFolder; |
| 84 |
// Check if the destination URL has a folder specified. If |
| 85 |
// not, we use the source folder name |
| 86 |
if (dstURLName.getFile() == null) { |
| 87 |
Store s = session.getStore(dstURLName); |
| 88 |
s.connect(); |
| 89 |
dstFolder = s.getFolder(srcFolder.getName()); |
| 90 |
} else |
| 91 |
dstFolder = session.getFolder(new URLName(dstURL)); |
| 92 |
|
| 93 |
if (clear && dstFolder.exists()) { |
| 94 |
if (!dstFolder.delete(true)) { |
| 95 |
System.out.println("couldn't delete " + |
| 96 |
dstFolder.getFullName()); |
| 97 |
return; |
| 98 |
} |
| 99 |
} |
| 100 |
copy(srcFolder, dstFolder); |
| 101 |
|
| 102 |
// Close the respective stores. |
| 103 |
dstFolder.getStore().close(); |
| 104 |
|
| 105 |
} catch (MessagingException mex) { |
| 106 |
System.out.println(mex.getMessage()); |
| 107 |
mex.printStackTrace(); |
| 108 |
} catch (IOException ioex) { |
| 109 |
System.out.println(ioex.getMessage()); |
| 110 |
ioex.printStackTrace(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private static void copy(File src, Folder dst) |
| 115 |
throws MessagingException, IOException { |
| 116 |
System.out.println("Populating " + dst.getFullName()); |
| 117 |
|
| 118 |
if (!dst.exists()) { |
| 119 |
// Create it. |
| 120 |
int type = holdsMessages(src) ? |
| 121 |
Folder.HOLDS_MESSAGES : Folder.HOLDS_FOLDERS; |
| 122 |
if (!dst.create(type)) { |
| 123 |
System.out.println("couldn't create " + dst.getFullName()); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
// Copy over any messages from src to dst |
| 128 |
if (holdsMessages(src)) |
| 129 |
copyMessages(src, dst); |
| 130 |
} else { |
| 131 |
System.out.println(dst.getFullName() + " already exists"); |
| 132 |
// Copy over any messges from src to dst |
| 133 |
if (force && holdsMessages(src)) |
| 134 |
copyMessages(src, dst); |
| 135 |
} |
| 136 |
|
| 137 |
// Copy over subfolders |
| 138 |
if (holdsFolders(src)) { |
| 139 |
String[] sf = src.list(); |
| 140 |
for (int i = 0; sf != null && i < sf.length; i++) { |
| 141 |
// skip SCCS directories? |
| 142 |
if (skipSCCS && sf[i].equals("SCCS")) |
| 143 |
continue; |
| 144 |
File f = new File(src, sf[i]); |
| 145 |
if (f.isDirectory()) |
| 146 |
copy(f, dst.getFolder(sf[i])); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Does this directory hold messages? |
| 153 |
* Return true if there's at least one message. |
| 154 |
*/ |
| 155 |
private static boolean holdsMessages(File f) { |
| 156 |
File msg = new File(f, "1"); |
| 157 |
return msg.exists(); |
| 158 |
} |
| 159 |
|
| 160 |
private static boolean holdsFolders(File f) { |
| 161 |
return !holdsMessages(f); // XXX - hack for now |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Copy message files from the source directory to the |
| 166 |
* destination folder. Message files must be named "1", |
| 167 |
* "2", etc. The first missing number terminates the |
| 168 |
* copy. |
| 169 |
*/ |
| 170 |
private static void copyMessages(File src, Folder dst) |
| 171 |
throws MessagingException, IOException { |
| 172 |
System.out.println(" Copy from " + src + " to " + dst); |
| 173 |
int msgnum = 1; |
| 174 |
Message[] msgs = new Message[1]; |
| 175 |
for (;;) { |
| 176 |
File f = new File(src, String.valueOf(msgnum)); |
| 177 |
if (!f.exists()) // break when we find a message missing |
| 178 |
break; |
| 179 |
FileInputStream fis = new FileInputStream(f); |
| 180 |
BufferedInputStream is = new BufferedInputStream(fis); |
| 181 |
is.mark(1024); |
| 182 |
DataInputStream dis = new DataInputStream(is); |
| 183 |
String line = dis.readLine(); |
| 184 |
/* |
| 185 |
* If it's in UNIX mbox format, we skip the first line, |
| 186 |
* otherwise we start reading at the beginning. |
| 187 |
*/ |
| 188 |
if (!line.startsWith("From ")) |
| 189 |
is.reset(); |
| 190 |
MimeMessage msg = new MimeMessage(session, is); |
| 191 |
fis.close(); |
| 192 |
msgs[0] = msg; |
| 193 |
dst.appendMessages(msgs); |
| 194 |
msgnum++; |
| 195 |
} |
| 196 |
System.out.println(" Copied " + (msgnum - 1) + " messages"); |
| 197 |
} |
| 198 |
|
| 199 |
private static void printUsage() { |
| 200 |
System.out.println("fpopulate [-D] [-f] [-S] [-c] " + |
| 201 |
"-s source_dir -d dest_url"); |
| 202 |
System.out.println("URLs are of the form: " + |
| 203 |
"protocol://username:password@hostname/foldername"); |
| 204 |
System.out.println("The destination URL does not need a foldername," + |
| 205 |
" in which case, the source foldername is used"); |
| 206 |
} |
| 207 |
} |