2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2006-12-12 18:57:24 -05:00
|
|
|
package davmail.pop;
|
|
|
|
|
|
|
|
import davmail.AbstractConnection;
|
2009-04-23 10:54:06 -04:00
|
|
|
import davmail.BundleMessage;
|
2006-12-12 18:57:24 -05:00
|
|
|
import davmail.exchange.ExchangeSession;
|
2008-10-31 13:12:30 -04:00
|
|
|
import davmail.exchange.ExchangeSessionFactory;
|
2009-04-03 03:38:31 -04:00
|
|
|
import davmail.ui.tray.DavGatewayTray;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
2008-01-31 18:42:46 -05:00
|
|
|
import java.io.FilterOutputStream;
|
2006-12-12 18:57:24 -05:00
|
|
|
import java.io.IOException;
|
2008-01-31 18:42:46 -05:00
|
|
|
import java.io.OutputStream;
|
2007-04-25 19:29:46 -04:00
|
|
|
import java.net.Socket;
|
2007-10-23 16:46:40 -04:00
|
|
|
import java.net.SocketException;
|
2007-04-25 19:29:46 -04:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.StringTokenizer;
|
2006-12-12 18:57:24 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dav Gateway pop connection implementation
|
|
|
|
*/
|
|
|
|
public class PopConnection extends AbstractConnection {
|
|
|
|
private List<ExchangeSession.Message> messages;
|
|
|
|
|
2009-07-21 18:16:39 -04:00
|
|
|
/**
|
|
|
|
* Initialize the streams and start the thread.
|
|
|
|
*
|
|
|
|
* @param clientSocket POP client socket
|
|
|
|
*/
|
2006-12-14 10:14:18 -05:00
|
|
|
public PopConnection(Socket clientSocket) {
|
2009-05-04 18:31:06 -04:00
|
|
|
super(PopConnection.class.getSimpleName(), clientSocket, null);
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected long getTotalMessagesLength() {
|
2006-12-12 18:57:24 -05:00
|
|
|
int result = 0;
|
|
|
|
for (ExchangeSession.Message message : messages) {
|
|
|
|
result += message.size;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected void printCapabilities() throws IOException {
|
2008-08-21 18:24:45 -04:00
|
|
|
sendClient("TOP");
|
|
|
|
sendClient("USER");
|
|
|
|
sendClient("UIDL");
|
|
|
|
sendClient(".");
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected void printList() throws IOException {
|
2006-12-12 18:57:24 -05:00
|
|
|
int i = 1;
|
|
|
|
for (ExchangeSession.Message message : messages) {
|
|
|
|
sendClient(i++ + " " + message.size);
|
|
|
|
}
|
|
|
|
sendClient(".");
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected void printUidList() throws IOException {
|
2006-12-12 18:57:24 -05:00
|
|
|
int i = 1;
|
|
|
|
for (ExchangeSession.Message message : messages) {
|
2009-07-31 02:21:46 -04:00
|
|
|
sendClient(i++ + " " + message.getUid());
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
sendClient(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 17:52:17 -04:00
|
|
|
@Override
|
2006-12-12 18:57:24 -05:00
|
|
|
public void run() {
|
|
|
|
String line;
|
|
|
|
StringTokenizer tokens;
|
|
|
|
|
|
|
|
try {
|
2008-10-31 13:12:30 -04:00
|
|
|
ExchangeSessionFactory.checkConfig();
|
2006-12-12 18:57:24 -05:00
|
|
|
sendOK("DavMail POP ready at " + new Date());
|
|
|
|
|
|
|
|
for (; ;) {
|
|
|
|
line = readClient();
|
|
|
|
// unable to read line, connection closed ?
|
|
|
|
if (line == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = new StringTokenizer(line);
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String command = tokens.nextToken();
|
|
|
|
|
|
|
|
if ("QUIT".equalsIgnoreCase(command)) {
|
2007-02-07 06:51:08 -05:00
|
|
|
// delete messages before quit
|
2007-10-09 05:49:57 -04:00
|
|
|
if (session != null) {
|
2008-12-05 05:45:23 -05:00
|
|
|
session.purgeOldestTrashAndSentMessages();
|
2007-10-09 05:49:57 -04:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
sendOK("Bye");
|
|
|
|
break;
|
|
|
|
} else if ("USER".equalsIgnoreCase(command)) {
|
|
|
|
userName = null;
|
|
|
|
password = null;
|
|
|
|
session = null;
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
userName = tokens.nextToken();
|
|
|
|
sendOK("USER : " + userName);
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.USER;
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
|
|
|
sendERR("invalid syntax");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.INITIAL;
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else if ("PASS".equalsIgnoreCase(command)) {
|
2009-01-29 17:18:53 -05:00
|
|
|
if (state != State.USER) {
|
2006-12-12 18:57:24 -05:00
|
|
|
sendERR("invalid state");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.INITIAL;
|
2006-12-12 18:57:24 -05:00
|
|
|
} else if (!tokens.hasMoreTokens()) {
|
|
|
|
sendERR("invalid syntax");
|
|
|
|
} else {
|
2008-11-03 20:58:08 -05:00
|
|
|
// bug 2194492 : allow space in password
|
2009-01-17 09:22:55 -05:00
|
|
|
password = line.substring("PASS".length() + 1);
|
2006-12-12 18:57:24 -05:00
|
|
|
try {
|
2008-10-31 13:12:30 -04:00
|
|
|
session = ExchangeSessionFactory.getInstance(userName, password);
|
2006-12-12 18:57:24 -05:00
|
|
|
sendOK("PASS");
|
2009-01-29 17:18:53 -05:00
|
|
|
state = State.AUTHENTICATED;
|
2007-10-23 16:46:40 -04:00
|
|
|
} catch (SocketException e) {
|
|
|
|
// can not send error to client after a socket exception
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_CLIENT_CLOSED_CONNECTION"), e);
|
2006-12-12 18:57:24 -05:00
|
|
|
} catch (Exception e) {
|
2008-11-24 07:35:58 -05:00
|
|
|
DavGatewayTray.error(e);
|
|
|
|
sendERR(e);
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ("CAPA".equalsIgnoreCase(command)) {
|
2008-08-21 18:24:45 -04:00
|
|
|
sendOK("Capability list follows");
|
|
|
|
printCapabilities();
|
2009-01-29 17:18:53 -05:00
|
|
|
} else if (state != State.AUTHENTICATED) {
|
2008-08-21 18:24:45 -04:00
|
|
|
sendERR("Invalid state not authenticated");
|
2006-12-12 18:57:24 -05:00
|
|
|
} else {
|
2009-05-10 10:52:09 -04:00
|
|
|
// load messages (once)
|
|
|
|
if (messages == null) {
|
|
|
|
messages = session.getAllMessageUidAndSize("INBOX");
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
if ("STAT".equalsIgnoreCase(command)) {
|
|
|
|
sendOK(messages.size() + " " +
|
|
|
|
getTotalMessagesLength());
|
2009-07-21 18:16:39 -04:00
|
|
|
} else if ("NOOP".equalsIgnoreCase(command)) {
|
|
|
|
sendOK("");
|
2006-12-12 18:57:24 -05:00
|
|
|
} else if ("LIST".equalsIgnoreCase(command)) {
|
2008-08-21 18:24:45 -04:00
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String token = tokens.nextToken();
|
|
|
|
try {
|
|
|
|
int messageNumber = Integer.valueOf(token);
|
2008-12-05 03:48:54 -05:00
|
|
|
ExchangeSession.Message message = messages.get(messageNumber - 1);
|
2009-04-23 16:53:22 -04:00
|
|
|
sendOK("" + messageNumber + ' ' + message.size);
|
2008-08-21 18:24:45 -04:00
|
|
|
} catch (NumberFormatException e) {
|
2008-12-05 03:48:54 -05:00
|
|
|
sendERR("Invalid message index: " + token);
|
2008-11-29 09:22:00 -05:00
|
|
|
} catch (IndexOutOfBoundsException e) {
|
2008-12-05 03:48:54 -05:00
|
|
|
sendERR("Invalid message index: " + token);
|
2008-08-21 18:24:45 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendOK(messages.size() +
|
|
|
|
" messages (" + getTotalMessagesLength() +
|
|
|
|
" octets)");
|
|
|
|
printList();
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else if ("UIDL".equalsIgnoreCase(command)) {
|
2009-01-17 09:22:55 -05:00
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
String token = tokens.nextToken();
|
|
|
|
try {
|
|
|
|
int messageNumber = Integer.valueOf(token);
|
2009-07-31 02:21:46 -04:00
|
|
|
sendOK(messageNumber + " " + messages.get(messageNumber - 1).getUid());
|
2009-01-17 09:22:55 -05:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
sendERR("Invalid message index: " + token);
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
sendERR("Invalid message index: " + token);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendOK(messages.size() +
|
|
|
|
" messages (" + getTotalMessagesLength() +
|
|
|
|
" octets)");
|
|
|
|
printUidList();
|
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} else if ("RETR".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
|
|
|
try {
|
2009-01-17 09:22:55 -05:00
|
|
|
int messageNumber = Integer.valueOf(tokens.nextToken()) - 1;
|
2006-12-12 18:57:24 -05:00
|
|
|
sendOK("");
|
2010-04-02 11:35:37 -04:00
|
|
|
messages.get(messageNumber).write(os, true);
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("");
|
|
|
|
sendClient(".");
|
2007-10-23 16:46:40 -04:00
|
|
|
} catch (SocketException e) {
|
|
|
|
// can not send error to client after a socket exception
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.warn(new BundleMessage("LOG_CLIENT_CLOSED_CONNECTION"), e);
|
2006-12-12 18:57:24 -05:00
|
|
|
} catch (Exception e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.error(new BundleMessage("LOG_ERROR_RETRIEVING_MESSAGE"), e);
|
2009-04-23 16:53:22 -04:00
|
|
|
sendERR("error retreiving message " + e + ' ' + e.getMessage());
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-23 10:54:06 -04:00
|
|
|
sendERR("invalid message index");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else if ("DELE".equalsIgnoreCase(command)) {
|
|
|
|
if (tokens.hasMoreTokens()) {
|
2009-04-23 10:54:06 -04:00
|
|
|
ExchangeSession.Message message;
|
2006-12-12 18:57:24 -05:00
|
|
|
try {
|
|
|
|
int messageNumber = Integer.valueOf(tokens.
|
|
|
|
nextToken()) - 1;
|
2009-04-23 10:54:06 -04:00
|
|
|
message = messages.get(messageNumber);
|
|
|
|
message.moveToTrash();
|
2006-12-12 18:57:24 -05:00
|
|
|
sendOK("DELETE");
|
2009-04-23 10:54:06 -04:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
sendERR("invalid message index");
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
sendERR("invalid message index");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
2009-04-23 10:54:06 -04:00
|
|
|
sendERR("invalid message index");
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else if ("TOP".equalsIgnoreCase(command)) {
|
2008-11-29 09:22:00 -05:00
|
|
|
int message = 0;
|
2006-12-12 18:57:24 -05:00
|
|
|
try {
|
2008-11-29 09:22:00 -05:00
|
|
|
message = Integer.valueOf(tokens.nextToken());
|
2008-01-31 18:42:46 -05:00
|
|
|
int lines = Integer.valueOf(tokens.nextToken());
|
2008-12-05 03:48:54 -05:00
|
|
|
ExchangeSession.Message m = messages.get(message - 1);
|
2008-01-31 18:42:46 -05:00
|
|
|
sendOK("");
|
2010-04-02 11:35:37 -04:00
|
|
|
m.write(new TopOutputStream(os, lines), true);
|
2008-01-31 18:42:46 -05:00
|
|
|
sendClient("");
|
|
|
|
sendClient(".");
|
2009-04-23 10:54:06 -04:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
sendERR("invalid command");
|
2008-11-29 09:22:00 -05:00
|
|
|
} catch (IndexOutOfBoundsException e) {
|
2009-04-23 10:54:06 -04:00
|
|
|
sendERR("invalid message index: " + message);
|
2006-12-12 18:57:24 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
sendERR("error retreiving top of messages");
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.error(e);
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
} else if ("RSET".equalsIgnoreCase(command)) {
|
|
|
|
sendOK("RSET");
|
|
|
|
} else {
|
|
|
|
sendERR("unknown command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sendERR("unknown command");
|
|
|
|
}
|
|
|
|
|
|
|
|
os.flush();
|
|
|
|
}
|
2009-04-23 10:54:06 -04:00
|
|
|
} catch (SocketException e) {
|
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_CONNECTION_CLOSED"));
|
2009-04-15 11:20:06 -04:00
|
|
|
} catch (Exception e) {
|
2009-07-15 18:47:31 -04:00
|
|
|
DavGatewayTray.log(e);
|
2007-04-25 19:29:46 -04:00
|
|
|
try {
|
|
|
|
sendERR(e.getMessage());
|
|
|
|
} catch (IOException e2) {
|
2009-04-23 10:54:06 -04:00
|
|
|
DavGatewayTray.debug(new BundleMessage("LOG_EXCEPTION_SENDING_ERROR_TO_CLIENT"), e2);
|
2007-04-25 19:29:46 -04:00
|
|
|
}
|
2006-12-12 18:57:24 -05:00
|
|
|
} finally {
|
2007-05-15 05:37:55 -04:00
|
|
|
close();
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
|
|
|
DavGatewayTray.resetIcon();
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected void sendOK(String message) throws IOException {
|
2006-12-12 18:57:24 -05:00
|
|
|
sendClient("+OK ", message);
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected void sendERR(Exception e) throws IOException {
|
2008-11-24 07:35:58 -05:00
|
|
|
String message = e.getMessage();
|
|
|
|
if (message == null) {
|
|
|
|
message = e.toString();
|
|
|
|
}
|
|
|
|
sendERR(message);
|
|
|
|
}
|
|
|
|
|
2008-12-05 05:01:24 -05:00
|
|
|
protected void sendERR(String message) throws IOException {
|
2008-11-03 05:56:57 -05:00
|
|
|
sendClient("-ERR ", message.replaceAll("\\n", " "));
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|
2008-01-31 18:42:46 -05:00
|
|
|
|
|
|
|
/**
|
2008-11-29 09:22:00 -05:00
|
|
|
* Filter to limit output lines to max body lines after header
|
2008-01-31 18:42:46 -05:00
|
|
|
*/
|
2009-09-21 17:34:13 -04:00
|
|
|
private static final class TopOutputStream extends FilterOutputStream {
|
|
|
|
private static final int START = 0;
|
|
|
|
private static final int CR = 1;
|
|
|
|
private static final int CRLF = 2;
|
|
|
|
private static final int CRLFCR = 3;
|
|
|
|
private static final int BODY = 4;
|
2008-01-31 18:42:46 -05:00
|
|
|
|
2009-09-21 17:34:13 -04:00
|
|
|
private int maxLines;
|
|
|
|
private int state = START;
|
2008-01-31 18:42:46 -05:00
|
|
|
|
2009-04-16 17:52:17 -04:00
|
|
|
private TopOutputStream(OutputStream os, int maxLines) {
|
2008-01-31 18:42:46 -05:00
|
|
|
super(os);
|
|
|
|
this.maxLines = maxLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(int b) throws IOException {
|
2008-12-17 10:31:08 -05:00
|
|
|
if (state != BODY || maxLines > 0) {
|
2008-01-31 18:42:46 -05:00
|
|
|
super.write(b);
|
|
|
|
}
|
2008-12-17 10:31:08 -05:00
|
|
|
if (state == BODY) {
|
2008-11-29 09:22:00 -05:00
|
|
|
if (b == '\n') {
|
|
|
|
maxLines--;
|
|
|
|
}
|
2008-12-17 10:31:08 -05:00
|
|
|
} else if (state == START) {
|
2008-11-29 09:22:00 -05:00
|
|
|
if (b == '\r') {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = CR;
|
2008-11-29 09:22:00 -05:00
|
|
|
}
|
2008-12-17 10:31:08 -05:00
|
|
|
} else if (state == CR) {
|
2008-11-29 09:22:00 -05:00
|
|
|
if (b == '\n') {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = CRLF;
|
2008-11-29 09:22:00 -05:00
|
|
|
} else {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = START;
|
2008-11-29 09:22:00 -05:00
|
|
|
}
|
2008-12-17 10:31:08 -05:00
|
|
|
} else if (state == CRLF) {
|
2008-12-05 03:48:54 -05:00
|
|
|
if (b == '\r') {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = CRLFCR;
|
2008-11-29 09:22:00 -05:00
|
|
|
} else {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = START;
|
2008-11-29 09:22:00 -05:00
|
|
|
}
|
2008-12-17 10:31:08 -05:00
|
|
|
} else if (state == CRLFCR) {
|
2008-12-05 03:48:54 -05:00
|
|
|
if (b == '\n') {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = BODY;
|
2008-11-29 09:22:00 -05:00
|
|
|
} else {
|
2008-12-17 10:31:08 -05:00
|
|
|
state = START;
|
2008-11-29 09:22:00 -05:00
|
|
|
}
|
2008-01-31 18:42:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-12 18:57:24 -05:00
|
|
|
}
|