POP support

pull/147/head
M66B 6 years ago
parent 1768028fd4
commit f246284812

File diff suppressed because it is too large Load Diff

@ -49,7 +49,7 @@ import io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 41,
version = 42,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -482,6 +482,13 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `message` ADD COLUMN `flags` TEXT");
}
})
.addMigrations(new Migration(41, 42) {
@Override
public void migrate(SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `pop` INTEGER NOT NULL DEFAULT 0");
}
})
.build();
}

@ -49,7 +49,9 @@ public class EntityAccount implements Serializable {
@NonNull
public Integer auth_type;
@NonNull
public String host; // IMAP
public Boolean pop = false;
@NonNull
public String host; // POP3/IMAP
@NonNull
public Boolean starttls;
@NonNull
@ -86,6 +88,10 @@ public class EntityAccount implements Serializable {
public String error;
public Long last_connected;
String getProtocol() {
return (pop ? "pop3" : "imap") + (starttls ? "" : "s");
}
static String getNotificationChannelName(long account) {
return "notification." + account;
}
@ -110,6 +116,7 @@ public class EntityAccount implements Serializable {
JSONObject json = new JSONObject();
json.put("id", id);
json.put("auth_type", auth_type);
json.put("pop", pop);
json.put("host", host);
json.put("starttls", starttls);
json.put("insecure", insecure);
@ -142,6 +149,8 @@ public class EntityAccount implements Serializable {
EntityAccount account = new EntityAccount();
// id
account.auth_type = json.getInt("auth_type");
if (json.has("pop"))
account.pop = json.getBoolean("pop");
account.host = json.getString("host");
account.starttls = (json.has("starttls") && json.getBoolean("starttls"));
account.insecure = (json.has("insecure") && json.getBoolean("insecure"));

@ -87,6 +87,10 @@ public class EntityIdentity {
public String error;
public Long last_connected;
String getProtocol() {
return (starttls ? "smtp" : "smtps");
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("id", id);

@ -74,9 +74,11 @@ import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Store;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction;
@ -91,6 +93,7 @@ public class FragmentAccount extends FragmentBase {
private Button btnAutoConfig;
private Button btnAuthorize;
private SwitchCompat swPop;
private EditText etHost;
private CheckBox cbStartTls;
private CheckBox cbInsecure;
@ -165,6 +168,7 @@ public class FragmentAccount extends FragmentBase {
btnAutoConfig = view.findViewById(R.id.btnAutoConfig);
btnAuthorize = view.findViewById(R.id.btnAuthorize);
swPop = view.findViewById(R.id.swPop);
etHost = view.findViewById(R.id.etHost);
etPort = view.findViewById(R.id.etPort);
cbStartTls = view.findViewById(R.id.cbStartTls);
@ -235,6 +239,7 @@ public class FragmentAccount extends FragmentBase {
auth_type = Helper.AUTH_TYPE_PASSWORD;
swPop.setChecked(false);
etHost.setText(provider.imap_host);
etPort.setText(provider.imap_host == null ? null : Integer.toString(provider.imap_port));
cbStartTls.setChecked(provider.imap_starttls);
@ -280,10 +285,33 @@ public class FragmentAccount extends FragmentBase {
}
});
swPop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
boolean starttls = cbStartTls.isChecked();
if (isChecked) {
etHost.setHint("pop.domain.tld");
etPort.setHint(starttls ? "110" : "995");
etRealm.setText(null);
cbBrowse.setChecked(false);
etPrefix.setText(null);
} else {
etHost.setHint("imap.domain.tld");
etPort.setHint(starttls ? "143" : "993");
}
etRealm.setEnabled(!isChecked);
cbBrowse.setEnabled(!isChecked);
etPrefix.setEnabled(!isChecked);
}
});
cbStartTls.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
etPort.setHint(checked ? "143" : "993");
if (swPop.isChecked())
etPort.setHint(checked ? "110" : "995");
else
etPort.setHint(checked ? "143" : "993");
}
});
@ -472,6 +500,7 @@ public class FragmentAccount extends FragmentBase {
@Override
protected void onExecuted(Bundle args, EmailProvider provider) {
swPop.setChecked(false);
etHost.setText(provider.imap_host);
etPort.setText(Integer.toString(provider.imap_port));
cbStartTls.setChecked(provider.imap_starttls);
@ -491,6 +520,7 @@ public class FragmentAccount extends FragmentBase {
Bundle args = new Bundle();
args.putLong("id", id);
args.putInt("auth_type", auth_type);
args.putBoolean("pop", swPop.isChecked());
args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked());
args.putBoolean("insecure", cbInsecure.isChecked());
@ -523,6 +553,7 @@ public class FragmentAccount extends FragmentBase {
protected CheckResult onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
int auth_type = args.getInt("auth_type");
boolean pop = args.getBoolean("pop");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
boolean insecure = args.getBoolean("insecure");
@ -534,7 +565,10 @@ public class FragmentAccount extends FragmentBase {
if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(context.getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
port = (starttls ? "143" : "993");
if (pop)
port = (starttls ? "110" : "995");
else
port = (starttls ? "143" : "993");
if (TextUtils.isEmpty(user))
throw new IllegalArgumentException(context.getString(R.string.title_no_user));
if (TextUtils.isEmpty(password) && !insecure)
@ -553,9 +587,9 @@ public class FragmentAccount extends FragmentBase {
Properties props = MessageHelper.getSessionProperties(auth_type, realm, insecure);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
IMAPStore istore = null;
Store istore = null;
try {
istore = (IMAPStore) isession.getStore(starttls ? "imap" : "imaps");
istore = isession.getStore((pop ? "pop3" : "imap") + (starttls ? "" : "s"));
try {
istore.connect(host, Integer.parseInt(port), user, password);
} catch (AuthenticationFailedException ex) {
@ -566,7 +600,8 @@ public class FragmentAccount extends FragmentBase {
throw ex;
}
result.idle = istore.hasCapability("IDLE");
if (istore instanceof IMAPStore)
result.idle = ((IMAPStore) istore).hasCapability("IDLE");
boolean inbox = false;
boolean archive = false;
@ -583,7 +618,11 @@ public class FragmentAccount extends FragmentBase {
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
// Check folder attributes
String fullName = ifolder.getFullName();
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
String[] attrs;
if (ifolder instanceof IMAPFolder)
attrs = ((IMAPFolder) ifolder).getAttributes();
else
attrs = new String[0];
Log.i(fullName + " attrs=" + TextUtils.join(" ", attrs));
String type = EntityFolder.getType(attrs, fullName);
@ -660,9 +699,15 @@ public class FragmentAccount extends FragmentBase {
@Override
protected void onExecuted(Bundle args, CheckResult result) {
tvIdle.setVisibility(result.idle ? View.GONE : View.VISIBLE);
boolean pop = args.getBoolean("pop");
setFolders(result.folders, result.account);
tvIdle.setVisibility(result.idle || pop ? View.GONE : View.VISIBLE);
if (pop) {
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.VISIBLE);
} else
setFolders(result.folders, result.account);
new Handler().post(new Runnable() {
@Override
@ -721,6 +766,7 @@ public class FragmentAccount extends FragmentBase {
args.putLong("id", id);
args.putInt("auth_type", auth_type);
args.putBoolean("pop", swPop.isChecked());
args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked());
args.putBoolean("insecure", cbInsecure.isChecked());
@ -770,6 +816,7 @@ public class FragmentAccount extends FragmentBase {
long id = args.getLong("id");
int auth_type = args.getInt("auth_type");
boolean pop = args.getBoolean("pop");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
boolean insecure = args.getBoolean("insecure");
@ -799,7 +846,10 @@ public class FragmentAccount extends FragmentBase {
if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(context.getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
port = (starttls ? "143" : "993");
if (pop)
port = (starttls ? "110" : "995");
else
port = (starttls ? "143" : "993");
if (TextUtils.isEmpty(user))
throw new IllegalArgumentException(context.getString(R.string.title_no_user));
if (synchronize && TextUtils.isEmpty(password) && !insecure)
@ -825,6 +875,7 @@ public class FragmentAccount extends FragmentBase {
boolean check = (synchronize && (account == null ||
auth_type != account.auth_type ||
pop != account.pop ||
!host.equals(account.host) || Integer.parseInt(port) != account.port ||
!user.equals(account.user) || !password.equals(account.password) ||
(realm == null ? accountRealm != null : !realm.equals(accountRealm))));
@ -844,9 +895,9 @@ public class FragmentAccount extends FragmentBase {
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
IMAPStore istore = null;
Store istore = null;
try {
istore = (IMAPStore) isession.getStore(starttls ? "imap" : "imaps");
istore = isession.getStore((pop ? "pop3" : "imap") + (starttls ? "" : "s"));
try {
istore.connect(host, Integer.parseInt(port), user, password);
} catch (AuthenticationFailedException ex) {
@ -861,7 +912,11 @@ public class FragmentAccount extends FragmentBase {
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
// Check folder attributes
String fullName = ifolder.getFullName();
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
String[] attrs;
if (ifolder instanceof IMAPFolder)
attrs = ((IMAPFolder) ifolder).getAttributes();
else
attrs = new String[0];
Log.i(fullName + " attrs=" + TextUtils.join(" ", attrs));
String type = EntityFolder.getType(attrs, fullName);
@ -894,6 +949,7 @@ public class FragmentAccount extends FragmentBase {
account = new EntityAccount();
account.auth_type = auth_type;
account.pop = pop;
account.host = host;
account.starttls = starttls;
account.insecure = insecure;
@ -1111,6 +1167,7 @@ public class FragmentAccount extends FragmentBase {
spProvider.setTag(1);
spProvider.setSelection(1);
}
swPop.setChecked(account.pop);
etHost.setText(account.host);
etPort.setText(Long.toString(account.port));
}
@ -1173,6 +1230,15 @@ public class FragmentAccount extends FragmentBase {
// Consider previous check/save/delete as cancelled
pbWait.setVisibility(View.GONE);
if (account != null && account.pop) {
etRealm.setEnabled(false);
cbBrowse.setEnabled(false);
etPrefix.setEnabled(false);
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.VISIBLE);
return;
}
args.putLong("account", account == null ? -1 : account.id);
new SimpleTask<List<EntityFolder>>() {

@ -581,11 +581,11 @@ public class FragmentIdentity extends FragmentBase {
// Check SMTP server
if (check) {
String transportType = (starttls ? "smtp" : "smtps");
String protocol = (starttls ? "smtp" : "smtps");
Properties props = MessageHelper.getSessionProperties(auth_type, realm, insecure);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
Transport itransport = isession.getTransport(transportType);
Transport itransport = isession.getTransport(protocol);
try {
try {
itransport.connect(host, Integer.parseInt(port), user, password);

@ -165,6 +165,26 @@ public class MessageHelper {
props.put("mail.smtp.writetimeout", Integer.toString(NETWORK_TIMEOUT)); // one thread overhead
props.put("mail.smtp.timeout", Integer.toString(NETWORK_TIMEOUT));
// https://javaee.github.io/javamail/docs/api/com/sun/mail/pop3/package-summary.html
props.put("mail.pop3s.ssl.checkserveridentity", checkserveridentity);
props.put("mail.pop3s.ssl.trust", "*");
props.put("mail.pop3s.starttls.enable", "false");
props.put("mail.pop3s.starttls.required", "false");
props.put("mail.pop3s.connectiontimeout", Integer.toString(NETWORK_TIMEOUT));
props.put("mail.pop3s.timeout", Integer.toString(NETWORK_TIMEOUT));
props.put("mail.pop3s.writetimeout", Integer.toString(NETWORK_TIMEOUT)); // one thread overhead
props.put("mail.pop3.ssl.checkserveridentity", checkserveridentity);
props.put("mail.pop3.ssl.trust", "*");
props.put("mail.pop3.starttls.enable", "true");
props.put("mail.pop3.starttls.required", "true");
props.put("mail.pop3.connectiontimeout", Integer.toString(NETWORK_TIMEOUT));
props.put("mail.pop3.timeout", Integer.toString(NETWORK_TIMEOUT));
props.put("mail.pop3.writetimeout", Integer.toString(NETWORK_TIMEOUT)); // one thread overhead
// MIME
props.put("mail.mime.allowutf8", "true"); // SMTPTransport, MimeMessage
props.put("mail.mime.address.strict", "false");

@ -817,7 +817,7 @@ public class ServiceSynchronize extends LifecycleService {
isession.setDebug(debug);
// adb -t 1 logcat | grep "fairemail\|System.out"
final IMAPStore istore = (IMAPStore) isession.getStore(account.starttls ? "imap" : "imaps");
final IMAPStore istore = (IMAPStore) isession.getStore(account.getProtocol());
final Map<EntityFolder, IMAPFolder> folders = new HashMap<>();
List<Thread> idlers = new ArrayList<>();
List<Handler> handlers = new ArrayList<>();
@ -1889,8 +1889,6 @@ public class ServiceSynchronize extends LifecycleService {
db.message().setMessageLastAttempt(message.id, message.last_attempt);
}
String transportType = (ident.starttls ? "smtp" : "smtps");
// Get properties
Properties props = MessageHelper.getSessionProperties(ident.auth_type, ident.realm, ident.insecure);
props.put("mail.smtp.localhost", ident.host);
@ -1925,7 +1923,7 @@ public class ServiceSynchronize extends LifecycleService {
// Create transport
// TODO: cache transport?
Transport itransport = isession.getTransport(transportType);
Transport itransport = isession.getTransport(ident.getProtocol());
try {
// Connect transport
db.identity().setIdentityState(ident.id, "connecting");

@ -158,7 +158,7 @@ public class ViewModelBrowse extends ViewModel {
isession.setDebug(true);
Log.i("Boundary connecting account=" + account.name);
state.istore = (IMAPStore) isession.getStore(account.starttls ? "imap" : "imaps");
state.istore = (IMAPStore) isession.getStore(account.getProtocol());
Helper.connect(state.context, state.istore, account);
Log.i("Boundary opening folder=" + folder.name);

@ -75,27 +75,37 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etDomain" />
<!-- IMAP -->
<!-- IMAP/POP3 -->
<TextView
android:id="@+id/tvImap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_imap"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@+id/swPop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/swPop" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swPop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="12dp"
app:layout_constraintStart_toEndOf="@id/tvImap"
app:layout_constraintTop_toBottomOf="@id/btnAutoConfig" />
<TextView
android:id="@+id/tvPop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_pop"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvImap" />
android:layout_marginStart="6dp"
android:text="@string/title_pop3"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/swPop"
app:layout_constraintStart_toEndOf="@id/swPop"
app:layout_constraintTop_toTopOf="@id/swPop" />
<!-- host -->
@ -107,7 +117,7 @@
android:text="@string/title_host"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvPop" />
app:layout_constraintTop_toBottomOf="@id/swPop" />
<EditText
android:id="@+id/etHost"
@ -671,24 +681,36 @@
android:id="@+id/grpServer"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvDomain,tvDomainHint,etDomain,btnAutoConfig,tvImap,tvPop,tvHost,etHost,cbStartTls,cbInsecure,tvPort,etPort" />
app:constraint_referenced_ids="
tvDomain,tvDomainHint,etDomain,btnAutoConfig,
tvImap,swPop,tvPop,tvHost,etHost,cbStartTls,cbInsecure,tvPort,etPort" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpAuthorize"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvUser,etUser,tvPassword,tilPassword,tvRealm,etRealm,tvName,tvNameRemark,etName,btnColor,vwColor,ibColorDefault" />
app:constraint_referenced_ids="
tvUser,etUser,tvPassword,tilPassword,tvRealm,etRealm,
tvName,tvNameRemark,etName,btnColor,vwColor,ibColorDefault" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpAdvanced"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvPrefix,tvPrefixRemark,etPrefix,cbNotify,cbSynchronize,cbPrimary,cbBrowse,tvBrowseHint,tvInterval,etInterval" />
app:constraint_referenced_ids="
tvPrefix,tvPrefixRemark,etPrefix,cbNotify,
cbSynchronize,cbPrimary,cbBrowse,tvBrowseHint,tvInterval,etInterval" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpFolders"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvDrafts,spDrafts,tvDraftsRemark,tvSent,spSent,tvAll,spAll,tvTrash,spTrash,tvJunk,spJunk,vSeparatorSwipe,tvLeft,spLeft,tvRight,spRight" />
app:constraint_referenced_ids="
tvDrafts,spDrafts,tvDraftsRemark,
tvSent,spSent,
tvAll,spAll,
tvTrash,spTrash,
tvJunk,spJunk,
vSeparatorSwipe,tvLeft,spLeft,tvRight,spRight" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

@ -210,6 +210,7 @@
<string name="title_domain">Domain name</string>
<string name="title_autoconfig">Get settings</string>
<string name="title_imap" translatable="false">IMAP</string>
<string name="title_pop3" translatable="false">POP3</string>
<string name="title_smtp" translatable="false">SMTP</string>
<string name="title_provider">Provider</string>
<string name="title_custom">Custom</string>

Loading…
Cancel
Save