📄 imapconnection.java
字号:
StringBuffer ids = new StringBuffer(); for (int i = 0; i < uids.length; i++) { if (i > 0) { ids.append(','); } ids.append(uids[i]); } return storeImpl(UID + ' ' + STORE, ids.toString(), flagCommand, flags); } private MessageStatus[] storeImpl(String cmd, String ids, String flagCommand, String[] flags) throws IOException { String tag = newTag(); StringBuffer buffer = new StringBuffer(cmd); buffer.append(' '); buffer.append(ids); buffer.append(' '); buffer.append(flagCommand); buffer.append(' '); buffer.append('('); for (int i = 0; i < flags.length; i++) { if (i > 0) { buffer.append(' '); } buffer.append(flags[i]); } buffer.append(')'); sendCommand(tag, buffer.toString()); List list = new ArrayList(); while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (response.isUntagged()) { int msgnum = response.getCount(); List code = response.getResponseCode(); // 2 different styles returned by server: FETCH or FETCH FLAGS if (id == FETCH) { MessageStatus mf = new MessageStatus(msgnum, code); list.add(mf); } else if (id == FETCH_FLAGS) { List base = new ArrayList(); base.add(FLAGS); base.add(code); MessageStatus mf = new MessageStatus(msgnum, base); list.add(mf); } else { asyncResponses.add(response); } } else if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { MessageStatus[] mf = new MessageStatus[list.size()]; list.toArray(mf); return mf; } else { throw new IMAPException(id, response.getText()); } } else { throw new IMAPException(id, response.getText()); } } } /** * Copies the specified messages to the end of the destination mailbox. * @param messages the message numbers * @param mailbox the destination mailbox */ public boolean copy(int[] messages, String mailbox) throws IOException { return copy(messages, mailbox, null); } /** * Copies the specified messages to the end of the destination mailbox. * @param messages the message numbers * @param mailbox the destination mailbox * @param uidplus UIDPLUS callback for COPYUID information */ public boolean copy(int[] messages, String mailbox, UIDPlusHandler uidplus) throws IOException { if (messages == null || messages.length < 1) { return true; } StringBuffer buffer = new StringBuffer(COPY) .append(' '); for (int i = 0; i < messages.length; i++) { if (i > 0) { buffer.append(','); } buffer.append(messages[i]); } buffer.append(' ').append(quote(UTF7imap.encode(mailbox))); String tag = newTag(); sendCommand(tag, buffer.toString()); while (true) { IMAPResponse 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()); } } } /** * Returns the namespaces available on the server. * See RFC 2342 for details. */ public Namespaces namespace() throws IOException { String tag = newTag(); sendCommand(tag, NAMESPACE); Namespaces namespaces = null; while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { return namespaces; } else { throw new IMAPException(id, response.getText()); } } else if (response.isUntagged()) { if (NAMESPACE.equals(response.getID())) { namespaces = new Namespaces(response.getText()); } else { asyncResponses.add(response); } } else { throw new IMAPException(id, response.getText()); } } } /** * Changes the access rights on the specified mailbox such that the * authentication principal is granted the specified permissions. * @param mailbox the mailbox name * @param principal the authentication identifier * @param rights the rights to assign */ public boolean setacl(String mailbox, String principal, int rights) throws IOException { String command = SETACL + ' ' + quote(UTF7imap.encode(mailbox)) + ' ' + UTF7imap.encode(principal) + ' ' + rightsToString(rights); return invokeSimpleCommand(command); } /** * Removes any access rights for the given authentication principal on the * specified mailbox. * @param mailbox the mailbox name * @param principal the authentication identifier */ public boolean deleteacl(String mailbox, String principal) throws IOException { String command = DELETEACL + ' ' + quote(UTF7imap.encode(mailbox)) + ' ' + UTF7imap.encode(principal); return invokeSimpleCommand(command); } /** * Returns the access control list for the specified mailbox. * The returned rights are a logical OR of RIGHTS_* bits. * @param mailbox the mailbox name * @return a map of principal names to Integer rights */ public Map getacl(String mailbox) throws IOException { String tag = newTag(); sendCommand(tag, GETACL + ' ' + quote(UTF7imap.encode(mailbox))); Map ret = new TreeMap(); while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { return ret; } else if (id == NO) { return null; } else { throw new IMAPException(id, response.getText()); } } else if (response.isUntagged()) { if (ACL.equals(response.getID())) { String text = response.getText(); List args = parseACL(text, 1); String rights = (String) args.get(2); ret.put(args.get(1), new Integer(stringToRights(rights))); } else { asyncResponses.add(response); } } else { throw new IMAPException(id, response.getText()); } } } /** * Returns the rights for the given principal for the specified mailbox. * The returned rights are a logical OR of RIGHTS_* bits. * @param mailbox the mailbox name * @param principal the authentication identity */ public int listrights(String mailbox, String principal) throws IOException { String tag = newTag(); String command = LISTRIGHTS + ' ' + quote(UTF7imap.encode(mailbox)) + ' ' + UTF7imap.encode(principal); sendCommand(tag, command); int ret = -1; while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { return ret; } else if (id == NO) { return -1; } else { throw new IMAPException(id, response.getText()); } } else if (response.isUntagged()) { if (LISTRIGHTS.equals(response.getID())) { String text = response.getText(); List args = parseACL(text, 1); ret = stringToRights((String) args.get(2)); } else { asyncResponses.add(response); } } else { throw new IMAPException(id, response.getText()); } } } /** * Returns the rights for the current principal for the specified mailbox. * The returned rights are a logical OR of RIGHTS_* bits. * @param mailbox the mailbox name */ public int myrights(String mailbox) throws IOException { String tag = newTag(); String command = MYRIGHTS + ' ' + quote(UTF7imap.encode(mailbox)); sendCommand(tag, command); int ret = -1; while (true) { IMAPResponse response = readResponse(); String id = response.getID(); if (tag.equals(response.getTag())) { processAlerts(response); if (id == OK) { return ret; } else if (id == NO) { return -1; } else { throw new IMAPException(id, response.getText()); } } else if (response.isUntagged()) { if (MYRIGHTS.equals(response.getID())) { String text = response.getText(); List args = parseACL(text, 0); ret = stringToRights((String) args.get(2)); } else { asyncResponses.add(response); } } else { throw new IMAPException(id, response.getText()); } } } private String rightsToString(int rights) { StringBuffer buf = new StringBuffer(); if ((rights & RIGHTS_LOOKUP) != 0) { buf.append('l'); } if ((rights & RIGHTS_READ) != 0) { buf.append('r'); } if ((rights & RIGHTS_SEEN) != 0) { buf.append('s'); } if ((rights & RIGHTS_WRITE) != 0) { buf.append('w'); } if ((rights & RIGHTS_INSERT) != 0) { buf.append('i'); } if ((rights & RIGHTS_POST) != 0) { buf.append('p'); } if ((rights & RIGHTS_CREATE) != 0) { buf.append('c'); } if ((rights & RIGHTS_DELETE) != 0) { buf.append('d'); } if ((rights & RIGHTS_ADMIN) != 0) { buf.append('a'); } return buf.toString(); } private int stringToRights(String text) { int ret = 0; int len = text.length(); for (int i = 0; i < len; i++) { switch (text.charAt(i)) { case 'l': ret |= RIGHTS_LOOKUP;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -