fixes and optimizations

This commit is contained in:
zefie
2025-08-11 17:42:57 -04:00
parent b95f58b381
commit e4aac5933e
9 changed files with 245 additions and 246 deletions

View File

@@ -22,8 +22,8 @@ class WTVMail {
constructor(minisrv_config, wtvclient) {
if (!minisrv_config) throw ("minisrv_config required");
var WTVShared = require("./WTVShared.js")['WTVShared'];
var WTVMime = require("./WTVMime.js");
const WTVShared = require("./WTVShared.js")['WTVShared'];
const WTVMime = require("./WTVMime.js");
this.WTVClientSessionData = require("./WTVClientSessionData.js");
this.minisrv_config = minisrv_config;
this.wtvshared = new WTVShared(minisrv_config);
@@ -39,7 +39,7 @@ class WTVMail {
this.trashMailboxName
];
this.defaultColors = {
bgcolor: "#191919",
bgcolor: "#1F2033",
text: "#82A9D9",
link: "#BDA73A",
vlink: "#62B362"
@@ -59,9 +59,9 @@ class WTVMail {
if (!this.isguest) {
if (this.mailstore_dir === null) {
// set mailstore directory local var so we don't call the function every time
var userstore_dir = this.wtvclient.getUserStoreDirectory();
const userstore_dir = this.wtvclient.getUserStoreDirectory();
// MailStore
var store_dir = "MailStore" + this.path.sep;
const store_dir = "MailStore" + this.path.sep;
this.mailstore_dir = userstore_dir + store_dir;
}
return this.fs.existsSync(this.mailstore_dir);
@@ -70,7 +70,7 @@ class WTVMail {
}
getSignatureColors(signature = null, sendmail = true) {
var colors = Object.assign({}, this.defaultColors); // start with default colors
const colors = Object.assign({}, this.defaultColors); // start with default colors
if (sendmail) colors.bgcolor = this.sendmailDefaultBGColor;
if (signature) {
@@ -95,14 +95,13 @@ class WTVMail {
}
mailboxExists(mailboxid) {
let store_dir;
if (mailboxid >= this.mailboxes.length) return null;
var mailbox_dir = null;
var store_dir = null;
if (this.mailstoreExists()) {
var mailbox_name = this.getMailboxById(mailboxid);
const mailbox_name = this.getMailboxById(mailboxid);
if (!mailbox_name) return null;
mailbox_dir = mailbox_name + this.path.sep;
const mailbox_dir = mailbox_name + this.path.sep;
store_dir = this.mailstore_dir + mailbox_dir;
}
return (store_dir !== null) ? this.fs.existsSync(store_dir) : false;
@@ -121,7 +120,7 @@ class WTVMail {
}
getMailboxByName(mailbox_name) {
var mailbox_id = false;
let mailbox_id = false;
this.mailboxes.every(function (v, k) {
if (v.toLowerCase() == mailbox_name.toLowerCase()) {
mailbox_id = k;
@@ -134,18 +133,18 @@ class WTVMail {
getMailboxStoreDir(mailboxid) {
if (this.mailboxExists(mailboxid)) {
var mailbox_name = this.getMailboxById(mailboxid);
const mailbox_name = this.getMailboxById(mailboxid);
return this.mailstore_dir + mailbox_name + this.path.sep;
}
return null;
}
createMailbox(mailboxid) {
var mailbox_exists = this.mailboxExists(mailboxid);
const mailbox_exists = this.mailboxExists(mailboxid);
if (mailbox_exists === false) {
var mailbox_name = this.getMailboxById(mailboxid);
var mailbox_dir = mailbox_name + this.path.sep;
var store_dir = this.mailstore_dir + mailbox_dir;
const mailbox_name = this.getMailboxById(mailboxid);
const mailbox_dir = mailbox_name + this.path.sep;
const store_dir = this.mailstore_dir + mailbox_dir;
if (!this.fs.existsSync(store_dir)) this.fs.mkdirSync(store_dir, { recursive: true });
return true;
}
@@ -160,11 +159,11 @@ class WTVMail {
if (this.createMailbox(mailboxid)) {
if (!date) date = Math.floor(Date.now() / 1000);
var mailbox_path = this.getMailboxStoreDir(mailboxid);
var message_id = this.createMessageID();
var message_file = message_id + this.msgFileExt;
var message_file_out = mailbox_path + message_file;
var message_data = {
const mailbox_path = this.getMailboxStoreDir(mailboxid);
const message_id = this.createMessageID();
const message_file = message_id + this.msgFileExt;
const message_file_out = mailbox_path + message_file;
const message_data = {
"from_addr": from_addr,
"from_name": from_name,
"to_addr": to_addr,
@@ -206,31 +205,31 @@ class WTVMail {
}
createWelcomeMessage() {
var welcomeTemplate = this.wtvshared.getTemplate("wtv-mail", "welcomeMail.txt").toString('ascii');
var end_of_headers = false;
var msg = "";
var self = this;
var to_addr = this.wtvclient.getSessionData("subscriber_username") + "@" + this.minisrv_config.config.service_name;
var to_name = this.wtvclient.getSessionData("subscriber_name");
var available_tags = {
const welcomeTemplate = this.wtvshared.getTemplate("wtv-mail", "welcomeMail.txt").toString('ascii');
let end_of_headers = false;
let msg = "";
const self = this;
const to_addr = this.wtvclient.getSessionData("subscriber_username") + "@" + this.minisrv_config.config.service_name;
const to_name = this.wtvclient.getSessionData("subscriber_name");
const available_tags = {
...this.minisrv_config.config,
"user_address": to_addr,
"user_name": to_name
}
var from_name, from_addr, subj = null;
var lines = welcomeTemplate.replace(/\r/g, '').split("\n");
let from_name, from_addr, subj = null;
const lines = welcomeTemplate.replace(/\r/g, '').split("\n");
lines.forEach((line) => {
if (line.indexOf(": ") > 1 && !end_of_headers) {
var header = [line.slice(0, line.indexOf(':')), line.slice(line.indexOf(':') + 2).trim()];
const header = [line.slice(0, line.indexOf(':')), line.slice(line.indexOf(':') + 2).trim()];
switch (header[0].toLowerCase()) {
case "from":
if (header[1].indexOf("<") >= 0) {
var email = header[1].match(/(.+) \<(.+)\>/);
let email = header[1].match(/(.+) \<(.+)\>/);
if (email) {
from_name = email[1];
from_addr = email[2];
} else {
var email = header[1].match(/\<(.+)\>/);
const email = header[1].match(/\<(.+)\>/);
from_addr = email[1];
}
} else if (header[1].indexOf('@') >= 0) {
@@ -245,8 +244,8 @@ class WTVMail {
} else if (line == '') end_of_headers = true;
else {
msg += line.replace(/\$\{(\w{1,})\}/g, function (x) {
var out = '';
var tag = x.replace("${", '').replace('}', '');
let out = '';
const tag = x.replace("${", '').replace('}', '');
if (available_tags[tag]) out = available_tags[tag];
return out
}) + "\n";
@@ -257,16 +256,16 @@ class WTVMail {
getMessage(mailboxid, messageid) {
if (this.createMailbox(mailboxid)) {
var mailbox_path = this.getMailboxStoreDir(mailboxid);
var message_file = messageid + this.msgFileExt;
var message_file_in = mailbox_path + this.path.sep + message_file;
var message_data_raw = null;
const mailbox_path = this.getMailboxStoreDir(mailboxid);
const message_file = messageid + this.msgFileExt;
const message_file_in = mailbox_path + this.path.sep + message_file;
let message_data_raw = null;
if (this.fs.existsSync(message_file_in)) message_data_raw = this.fs.readFileSync(message_file_in);
else console.error(" # MailErr: could not find ", message_file_in);
if (message_data_raw) {
var message_data = JSON.parse(message_data_raw);
const message_data = JSON.parse(message_data_raw);
message_data.mailbox_path = mailbox_path;
message_data.message_file = message_file;
if (message_data) {
@@ -284,15 +283,15 @@ class WTVMail {
updateMessage(message_data) {
// encode message into json
var message_out = new Object();
const message_out = {};
Object.assign(message_out, message_data);
delete message_out.mailbox_path;
delete message_out.message_file;
var result = this.fs.writeFileSync(message_data.mailbox_path + this.path.sep + message_data.message_file, JSON.stringify(message_out));
const result = this.fs.writeFileSync(message_data.mailbox_path + this.path.sep + message_data.message_file, JSON.stringify(message_out));
if (!result) return false;
// rely on filesystem times for sorting as it is quicker then reading every file
var file_timestamp = new Date(message_data.date * 1000);
const file_timestamp = new Date(message_data.date * 1000);
fs.utimesSync(message_file, Date.now(), file_timestamp);
if (!result) console.error(" WARNING: Setting timestamp on " + message_file + " failed, mail dates will be inaccurate.");
}
@@ -304,19 +303,19 @@ class WTVMail {
listMessages(mailboxid, limit, reverse_sort = false, offset = 0) {
if (this.createMailbox(mailboxid)) {
var mailbox_path = this.getMailboxStoreDir(mailboxid);
var self = this;
var files = this.fs.readdirSync(mailbox_path)
const mailbox_path = this.getMailboxStoreDir(mailboxid);
const self = this;
const files = this.fs.readdirSync(mailbox_path)
.map(function (v) {
var message_data_raw = null;
var message_date = null;
var message_path = mailbox_path + self.path.sep + v;
let message_data_raw = null;
let message_date = null;
const message_path = mailbox_path + self.path.sep + v;
if (self.fs.existsSync(message_path)) message_data_raw = self.fs.readFileSync(message_path);
if (message_data_raw) {
var message_data = JSON.parse(message_data_raw);
const message_data = JSON.parse(message_data_raw);
if (message_data) message_date = message_data.date;
}
var message_date_ret = (message_date) ? message_date : self.fs.statSync(mailbox_path + self.path.sep + v).mtime.getTime();
const message_date_ret = (message_date) ? message_date : self.fs.statSync(mailbox_path + self.path.sep + v).mtime.getTime();
self.fs.statSync(mailbox_path + self.path.sep + v).mtime.getTime()
return {
name: v,
@@ -334,9 +333,9 @@ class WTVMail {
if (files.length == 0) return false; // no messages
else {
// todo filter previous results when offset
var messagelist_out = new Array();
const messagelist_out = new Array();
Object.keys(files).forEach(function (k) {
var message = self.getMessage(mailboxid, files[k]);
const message = self.getMessage(mailboxid, files[k]);
if (message) messagelist_out.push(mailboxid, message);
else console.error(" # MailErr: reading message ID: ", files[k]);
})
@@ -347,14 +346,14 @@ class WTVMail {
}
countMessages(mailboxid) {
var messages = this.listMessages(mailboxid, 100, false);
var message_count = Object.keys(messages).length;
const messages = this.listMessages(mailboxid, 100, false);
const message_count = Object.keys(messages).length;
return (message_count) ? message_count : 0;
}
countUnreadMessages(mailboxid) {
var messages = this.listMessages(mailboxid, 100, false);
var unread = 0;
const messages = this.listMessages(mailboxid, 100, false);
let unread = 0;
Object.keys(messages).forEach(function (k) {
if (messages[k].unread) unread++;
});
@@ -362,7 +361,7 @@ class WTVMail {
}
getMailboxIcon() {
var icon_image = null;
let icon_image = null;
switch (this.countMessages(0)) {
case 0:
icon_image = "OpenMailbox0.gif";
@@ -379,9 +378,9 @@ class WTVMail {
checkUserExists(username, directory = null) {
// returns the user's ssid, and user_id and userid in an array if true, false if not
var search_dir = this.wtvshared.getAbsolutePath(this.minisrv_config.config.SessionStore + this.path.sep + "accounts");
var return_val = false;
var self = this;
let search_dir = this.wtvshared.getAbsolutePath(this.minisrv_config.config.SessionStore + this.path.sep + "accounts");
let return_val = false;
const self = this;
if (directory) search_dir = directory;
this.fs.readdirSync(search_dir).forEach(file => {
if (self.fs.lstatSync(search_dir + self.path.sep + file).isDirectory() && !return_val) {
@@ -389,22 +388,22 @@ class WTVMail {
}
if (!file.match(/.*\.json/ig)) return;
try {
var temp_session_data_file = self.fs.readFileSync(search_dir + self.path.sep + file, 'Utf8');
var temp_session_data = JSON.parse(temp_session_data_file);
const temp_session_data_file = self.fs.readFileSync(search_dir + self.path.sep + file, 'Utf8');
const temp_session_data = JSON.parse(temp_session_data_file);
if (temp_session_data.subscriber_username) {
if (temp_session_data.subscriber_username.toLowerCase() == username.toLowerCase()) {
// Use the absolute path for replacement since search_dir is now absolute
var accounts_dir = self.wtvshared.getAbsolutePath(self.minisrv_config.config.SessionStore + self.path.sep + "accounts" + self.path.sep);
var path_after_replace = search_dir.replace(accounts_dir, '');
const accounts_dir = self.wtvshared.getAbsolutePath(self.minisrv_config.config.SessionStore + self.path.sep + "accounts" + self.path.sep);
let path_after_replace = search_dir.replace(accounts_dir, '');
// Remove leading path separator if present
if (path_after_replace.startsWith(self.path.sep)) {
path_after_replace = path_after_replace.slice(1);
}
var path_split = path_after_replace.split(self.path.sep);
const path_split = path_after_replace.split(self.path.sep);
// The path should be like "ssid/user0", so extract ssid and user_id
var ssid = path_split[0];
var user_part = path_split[1] || '';
var user_id = user_part.replace('user', '');
const ssid = path_split[0];
const user_part = path_split[1] || '';
const user_id = user_part.replace('user', '');
return_val = [ssid, user_id, temp_session_data.subscriber_name];
}
}
@@ -416,11 +415,11 @@ class WTVMail {
}
getUserMailstore(username) {
var user_data = this.checkUserExists(username);
const user_data = this.checkUserExists(username);
if (user_data) {
var user_wtvsession = new this.WTVClientSessionData(this.minisrv_config, user_data[0]);
const user_wtvsession = new this.WTVClientSessionData(this.minisrv_config, user_data[0]);
user_wtvsession.user_id = user_data[1];
var user_mailstore = new WTVMail(this.minisrv_config, user_wtvsession)
const user_mailstore = new WTVMail(this.minisrv_config, user_wtvsession)
return user_mailstore;
}
return false;
@@ -431,8 +430,8 @@ class WTVMail {
if (to_addr.indexOf('@') === -1) to_addr += "@"+this.minisrv_config.config.service_name;
var username = to_addr.split("@")[0];
var dest_minisrv = to_addr.split("@")[1] || this.minisrv_config.config.service_name;
const username = to_addr.split("@")[0];
const dest_minisrv = to_addr.split("@")[1] || this.minisrv_config.config.service_name;
// local only for now
if (dest_minisrv.toLowerCase() !== this.minisrv_config.config.service_name.toLowerCase()) {
@@ -441,25 +440,25 @@ class WTVMail {
// find user if local
if (dest_minisrv.toLowerCase() === this.minisrv_config.config.service_name.toLowerCase()) {
var dest_user_mailstore = this.getUserMailstore(username);
const dest_user_mailstore = this.getUserMailstore(username);
// user does not exist
if (!dest_user_mailstore) return "The user <strong>" + username + "</strong> does not exist on MiniSrv <strong>" + dest_minisrv + "</strong>";
if (!to_name) {
var userExistsData = this.checkUserExists(username);
const userExistsData = this.checkUserExists(username);
to_name = userExistsData[2];
}
// check if the destination user's Inbox exists yet
if (!dest_user_mailstore.mailboxExists(0)) {
// mailbox does not yet exist, create it
var mailbox_exists = dest_user_mailstore.createMailbox(0);
const mailbox_exists = dest_user_mailstore.createMailbox(0);
// Just created Inbox for the first time, so create the welcome message
if (mailbox_exists) dest_user_mailstore.createWelcomeMessage();
}
// if the mailbox exists, deliver the message
if (dest_user_mailstore.mailboxExists(0)) {
var createResult = dest_user_mailstore.createMessage(0, from_addr, to_addr, msgbody, subject, from_name, to_name, signature, null, this.isInUserAddressBook(to_addr, from_addr), attachments, url, url_title);
const createResult = dest_user_mailstore.createMessage(0, from_addr, to_addr, msgbody, subject, from_name, to_name, signature, null, this.isInUserAddressBook(to_addr, from_addr), attachments, url, url_title);
if (!createResult) {
return "There was an error creating the message in the recipient's mailbox.";
}
@@ -467,7 +466,6 @@ class WTVMail {
else return "There was an internal error sending the message to <strong>" + to_addr + "</strong>. Please try again later";
// clean up
dest_user_mailstore = null;
return true;
}
return "Unknown error";
@@ -480,8 +478,8 @@ class WTVMail {
getMessageMailboxName(messageid) {
// returns the mailbox id of which the message was found for the current user
var self = this;
var mailbox_name = false;
const self = this;
let mailbox_name = false;
if (this.checkMessageIdSanity(messageid)) {
if (this.mailstoreExists()) {
this.fs.readdirSync(this.mailstore_dir).every(mailbox => {
@@ -501,16 +499,16 @@ class WTVMail {
}
getMessageMailboxID(messageid) {
var mailbox_name = this.getMessageMailboxName(messageid);
const mailbox_name = this.getMessageMailboxName(messageid);
if (!mailbox_name) return false;
return this.getMailboxByName(mailbox_name);
}
getMessageByID(messageid) {
var mailbox_name = this.getMessageMailboxName(messageid);
const mailbox_name = this.getMessageMailboxName(messageid);
if (!mailbox_name) return false;
var mailboxid = this.mailboxes.findIndex((value) => value == mailbox_name);
const mailboxid = this.mailboxes.findIndex((value) => value == mailbox_name);
if (mailboxid !== false) return this.getMessage(mailboxid, messageid);
return null;
@@ -518,7 +516,7 @@ class WTVMail {
moveMailMessage(messageid, dest_mailbox_id) {
// returns true if successful, false if failed.
var currentMailbox = this.getMessageMailboxID(messageid);
const currentMailbox = this.getMessageMailboxID(messageid);
// Same mailbox
if (dest_mailbox_id == currentMailbox) return false;
@@ -527,14 +525,14 @@ class WTVMail {
if (!this.mailboxExists(dest_mailbox_id)) this.createMailbox(dest_mailbox_id);
var currentMailStoreDir = this.getMailboxStoreDir(currentMailbox);
const currentMailStoreDir = this.getMailboxStoreDir(currentMailbox);
if (!currentMailStoreDir) return false;
var destMailStoreDir = this.getMailboxStoreDir(dest_mailbox_id);
const destMailStoreDir = this.getMailboxStoreDir(dest_mailbox_id);
if (!destMailStoreDir) return false;
var currentMailFile = currentMailStoreDir + this.path.sep + messageid + this.msgFileExt;
var destMailFile = destMailStoreDir + this.path.sep + messageid + this.msgFileExt;
const currentMailFile = currentMailStoreDir + this.path.sep + messageid + this.msgFileExt;
const destMailFile = destMailStoreDir + this.path.sep + messageid + this.msgFileExt;
// File exists
if (this.fs.existsSync(destMailFile)) return false;
@@ -543,14 +541,14 @@ class WTVMail {
}
deleteMessage(messageid) {
var currentMailbox = this.getMessageMailboxName(messageid);
var trashMailbox = this.getMailboxByName(this.trashMailboxName);
const currentMailbox = this.getMessageMailboxName(messageid);
const trashMailbox = this.getMailboxByName(this.trashMailboxName);
if (currentMailbox != trashMailbox) {
// if not in the trash, move it to trash
return this.moveMailMessage(messageid, trashMailbox);
} else {
// if its already in the trash, delete it forever
var currentMailFile = this.getMailboxStoreDir(trashMailbox) + this.path.sep + messageid + this.msgFileExt;
const currentMailFile = this.getMailboxStoreDir(trashMailbox) + this.path.sep + messageid + this.msgFileExt;
if (this.fs.fileExistsSync(currentMailFile))
return this.fs.unlink(currentMailFile);
else
@@ -559,7 +557,7 @@ class WTVMail {
}
setMessageReadStatus(messageid, read = true) {
var message = this.getMessageByID(messageid);
const message = this.getMessageByID(messageid);
if (!message) return false;
message.unread = !read;