Prevent too long IMAP commands

pull/156/head
M66B 5 years ago
parent 1d98fea2df
commit b20c12edd1

@ -1029,9 +1029,12 @@ class Core {
MessagingException ex = (MessagingException) ifolder.doCommand(new IMAPFolder.ProtocolCommand() { MessagingException ex = (MessagingException) ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
@Override @Override
public Object doCommand(IMAPProtocol protocol) { public Object doCommand(IMAPProtocol protocol) {
Log.i("Executing uid fetch count=" + uids.size()); Log.i(folder.name + " executing uid fetch count=" + uids.size());
List<List<Long>> chunked = Helper.chunkList(new ArrayList<>(uids), 25);
for (int c = 0; c < chunked.size(); c++) {
Log.i(folder.name + " chunk #" + c + " size=" + chunked.get(c).size());
Response[] responses = protocol.command( Response[] responses = protocol.command(
"UID FETCH " + TextUtils.join(",", uids) + " (UID)", null); "UID FETCH " + TextUtils.join(",", chunked.get(c)) + " (UID)", null);
if (responses.length > 0 && responses[responses.length - 1].isOK()) { if (responses.length > 0 && responses[responses.length - 1].isOK()) {
for (Response response : responses) for (Response response : responses)
@ -1041,7 +1044,6 @@ class Core {
if (uid != null) if (uid != null)
uids.remove(uid.uid); uids.remove(uid.uid);
} }
return null;
} else { } else {
for (Response response : responses) for (Response response : responses)
if (response.isNO() || response.isBAD() || response.isBYE()) if (response.isNO() || response.isBAD() || response.isBYE())
@ -1049,6 +1051,8 @@ class Core {
return new MessagingException("UID FETCH failed"); return new MessagingException("UID FETCH failed");
} }
} }
return null;
}
}); });
if (ex != null) if (ex != null)
throw ex; throw ex;

@ -576,6 +576,13 @@ public class Helper {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("pro", false); return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("pro", false);
} }
public static <T> List<List<T>> chunkList(List<T> list, int size) {
List<List<T>> result = new ArrayList<>(list.size() / size);
for (int i = 0; i < list.size(); i += size)
result.add(list.subList(i, i + size < list.size() ? i + size : list.size()));
return result;
}
static long[] toLongArray(List<Long> list) { static long[] toLongArray(List<Long> list) {
long[] result = new long[list.size()]; long[] result = new long[list.size()];
for (int i = 0; i < list.size(); i++) for (int i = 0; i < list.size(); i++)

Loading…
Cancel
Save