📄 imapconnection.java
字号:
{ Object ocmd = rc.get(i); if (ocmd instanceof String) { String cmd = (String) ocmd; if (i + 1 < len) { Object oparam = rc.get(i + 1); if (oparam instanceof String) { String param = (String) oparam; try { if (cmd == UNSEEN) { ms.firstUnreadMessage = Integer.parseInt(param); i++; changed = true; } else if (cmd == UIDVALIDITY) { ms.uidValidity = Integer.parseInt(param); i++; changed = true; } } catch (NumberFormatException e) { throw new ProtocolException("Illegal " + cmd + " value: " + param); } } else if (oparam instanceof List) { if (cmd == PERMANENTFLAGS) { ms.permanentFlags = (List) oparam; i++; changed = true; } } } } } return changed; } else if (id == EXISTS) { ms.messageCount = response.getCount(); return true; } else if (id == RECENT) { ms.newMessageCount = response.getCount(); return true; } else if (id == FLAGS) { ms.flags = response.getResponseCode(); return true; } else { return false; } } /** * Creates a mailbox with the specified name. * @param mailbox the mailbox name * @return true if the mailbox was successfully created, false otherwise */ public boolean create(String mailbox) throws IOException { return invokeSimpleCommand(CREATE + ' ' + quote(UTF7imap.encode(mailbox))); } /** * Deletes the mailbox with the specified name. * @param mailbox the mailbox name * @return true if the mailbox was successfully deleted, false otherwise */ public boolean delete(String mailbox) throws IOException { return invokeSimpleCommand(DELETE + ' ' + quote(UTF7imap.encode(mailbox))); } /** * Renames the source mailbox to the specified name. * @param source the source mailbox name * @param target the target mailbox name * @return true if the mailbox was successfully renamed, false otherwise */ public boolean rename(String source, String target) throws IOException { return invokeSimpleCommand(RENAME + ' ' + quote(UTF7imap.encode(source)) + ' ' + quote(UTF7imap.encode(target))); } /** * Adds the specified mailbox to the set of subscribed mailboxes as * returned by the LSUB command. * @param mailbox the mailbox name * @return true if the mailbox was successfully subscribed, false otherwise */ public boolean subscribe(String mailbox) throws IOException { return invokeSimpleCommand(SUBSCRIBE + ' ' + quote(UTF7imap.encode(mailbox))); } /** * Removes the specified mailbox from the set of subscribed mailboxes as * returned by the LSUB command. * @param mailbox the mailbox name * @return true if the mailbox was successfully unsubscribed, false otherwise */ public boolean unsubscribe(String mailbox) throws IOException { return invokeSimpleCommand(UNSUBSCRIBE + ' ' + quote(UTF7imap.encode(mailbox))); } /** * Returns a subset of names from the compete set of names available to * the client. * @param reference the context relative to which mailbox names are * defined * @param mailbox a mailbox name, possibly including IMAP wildcards */ public ListEntry[] list(String reference, String mailbox) throws IOException { return listImpl(LIST, reference, mailbox); } /** * Returns a subset of subscribed names. * @see #list */ public ListEntry[] lsub(String reference, String mailbox) throws IOException { return listImpl(LSUB, reference, mailbox); } protected ListEntry[] listImpl(String command, String reference, String mailbox) throws IOException { if (reference == null) { reference = ""; } if (mailbox == null) { mailbox = ""; } String tag = newTag(); sendCommand(tag, command + ' ' + quote(UTF7imap.encode(reference)) + ' ' + quote(UTF7imap.encode(mailbox))); List acc = new ArrayList(); while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (response.isUntagged()) { if (id.equals(command)) { List code = response.getResponseCode(); String text = response.getText(); // Populate entry attributes with the interned versions // of the response code. // NB IMAP servers do not necessarily pay attention to case. int alen = (code == null) ? 0 : code.size(); boolean noinferiors = false; boolean noselect = false; boolean marked = false; boolean unmarked = false; for (int i = 0; i < alen; i++) { String attribute = (String) code.get(i); if (attribute.equalsIgnoreCase(LIST_NOINFERIORS)) { noinferiors = true; } else if (attribute.equalsIgnoreCase(LIST_NOSELECT)) { noselect = true; } else if (attribute.equalsIgnoreCase(LIST_MARKED)) { marked = true; } else if (attribute.equalsIgnoreCase(LIST_UNMARKED)) { unmarked = true; } } int si = text.indexOf(' '); char delimiter = '\u0000'; String d = text.substring(0, si); if (!d.equalsIgnoreCase(NIL)) { delimiter = stripQuotes(d).charAt(0); } String mbox = stripQuotes(text.substring(si + 1)); mbox = UTF7imap.decode(mbox); ListEntry entry = new ListEntry(mbox, delimiter, noinferiors, noselect, marked, unmarked); acc.add(entry); } else { asyncResponses.add(response); } } else if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { ListEntry[] entries = new ListEntry[acc.size()]; acc.toArray(entries); return entries; } else { throw new IMAPException(id, response.getText()); } } else { throw new IMAPException(id, response.getText()); } } } /** * Requests the status of the specified mailbox. */ public MailboxStatus status(String mailbox, String[] statusNames) throws IOException { String tag = newTag(); StringBuffer buffer = new StringBuffer(STATUS) .append(' ') .append(quote(UTF7imap.encode(mailbox))) .append(' ') .append('('); for (int i = 0; i < statusNames.length; i++) { if (i > 0) { buffer.append(' '); } buffer.append(statusNames[i]); } buffer.append(')'); sendCommand(tag, buffer.toString()); MailboxStatus ms = new MailboxStatus(); while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (response.isUntagged()) { if (id == STATUS) { List code = response.getResponseCode(); int last = (code == null) ? 0 : code.size() - 1; for (int i = 0; i < last; i += 2) { try { String statusName = ((String) code.get(i)).intern(); int value = Integer.parseInt((String) code.get(i + 1)); if (statusName == MESSAGES) { ms.messageCount = value; } else if (statusName == RECENT) { ms.newMessageCount = value; } else if (statusName == UIDNEXT) { ms.uidNext = value; } else if (statusName == UIDVALIDITY) { ms.uidValidity = value; } else if (statusName == UNSEEN) { ms.firstUnreadMessage = value; } } catch (NumberFormatException e) { throw new IMAPException(id, "Invalid code: " + code); } } } else { asyncResponses.add(response); } } else if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { return ms; } else { throw new IMAPException(id, response.getText()); } } else { throw new IMAPException(id, response.getText()); } } } /** * Append a message to the specified mailbox. * This method returns an OutputStream to which the message should be * written and then closed. * @param mailbox the mailbox name * @param flags optional list of flags to specify for the message * @param content the message body(including headers) * @return true if successful, false if error in flags/text */ public boolean append(String mailbox, String[] flags, byte[] content) throws IOException { return append(mailbox, flags, content, null); } /** * Append a message to the specified mailbox. * This method returns an OutputStream to which the message should be * written and then closed. * @param mailbox the mailbox name * @param flags optional list of flags to specify for the message * @param content the message body(including headers) * @param uidplus handler for any APPENDUID information in the response * @return true if successful, false if error in flags/text */ public boolean append(String mailbox, String[] flags, byte[] content, UIDPlusHandler uidplus) throws IOException { String tag = newTag(); StringBuffer buffer = new StringBuffer(APPEND) .append(' ') .append(quote(UTF7imap.encode(mailbox))) .append(' '); if (flags != null) { buffer.append('('); for (int i = 0; i < flags.length; i++) { if (i > 0) { buffer.append(' '); } buffer.append(flags[i]); } buffer.append(')'); buffer.append(' '); } buffer.append('{'); buffer.append(content.length); buffer.append('}'); sendCommand(tag, buffer.toString()); IMAPResponse response = readResponse(); if (!response.isContinuation()) { throw new IMAPException(response.getID(), response.getText()); } out.write(content); // write the message body out.writeln(); out.flush(); while (true) { response = readResponse(); String id = response.getID(); if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { if (uidplus != null) { processUIDPlus(response.getResponseCode(), uidplus); } return true; } else if (id == NO) { return false; } else { throw new IMAPException(id, response.getText()); } } else if (response.isUntagged()) { asyncResponses.add(response); } else { throw new IMAPException(id, response.getText()); } } } void processUIDPlus(List code, UIDPlusHandler uidplus) { int len = code.size(); for (int i = 0; i < len; i++) { Object item = code.get(i); if (item instanceof String) { if ("APPENDUID".equals(item) && i < len - 2) { long uidvalidity = Long.parseLong((String) code.get(i + 1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -