📄 imap4.java
字号:
}
return;
}
if ( reply.startsWith("* xMujmail-url") == false )
throw new MyException( MyException.PROTOCOL_BASE, "200: " + "Internal error - unknown server reply 1- " + reply);
// Parse length and read HTML
int len = 0; // HTML length
String num = reply.substring(reply.indexOf("{")+1, reply.indexOf("}"));
len = Integer.parseInt(num);
System.out.print("getURLCommand - len"); System.out.println(len);
URL = "";
while ( URL.length() <= len ) URL = URL + connection.getLine();
// Parse final tag
reply = connection.getLine();
if ( reply.startsWith(tag + "OK") == false )
throw new MyException( MyException.PROTOCOL_BASE, "200: " + "Internal error - unknown server reply 2 - " + reply);
}
//For push mail testing
public boolean isImap(){
return true;
}
// IMAP IDLE
/* TODO: Prevent connection time out by sending NOOP every 15 min?
*
*/
public synchronized void push(){
PushTask pushTask = new PushTask();
pushTask.disableDisplayingProgress();
pushTask.start();
}
private void removeMessageFromBox(int actual) {
String msgID = ((MessageHeader) deleted.elementAt(actual)).getMessageID();
mailsOnServer.remove(msgID);
// Remove from global seen mail database
MujMail.mujmail.getMailDBSeen().deleteSeen(account.getEmail(), msgID);
inProtocolTask.incActual(1);
}
private class PushTask extends StoppableBackgroundTask {
public PushTask() {
super(Lang.get(Lang.AC_PUSH) + " " + account.getEmail() + " PushTask");
}
public void doWork() {
Vector newMails = null;
String ID = null;
//lock();
System.out.println("In IMAP IDLE run 1");
try {
connection.unQuit();
connection.clearInput();
if (open(this)) {
Vector tempVector = new Vector();
while (getTargetBox().isPushActive()) {
selectMailBox("INBOX");
execute("IDLE", false);
String reply = connection.getLine();
System.out.println("Entering IDLE mode");
System.out.println("Reply IDLE: " + reply);
while(connection.available()==false){
if(!getTargetBox().isPushActive()){
System.out.println("PUSH IS NOT ACTIVE");
connection.sendCRLF("DONE");
System.out.println("End IDLE execution");
return;
}
Thread.sleep(20 * 1000); //Sleep for 20 seconds
}
System.out.println("Mail(s) arrived");
System.out.println("Reply2: " + connection.getLine());
connection.sendCRLF("DONE");
System.out.println("Reply3: " + connection.getLine());
newMails = searchMailsMatching("UNSEEN");
for (int i = newMails.size(); i > 0; --i) {
ID = (String) newMails.elementAt(i - 1);
if (!getTargetBox().wasOnceDownloaded(account.getEmail(), ID)) {
//Actually if activate in settings
new AudioAlert();
//getNewMails(); --> Problem with GUI because of busy thread (TODO)
//Another possible solution, but stupid one:
//runMode = InProtocol.GET_NEW_MAILS;
//run();
// Really not beautiful solution, but does work very well --> (redundant)
// Just copy and paste the GET_NEW_MAILS part from run()
long startTime, wholeTime;
String waitTime;
int actual = 0;
String line, sld_box;
startTime = System.currentTimeMillis();
if (getTargetBox().isSyncRunning()) //if sync is running
{
mailsOnServer.clear();
}//we need to recreate a new mailsOnServer lists
//if its server->inbox sync is called then we want all mails
//otherwise we just want to check new mails
String criterium = getTargetBox().isSyncRunning() ? "UNSEEN" : "ALL";
setTitle(Lang.get(Lang.ALRT_INPL_CHECK_MAILS) + account.getEmail());
MessageHeader header;
String mailBoxes = account.getIMAPPprimaryBox();
if (!mailBoxes.endsWith(",")) {
mailBoxes += ",";
}
while (mailBoxes.length() != 0) {
//choose next mailbox
sld_box = mailBoxes.substring(0, mailBoxes.indexOf(','));
//define what's the next mailbox
mailBoxes = mailBoxes.substring(sld_box.length() + 1);
//if selecting a mailbox is unsuccessful skip to the next one
if (!selectMailBox(sld_box)) {
setTitle(Lang.get(Lang.ALRT_PL_CONNECTING) + account.getEmail() + ": " + sld_box + Lang.get(Lang.FAILED));
continue;
}
Vector newMails1 = new Vector();
Vector fetchTags = new Vector();
String fetchTag;
//let's find UIDs of mails
Vector tmp = searchMailsMatching(criterium);
for (int x = 0; x < tmp.size(); ++x) {
header = new MessageHeader(getTargetBox());
header.setMessageID(sld_mailBox_uidval + tmp.elementAt(x));
newMails1.addElement(header); //mark it to potentially new mails
}
int n = 0;
updateProgress(newMails.size(), 0);
//for all potentially new mails, the newest first
for (actual = newMails1.size() - 1; actual >= 0; --actual) {
header = (MessageHeader) newMails1.elementAt(actual);
//let's remember that this mail is stored on the server
mailsOnServer.put(header.getMessageID(), String.valueOf(actual));
getTargetBox().newMailOnServer(); //increase synchronization counter
if (!getTargetBox().wasOnceDownloaded(account.getEmail(), header.getMessageID())) { //check if that mail wasnt already downloaded
fetchTag = execute("UID FETCH " + parseUID(header.getMessageID()) + " (RFC822.SIZE)", false);
if (!Settings.downWholeMail || Settings.safeMode) {
execute("UID FETCH " + parseUID(header.getMessageID()) + " (RFC822.HEADER)", false);
} else {
execute("UID FETCH " + parseUID(header.getMessageID()) + " (RFC822)", false);
}
fetchTags.addElement(fetchTag);
++n;
if (Settings.maxMailsRetrieve > 0 && n >= Settings.maxMailsRetrieve) //limit excessed
{
break;
}
} else { //if the mail was already downloaded, remove it from
newMails1.removeElementAt(actual); //remove it from potentially new mails list
incActual(1);
}
if (stopped()) {
break;
}
}
int j = fetchTags.size() - 1;
n = newMails1.size() - n; //we will parse only mails whose size and header were required
for (actual = newMails1.size() - 1; actual >= n; --actual) { //now lets parse new mails' headers
header = (MessageHeader) newMails1.elementAt(actual);
fetchTag = (String) fetchTags.elementAt(j--);
do {
//skip useless lines of previous iteration or of fetch response
//until we get tagged BAD or NO response or good UID response
line = connection.getLine();
} while (!(line.startsWith(fetchTag) || line.indexOf("UID " + parseUID(header.getMessageID())) != -1));
if (line.startsWith(fetchTag)) { //bad response
setTitle(account.getEmail() + ": " + line);
continue;
}
int y = line.indexOf("RFC822.SIZE") + 12;
line = line.substring(y);
for (y = 0; y < line.length(); ++y) {
if (!('0' <= line.charAt(y) && line.charAt(y) <= '9')) {
break;
}
}
header.setSize(Integer.parseInt(line.substring(0, y)));
parseHeaders(header);
if (Settings.downWholeMail && !Settings.safeMode) {
setTitle(Lang.get(Lang.ALRT_INPL_DOWN_MAIL) + header.getSubject());
try {
parseBody(header, this);
setTitle(Lang.get(Lang.ALRT_INPL_DOWN_MAIL) + header.getSubject() + Lang.get(Lang.SUCCESS));
} catch (MyException me) {
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);
} catch (MyException exp) {
clear(header); //markAsDeleted partially downloaded bodies
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;
}
}
//also mark this mail as checked
getTargetBox().addToMsgIDs(header);
tempVector.addElement( header ); //store the mail
if (!header.wasRead()) {
getTargetBox().changeUnreadMails(1);
}
incActual(1);
if (stopped()) {
break;
}
}
if (stopped()) {
break;
}
}
wholeTime = System.currentTimeMillis() - startTime;
waitTime = wholeTime > 1000 ? wholeTime / 1000 + "sec" : wholeTime + "msec";
setTitle(Lang.get(Lang.ALRT_INPL_CHECK_MAILS) + account.getEmail() + "" + Lang.get(Lang.IN) + waitTime);
//Threading algorithm has to be called each time
//new mails have been retrieved.
synchronized (getTargetBox().getStorage()) {
Functions.addMailsInStorageToVector(getTargetBox().getStorage(), tempVector);
getTargetBox().setStorage( Algorithm.getAlgorithm().invoke( tempVector ) );
}
MujMail.mujmail.getMenu().refreshAll(true);
break;
}
}
connection.clearInput();
} // while loop end
if (PushTask.DEBUG) System.out.println( "DEBUG PushTask.doRunWork() - box name: " + getTargetBox().getName() );
} // if ( open() ) condition end
System.out.println("End IDLE execution");
_close(this);
} catch (MyException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -