📄 inprotocol.java
字号:
connection.unQuit(); connection.clearInput(); switch (runMode) { case InProtocol.CLOSE: _close(inProtocolTask); break; case InProtocol.GET_NEW_MAILS: getNewMails(); break; case InProtocol.REDOWNLOAD_BODY: case InProtocol.RETRIEVE_BODY: case InProtocol.CONVERT_BODY: downloadBody(); break; case InProtocol.REMOVE_MAILS: removeMails(); break; case InProtocol.SET_FLAGS: setFlags(); break; case InProtocol.REMOVE_FLAGS: removeFlags(); break; case IMAP4.GET_URL: getURL(); break; } } catch (MyException ex) { ex.printStackTrace(); resolveMyExceptionWhileRunning( ex); } catch (Error e) { e.printStackTrace(); resolveExceptions("100: " + e + "/ " + account.getEmail(), SOURCE_FILE); } catch (Exception ex) { ex.printStackTrace(); resolveExceptions("100: " + ex + "/ " + account.getEmail(), SOURCE_FILE); } actionAfterTasksCompleted(); } public synchronized void getBody(MessageHeader header, InBox box) { if (DEBUG) System.out.println("DEBUG InProtocol.getBody(MessageHeader) - before execution"); if (actHeader != null) //already fetching { if (DEBUG) { System.out.println("DEBUG InProtocol.getBody already fetching"); } return; } //ok, its not nice thing to put inThreadGlobal() immediately here, to make InProtocol be busy //but it triggers inBox to draw the progress bar right after we press an action button //otherwise it can make an impatient user pressing the buttons more times thinking that the operation didnt undergo. //and mess our parsing incThreadGlobal(); actHeader = header; reDownloadMode = -1; runMode = InProtocol.RETRIEVE_BODY; this.targetBox = box; this.reportBox = box; if (DEBUG) { System.out.println("DEBUG InProtocol.getBody starting retrieving body"); } inProtocolTask = new InProtocolTask(this, "Retrieving mail body"); inProtocolTask.start(); } /** * Sets the given message flags at server side. * @param header Message to set flags for * @param flags Flags to set, e.g. "(\Seen \Answered)" */ public synchronized void setFlags(MessageHeader header, String flags, int ACTION, TheBox reportBox) { //ok, its not nice thing to put inThreadGlobal() immediately here, to make InProtocol be busy //but it triggers inBox to draw the progress bar right after we press an action button //otherwise it can make an impatient user pressing the buttons more times thinking that the operation didnt undergo. //and mess our parsing incThreadGlobal(); actHeader = header; flagsToSet = flags; this.reportBox = reportBox; this.targetBox = (InBox)reportBox; //TODO Ask David if it is correct if (ACTION == SET_FLAGS) runMode = InProtocol.SET_FLAGS; else runMode = InProtocol.REMOVE_FLAGS; inProtocolTask = new InProtocolTask(this, "Setting flags at IMAP server"); inProtocolTask.start(); } //called to redownload a partial mail (some parts of a mail is missing) public synchronized void getConvertedBody(MessageHeader header, byte mode, InBox box) { if (DEBUG) { System.out.println("DEBUG InProtocol.getConvertedBody before execution"); } if (actHeader != null) //already fetching { return; } incThreadGlobal(); actHeader = header; reDownloadMode = mode; runMode = InProtocol.CONVERT_BODY; this.targetBox = box; this.reportBox = box; //Thread t = new Thread(this); //t.start(); inProtocolTask = new InProtocolTask(this, "Converting bodypart"); inProtocolTask.start(); //t.setPriority(Thread.MAX_PRIORITY); } //called to redownload a partial mail (some parts of a mail is missing) public synchronized void regetBody(MessageHeader header, byte mode, InBox box) { if (DEBUG) { System.out.println("DEBUG InProtocol.regetBody - before execution"); } if (actHeader != null) //already fetching { return; } incThreadGlobal(); actHeader = header; reDownloadMode = mode; runMode = InProtocol.REDOWNLOAD_BODY; this.targetBox = box; this.reportBox = box; inProtocolTask = new InProtocolTask(this, "Redownloading mail body"); inProtocolTask.start(); } // deletes all the mails that are in the queue deleted public synchronized void removeMsgs(InBox box) { if (DEBUG) { System.out.println("DEBUG InProtocol.removeMsgs - before execution"); } incThreadGlobal(); runMode = InProtocol.REMOVE_MAILS; this.targetBox = box; this.reportBox = box; inProtocolTask = new InProtocolTask(this, "Removing mails"); inProtocolTask.start(); } public synchronized void close(boolean forcedDisc, TheBox reportBox) { //closes and must close streams for correct reopen/relogin if (this.forcedDisc) //some thread allready wants really to disconnect { return; } incThreadGlobal(); this.forcedDisc = forcedDisc; runMode = InProtocol.CLOSE; this.reportBox = reportBox; inProtocolTask = new InProtocolTask(this, "Closing connection"); inProtocolTask.start(); } public synchronized void poll(InBox box) { runMode = InProtocol.POLL; this.targetBox = box; this.reportBox = box; inProtocolTask = new InProtocolTask(this, "Polling"); inProtocolTask.disableDisplayingProgress(); inProtocolTask.start(); } public synchronized void saveMailToSent(String inMailText, InBox box) { if (DEBUG) { System.out.println("DEBUG InProtocol.saveMailToSent " + inMailText); } saveMailData = inMailText; incThreadGlobal(); runMode = InProtocol.SAVE_MAIL_SENT; this.targetBox = box; this.reportBox = box; inProtocolTask = new InProtocolTask(this, "Saving mails to server's Sent folder"); inProtocolTask.start(); } private boolean doPolling() { if (runMode == InProtocol.POLL) { lock(); try { connection.unQuit(); connection.clearInput(); if (open(null)) { findFirstNewMailWhilePolling(); } } catch (MyException ex) { ex.printStackTrace(); //do nothing, be silent bitch } catch (Exception ex) { ex.printStackTrace(); } unlock(); return true; } return false; } /** * Handles input line with flags - set processed message header flags * according to these flags. * @param line protocol line with flags */ private void handleLineWithFlags(String line) { if (!line.startsWith(" FLAGS (")) return; String flags = line.substring(8); //length of " FLAGS (" //length of " FLAGS (" flags = flags.substring(0, flags.indexOf(")")); handleFlags(actHeader, flags); } /** * Parses body of the multipart e-mail. * * @param header * @param progress * @throws MyException */ private void parseBodyOfMultipartMessage(MessageHeader header, StoppableProgress progress) throws MyException { //boundary of the actual email body, //it may be changed if the email has many encapsulated emails within its body //multipart mail: fetch all body parts that we want to fetch //StringBuffer bf = new StringBuffer(); int linesCount = 0; final String endBoundary = header.getBoundary() + "--"; //this is where the email body ends if (DEBUG) System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - endBoundary=" + endBoundary); byte nextIncomplete = 0; byte actualBPNumber = -1; //counter of actual body part //boundary of the actual email body, //it may be changed if the email has many encapsulated emails within its body String bodyBoundary = header.getBoundary(); Stack/*<String>*/ boundaries = new Stack(); if (DEBUG) System.out.println("DEBUG InProtocol.parseBodyOfMultipartMessage - bodyBoundary=" + bodyBoundary); String line = connection.getLine(); if (DEBUG) System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); // process the body until end of mail (boundary end is found or END_OF_MAIL) boolean wasStopped = false; // true if fetching of bodypart was stopped boolean redownloaded = false; while ( !line.startsWith(END_OF_MAIL) && !line.startsWith(endBoundary) ) { ++actualBPNumber; ////if it's not IMAP and redownloading a particular bodypart //if ( ! (account.isIMAP() && reDownloadMode != -1) ) { if ( !account.isIMAP() || reDownloadMode == -1 ) { // Betlista: De Morgan, hope it's the same as line above actuallyParsedBodyPart = new BodyPart(header); //skip everything to the boundary while (!line.startsWith(bodyBoundary)) { line = connection.getLine(); if (DEBUG) System.out.print("DEBUG InProtocol.parseBodyOfMultipartMessage - line='" + line + "'"); } //but if its also endBoundary then its the end of the mail if (line.startsWith(endBoundary)) { break; } if (runMode == REDOWNLOAD_BODY) { //find an incomplete body part if (reDownloadMode == -1) { nextIncomplete = actualBPNumber; } else { //redownloading a particular part nextIncomplete = reDownloadMode; } //if we want to redownload a body part but it's not the actual one if (actualBPNumber < nextIncomplete) { //skip the line to the body part continue; } //if all incomplete parts have been downloaded, stop the downloading operation if (actualBPNumber > nextIncomplete) { //close(false); //let's save the connection and save the bandwidth break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -