mirror of https://github.com/M66B/FairEmail.git
Workaround JavaMail UTF-8 search bug
This commit is contained in:
parent
fcf60f7fa5
commit
3eb2798a17
|
@ -21,12 +21,15 @@ package eu.faircode.email;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.sun.mail.iap.Argument;
|
||||||
|
import com.sun.mail.iap.Response;
|
||||||
import com.sun.mail.imap.IMAPFolder;
|
import com.sun.mail.imap.IMAPFolder;
|
||||||
import com.sun.mail.imap.IMAPMessage;
|
import com.sun.mail.imap.IMAPMessage;
|
||||||
import com.sun.mail.imap.IMAPStore;
|
import com.sun.mail.imap.IMAPStore;
|
||||||
|
import com.sun.mail.imap.protocol.IMAPProtocol;
|
||||||
|
import com.sun.mail.imap.protocol.IMAPResponse;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.Normalizer;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -91,7 +94,7 @@ public class ViewModelBrowse extends ViewModel {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
DB db = DB.getInstance(state.context);
|
DB db = DB.getInstance(state.context);
|
||||||
EntityFolder folder = db.folder().getFolder(state.fid);
|
final EntityFolder folder = db.folder().getFolder(state.fid);
|
||||||
if (folder == null) // unified inbox
|
if (folder == null) // unified inbox
|
||||||
return;
|
return;
|
||||||
if (folder.account == null) // outbox
|
if (folder.account == null) // outbox
|
||||||
|
@ -175,14 +178,80 @@ public class ViewModelBrowse extends ViewModel {
|
||||||
state.imessages = state.ifolder.getMessages();
|
state.imessages = state.ifolder.getMessages();
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
state.imessages = state.ifolder.search(
|
SearchTerm term = new OrTerm(
|
||||||
getSearchTerm(state.search, folder.keywords.length > 0));
|
new OrTerm(
|
||||||
} catch (MessagingException ex) {
|
new FromStringTerm(state.search),
|
||||||
String search = Normalizer
|
new RecipientStringTerm(Message.RecipientType.TO, state.search)
|
||||||
.normalize(state.search, Normalizer.Form.NFD)
|
),
|
||||||
.replaceAll("[^\\p{ASCII}]", "");
|
new OrTerm(
|
||||||
state.imessages = state.ifolder.search(
|
new SubjectTerm(state.search),
|
||||||
getSearchTerm(search, folder.keywords.length > 0));
|
new BodyTerm(state.search)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (folder.keywords.length > 0)
|
||||||
|
term = new OrTerm(term, new FlagTerm(
|
||||||
|
new Flags(Helper.sanitizeKeyword(state.search)), true));
|
||||||
|
|
||||||
|
state.imessages = state.ifolder.search(term);
|
||||||
|
} catch (final MessagingException ex) {
|
||||||
|
Object result = state.ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
|
||||||
|
@Override
|
||||||
|
public Object doCommand(IMAPProtocol protocol) {
|
||||||
|
try {
|
||||||
|
if (!protocol.supportsUtf8())
|
||||||
|
throw ex;
|
||||||
|
|
||||||
|
// SEARCH OR OR FROM "x" TO "x" OR SUBJECT "x" BODY "x" ALL
|
||||||
|
// SEARCH OR OR OR FROM "x" TO "x" OR SUBJECT "x" BODY "x" (KEYWORD x) ALL
|
||||||
|
Argument a = new Argument();
|
||||||
|
a.writeAtom("OR");
|
||||||
|
a.writeAtom("OR");
|
||||||
|
a.writeAtom("FROM");
|
||||||
|
a.writeBytes(state.search.getBytes());
|
||||||
|
a.writeAtom("TO");
|
||||||
|
a.writeBytes(state.search.getBytes());
|
||||||
|
a.writeAtom("OR");
|
||||||
|
a.writeAtom("SUBJECT");
|
||||||
|
a.writeBytes(state.search.getBytes());
|
||||||
|
a.writeAtom("BODY");
|
||||||
|
a.writeBytes(state.search.getBytes());
|
||||||
|
a.writeAtom("ALL");
|
||||||
|
Response[] responses = protocol.command("SEARCH", a);
|
||||||
|
|
||||||
|
int msgnum;
|
||||||
|
List<Integer> msgnums = new ArrayList<>();
|
||||||
|
for (int i = 0; i < responses.length; i++) {
|
||||||
|
if (responses[i] instanceof IMAPResponse) {
|
||||||
|
IMAPResponse ir = (IMAPResponse) responses[i];
|
||||||
|
if (ir.keyEquals("SEARCH")) {
|
||||||
|
while ((msgnum = ir.readNumber()) != -1)
|
||||||
|
msgnums.add(msgnum);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (responses[i].isOK())
|
||||||
|
Log.i(folder.name + " response=" + responses[i]);
|
||||||
|
else
|
||||||
|
throw new MessagingException(responses[i].toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Message[] imessages = new Message[msgnums.size()];
|
||||||
|
for (int i = 0; i < msgnums.size(); i++)
|
||||||
|
imessages[i] = state.ifolder.getMessage(msgnums.get(i));
|
||||||
|
|
||||||
|
return imessages;
|
||||||
|
} catch (MessagingException ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
return ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result instanceof Throwable)
|
||||||
|
throw (MessagingException) result;
|
||||||
|
|
||||||
|
state.imessages = (Message[]) result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.i("Boundary found messages=" + state.imessages.length);
|
Log.i("Boundary found messages=" + state.imessages.length);
|
||||||
|
@ -259,25 +328,6 @@ public class ViewModelBrowse extends ViewModel {
|
||||||
Log.i("Boundary done");
|
Log.i("Boundary done");
|
||||||
}
|
}
|
||||||
|
|
||||||
private SearchTerm getSearchTerm(String search, boolean keywords) {
|
|
||||||
SearchTerm term = new OrTerm(
|
|
||||||
new OrTerm(
|
|
||||||
new FromStringTerm(search),
|
|
||||||
new RecipientStringTerm(Message.RecipientType.TO, search)
|
|
||||||
),
|
|
||||||
new OrTerm(
|
|
||||||
new SubjectTerm(search),
|
|
||||||
new BodyTerm(search)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (keywords)
|
|
||||||
term = new OrTerm(term, new FlagTerm(
|
|
||||||
new Flags(Helper.sanitizeKeyword(search)), true));
|
|
||||||
|
|
||||||
return term;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
State state = currentState;
|
State state = currentState;
|
||||||
if (state == null)
|
if (state == null)
|
||||||
|
|
Loading…
Reference in New Issue