FairEmail/app/src/main/java/eu/faircode/email/EntityMessage.java

840 lines
33 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
2023-01-01 07:52:55 +00:00
Copyright 2018-2023 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2022-02-11 09:44:59 +00:00
import static androidx.room.ForeignKey.CASCADE;
import static androidx.room.ForeignKey.SET_NULL;
2019-01-07 15:05:24 +00:00
import android.app.AlarmManager;
import android.app.PendingIntent;
2018-08-19 06:53:56 +00:00
import android.content.Context;
2019-01-07 15:05:24 +00:00
import android.content.Intent;
2020-08-07 08:40:24 +00:00
import android.content.SharedPreferences;
2023-08-28 05:58:38 +00:00
import android.text.TextDirectionHeuristics;
2020-08-29 11:10:44 +00:00
import android.text.TextUtils;
2021-06-10 13:48:17 +00:00
import android.util.Pair;
2018-08-19 06:53:56 +00:00
import androidx.annotation.NonNull;
2020-08-07 08:40:24 +00:00
import androidx.preference.PreferenceManager;
2021-02-01 08:06:56 +00:00
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
2020-08-07 08:40:24 +00:00
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
2018-08-19 06:53:56 +00:00
import java.io.File;
2018-08-26 13:24:16 +00:00
import java.io.Serializable;
2020-08-07 08:40:24 +00:00
import java.text.DateFormat;
2022-12-26 21:43:07 +00:00
import java.text.SimpleDateFormat;
2019-05-29 10:13:49 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2018-08-14 05:32:17 +00:00
import java.util.Date;
2019-05-29 10:13:49 +00:00
import java.util.List;
2019-11-12 09:57:26 +00:00
import java.util.Locale;
2019-02-26 10:05:21 +00:00
import java.util.Objects;
2022-09-14 06:24:46 +00:00
import java.util.TimeZone;
2019-09-01 14:02:48 +00:00
import java.util.UUID;
2021-12-23 14:22:27 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.Address;
2019-11-12 09:57:26 +00:00
import javax.mail.internet.InternetAddress;
2018-08-02 13:33:06 +00:00
// https://developer.android.com/training/data-storage/room/defining-data
@Entity(
tableName = EntityMessage.TABLE_NAME,
foreignKeys = {
2018-08-08 06:55:47 +00:00
@ForeignKey(childColumns = "account", entity = EntityAccount.class, parentColumns = "id", onDelete = CASCADE),
@ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
2018-11-12 13:45:02 +00:00
@ForeignKey(childColumns = "identity", entity = EntityIdentity.class, parentColumns = "id", onDelete = SET_NULL),
@ForeignKey(childColumns = "replying", entity = EntityMessage.class, parentColumns = "id", onDelete = SET_NULL),
@ForeignKey(childColumns = "forwarding", entity = EntityMessage.class, parentColumns = "id", onDelete = SET_NULL)
2018-08-02 13:33:06 +00:00
},
indices = {
@Index(value = {"account"}),
@Index(value = {"folder"}),
@Index(value = {"identity"}),
2023-01-28 19:59:42 +00:00
@Index(value = {"replying"}),
@Index(value = {"forwarding"}),
2018-12-06 10:59:57 +00:00
@Index(value = {"folder", "uid"}, unique = true),
2020-05-01 09:14:09 +00:00
@Index(value = {"inreplyto"}),
@Index(value = {"msgid"}),
2018-08-02 13:33:06 +00:00
@Index(value = {"thread"}),
2018-12-27 11:32:20 +00:00
@Index(value = {"sender"}),
2018-08-05 11:44:46 +00:00
@Index(value = {"received"}),
2019-03-05 13:38:05 +00:00
@Index(value = {"subject"}),
2018-08-05 11:44:46 +00:00
@Index(value = {"ui_seen"}),
@Index(value = {"ui_flagged"}),
2018-09-04 14:07:50 +00:00
@Index(value = {"ui_hide"}),
2018-10-16 11:29:12 +00:00
@Index(value = {"ui_found"}),
2018-11-26 15:57:00 +00:00
@Index(value = {"ui_ignored"}),
2019-01-07 15:05:24 +00:00
@Index(value = {"ui_browsed"}),
@Index(value = {"ui_snoozed"})
2018-08-02 13:33:06 +00:00
}
)
2018-08-26 13:24:16 +00:00
public class EntityMessage implements Serializable {
2018-08-02 13:33:06 +00:00
static final String TABLE_NAME = "message";
2021-01-27 14:48:49 +00:00
static final int NOTIFYING_IGNORE = -2;
2019-11-30 08:22:16 +00:00
static final Integer ENCRYPT_NONE = 0;
static final Integer PGP_SIGNENCRYPT = 1;
static final Integer PGP_SIGNONLY = 2;
2019-12-02 07:35:09 +00:00
static final Integer SMIME_SIGNENCRYPT = 3;
static final Integer SMIME_SIGNONLY = 4;
2023-03-11 08:52:20 +00:00
static final Integer PGP_ENCRYPTONLY = 5;
2019-11-27 09:40:43 +00:00
2019-09-30 14:55:58 +00:00
static final Integer PRIORITIY_LOW = 0;
static final Integer PRIORITIY_NORMAL = 1;
static final Integer PRIORITIY_HIGH = 2;
2022-01-18 09:07:13 +00:00
static final Integer SENSITIVITY_PERSONAL = 1;
static final Integer SENSITIVITY_PRIVATE = 2;
static final Integer SENSITIVITY_CONFIDENTIAL = 3;
2021-02-01 08:06:56 +00:00
static final Integer DSN_NONE = 0;
static final Integer DSN_RECEIPT = 1;
2021-02-02 10:16:14 +00:00
static final Integer DSN_HARD_BOUNCE = 2;
2021-02-01 08:06:56 +00:00
2020-07-02 08:10:47 +00:00
static final Long SWIPE_ACTION_ASK = -1L;
static final Long SWIPE_ACTION_SEEN = -2L;
static final Long SWIPE_ACTION_SNOOZE = -3L;
static final Long SWIPE_ACTION_HIDE = -4L;
static final Long SWIPE_ACTION_MOVE = -5L;
static final Long SWIPE_ACTION_FLAG = -6L;
static final Long SWIPE_ACTION_DELETE = -7L;
static final Long SWIPE_ACTION_JUNK = -8L;
2021-11-22 11:10:57 +00:00
static final Long SWIPE_ACTION_REPLY = -9L;
2023-12-02 16:10:39 +00:00
static final Long SWIPE_ACTION_IMPORTANCE = -10L;
2020-07-02 08:10:47 +00:00
private static final int MAX_SNOOZED = 300;
2018-08-02 13:33:06 +00:00
@PrimaryKey(autoGenerate = true)
public Long id;
2018-11-12 13:45:02 +00:00
@NonNull
2018-09-13 07:06:06 +00:00
public Long account; // performance
2018-08-02 13:33:06 +00:00
@NonNull
public Long folder;
public Long identity;
2018-11-09 07:22:44 +00:00
public String extra; // plus
2019-01-21 16:45:05 +00:00
public Long replying; // obsolete
public Long forwarding; // obsolete
2019-01-07 17:50:23 +00:00
public Long uid; // compose/moved = null
2020-05-09 05:56:06 +00:00
public String uidl; // POP3
2018-08-02 13:33:06 +00:00
public String msgid;
public String hash; // headers hash
2018-08-02 13:33:06 +00:00
public String references;
2018-09-18 08:55:59 +00:00
public String deliveredto;
2018-08-02 13:33:06 +00:00
public String inreplyto;
2020-05-06 12:50:30 +00:00
public String wasforwardedfrom;
2018-08-02 13:33:06 +00:00
public String thread; // compose = null
2019-09-30 14:55:58 +00:00
public Integer priority;
2020-02-01 12:51:32 +00:00
public Integer importance;
2022-01-18 09:07:13 +00:00
public Integer sensitivity;
2020-10-02 13:54:45 +00:00
public Boolean auto_submitted;
2021-02-01 08:06:56 +00:00
@ColumnInfo(name = "receipt")
public Integer dsn;
public Boolean receipt_request;
2019-04-18 17:13:38 +00:00
public Address[] receipt_to;
2021-07-16 18:03:27 +00:00
public String bimi_selector;
2023-02-13 20:54:33 +00:00
public String signedby;
2022-01-06 21:46:11 +00:00
public Boolean tls;
public Boolean dkim;
public Boolean spf;
public Boolean dmarc;
2020-12-23 07:51:26 +00:00
public Boolean mx;
2021-06-19 08:00:38 +00:00
public Boolean blocklist;
2021-12-12 13:02:34 +00:00
public Boolean from_domain; // spf/smtp.mailfrom <> from
public Boolean reply_domain; // reply-to <> from
2019-02-04 11:45:38 +00:00
public String avatar; // lookup URI from sender
public String sender; // sort key: from email address
2021-02-01 10:54:43 +00:00
public Address[] return_path;
2021-12-12 13:02:34 +00:00
public Address[] smtp_from;
2021-01-11 18:43:17 +00:00
public Address[] submitter; // sent on behalf of
public Address[] from;
public Address[] to;
public Address[] cc;
public Address[] bcc;
public Address[] reply;
2019-04-23 09:47:56 +00:00
public Address[] list_post;
2019-09-08 10:57:21 +00:00
public String unsubscribe;
2019-12-24 16:18:48 +00:00
public String autocrypt;
2018-09-05 07:23:51 +00:00
public String headers;
2021-11-09 17:44:54 +00:00
public String infrastructure;
public Boolean raw;
2018-08-02 13:33:06 +00:00
public String subject;
2019-04-05 09:08:18 +00:00
public Long size;
2019-09-30 19:00:28 +00:00
public Long total;
2018-09-15 07:22:42 +00:00
@NonNull
public Integer attachments = 0; // performance
@NonNull
public Boolean content = false;
2020-03-26 12:29:43 +00:00
public String language = null; // classified
2022-02-13 10:36:38 +00:00
public Integer plain_only = null; // 1=true; 0x80=alt
2023-11-10 09:03:28 +00:00
public Boolean write_below;
2021-12-18 07:05:04 +00:00
public Boolean resend = null;
2019-11-27 09:40:43 +00:00
public Integer encrypt = null;
2020-01-18 10:28:37 +00:00
public Integer ui_encrypt = null;
2020-06-06 10:14:32 +00:00
@NonNull
public Boolean verified = false;
2018-11-04 15:34:30 +00:00
public String preview;
2021-01-21 15:23:00 +00:00
public String notes;
2021-04-08 07:40:21 +00:00
public Integer notes_color;
@NonNull
public Boolean signature = true;
2018-08-02 13:33:06 +00:00
public Long sent; // compose = null
@NonNull
public Long received; // compose = stored
@NonNull
2018-08-14 05:32:17 +00:00
public Long stored = new Date().getTime();
@NonNull
2022-05-28 09:25:08 +00:00
public Boolean recent = false;
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean seen = false;
2018-08-02 13:33:06 +00:00
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean answered = false;
2018-11-24 18:14:28 +00:00
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean flagged = false;
2021-02-11 08:51:48 +00:00
@NonNull
public Boolean deleted = false;
2019-01-29 20:15:24 +00:00
public String flags; // system flags
2018-11-25 16:52:30 +00:00
public String[] keywords; // user flags
2020-06-25 07:14:05 +00:00
public String[] labels; // Gmail
2018-09-07 15:12:43 +00:00
@NonNull
2020-01-14 21:39:35 +00:00
public Boolean fts = false;
@NonNull
2021-01-04 08:27:27 +00:00
public Boolean auto_classified = false;
@NonNull
2021-08-14 18:02:46 +00:00
public Integer notifying = 0;
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean ui_seen = false;
2018-08-02 13:33:06 +00:00
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean ui_answered = false;
2018-11-24 18:14:28 +00:00
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean ui_flagged = false;
2018-09-07 15:12:43 +00:00
@NonNull
2021-02-11 08:51:48 +00:00
public Boolean ui_deleted = false;
@NonNull
2019-09-27 16:25:55 +00:00
public Boolean ui_hide = false;
2018-09-01 16:34:16 +00:00
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean ui_found = false;
2018-10-16 11:29:12 +00:00
@NonNull
2018-11-26 15:41:33 +00:00
public Boolean ui_ignored = false;
2018-11-26 15:57:00 +00:00
@NonNull
2021-02-15 07:58:05 +00:00
public Boolean ui_silent = false;
@NonNull
public Boolean ui_local_only = false;
@NonNull
2018-11-26 15:57:00 +00:00
public Boolean ui_browsed = false;
2019-10-14 09:29:46 +00:00
public Long ui_busy;
2019-01-07 15:05:24 +00:00
public Long ui_snoozed;
2020-05-19 10:46:35 +00:00
@NonNull
public Boolean ui_unsnoozed = false;
2021-05-30 06:51:14 +00:00
@NonNull
public Boolean show_images = false;
@NonNull
public Boolean show_full = false;
2019-05-15 09:10:47 +00:00
public Integer color;
2019-04-18 07:12:30 +00:00
public Integer revision; // compose
2019-04-18 09:29:15 +00:00
public Integer revisions; // compose
2019-01-17 10:49:18 +00:00
public String warning; // persistent
public String error; // volatile
2018-11-19 13:14:02 +00:00
public Long last_attempt; // send
2018-08-02 13:33:06 +00:00
static String generateMessageId() {
2020-05-19 05:36:41 +00:00
return generateMessageId("localhost");
}
static String generateMessageId(String domain) {
2020-09-28 19:07:35 +00:00
// https://www.jwz.org/doc/mid.html
// https://tools.ietf.org/html/rfc2822.html#section-3.6.4
2020-05-19 05:36:41 +00:00
return "<" + UUID.randomUUID() + "@" + domain + '>';
}
2022-07-21 14:03:07 +00:00
String getLink() {
2023-03-04 15:49:33 +00:00
// adb shell pm set-app-links --package eu.faircode.email 0 all
// adb shell pm verify-app-links --re-verify eu.faircode.email
// adb shell pm get-app-links eu.faircode.email
2023-03-03 07:47:28 +00:00
return "https://link.fairemail.net/#" + id;
2022-07-21 14:03:07 +00:00
}
2022-02-13 10:36:38 +00:00
boolean isPlainOnly() {
return (this.plain_only != null && (this.plain_only & 1) != 0);
}
boolean hasAlt() {
return (this.plain_only != null && (this.plain_only & 0x80) != 0);
}
2022-06-05 08:01:02 +00:00
boolean fromSelf(List<TupleIdentityEx> identities) {
List<Address> senders = new ArrayList<>();
if (from != null)
senders.addAll(Arrays.asList(from));
2023-11-07 10:04:16 +00:00
//if (reply != null)
// senders.addAll(Arrays.asList(reply));
2022-06-05 08:01:02 +00:00
if (identities != null)
for (TupleIdentityEx identity : identities)
for (Address sender : senders)
if (identity.self && identity.similarAddress(sender))
return true;
return false;
}
boolean replySelf(List<TupleIdentityEx> identities, long account) {
2019-09-27 07:17:53 +00:00
Address[] senders = (reply == null || reply.length == 0 ? from : reply);
if (identities != null && senders != null)
for (Address sender : senders)
2019-09-26 10:51:39 +00:00
for (TupleIdentityEx identity : identities)
2020-07-03 05:54:48 +00:00
if (identity.account == account &&
identity.self &&
identity.similarAddress(sender))
return true;
2019-09-22 18:03:31 +00:00
return false;
2019-05-29 14:03:00 +00:00
}
2019-10-02 11:10:39 +00:00
Address[] getAllRecipients(List<TupleIdentityEx> identities, long account) {
2019-05-29 10:13:49 +00:00
List<Address> addresses = new ArrayList<>();
2019-05-29 12:09:54 +00:00
2020-07-22 05:36:17 +00:00
if (!replySelf(identities, account)) {
if (to != null)
addresses.addAll(Arrays.asList(to));
}
2019-05-29 10:13:49 +00:00
if (cc != null)
addresses.addAll(Arrays.asList(cc));
2020-10-30 12:06:38 +00:00
// Filter from
if (from != null)
for (Address address : new ArrayList<>(addresses))
for (Address f : from)
if (MessageHelper.equalEmail(address, f)) {
addresses.remove(address);
break;
}
2019-05-29 12:09:54 +00:00
// Filter self
2019-09-23 11:04:34 +00:00
if (identities != null)
2019-09-22 19:04:21 +00:00
for (Address address : new ArrayList<>(addresses))
2019-09-23 11:04:34 +00:00
for (TupleIdentityEx identity : identities)
2020-06-30 06:31:07 +00:00
if (identity.account == account &&
identity.self &&
2020-06-30 06:31:07 +00:00
identity.similarAddress(address))
2019-09-23 11:04:34 +00:00
addresses.remove(address);
2019-05-29 12:09:54 +00:00
2019-05-29 10:13:49 +00:00
return addresses.toArray(new Address[0]);
}
2021-04-28 06:09:45 +00:00
boolean hasKeyword(@NonNull String value) {
2021-04-28 06:26:01 +00:00
// https://tools.ietf.org/html/rfc5788
if (keywords == null)
return false;
for (String keyword : keywords)
if (value.equalsIgnoreCase(keyword))
return true;
2020-05-06 12:27:28 +00:00
return false;
}
boolean isForwarded() {
return hasKeyword(MessageHelper.FLAG_FORWARDED);
}
boolean isFiltered() {
return hasKeyword(MessageHelper.FLAG_FILTERED);
}
2022-01-20 08:06:19 +00:00
boolean isSigned() {
return (EntityMessage.PGP_SIGNONLY.equals(ui_encrypt) ||
EntityMessage.SMIME_SIGNONLY.equals(ui_encrypt));
}
boolean isEncrypted() {
return (EntityMessage.PGP_SIGNENCRYPT.equals(ui_encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(ui_encrypt));
}
boolean isVerifiable() {
return (EntityMessage.PGP_SIGNONLY.equals(encrypt) ||
EntityMessage.SMIME_SIGNONLY.equals(encrypt));
}
boolean isUnlocked() {
return (EntityMessage.PGP_SIGNENCRYPT.equals(ui_encrypt) &&
!EntityMessage.PGP_SIGNENCRYPT.equals(encrypt)) ||
(EntityMessage.SMIME_SIGNENCRYPT.equals(ui_encrypt) &&
!EntityMessage.SMIME_SIGNENCRYPT.equals(encrypt));
}
2022-09-19 13:35:26 +00:00
boolean isNotJunk(Context context) {
DB db = DB.getInstance(context);
boolean notJunk = false;
2022-09-27 13:14:52 +00:00
if (from != null)
for (Address sender : from) {
String email = ((InternetAddress) sender).getAddress();
if (TextUtils.isEmpty(email))
continue;
EntityContact contact = db.contact().getContact(account, EntityContact.TYPE_NO_JUNK, email);
if (contact != null) {
contact.times_contacted++;
contact.last_contacted = new Date().getTime();
db.contact().updateContact(contact);
notJunk = true;
}
2022-09-19 13:35:26 +00:00
}
return notJunk;
}
boolean isForwarder() {
if (from == null || from.length != 1)
return false;
if (submitter == null || submitter.length != 1)
return false;
String email = ((InternetAddress) from[0]).getAddress();
String domain = UriHelper.getEmailDomain(email);
return "duck.com".equals(domain) ||
"simplelogin.co".equals(domain) ||
"mozmail.com".equals(domain) ||
"anonaddy.me".equals(domain);
}
2021-12-12 13:02:34 +00:00
String[] checkFromDomain(Context context) {
2022-05-02 07:55:27 +00:00
return MessageHelper.equalRootDomain(context, from, smtp_from);
2021-12-12 13:02:34 +00:00
}
2020-12-23 07:25:34 +00:00
2021-12-12 13:02:34 +00:00
String[] checkReplyDomain(Context context) {
2022-05-02 07:55:27 +00:00
return MessageHelper.equalRootDomain(context, reply, from);
2021-06-10 13:48:17 +00:00
}
2021-12-24 13:00:12 +00:00
static String getSubject(Context context, String language, String subject, boolean forward) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean prefix_once = prefs.getBoolean("prefix_once", true);
boolean prefix_count = prefs.getBoolean("prefix_count", false);
boolean alt = prefs.getBoolean(forward ? "alt_fwd" : "alt_re", false);
2021-12-24 10:58:37 +00:00
2021-12-24 13:00:12 +00:00
if (subject == null)
subject = "";
2021-12-24 10:58:37 +00:00
2021-12-24 13:00:12 +00:00
int resid = forward
? (alt ? R.string.title_subject_forward_alt : R.string.title_subject_forward)
: (alt ? R.string.title_subject_reply_alt : R.string.title_subject_reply);
2021-12-24 10:58:37 +00:00
2021-12-24 13:00:12 +00:00
if (!prefix_once)
return Helper.getString(context, language, resid, subject);
2021-12-23 15:37:10 +00:00
2021-06-10 13:48:17 +00:00
List<Pair<String, Boolean>> prefixes = new ArrayList<>();
for (String re : Helper.getStrings(context, language, R.string.title_subject_reply, ""))
2021-12-23 18:12:02 +00:00
prefixes.add(new Pair<>(re, false));
2021-06-10 13:48:17 +00:00
for (String re : Helper.getStrings(context, language, R.string.title_subject_reply_alt, ""))
2021-12-23 18:12:02 +00:00
prefixes.add(new Pair<>(re, false));
2021-06-10 13:48:17 +00:00
for (String fwd : Helper.getStrings(context, language, R.string.title_subject_forward, ""))
2021-12-23 18:12:02 +00:00
prefixes.add(new Pair<>(fwd, true));
2021-06-10 13:48:17 +00:00
for (String fwd : Helper.getStrings(context, language, R.string.title_subject_forward_alt, ""))
2021-12-23 18:12:02 +00:00
prefixes.add(new Pair<>(fwd, true));
2021-06-10 13:48:17 +00:00
2021-12-24 13:00:12 +00:00
int replies = 0;
boolean re = !forward;
2021-06-10 13:48:17 +00:00
List<Boolean> scanned = new ArrayList<>();
2021-12-24 13:00:12 +00:00
String subj = subject.trim();
2021-06-10 13:48:17 +00:00
while (true) {
boolean found = false;
2021-12-23 14:22:27 +00:00
for (Pair<String, Boolean> prefix : prefixes) {
2021-12-24 13:00:12 +00:00
Matcher m = getPattern(prefix.first.trim()).matcher(subj);
2021-12-23 14:22:27 +00:00
if (m.matches()) {
2021-06-10 13:48:17 +00:00
found = true;
2021-12-24 13:00:12 +00:00
subj = m.group(m.groupCount()).trim();
re = (re && !prefix.second);
if (re)
if (prefix.first.trim().endsWith(":"))
try {
String n = m.group(2);
if (n == null)
replies++;
else
replies += Integer.parseInt(n.substring(1, n.length() - 1));
} catch (NumberFormatException ex) {
Log.e(ex);
replies++;
}
else
replies++;
2021-12-24 10:58:37 +00:00
2021-06-10 13:48:17 +00:00
int count = scanned.size();
if (!prefix.second.equals(count == 0 ? forward : scanned.get(count - 1)))
scanned.add(prefix.second);
2021-12-24 10:58:37 +00:00
2021-06-10 13:48:17 +00:00
break;
}
2021-12-23 14:22:27 +00:00
}
2021-06-10 13:48:17 +00:00
if (!found)
break;
}
2021-12-24 13:00:12 +00:00
String pre = Helper.getString(context, language, resid, "");
int semi = pre.lastIndexOf(':');
if (prefix_count && replies > 0 && semi > 0)
pre = pre.substring(0, semi) + "[" + (replies + 1) + "]" + pre.substring(semi);
StringBuilder result = new StringBuilder(pre);
2021-06-10 13:48:17 +00:00
for (int i = 0; i < scanned.size(); i++)
result.append(context.getString(scanned.get(i) ? R.string.title_subject_forward : R.string.title_subject_reply, ""));
2021-12-24 13:00:12 +00:00
result.append(subj);
2021-06-10 13:48:17 +00:00
return result.toString();
2020-12-23 07:25:34 +00:00
}
2021-12-23 18:12:02 +00:00
private static Pattern getPattern(String prefix) {
2021-12-24 13:00:12 +00:00
String pat = prefix.endsWith(":")
2021-12-23 18:12:02 +00:00
? "(^" + Pattern.quote(prefix.substring(0, prefix.length() - 1)) + ")" + "((\\[\\d+\\])|(\\(\\d+\\)))?" + ":"
: "(^" + Pattern.quote(prefix) + ")";
2021-12-24 13:00:12 +00:00
return Pattern.compile(pat + "(\\s*)(.*)", Pattern.CASE_INSENSITIVE);
2021-12-23 18:12:02 +00:00
}
Element getReplyHeader(Context context, Document document, boolean separate, boolean extended) {
2020-08-07 08:40:24 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2022-09-14 06:24:46 +00:00
boolean hide_timezone = prefs.getBoolean("hide_timezone", false);
2020-08-07 08:40:24 +00:00
boolean language_detection = prefs.getBoolean("language_detection", false);
String compose_font = prefs.getString("compose_font", "");
2020-08-07 08:40:24 +00:00
String l = (language_detection ? language : null);
2022-12-26 21:43:07 +00:00
DateFormat DTF = (hide_timezone
? new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US)
: Helper.getDateTimeInstance(context));
2022-09-14 06:24:46 +00:00
DTF.setTimeZone(hide_timezone ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault());
2022-10-01 07:13:05 +00:00
String date = (received instanceof Number ? DTF.format(received) : "-");
2020-08-07 08:40:24 +00:00
Element p = document.createElement("p");
if (extended) {
if (from != null && from.length > 0) {
Element strong = document.createElement("strong");
strong.text(Helper.getString(context, l, R.string.title_from) + " ");
p.appendChild(strong);
p.appendText(MessageHelper.formatAddresses(from));
p.appendElement("br");
}
if (to != null && to.length > 0) {
Element strong = document.createElement("strong");
strong.text(Helper.getString(context, l, R.string.title_to) + " ");
p.appendChild(strong);
p.appendText(MessageHelper.formatAddresses(to));
p.appendElement("br");
}
if (cc != null && cc.length > 0) {
Element strong = document.createElement("strong");
strong.text(Helper.getString(context, l, R.string.title_cc) + " ");
p.appendChild(strong);
p.appendText(MessageHelper.formatAddresses(cc));
p.appendElement("br");
}
2020-08-24 16:40:26 +00:00
if (received != null) { // embedded messages
2020-08-07 08:40:24 +00:00
Element strong = document.createElement("strong");
strong.text(Helper.getString(context, l, R.string.title_date) + " ");
2020-08-07 08:40:24 +00:00
p.appendChild(strong);
2022-09-14 06:24:46 +00:00
p.appendText(date);
2020-08-07 08:40:24 +00:00
p.appendElement("br");
}
2020-08-29 11:10:44 +00:00
if (!TextUtils.isEmpty(subject)) {
2020-08-07 08:40:24 +00:00
Element strong = document.createElement("strong");
strong.text(Helper.getString(context, l, R.string.title_subject) + " ");
p.appendChild(strong);
2020-08-29 11:10:44 +00:00
p.appendText(subject);
2020-08-07 08:40:24 +00:00
p.appendElement("br");
}
} else
2022-09-14 06:24:46 +00:00
p.text(date + " " + MessageHelper.formatAddresses(from) + ":");
2020-08-07 08:40:24 +00:00
2021-07-18 19:31:43 +00:00
Element div = document.createElement("div")
.attr("fairemail", "reply");
2023-08-28 05:58:38 +00:00
try {
String text = p.text();
boolean rtl = TextDirectionHeuristics.FIRSTSTRONG_LTR.isRtl(text, 0, text.length());
div.attr("dir", rtl ? "rtl" : "ltr");
} catch (Throwable ex) {
Log.e(ex);
}
if (!TextUtils.isEmpty(compose_font))
div.attr("style", "font-family: " + StyleHelper.getFamily(compose_font));
2021-07-18 19:31:43 +00:00
if (separate)
div.appendElement("hr");
2021-07-18 19:31:43 +00:00
div.appendChild(p);
return div;
2020-08-07 08:40:24 +00:00
}
2019-11-12 09:57:26 +00:00
String getNotificationChannelId() {
if (from == null || from.length == 0)
return null;
InternetAddress sender = (InternetAddress) from[0];
return "notification." + sender.getAddress().toLowerCase(Locale.ROOT);
}
2020-06-25 17:53:30 +00:00
boolean setLabel(String label, boolean set) {
2020-06-25 16:32:28 +00:00
List<String> list = new ArrayList<>();
if (labels != null)
list.addAll(Arrays.asList(labels));
2020-06-25 17:53:30 +00:00
boolean changed = false;
if (set) {
if (!list.contains(label)) {
changed = true;
list.add(label);
}
} else {
if (list.contains(label)) {
changed = true;
list.remove(label);
}
}
if (changed)
labels = list.toArray(new String[0]);
return changed;
2020-06-25 16:32:28 +00:00
}
2019-09-02 08:48:11 +00:00
static File getFile(Context context, Long id) {
File root = Helper.ensureExists(new File(getRoot(context), "messages"));
2022-10-09 05:24:58 +00:00
File dir = Helper.ensureExists(new File(root, "D" + (id / 1000)));
2018-08-21 14:25:42 +00:00
return new File(dir, id.toString());
}
static File getRoot(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean external_storage = prefs.getBoolean("external_storage_message", false);
File root = (external_storage
? Helper.getExternalFilesDir(context)
: context.getFilesDir());
return root;
}
2022-10-09 05:24:58 +00:00
static void convert(Context context) {
File root = new File(context.getFilesDir(), "messages");
2022-10-09 05:24:58 +00:00
List<File> files = Helper.listFiles(root);
for (File file : files)
if (file.isFile())
try {
long id = Long.parseLong(file.getName());
File target = getFile(context, id);
2022-10-09 05:24:58 +00:00
if (!file.renameTo(target))
2023-12-06 16:06:46 +00:00
Log.e("Move failed: " + file);
} catch (Throwable ex) {
2022-10-09 05:24:58 +00:00
Log.e(ex);
}
}
2019-09-02 08:48:11 +00:00
File getFile(Context context) {
return getFile(context, id);
}
2019-04-18 07:12:30 +00:00
File getFile(Context context, int revision) {
2022-10-06 12:43:44 +00:00
File dir = Helper.ensureExists(new File(context.getFilesDir(), "revision"));
2019-04-18 07:12:30 +00:00
return new File(dir, id + "." + revision);
}
2019-11-19 20:53:12 +00:00
File getRefFile(Context context) {
2022-10-06 12:43:44 +00:00
File dir = Helper.ensureExists(new File(context.getFilesDir(), "references"));
2019-01-21 16:45:05 +00:00
return new File(dir, id.toString());
2018-08-19 06:53:56 +00:00
}
2019-03-14 07:45:13 +00:00
File getRawFile(Context context) {
2021-03-20 16:10:15 +00:00
return getRawFile(context, id);
}
static File getRawFile(Context context, Long id) {
2022-10-06 12:43:44 +00:00
File dir = Helper.ensureExists(new File(context.getFilesDir(), "raw"));
2019-12-13 08:09:45 +00:00
return new File(dir, id + ".eml");
}
2019-01-07 15:05:24 +00:00
static void snooze(Context context, long id, Long wakeup) {
2022-07-01 19:54:27 +00:00
if (wakeup != null && wakeup != Long.MAX_VALUE) {
/*
java.lang.IllegalStateException: Maximum limit of concurrent alarms 500 reached for uid: u0a601, callingPackage: eu.faircode.email
at android.os.Parcel.createExceptionOrNull(Parcel.java:2433)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.os.Parcel.readException(Parcel.java:2334)
at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:359)
at android.app.AlarmManager.setImpl(AlarmManager.java:947)
at android.app.AlarmManager.setImpl(AlarmManager.java:907)
at android.app.AlarmManager.setExactAndAllowWhileIdle(AlarmManager.java:1175)
at androidx.core.app.AlarmManagerCompat$Api23Impl.setExactAndAllowWhileIdle(Unknown Source:0)
at androidx.core.app.AlarmManagerCompat.setExactAndAllowWhileIdle(SourceFile:2)
at eu.faircode.email.AlarmManagerCompatEx.setAndAllowWhileIdle(SourceFile:2)
at eu.faircode.email.EntityMessage.snooze(SourceFile:7)
*/
DB db = DB.getInstance(context);
int count = db.message().getSnoozedCount();
Log.i("Snoozed=" + count + "/" + MAX_SNOOZED);
if (count > MAX_SNOOZED)
throw new IllegalArgumentException(
String.format("Due to Android limitations, no more than %d messages can be snoozed or delayed", MAX_SNOOZED));
}
2021-03-30 07:12:36 +00:00
Intent snoozed = new Intent(context, ServiceSynchronize.class);
snoozed.setAction("unsnooze:" + id);
PendingIntent pi = PendingIntentCompat.getForegroundService(
context, ServiceSynchronize.PI_UNSNOOZE, snoozed, PendingIntent.FLAG_UPDATE_CURRENT);
2019-01-07 15:05:24 +00:00
2022-04-13 20:27:33 +00:00
AlarmManager am = Helper.getSystemService(context, AlarmManager.class);
2019-10-12 09:09:54 +00:00
if (wakeup == null || wakeup == Long.MAX_VALUE) {
2019-01-07 15:05:24 +00:00
Log.i("Cancel snooze id=" + id);
am.cancel(pi);
} else {
Log.i("Set snooze id=" + id + " wakeup=" + new Date(wakeup));
2021-04-27 05:53:09 +00:00
AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, wakeup, pi);
2019-01-07 15:05:24 +00:00
}
}
2022-03-28 06:32:50 +00:00
static String getSwipeType(Long type) {
if (type == null)
return "none";
if (type > 0)
return "folder";
if (SWIPE_ACTION_ASK.equals(type))
return "ask";
if (SWIPE_ACTION_SEEN.equals(type))
return "seen";
if (SWIPE_ACTION_SNOOZE.equals(type))
return "snooze";
if (SWIPE_ACTION_HIDE.equals(type))
return "hide";
if (SWIPE_ACTION_MOVE.equals(type))
return "move";
if (SWIPE_ACTION_FLAG.equals(type))
return "flag";
2023-12-02 16:10:39 +00:00
if (SWIPE_ACTION_IMPORTANCE.equals(type))
return "importance";
2022-03-28 06:32:50 +00:00
if (SWIPE_ACTION_DELETE.equals(type))
return "delete";
if (SWIPE_ACTION_JUNK.equals(type))
return "junk";
if (SWIPE_ACTION_REPLY.equals(type))
return "reply";
return "???";
}
2018-08-02 13:33:06 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityMessage) {
EntityMessage other = (EntityMessage) obj;
2019-02-26 10:05:21 +00:00
return (Objects.equals(this.account, other.account) &&
2018-08-02 13:33:06 +00:00
this.folder.equals(other.folder) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.identity, other.identity) &&
2020-01-18 10:28:37 +00:00
// extra
2019-02-26 10:05:21 +00:00
Objects.equals(this.uid, other.uid) &&
Objects.equals(this.msgid, other.msgid) &&
Objects.equals(this.references, other.references) &&
Objects.equals(this.deliveredto, other.deliveredto) &&
Objects.equals(this.inreplyto, other.inreplyto) &&
2020-05-07 16:27:50 +00:00
Objects.equals(this.wasforwardedfrom, other.wasforwardedfrom) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.thread, other.thread) &&
2019-10-01 11:56:48 +00:00
Objects.equals(this.priority, other.priority) &&
2022-01-18 09:07:13 +00:00
Objects.equals(this.importance, other.importance) &&
Objects.equals(this.sensitivity, other.sensitivity) &&
2021-02-01 08:06:56 +00:00
Objects.equals(this.dsn, other.dsn) &&
2019-04-25 15:35:27 +00:00
Objects.equals(this.receipt_request, other.receipt_request) &&
MessageHelper.equal(this.receipt_to, other.receipt_to) &&
2021-07-16 18:03:27 +00:00
Objects.equals(this.bimi_selector, other.bimi_selector) &&
2022-01-06 21:46:11 +00:00
Objects.equals(this.tls, other.tls) &&
2019-04-25 15:35:27 +00:00
Objects.equals(this.dkim, other.dkim) &&
Objects.equals(this.spf, other.spf) &&
Objects.equals(this.dmarc, other.dmarc) &&
Objects.equals(this.mx, other.mx) &&
2021-06-19 08:00:38 +00:00
Objects.equals(this.blocklist, other.blocklist) &&
2021-12-12 13:02:34 +00:00
Objects.equals(this.from_domain, other.from_domain) &&
2020-12-23 07:51:26 +00:00
Objects.equals(this.reply_domain, other.reply_domain) &&
2019-06-25 07:46:31 +00:00
Objects.equals(this.avatar, other.avatar) &&
Objects.equals(this.sender, other.sender) &&
2021-02-03 16:37:37 +00:00
MessageHelper.equal(this.return_path, other.return_path) &&
2021-12-12 13:02:34 +00:00
MessageHelper.equal(this.smtp_from, other.smtp_from) &&
2021-01-11 18:43:17 +00:00
MessageHelper.equal(this.submitter, other.submitter) &&
2018-12-25 08:22:07 +00:00
MessageHelper.equal(this.from, other.from) &&
MessageHelper.equal(this.to, other.to) &&
MessageHelper.equal(this.cc, other.cc) &&
MessageHelper.equal(this.bcc, other.bcc) &&
MessageHelper.equal(this.reply, other.reply) &&
2019-04-25 15:35:27 +00:00
MessageHelper.equal(this.list_post, other.list_post) &&
2020-01-18 10:28:37 +00:00
Objects.equals(this.unsubscribe, other.unsubscribe) &&
Objects.equals(this.autocrypt, other.autocrypt) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.headers, other.headers) &&
2021-11-09 17:44:54 +00:00
Objects.equals(this.infrastructure, other.infrastructure) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.raw, other.raw) &&
Objects.equals(this.subject, other.subject) &&
Objects.equals(this.size, other.size) &&
2019-09-30 19:00:28 +00:00
Objects.equals(this.total, other.total) &&
Objects.equals(this.attachments, other.attachments) &&
2018-10-15 10:05:42 +00:00
this.content == other.content &&
2020-03-26 12:29:43 +00:00
Objects.equals(this.language, other.language) &&
2019-05-04 18:52:21 +00:00
Objects.equals(this.plain_only, other.plain_only) &&
2020-01-18 10:28:37 +00:00
Objects.equals(this.encrypt, other.encrypt) &&
Objects.equals(this.ui_encrypt, other.ui_encrypt) &&
2020-06-06 10:14:32 +00:00
this.verified == other.verified &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.preview, other.preview) &&
2021-01-21 15:23:00 +00:00
Objects.equals(this.notes, other.notes) &&
2021-04-08 07:40:21 +00:00
Objects.equals(this.notes_color, other.notes_color) &&
2020-01-18 10:28:37 +00:00
this.signature.equals(other.signature) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.sent, other.sent) &&
2018-08-02 13:33:06 +00:00
this.received.equals(other.received) &&
2018-09-07 15:12:43 +00:00
this.stored.equals(other.stored) &&
2022-05-28 09:25:08 +00:00
this.recent.equals(other.recent) &&
2018-08-02 13:33:06 +00:00
this.seen.equals(other.seen) &&
2018-11-24 18:14:28 +00:00
this.answered.equals(other.answered) &&
2018-09-07 15:12:43 +00:00
this.flagged.equals(other.flagged) &&
2021-02-11 08:51:48 +00:00
this.deleted.equals(other.deleted) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.flags, other.flags) &&
2018-11-26 10:30:04 +00:00
Helper.equal(this.keywords, other.keywords) &&
2021-08-14 18:02:46 +00:00
this.auto_classified.equals(other.auto_classified) &&
2019-07-06 08:16:42 +00:00
this.notifying.equals(other.notifying) &&
2018-10-15 10:05:42 +00:00
this.ui_seen.equals(other.ui_seen) &&
2018-11-24 18:14:28 +00:00
this.ui_answered.equals(other.ui_answered) &&
2018-09-07 15:12:43 +00:00
this.ui_flagged.equals(other.ui_flagged) &&
2021-02-11 08:51:48 +00:00
this.ui_deleted.equals(other.ui_deleted) &&
this.ui_hide.equals(other.ui_hide) &&
2018-09-07 15:12:43 +00:00
this.ui_found.equals(other.ui_found) &&
2018-10-16 11:29:12 +00:00
this.ui_ignored.equals(other.ui_ignored) &&
2021-02-15 10:56:37 +00:00
this.ui_silent.equals(other.ui_silent) &&
this.ui_local_only.equals(other.ui_local_only) &&
2019-01-07 15:05:24 +00:00
this.ui_browsed.equals(other.ui_browsed) &&
2020-05-19 10:46:35 +00:00
Objects.equals(this.ui_busy, other.ui_busy) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.ui_snoozed, other.ui_snoozed) &&
2020-05-19 10:46:35 +00:00
this.ui_unsnoozed.equals(other.ui_unsnoozed) &&
2021-05-30 06:51:14 +00:00
this.show_images.equals(other.show_images) &&
this.show_full.equals(other.show_full) &&
2019-05-15 09:10:47 +00:00
Objects.equals(this.color, other.color) &&
2019-04-25 15:35:27 +00:00
Objects.equals(this.revision, other.revision) &&
Objects.equals(this.revisions, other.revisions) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.warning, other.warning) &&
2019-06-25 07:46:31 +00:00
Objects.equals(this.error, other.error) &&
Objects.equals(this.last_attempt, other.last_attempt));
2018-08-02 13:33:06 +00:00
}
return false;
}
}