1 |
/* ============================================================================ |
2 |
* Douglas Thrift's IMAP Handler License |
3 |
* |
4 |
* Copyright (C) 2002, Douglas Thrift. 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 are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright notice, |
10 |
* this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
13 |
* this list of conditions and the following disclaimer in the documentation |
14 |
* and/or other materials provided with the distribution. |
15 |
* |
16 |
* 3. The end-user documentation included with the redistribution, if any, must |
17 |
* include the following acknowledgment: |
18 |
* |
19 |
* "This product includes software developed by Douglas Thrift |
20 |
* (http://computers.douglasthrift.net/webcontact.html)." |
21 |
* |
22 |
* Alternately, this acknowledgment may appear in the software itself, if |
23 |
* and wherever such third-party acknowledgments normally appear. |
24 |
* |
25 |
* 4. The names "Douglas Thrift" and "Douglas Thrift's IMAP Handler" must not |
26 |
* be used to endorse or promote products derived from this software without |
27 |
* specific prior written permission. For written permission, please visit |
28 |
* http://www.douglasthrift.net/contact.html for contact information. |
29 |
* |
30 |
* 5. Products derived from this software may not be called "Douglas Thrift's |
31 |
* IMAP Handler", nor may "Douglas Thrift's IMAP Handler" appear in their |
32 |
* name, without prior written permission. |
33 |
* |
34 |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
35 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
36 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
37 |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
38 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
39 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
40 |
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
41 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
42 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
43 |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
44 |
* ============================================================================ |
45 |
*/ |
46 |
// IMAP Handler |
47 |
// |
48 |
// Douglas Thrift |
49 |
// |
50 |
// IMAPHandler.java |
51 |
|
52 |
import java.io.*; |
53 |
import java.net.*; |
54 |
import java.text.*; |
55 |
|
56 |
public class IMAPHandler |
57 |
{ |
58 |
IMAPHandler(String host, int port, boolean debug) throws IOException |
59 |
{ |
60 |
this.debug = debug; |
61 |
|
62 |
letter = 'a'; |
63 |
number = 0; |
64 |
|
65 |
format = NumberFormat.getInstance(); |
66 |
format.setMaximumIntegerDigits(3); |
67 |
format.setMinimumIntegerDigits(3); |
68 |
|
69 |
socket = new Socket(host, port); |
70 |
out = new PrintWriter(socket.getOutputStream(), true); |
71 |
in = new BufferedReader(new |
72 |
InputStreamReader(socket.getInputStream())); |
73 |
|
74 |
String input = in.readLine(); |
75 |
if (debug) System.err.println(input); |
76 |
} |
77 |
|
78 |
public String capability() throws IOException |
79 |
{ |
80 |
return imap("CAPABILITY"); |
81 |
} |
82 |
|
83 |
public String noop() throws IOException |
84 |
{ |
85 |
return imap("NOOP"); |
86 |
} |
87 |
|
88 |
public String logout() throws IOException |
89 |
{ |
90 |
return imap("LOGOUT"); |
91 |
} |
92 |
|
93 |
public String login(String args) throws IOException |
94 |
{ |
95 |
return imap("LOGIN", args); |
96 |
} |
97 |
|
98 |
public String select(String args) throws IOException |
99 |
{ |
100 |
return imap("SELECT", args); |
101 |
} |
102 |
|
103 |
public String examine(String args) throws IOException |
104 |
{ |
105 |
return imap("EXAMINE", args); |
106 |
} |
107 |
|
108 |
public String create(String args) throws IOException |
109 |
{ |
110 |
return imap("CREATE", args); |
111 |
} |
112 |
|
113 |
public String delete(String args) throws IOException |
114 |
{ |
115 |
return imap("DELETE", args); |
116 |
} |
117 |
|
118 |
public String rename(String args) throws IOException |
119 |
{ |
120 |
return imap("RENAME", args); |
121 |
} |
122 |
|
123 |
public String subscribe(String args) throws IOException |
124 |
{ |
125 |
return imap("SUBSCRIBE", args); |
126 |
} |
127 |
|
128 |
public String unsubscribe(String args) throws IOException |
129 |
{ |
130 |
return imap("UNSUBSCRIBE", args); |
131 |
} |
132 |
|
133 |
public String list(String args) throws IOException |
134 |
{ |
135 |
return imap("LIST", args); |
136 |
} |
137 |
|
138 |
public String lsub(String args) throws IOException |
139 |
{ |
140 |
return imap("LSUB", args); |
141 |
} |
142 |
|
143 |
public String status(String args) throws IOException |
144 |
{ |
145 |
return imap("STATUS", args); |
146 |
} |
147 |
|
148 |
public String append(String args, String message) throws IOException |
149 |
{ |
150 |
return imap("APPEND", args, message); |
151 |
} |
152 |
|
153 |
public String check() throws IOException |
154 |
{ |
155 |
return imap("CHECK"); |
156 |
} |
157 |
|
158 |
public String close() throws IOException |
159 |
{ |
160 |
return imap("CLOSE"); |
161 |
} |
162 |
|
163 |
public String expunge() throws IOException |
164 |
{ |
165 |
return imap("EXPUNGE"); |
166 |
} |
167 |
|
168 |
public String search(String args) throws IOException |
169 |
{ |
170 |
return imap("SEARCH", args); |
171 |
} |
172 |
|
173 |
public String fetch(String args) throws IOException |
174 |
{ |
175 |
return imap("FETCH", args); |
176 |
} |
177 |
|
178 |
public String store(String args) throws IOException |
179 |
{ |
180 |
return imap("STORE", args); |
181 |
} |
182 |
|
183 |
public String copy(String args) throws IOException |
184 |
{ |
185 |
return imap("COPY", args); |
186 |
} |
187 |
|
188 |
public String uid(String args) throws IOException |
189 |
{ |
190 |
return imap("UID", args); |
191 |
} |
192 |
|
193 |
public boolean successful() |
194 |
{ |
195 |
return success; |
196 |
} |
197 |
|
198 |
private Socket socket; |
199 |
private PrintWriter out; |
200 |
private BufferedReader in; |
201 |
private boolean debug; |
202 |
private NumberFormat format; |
203 |
private char letter; |
204 |
private int number; |
205 |
private boolean success; |
206 |
|
207 |
private String command() |
208 |
{ |
209 |
String command = letter + format.format(number++); |
210 |
|
211 |
if (number > 999) |
212 |
{ |
213 |
letter++; |
214 |
number = 0; |
215 |
} |
216 |
|
217 |
if (letter > 'z') |
218 |
{ |
219 |
letter = 'a'; |
220 |
} |
221 |
|
222 |
return command; |
223 |
} |
224 |
|
225 |
private String imap(String imap) throws IOException |
226 |
{ |
227 |
StringBuffer result = new StringBuffer(); |
228 |
String command = command(); |
229 |
|
230 |
String output = command + " " + imap; |
231 |
|
232 |
out.println(output); |
233 |
if (debug) System.err.println(output); |
234 |
|
235 |
while (true) |
236 |
{ |
237 |
String input = in.readLine(); |
238 |
if (debug) System.err.println(input); |
239 |
|
240 |
if (input.indexOf(command + " OK") == 0) |
241 |
{ |
242 |
success = true; |
243 |
break; |
244 |
} |
245 |
else if (input.indexOf(command + " NO") == 0) |
246 |
{ |
247 |
success = false; |
248 |
break; |
249 |
} |
250 |
else if (input.indexOf(command + " BAD") == 0) |
251 |
{ |
252 |
success = false; |
253 |
break; |
254 |
} |
255 |
else |
256 |
{ |
257 |
result.append(input + "\r\n"); |
258 |
} |
259 |
} |
260 |
|
261 |
return result.toString(); |
262 |
} |
263 |
|
264 |
private String imap(String imap, String args) throws IOException |
265 |
{ |
266 |
StringBuffer result = new StringBuffer(); |
267 |
String command = command(); |
268 |
|
269 |
String output = command + " " + imap + " " + args; |
270 |
|
271 |
out.println(output); |
272 |
if (debug) System.err.println(output); |
273 |
|
274 |
while (true) |
275 |
{ |
276 |
String input = in.readLine(); |
277 |
if (debug) System.err.println(input); |
278 |
|
279 |
if (input.indexOf(command + " OK") == 0) |
280 |
{ |
281 |
success = true; |
282 |
break; |
283 |
} |
284 |
else if (input.indexOf(command + " NO") == 0) |
285 |
{ |
286 |
success = false; |
287 |
break; |
288 |
} |
289 |
else if (input.indexOf(command + " BAD") == 0) |
290 |
{ |
291 |
success = false; |
292 |
break; |
293 |
} |
294 |
else |
295 |
{ |
296 |
result.append(input + "\r\n"); |
297 |
} |
298 |
} |
299 |
|
300 |
return result.toString(); |
301 |
} |
302 |
|
303 |
private String imap(String imap, String args, String message) |
304 |
throws IOException |
305 |
{ |
306 |
StringBuffer result = new StringBuffer(); |
307 |
String command = command(); |
308 |
|
309 |
String output = command + " " + imap + " " + args; |
310 |
|
311 |
out.println(output); |
312 |
if (debug) System.err.println(output); |
313 |
|
314 |
output = message; |
315 |
|
316 |
out.println(output); |
317 |
if (debug) System.err.println(output); |
318 |
|
319 |
while (true) |
320 |
{ |
321 |
String input = in.readLine(); |
322 |
if (debug) System.err.println(input); |
323 |
|
324 |
if (input.indexOf(command + " OK") == 0) |
325 |
{ |
326 |
success = true; |
327 |
break; |
328 |
} |
329 |
else if (input.indexOf(command + " NO") == 0) |
330 |
{ |
331 |
success = false; |
332 |
break; |
333 |
} |
334 |
else if (input.indexOf(command + " BAD") == 0) |
335 |
{ |
336 |
success = false; |
337 |
break; |
338 |
} |
339 |
else |
340 |
{ |
341 |
result.append(input + "\r\n"); |
342 |
} |
343 |
} |
344 |
|
345 |
return result.toString(); |
346 |
} |
347 |
} |