/** * Handler method called upon receipt of a PASS command. Reads in and * validates the password. */ @Override public Response onCommand(POP3Session session, Request request) { String parameters = request.getArgument(); if (session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET && parameters != null) { return doAuth(session, session.getUser(), parameters); } else { session.setHandlerState(POP3Session.AUTHENTICATION_READY); return AUTH_FAILED; } }
@Override public Response onConnect(POP3Session session) { StringBuilder responseBuffer = new StringBuilder(); // Generate the timestamp which can be also used with APOP. See RFC1939 APOP responseBuffer.append("<").append(session.getSessionID()).append(".").append(System.currentTimeMillis()).append("@").append(session.getConfiguration().getHelloName()).append("> "); // store the timestamp for later usage session.setAttachment(POP3Session.APOP_TIMESTAMP, responseBuffer.toString(), State.Connection); // complete the response banner and send it back to the client responseBuffer.append("POP3 server (").append(session.getConfiguration().getSoftwareName()).append(") ready "); return new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString()); }
/** * Returns the {@link MessageMetaData} for the given message number or <code>null</code> if it can not be * found. * * @param session * @param number * @return data */ public static MessageMetaData getMetaData(POP3Session session, int number) { @SuppressWarnings("unchecked") List<MessageMetaData> uidList = (List<MessageMetaData>) session.getAttachment(POP3Session.UID_LIST, State.Transaction); if (uidList == null || number > uidList.size()) { return null; } else { return uidList.get(number - 1); } }
@Override @SuppressWarnings("unchecked") public Set<String> getImplementedCapabilities(POP3Session session) { if (session.isStartTLSSupported() && session.getHandlerState() == POP3Session.AUTHENTICATION_READY) { return CAPS; } else { return Collections.EMPTY_SET; } }
@Override public Response onCommand(POP3Session session, Request request) { // check if starttls is supported, the state is the right one and it was // not started before if (session.isStartTLSSupported() && session.getHandlerState() == POP3Session.AUTHENTICATION_READY && session.isTLSStarted() == false) { return BEGIN_TLS; } else { return POP3Response.ERR; } }
/** * Handler method called upon receipt of a USER command. Reads in the user * id. */ @Override public Response onCommand(POP3Session session, Request request) { String parameters = request.getArgument(); if (session.getHandlerState() == POP3Session.AUTHENTICATION_READY && parameters != null) { session.setUser(parameters); session.setHandlerState(POP3Session.AUTHENTICATION_USERSET); return POP3Response.OK; } else { return POP3Response.ERR; } }
/** * Handler method called upon receipt of a QUIT command. This method handles * cleanup of the POP3Handler state. */ @Override @SuppressWarnings("unchecked") public Response onCommand(POP3Session session, Request request) { Response response = null; if (session.getHandlerState() == POP3Session.AUTHENTICATION_READY || session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET) { return SIGN_OFF; } List<String> toBeRemoved = (List<String>) session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction); Mailbox mailbox = session.getUserMailbox(); try { String[] uids = toBeRemoved.toArray(new String[toBeRemoved.size()]); mailbox.remove(uids); response = SIGN_OFF; } catch (Exception ex) { response = SIGN_OFF_NOT_CLEAN; LOGGER.error("Some deleted messages were not removed", ex); } try { mailbox.close(); } catch (IOException e) { // ignore on close } return response; }
@Override public Response onCommand(POP3Session session, Request request) { if (session.getAttachment(POP3Session.APOP_TIMESTAMP, State.Connection) == null) { syntaxError = true; if (!syntaxError && session.getHandlerState() == POP3Session.AUTHENTICATION_READY) { session.setUser(parts[0]); session.setHandlerState(POP3Session.AUTHENTICATION_READY); return AUTH_FAILED;
/** * Handler method called upon receipt of a STAT command. Returns the number * of messages in the mailbox and its aggregate size. */ @Override @SuppressWarnings("unchecked") public Response onCommand(POP3Session session, Request request) { if (session.getHandlerState() == POP3Session.TRANSACTION) { List<MessageMetaData> uidList = (List<MessageMetaData>) session.getAttachment(POP3Session.UID_LIST, State.Transaction); List<String> deletedUidList = (List<String>) session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction); long size = 0; int count = 0; if (uidList.isEmpty() == false) { List<MessageMetaData> validResults = new ArrayList<>(); for (MessageMetaData data : uidList) { if (deletedUidList.contains(data.getUid()) == false) { size += data.getSize(); count++; validResults.add(data); } } } StringBuilder responseBuffer = new StringBuilder(32).append(count).append(" ").append(size); return new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString()); } else { return POP3Response.ERR; } }
/** * Handler method called upon receipt of a NOOP command. Like all good * NOOPs, does nothing much. */ @Override public Response onCommand(POP3Session session, Request request) { if (session.getHandlerState() == POP3Session.TRANSACTION) { return POP3Response.OK; } else { return POP3Response.ERR; } }
/** * Authenticate a user and return the {@link Response} * * @param session * @param user * @param pass * @return response */ protected final Response doAuth(POP3Session session, String user, String pass) { try { Mailbox mailbox = auth(session, user, pass); if (mailbox != null) { session.setUserMailbox(mailbox); stat(session); session.setHandlerState(POP3Session.TRANSACTION); StringBuilder responseBuffer = new StringBuilder(64).append("Welcome ").append(session.getUser()); return new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString()); } else { session.setHandlerState(POP3Session.AUTHENTICATION_READY); return AUTH_FAILED; } } catch (Exception e) { LOGGER.error("Unexpected error accessing mailbox for {}", session.getUser(), e); session.setHandlerState(POP3Session.AUTHENTICATION_READY); return UNEXPECTED_ERROR; } }
new Object[] { session.getSessionID(), username, password }); session.getLogger().debug("Validated account (" + username + ") status is " + status.toString());
/** * Implements a "stat". If the handler is currently in a transaction state, * this amounts to a rollback of the mailbox contents to the beginning of * the transaction. This method is also called when first entering the * transaction state to initialize the handler copies of the user inbox. */ protected void stat(POP3Session session) { try { List<MessageMetaData> messages = session.getUserMailbox().getMessages(); session.setAttachment(POP3Session.UID_LIST, messages, State.Transaction); session.setAttachment(POP3Session.DELETED_UID_LIST, new ArrayList<String>(), State.Transaction); } catch (IOException e) { // In the event of an exception being thrown there may or may not be // anything in userMailbox LOGGER.error("Unable to STAT mail box ", e); } }
@Override protected Mailbox auth(POP3Session session, String username, String password) throws Exception { MailboxSession mSession = null; try { mSession = manager.login(session.getUser(), password); manager.startProcessingRequest(mSession); MailboxPath inbox = MailboxPath.inbox(mSession); // check if the mailbox exists, if not create it if (!manager.mailboxExists(inbox, mSession)) { Optional<MailboxId> mailboxId = manager.createMailbox(inbox, mSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId); } MessageManager mailbox = manager.getMailbox(MailboxPath.inbox(mSession), mSession); return new MailboxAdapter(manager, mailbox, mSession); } catch (BadCredentialsException e) { return null; } catch (MailboxException e) { throw new IOException("Unable to access mailbox for user " + session.getUser(), e); } finally { if (mSession != null) { manager.endProcessingRequest(mSession); } } }
@Override public Response onCommand(POP3Session session, Request request) { Response response = super.onCommand(session, request); if (POP3Response.OK_RESPONSE.equals(response.getRetCode())) { POP3BeforeSMTPHelper.addIPAddress(session.getRemoteAddress().getAddress().getHostAddress()); } return response; }
POP3Response response = null; String parameters = request.getArgument(); if (session.getHandlerState() == POP3Session.TRANSACTION) { List<MessageMetaData> uidList = (List<MessageMetaData>) session.getAttachment(POP3Session.UID_LIST, State.Transaction); List<String> deletedUidList = (List<String>) session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction); try { String identifier = session.getUserMailbox().getIdentifier(); if (parameters == null) { response = new POP3Response(POP3Response.OK_RESPONSE, "unique-id listing follows");
@SuppressWarnings("unchecked") public Response onCommand(POP3Session session, Request request) { if (session.getHandlerState() == POP3Session.TRANSACTION) { int num = 0; try { return new POP3Response(POP3Response.ERR_RESPONSE, responseBuffer.toString()); List<String> deletedUidList = (List<String>) session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction);
@Override @SuppressWarnings("unchecked") public Set<String> getImplementedCapabilities(POP3Session session) { if (session.getHandlerState() == POP3Session.TRANSACTION) { return CAPS; } else { return Collections.EMPTY_SET; } }
POP3Response response = null; String parameters = request.getArgument(); if (session.getHandlerState() == POP3Session.TRANSACTION) { int num = 0; try { return response; List<String> deletedUidList = (List<String>) session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction); InputStream content = session.getUserMailbox().getMessage(uid);
public Response onCommand(POP3Session session, Request request) { String parameters = request.getArgument(); List<MessageMetaData> uidList = (List<MessageMetaData>) session.getAttachment(POP3Session.UID_LIST, State.Transaction); List<String> deletedUidList = (List<String>) session.getAttachment(POP3Session.DELETED_UID_LIST, State.Transaction); if (session.getHandlerState() == POP3Session.TRANSACTION) { POP3Response response = null;