Files
FairEmail/app/src/main/java/eu/faircode/email/EntityIdentity.java

278 lines
9.2 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2019-09-23 10:47:05 +02:00
import android.text.TextUtils;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.room.Entity;
2018-08-08 10:22:12 +00:00
import androidx.room.ForeignKey;
import androidx.room.Index;
2018-08-08 06:55:47 +00:00
import androidx.room.PrimaryKey;
2018-08-02 13:33:06 +00:00
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Objects;
2019-09-23 10:47:05 +02:00
import java.util.regex.Pattern;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2018-08-08 10:22:12 +00:00
import static androidx.room.ForeignKey.CASCADE;
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityIdentity.TABLE_NAME,
2018-08-08 10:22:12 +00:00
foreignKeys = {
@ForeignKey(childColumns = "account", entity = EntityAccount.class, parentColumns = "id", onDelete = CASCADE)
},
2018-08-02 13:33:06 +00:00
indices = {
2018-11-10 16:47:11 +00:00
@Index(value = {"account"}),
@Index(value = {"account", "email"})
2018-08-02 13:33:06 +00:00
}
)
public class EntityIdentity {
static final String TABLE_NAME = "identity";
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
public String name;
@NonNull
public String email;
@NonNull
2018-08-08 10:22:12 +00:00
public Long account;
2018-12-26 10:54:48 +00:00
public String display;
public Integer color;
public String signature;
@NonNull
2018-08-02 13:33:06 +00:00
public String host; // SMTP
@NonNull
public Boolean starttls;
@NonNull
2019-09-18 16:34:07 +02:00
public Boolean insecure = false;
2018-10-23 07:52:20 +00:00
@NonNull
public Integer port;
@NonNull
2019-09-18 16:34:07 +02:00
public Integer auth_type;
@NonNull
2018-08-02 13:33:06 +00:00
public String user;
@NonNull
public String password;
2019-01-10 18:24:20 +00:00
public String realm;
2018-08-02 13:33:06 +00:00
@NonNull
2019-02-28 16:05:55 +00:00
public Boolean use_ip = true; // instead of domain name
2019-02-21 20:13:11 +01:00
@NonNull
2018-12-26 10:54:48 +00:00
public Boolean synchronize;
2018-08-27 14:31:45 +00:00
@NonNull
2018-08-02 13:33:06 +00:00
public Boolean primary;
2019-04-17 17:27:57 +02:00
@NonNull
2019-04-25 17:35:27 +02:00
public Boolean sender_extra = false;
2019-09-23 10:47:05 +02:00
public String sender_extra_regex;
2018-12-26 10:54:48 +00:00
public String replyto;
public String bcc;
2018-08-02 13:33:06 +00:00
@NonNull
2019-05-04 20:52:21 +02:00
public Boolean plain_only = false; // obsolete
@NonNull
public Boolean encrypt = false; // obsolete
2019-02-18 16:54:33 +00:00
@NonNull
public Boolean delivery_receipt = false; // obsolete
2018-12-26 10:54:48 +00:00
@NonNull
public Boolean read_receipt = false; // obsolete
2018-08-25 14:24:26 +00:00
@NonNull
2019-09-23 17:39:45 +02:00
public Boolean store_sent = false; // obsolete
public Long sent_folder = null; // obsolete
2019-07-18 17:38:51 +02:00
public Long sign_key = null; // OpenPGP
2019-12-05 20:23:31 +01:00
public String sign_key_alias = null; // S/MIME
public Boolean tbd; // obsolete
2018-08-15 11:26:59 +00:00
public String state;
public String error;
2019-01-25 18:13:00 +00:00
public Long last_connected;
2018-08-02 13:33:06 +00:00
2019-02-09 21:03:53 +00:00
String getProtocol() {
return (starttls ? "smtp" : "smtps");
}
2019-09-23 10:47:05 +02:00
boolean sameAddress(Address address) {
String other = ((InternetAddress) address).getAddress();
if (other == null)
return false;
return other.equalsIgnoreCase(email);
}
boolean similarAddress(Address address) {
String other = ((InternetAddress) address).getAddress();
if (other == null)
return false;
if (!other.contains("@") || !email.contains("@"))
return false;
String[] cother = other.split("@");
String[] cemail = email.split("@");
if (cother.length != 2 || cemail.length != 2)
return false;
// Domain
if (!cother[1].equalsIgnoreCase(cemail[1]))
return false;
// User
if (TextUtils.isEmpty(sender_extra_regex)) {
String user = (cother[0].contains("+") ? cother[0].split("\\+")[0] : cother[0]);
if (user.equalsIgnoreCase(cemail[0]))
return true;
} else {
if (Pattern.matches(sender_extra_regex, cother[0]))
return true;
}
return false;
}
2018-09-14 11:33:22 +00:00
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("id", id);
2018-09-14 11:33:22 +00:00
json.put("name", name);
json.put("email", email);
2018-12-26 10:54:48 +00:00
json.put("display", display);
if (color != null)
json.put("color", color);
json.put("signature", signature);
2018-11-10 10:45:03 +00:00
// not account
2018-09-14 11:33:22 +00:00
json.put("host", host);
json.put("starttls", starttls);
2018-10-24 11:14:56 +00:00
json.put("insecure", insecure);
2018-11-10 10:45:03 +00:00
json.put("port", port);
2019-09-18 16:34:07 +02:00
json.put("auth_type", auth_type);
2018-09-14 11:33:22 +00:00
json.put("user", user);
json.put("password", password);
json.put("realm", realm);
2019-02-21 20:13:11 +01:00
json.put("use_ip", use_ip);
json.put("synchronize", synchronize);
2018-12-26 10:54:48 +00:00
json.put("primary", primary);
2019-04-25 17:35:27 +02:00
json.put("sender_extra", sender_extra);
2019-09-23 17:39:45 +02:00
json.put("sender_extra_regex", sender_extra_regex);
2018-12-26 10:54:48 +00:00
json.put("replyto", replyto);
json.put("bcc", bcc);
2019-09-23 17:39:45 +02:00
// not plain_only
2019-02-18 16:54:33 +00:00
json.put("encrypt", encrypt);
2019-09-23 17:39:45 +02:00
// not store_sent
// not sent_folder
// not sign_key
// not tbd
2018-11-10 10:45:03 +00:00
// not state
// not error
2019-09-23 17:39:45 +02:00
// not last_connected
2018-09-14 11:33:22 +00:00
return json;
}
public static EntityIdentity fromJSON(JSONObject json) throws JSONException {
EntityIdentity identity = new EntityIdentity();
2019-06-07 17:42:55 +02:00
identity.id = json.getLong("id");
2018-09-14 11:33:22 +00:00
identity.name = json.getString("name");
2018-12-26 10:54:48 +00:00
identity.email = json.getString("email");
2019-03-14 17:52:15 +00:00
if (json.has("display") && !json.isNull("display"))
identity.display = json.getString("display");
2018-12-26 10:54:48 +00:00
if (json.has("color"))
identity.color = json.getInt("color");
2019-03-14 17:52:15 +00:00
if (json.has("signature") && !json.isNull("signature"))
2018-12-26 10:54:48 +00:00
identity.signature = json.getString("signature");
identity.host = json.getString("host");
identity.starttls = json.getBoolean("starttls");
identity.insecure = (json.has("insecure") && json.getBoolean("insecure"));
identity.port = json.getInt("port");
2019-09-18 16:34:07 +02:00
identity.auth_type = json.getInt("auth_type");
2018-12-26 10:54:48 +00:00
identity.user = json.getString("user");
identity.password = json.getString("password");
2019-03-14 17:52:15 +00:00
if (json.has("realm") && !json.isNull("realm"))
identity.realm = json.getString("realm");
2019-02-21 20:13:11 +01:00
if (json.has("use_ip"))
identity.use_ip = json.getBoolean("use_ip");
2018-12-26 10:54:48 +00:00
identity.synchronize = json.getBoolean("synchronize");
identity.primary = json.getBoolean("primary");
2019-04-25 17:35:27 +02:00
if (json.has("sender_extra"))
identity.sender_extra = json.getBoolean("sender_extra");
2019-09-23 17:39:45 +02:00
if (json.has("sender_extra_regex"))
identity.sender_extra_regex = json.getString("sender_extra_regex");
2018-12-07 20:25:16 +01:00
2019-03-14 17:52:15 +00:00
if (json.has("replyto") && !json.isNull("replyto"))
2018-09-14 11:33:22 +00:00
identity.replyto = json.getString("replyto");
2019-03-14 17:52:15 +00:00
if (json.has("bcc") && !json.isNull("bcc"))
identity.bcc = json.getString("bcc");
2019-02-18 16:54:33 +00:00
if (json.has("encrypt"))
identity.encrypt = json.getBoolean("encrypt");
2019-01-19 18:13:48 +00:00
2018-09-14 11:33:22 +00:00
return identity;
}
2018-08-02 13:33:06 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityIdentity) {
EntityIdentity other = (EntityIdentity) obj;
return (this.name.equals(other.name) &&
this.email.equals(other.email) &&
2018-08-08 10:22:12 +00:00
this.account.equals(other.account) &&
2019-02-26 11:05:21 +01:00
Objects.equals(this.display, other.display) &&
Objects.equals(this.color, other.color) &&
Objects.equals(this.signature, other.signature) &&
2018-08-02 13:33:06 +00:00
this.host.equals(other.host) &&
this.starttls.equals(other.starttls) &&
2018-11-10 10:45:03 +00:00
this.insecure.equals(other.insecure) &&
this.port.equals(other.port) &&
2019-09-18 16:34:07 +02:00
this.auth_type.equals(other.auth_type) &&
2018-08-02 13:33:06 +00:00
this.user.equals(other.user) &&
this.password.equals(other.password) &&
2019-02-26 11:05:21 +01:00
Objects.equals(this.realm, other.realm) &&
2019-02-21 20:13:11 +01:00
this.use_ip == other.use_ip &&
2018-08-15 11:26:59 +00:00
this.synchronize.equals(other.synchronize) &&
2018-12-26 10:54:48 +00:00
this.primary.equals(other.primary) &&
2019-04-25 17:35:27 +02:00
this.sender_extra.equals(sender_extra) &&
2019-09-23 17:39:45 +02:00
Objects.equals(this.sender_extra_regex, other.sender_extra_regex) &&
2019-02-26 11:05:21 +01:00
Objects.equals(this.replyto, other.replyto) &&
Objects.equals(this.bcc, other.bcc) &&
Objects.equals(this.sign_key, other.sign_key) &&
2019-02-26 11:05:21 +01:00
Objects.equals(this.state, other.state) &&
Objects.equals(this.error, other.error) &&
Objects.equals(this.last_connected, other.last_connected));
2018-08-02 13:33:06 +00:00
} else
return false;
}
String getDisplayName() {
return (display == null ? name : display);
}
2019-01-18 18:40:51 +00:00
@NonNull
@Override
public String toString() {
2019-01-21 18:37:09 +00:00
return getDisplayName() + " <" + email + ">" + (primary ? "" : "");
2019-01-18 18:40:51 +00:00
}
2018-08-02 13:33:06 +00:00
}