📄 pop3.java
字号:
{ max = Settings.maxMailsRetrieve; } inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_CHECK_MAILS) + account.getEmail()); inProtocolTask.updateProgress(max, 0); int actual = 0, mailsCount = 0; String idLine, sizeLine; MessageHeader header; Vector newMails = new Vector(actual); Stack msgNumbers = new Stack(); //lets send commands to get msgIDs and size for the //mails and parse MsgIDs and size from input stream while (mailsCount < max && actual < newMailsCount) { //sending command connection.sendCRLF("UIDL " + (actual + 1) + "\r\n" + "LIST " + (actual + 1)); //parsing reply idLine = connection.getLine(); sizeLine = connection.getLine(); //if we could get its MsgID and size, put it to newMails - mark it as a potentially new mail if (idLine.startsWith("+OK") && sizeLine.startsWith("+OK")) { header = new MessageHeader(targetBox); header.setMessageID(idLine.substring(idLine.lastIndexOf(' ') + 1).trim()); header.setSize(Integer.parseInt(sizeLine.substring(sizeLine.lastIndexOf(' ') + 1).trim())); if (targetBox.isSyncRunning()) { newMails.addElement(header); //save message-numbers to be used for TOP command msgNumbers.push(new Integer(actual + 1)); } //check if that mail wasnt already downloaded else if (!targetBox.wasOnceDownloaded(account.getEmail(), header.getMessageID())) { newMails.addElement(header); //save message-numbers to be used for TOP command msgNumbers.push(new Integer(actual + 1)); mailsCount++; } } ++actual; if (inProtocolTask.stopped()) { _close(inProtocolTask, false); return; } } Vector tempStorage = new Vector(); int n = 0; for (actual = newMails.size() - 1; actual >= 0; --actual) { //for all potentially new mails in the list, the newest first header = (MessageHeader) newMails.elementAt(actual); mailsOnServer.put(header.getMessageID(), String.valueOf(actual + 1)); getTargetBox().newMailOnServer();//increase synchronization counter if (!targetBox.wasOnceDownloaded(account.getEmail(), header.getMessageID())) { if (!Settings.downWholeMail || Settings.safeMode) { connection.sendCRLF("TOP " + (Integer)msgNumbers.pop() + " 0"); } else { //we send connection.sendCRLF("TOP "+n+" 1000000"); //instead of connection.sendCRLF("RETR "+n); here because RETR returns data in a messed order //not the same as we wanted!!! //ei: RETR 1,RETR 2, RETR3 are returned as result_of_RETR1 half_of_RETR2, result_RETR3 and then rest of RETR2 //very annoying thing. I spent 3 days with this (Tung). connection.sendCRLF("TOP " + (actual + 1) + " 1000000"); } ++n; if (Settings.maxMailsRetrieve > 0 && n >= Settings.maxMailsRetrieve) //limit excessed { break; } } else { //if the mail was already downloaded, remove it from the potentially new mails list newMails.removeElementAt(actual); inProtocolTask.incActual(1); } if (inProtocolTask.stopped()) { break; } } n = newMails.size() - n; for (actual = newMails.size() - 1; actual >= n; --actual) { //now lets parse new mails' headers header = (MessageHeader) newMails.elementAt(actual); String line; //skip useless lines of previous iteration do { line = connection.getLine(); } while (!line.startsWith("+OK") && !line.startsWith("-ERR")); if (line.startsWith("-ERR")) { inProtocolTask.setTitle(account.getEmail() + ": " + line); continue; } parseHeaders(header); if (Settings.downWholeMail && !Settings.safeMode) { inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_DOWN_MAIL) + header.getSubject()); try { parseBody(header, inProtocolTask); inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_DOWN_MAIL) + header.getSubject() + Lang.get(Lang.SUCCESS)); } catch (MyException me) { inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_DOWN_MAIL) + header.getSubject() + " " + Lang.get(Lang.FAILED) + " " + me.getDetails()); } } try { header.saveHeader(); //cache the mail so next time we can quickly recognize it as already downloaded getTargetBox().addToOnceDownloaded(header); getTargetBox().addToMsgIDs(header); tempStorage.addElement( header ); //store the mail to box's storage vector if (!header.wasRead()) { getTargetBox().changeUnreadMails(1); } } catch (MyException exp) { clear(header); //markAsDeleted partially downloaded bodies inProtocolTask.setTitle(Lang.get(Lang.ALRT_SAVING) + header.getSubject() + " " + Lang.get(Lang.FAILED) + " " + exp.getDetails()); if (getTargetBox().isSyncRunning()) //something's gone wrong, now we have to stop sync { throw exp; } } inProtocolTask.incActual(1); if (inProtocolTask.stopped()) { if (DEBUG) System.out.println("DEBUG GetMailsTask.doRunWork(): stopped."); break; } } if (inProtocolTask.stopped()) { _close(inProtocolTask, false); } if (DEBUG) { System.out.println( "DEBUG GetMailsTask.doRunWork() - box name: " + getTargetBox() ); System.out.println( "DEBUG GetMailsTask.doRunWork() - tempStorage: " + tempStorage); } // In case this method is called from serversSync() method // addMailsInStorageToVector() and setStorage() methods // have to be atomic, otherwise TheBox.storage can be // overwritten by the last thread running getNewMails() method synchronized (getTargetBox().getStorage()) { Functions.addMailsInStorageToVector( getTargetBox().getStorage(), tempStorage ); getTargetBox().setStorage( Algorithm.getAlgorithm().invoke( tempStorage ) ); } if (DEBUG) System.out.println("DEBUG GetMailsTask.doRunWork() - saved to storage."); long wholeTime = System.currentTimeMillis() - startTime; String waitTime = wholeTime > 1000 ? wholeTime / 1000 + "sec" : wholeTime + "msec"; inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_CHECK_MAILS) + account.getEmail() + "" + Lang.get(Lang.IN) + waitTime); } catch (MyException myException) { if (POP3.DEBUG) { myException.printStackTrace(); } resolveMyExceptionWhileRunning(myException); } catch (Exception ex) { if (POP3.DEBUG) { ex.printStackTrace(); } resolveExceptions("100: " + ex + "/ " + account.getEmail(), SOURCE_FILE); } } protected void downloadBody() { try { long startTime = System.currentTimeMillis(); if (!open(inProtocolTask)) { synchronized (actHeader) { actHeader.notify(); actHeader = null; } return; } inProtocolTask.setTitle(Lang.get(runMode == RETRIEVE_BODY ? Lang.ALRT_INPL_DOWN_MAIL : Lang.ALRT_INPL_REDOWN_MAIL) + actHeader.getSubject()); inProtocolTask.updateProgress(actHeader.getSize(), 0); connection.sendCRLF("RETR " + getMsgNum(actHeader)); if (connection.getLine().startsWith("-ERR")) { throw new MyException(MyException.PROTOCOL_CANNOT_RETRIEVE_BODY, "200: " + Lang.get(Lang.ALRT_INPL_NO_LONGER_ON_SERVER)); } // parse body parseBody(actHeader, inProtocolTask); actHeader.saveHeader(); //update new data into DBase if (Settings.safeMode) { getTargetBox().setLastSafeMail(actHeader); } long wholeTime = System.currentTimeMillis() - startTime; String waitTime = wholeTime > 1000 ? wholeTime / 1000 + "sec" : wholeTime + "msec"; inProtocolTask.setTitle("*" + Lang.get(runMode == RETRIEVE_BODY ? Lang.ALRT_INPL_DOWN_MAIL : Lang.ALRT_INPL_REDOWN_MAIL) + actHeader.getSubject() + " " + Lang.get(Lang.IN) + waitTime); inProtocolTask.updateProgress(actHeader.getSize(), actHeader.getSize()); synchronized (actHeader) { //everything is ok now actHeader.notify(); actHeader = null; } } catch (MyException myException) { resolveMyExceptionWhileRunning(myException); } } protected void removeMails() { try { long startTime = System.currentTimeMillis(); if (deleted.isEmpty() || !open(inProtocolTask)) { return; } int max = deleted.size(); int actual = 0; inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_DEL_MAILS) + account.getEmail()); inProtocolTask.updateProgress(max, actual); connection.sendCRLF("UIDL"); connection.getLine(); String reply, ID, num; //browse the whole UIDL list and try to markAsDeleted all marked mails while (!(reply = connection.getLine()).startsWith(".")) { ID = reply.substring(reply.indexOf(' ') + 1).trim(); //get its ID on the server if (deleted.containsKey(ID)) { //was is marked as deleted? num = reply.substring(0, reply.indexOf(' ')).trim(); //get its number offset on the server connection.sendCRLF("DELE " + num); mailsOnServer.remove(ID); MujMail.mujmail.getMailDBSeen().deleteSeen(account.getEmail(), ID); inProtocolTask.incActual(1); actual++; } if (inProtocolTask.stopped()) { break; } } while (actual-- > 0) { connection.getLine(); } //get server's response line. //We don't check the responses here just because of laziness :) _close(inProtocolTask); deleted.clear(); //all mails marked as deleted should be deleted by now long wholeTime = System.currentTimeMillis() - startTime; String waitTime = wholeTime > 1000 ? wholeTime / 1000 + "sec" : wholeTime + "msec"; inProtocolTask.setTitle(Lang.get(Lang.ALRT_INPL_DEL_MAILS) + account.getEmail() + Lang.get(Lang.IN) + waitTime); } catch (MyException myException) { resolveMyExceptionWhileRunning(myException); } } protected void setFlags() { // not supported by pop3 } protected void removeFlags() { // not supported by pop3 } protected void getURL() throws MyException { // not supported by pop3 }} //end of POP3 class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -