fixes and optimizations
This commit is contained in:
@@ -232,7 +232,7 @@ const runScriptInVM = function (script_data, user_contextObj = {}, privileged =
|
||||
}
|
||||
|
||||
// create global context object
|
||||
const contextObj = {
|
||||
let contextObj = {
|
||||
// node core variables and functions
|
||||
"console": console, // needed for per-script debugging
|
||||
"__dirname": __dirname, // needed by services such as wtv-flashrom and wtv-disk
|
||||
@@ -1478,9 +1478,9 @@ async function sendToClient(socket, headers_obj, data = null) {
|
||||
if (socket_sessions[socket.id].request_headers) {
|
||||
if (socket_sessions[socket.id].request_headers.service_file_path) {
|
||||
// Don't change Last-modified header if provided already
|
||||
if (!headers['Last-Modified']) {
|
||||
// Only add the header if not a js, php, or cgi file
|
||||
if (wtvshared.getFileExt(socket_sessions[socket.id].request_headers.service_file_path).toLowerCase() !== "js" ||
|
||||
if (!headers['Last-Modified'] && !headers['minisrv-no-last-modified']) {
|
||||
// Only add the header if not a js, php, or cgi file
|
||||
if (wtvshared.getFileExt(socket_sessions[socket.id].request_headers.service_file_path).toLowerCase() !== "js" ||
|
||||
wtvshared.getFileExt(socket_sessions[socket.id].request_headers.service_file_path).toLowerCase() !== "php" ||
|
||||
wtvshared.getFileExt(socket_sessions[socket.id].request_headers.service_file_path).toLowerCase() !== "cgi" ||
|
||||
socket_sessions[socket.id].request_headers.raw_file === true) {
|
||||
@@ -1500,6 +1500,14 @@ async function sendToClient(socket, headers_obj, data = null) {
|
||||
}
|
||||
}
|
||||
|
||||
if (headers_obj['minisrv-no-last-modified']) {
|
||||
if (headers_obj['Last-Modified']) {
|
||||
delete headers_obj['Last-Modified'];
|
||||
}
|
||||
delete headers_obj['minisrv-no-last-modified'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// if client can do compression, see if its worth enabling
|
||||
// small files actually get larger, so don't compress them
|
||||
@@ -1667,7 +1675,7 @@ async function sendToClient(socket, headers_obj, data = null) {
|
||||
|
||||
// Delete any other stray minisrv headers (we process them all before this)
|
||||
Object.keys(headers_obj).forEach(function (k) {
|
||||
if (k.indexOf("minisrv-") == 0) {
|
||||
if (k.startsWith("minisrv-")) {
|
||||
delete headers_obj[k];
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1042,7 +1042,7 @@ class WebTVClientSimulator {
|
||||
this.followVisit(headers['wtv-visit'])
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
}, this.requestDelay);
|
||||
}, this.requestDelay);
|
||||
return;
|
||||
} else {
|
||||
this.debugLog(`Skipping wtv-visit redirect: ${headers['wtv-visit']}`);
|
||||
@@ -1252,7 +1252,7 @@ class WebTVClientSimulator {
|
||||
} else {
|
||||
// Detect text content for CLI output
|
||||
const contentType = result.headers['content-type'] || '';
|
||||
if (/^text\//.test(contentType) || /json|xml|javascript|download-list/.test(contentType)) {
|
||||
if (/^text\//.test(contentType) || /json|xml|javascript||download-list/.test(contentType) || contentType === "x-wtv-addresses") {
|
||||
console.log(result.body.toString('utf8'));
|
||||
} else if (result.body.length === 0) {
|
||||
console.log('<empty response>');
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
var minisrv_service_file = true;
|
||||
|
||||
headers = `200 OK
|
||||
Content-Type: x-wtv-addresses`;
|
||||
Content-Type: x-wtv-addresses
|
||||
wtv-transition: light
|
||||
minisrv-no-last-modified: true
|
||||
minisrv-no-mail-count: true`;
|
||||
|
||||
const address_book = session_data.getSessionData("address_book")
|
||||
|
||||
@@ -9,7 +12,7 @@ if (session_data.getSessionData("address_book") != null) {
|
||||
data = ``
|
||||
for (let i = 0; i < address_book.length; i++) {
|
||||
data += address_book[i].name + '\0' + address_book[i].address + '\0'
|
||||
}
|
||||
};
|
||||
} else {
|
||||
session_data.setSessionData("address_book", [])
|
||||
}
|
||||
|
||||
@@ -41,14 +41,11 @@ Location: wtv-mail:/listmail`;
|
||||
Content-type: text/html`;
|
||||
|
||||
|
||||
var message_colors = null;
|
||||
let message_colors = session_data.mailstore.defaultColors;
|
||||
if (message.body) {
|
||||
if (message.body.indexOf("<body")) {
|
||||
var default_colors = session_data.mailstore.defaultColors;
|
||||
var message_colors = session_data.mailstore.getSignatureColors(message.body);
|
||||
message_colors = session_data.mailstore.getSignatureColors(message.body);
|
||||
}
|
||||
} else {
|
||||
message_colors = session_data.mailstore.defaultColors;
|
||||
}
|
||||
if (message.signature) message_colors = session_data.mailstore.getSignatureColors(message.signature);
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@ class WTVFavorites {
|
||||
constructor(minisrv_config, wtvclient) {
|
||||
if (!minisrv_config) throw ("minisrv_config required");
|
||||
if (!wtvclient) throw ("WTVClientSessionData 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);
|
||||
@@ -42,10 +42,10 @@ class WTVFavorites {
|
||||
if (!this.isguest) {
|
||||
if (this.favstore_dir === null) {
|
||||
// set favstore directory local var so we don't call the function every time
|
||||
var userstore_dir = this.wtvclient.getUserStoreDirectory();
|
||||
const userstore_dir = this.wtvclient.getUserStoreDirectory();
|
||||
|
||||
// FavStore
|
||||
var store_dir = "FavStore" + this.path.sep;
|
||||
const store_dir = "FavStore" + this.path.sep;
|
||||
this.favstore_dir = userstore_dir + store_dir;
|
||||
}
|
||||
return this.fs.existsSync(this.favstore_dir);
|
||||
@@ -54,12 +54,12 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
folderExists(foldername) {
|
||||
var folder_dir = null;
|
||||
if (this.favstoreExists()) {
|
||||
let folder_dir, store_dir = null;
|
||||
if (this.favstoreExists()) {
|
||||
if (!foldername) return null;
|
||||
|
||||
var folder_dir = foldername + this.path.sep;
|
||||
var store_dir = this.favstore_dir + folder_dir;
|
||||
folder_dir = foldername + this.path.sep;
|
||||
store_dir = this.favstore_dir + folder_dir;
|
||||
}
|
||||
if (store_dir) {
|
||||
if (this.fs.existsSync(store_dir)) {
|
||||
@@ -72,12 +72,12 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
getFolderDir(foldername) {
|
||||
var folder_dir = null;
|
||||
let folder_dir, store_dir = null;
|
||||
if (this.favstoreExists()) {
|
||||
if (!foldername) return null;
|
||||
|
||||
var folder_dir = foldername + this.path.sep;
|
||||
var store_dir = this.favstore_dir + folder_dir;
|
||||
folder_dir = foldername + this.path.sep;
|
||||
store_dir = this.favstore_dir + folder_dir;
|
||||
}
|
||||
return store_dir;
|
||||
}
|
||||
@@ -85,9 +85,9 @@ class WTVFavorites {
|
||||
createTemplateFolder(folder) {
|
||||
// create emply folder
|
||||
this.createFolder(folder)
|
||||
var folder_templates = this.minisrv_config.favorites.folder_templates;
|
||||
const folder_templates = this.minisrv_config.favorites.folder_templates;
|
||||
// populate it if a template exists
|
||||
var self = this;
|
||||
const self = this;
|
||||
if (folder_templates[folder]) {
|
||||
Object.keys(folder_templates[folder]).forEach(function (k) {
|
||||
self.createFavorite(folder_templates[folder][k].title, folder_templates[folder][k].url, folder, (folder_templates[folder][k].image_type == "image/wtv-bitmap") ? atob(folder_templates[folder][k].image) : folder_templates[folder][k].image, folder_templates[folder][k].image_type);
|
||||
@@ -96,7 +96,7 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
createDefaultFolders() {
|
||||
var brandId = this.ssid.charAt(8);
|
||||
const brandId = this.ssid.charAt(8);
|
||||
this.createTemplateFolder("Recommended");
|
||||
if (brandId == 7)
|
||||
this.createTemplateFolder("Personal (Samsung)");
|
||||
@@ -118,10 +118,10 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
createFolder(foldername) {
|
||||
var folder_exists = this.folderExists(foldername);
|
||||
const folder_exists = this.folderExists(foldername);
|
||||
if (folder_exists === false) {
|
||||
var folderdir = foldername + this.path.sep;
|
||||
var store_dir = this.favstore_dir + folderdir;
|
||||
const folderdir = foldername + this.path.sep;
|
||||
const store_dir = this.favstore_dir + folderdir;
|
||||
if (!this.fs.existsSync(store_dir)) this.fs.mkdirSync(store_dir, { recursive: true });
|
||||
return true;
|
||||
}
|
||||
@@ -129,8 +129,8 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
getFolders() {
|
||||
var path = this.favstore_dir;
|
||||
var self = this;
|
||||
const path = this.favstore_dir;
|
||||
const self = this;
|
||||
return this.fs.readdirSync(path).filter(function (file) {
|
||||
if (self.folderExists(file)) {
|
||||
self.folderArr.push(file);
|
||||
@@ -144,16 +144,16 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
createFavorite(title, url, folder, image, imagetype) {
|
||||
var folderpath = this.getFolderDir(folder);
|
||||
var favoriteid = this.createFavoriteID();
|
||||
var favoritefile = favoriteid + this.favFileExt;
|
||||
var favoritefileout = folderpath + favoritefile;
|
||||
const folderpath = this.getFolderDir(folder);
|
||||
const favoriteid = this.createFavoriteID();
|
||||
const favoritefile = favoriteid + this.favFileExt;
|
||||
const favoritefileout = folderpath + favoritefile;
|
||||
if (imagetype != "url")
|
||||
image = btoa(image);
|
||||
|
||||
title = decodeURIComponent(title).replaceAll("+", " ");
|
||||
url = decodeURIComponent(url)
|
||||
var favoritedata = {
|
||||
const favoritedata = {
|
||||
"title": title,
|
||||
"url": url,
|
||||
"folder": folder,
|
||||
@@ -168,7 +168,7 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
// encode favorite into json
|
||||
var result = this.fs.writeFileSync(favoritefileout, JSON.stringify(favoritedata));
|
||||
const result = this.fs.writeFileSync(favoritefileout, JSON.stringify(favoritedata));
|
||||
if (!result) return false;
|
||||
|
||||
} catch (e) {
|
||||
@@ -178,8 +178,8 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
listFavorites(folder) {
|
||||
var folderpath = this.getFolderDir(folder);
|
||||
var self = this;
|
||||
const folderpath = this.getFolderDir(folder);
|
||||
const self = this;
|
||||
self.messageArr = [];
|
||||
this.fs.readdirSync(folderpath)
|
||||
.map(function (v) {
|
||||
@@ -211,16 +211,16 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
getFavorite(folder, favoriteid) {
|
||||
var folder_path = this.getFolderDir(folder);
|
||||
var folder_file = favoriteid + this.favFileExt;
|
||||
var folder_file_in = folder_path + this.path.sep + folder_file;
|
||||
var folder_data_raw = null;
|
||||
const folder_path = this.getFolderDir(folder);
|
||||
const folder_file = favoriteid + this.favFileExt;
|
||||
const folder_file_in = folder_path + this.path.sep + folder_file;
|
||||
let folder_data_raw = null;
|
||||
|
||||
if (this.fs.existsSync(folder_file_in)) folder_data_raw = this.fs.readFileSync(folder_file_in);
|
||||
else console.error(" # FavErr: could not find ", folder_file_in);
|
||||
|
||||
if (folder_data_raw) {
|
||||
var folder_data = JSON.parse(folder_data_raw);
|
||||
const folder_data = JSON.parse(folder_data_raw);
|
||||
folder_data.folder_path = folder_path;
|
||||
folder_data.folder_file = folder_file;
|
||||
if (folder_data) {
|
||||
@@ -234,7 +234,7 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
deleteFolder(folder){
|
||||
var dir = this.getFolderDir(folder);
|
||||
const dir = this.getFolderDir(folder);
|
||||
if (dir) {
|
||||
try {
|
||||
this.fs.rm(dir, { recursive: true });
|
||||
@@ -251,29 +251,29 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
deleteFavorite(favoriteid, folder) {
|
||||
var folderdir = this.getFolderDir(folder);
|
||||
const folderdir = this.getFolderDir(folder);
|
||||
this.fs.unlinkSync(folderdir + favoriteid + ".zfav", { recursive: true });
|
||||
}
|
||||
|
||||
clearFolder(folder) {
|
||||
const { readdirSync, rmSync } = require('fs');
|
||||
var dir = this.getFolderDir(folder);
|
||||
const dir = this.getFolderDir(folder);
|
||||
readdirSync(dir).forEach(f => rmSync(`${dir}${f}`));
|
||||
}
|
||||
|
||||
updateFavorite(favoritedata, folder) {
|
||||
// encode message into json
|
||||
var favoriteout = new Object();
|
||||
var folderpath = this.getFolderDir(folder);
|
||||
const favoriteout = new Object();
|
||||
const folderpath = this.getFolderDir(folder);
|
||||
Object.assign(favoriteout, favoritedata);
|
||||
delete favoriteout.folderpath;
|
||||
delete favoriteout.favoritefile;
|
||||
var result = this.fs.writeFileSync(folderpath + favoritedata.id + ".zfav", JSON.stringify(favoriteout));
|
||||
const result = this.fs.writeFileSync(folderpath + favoritedata.id + ".zfav", JSON.stringify(favoriteout));
|
||||
if (!result) return false;
|
||||
}
|
||||
|
||||
changeFavoriteName(favoriteid, folder, name) {
|
||||
var favorite = this.getFavorite(folder, favoriteid);
|
||||
const favorite = this.getFavorite(folder, favoriteid);
|
||||
if (!favorite) return false;
|
||||
|
||||
favorite.title = name;
|
||||
@@ -282,19 +282,19 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
moveFavorite(oldfolder, newfolder, favoriteid) {
|
||||
var favorite = this.getFavorite(oldfolder, favoriteid);
|
||||
const favorite = this.getFavorite(oldfolder, favoriteid);
|
||||
if (!favorite) return false;
|
||||
|
||||
var newfolderdata = this.listFavorites(newfolder);
|
||||
var newfoldernum = newfolderdata.length
|
||||
const newfolderdata = this.listFavorites(newfolder);
|
||||
const newfoldernum = newfolderdata.length
|
||||
|
||||
if (newfoldernum > 17)
|
||||
return;
|
||||
|
||||
favorite.folder = newfolder;
|
||||
this.updateFavorite(favorite, oldfolder);
|
||||
var favoriteout = new Object();
|
||||
var folderpath = this.getFolderDir(newfolder);
|
||||
const favoriteout = new Object();
|
||||
const folderpath = this.getFolderDir(newfolder);
|
||||
Object.assign(favoriteout, favorite);
|
||||
delete favoriteout.folderpath;
|
||||
delete favoriteout.favoritefile;
|
||||
@@ -305,14 +305,13 @@ class WTVFavorites {
|
||||
|
||||
|
||||
isFavoriteAShortcut(favoriteid) {
|
||||
var favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
const favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
if (!this.fs.existsSync(favoritefileout)) {
|
||||
this.createShortcutKey();
|
||||
}
|
||||
var keydata = {};
|
||||
keydata = JSON.parse(this.fs.readFileSync(favoritefileout));
|
||||
var keys = Object.keys(keydata);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
const keydata = JSON.parse(this.fs.readFileSync(favoritefileout));
|
||||
const keys = Object.keys(keydata);
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
if (keydata[keys[i]].id == favoriteid) {
|
||||
return { key: keys[i], folder: keydata[keys[i]].folder };
|
||||
}
|
||||
@@ -321,20 +320,19 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
getShortcutKey(key) {
|
||||
var favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
const favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
if (!this.fs.existsSync(favoritefileout)) {
|
||||
this.createShortcutKey();
|
||||
}
|
||||
var keydata = {};
|
||||
keydata = JSON.parse(this.fs.readFileSync(favoritefileout));
|
||||
const keydata = JSON.parse(this.fs.readFileSync(favoritefileout));
|
||||
if (key && keydata[key]) {
|
||||
return { folder: keydata[key].folder, id: keydata[key].id };
|
||||
}
|
||||
}
|
||||
|
||||
createShortcutKey() {
|
||||
var favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
var keydata = {};
|
||||
const favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
const keydata = {};
|
||||
|
||||
keydata.F1 = {
|
||||
folder: "none",
|
||||
@@ -367,7 +365,7 @@ class WTVFavorites {
|
||||
|
||||
try {
|
||||
// encode favorite into json
|
||||
var result = this.fs.writeFileSync(favoritefileout, JSON.stringify(keydata));
|
||||
const result = this.fs.writeFileSync(favoritefileout, JSON.stringify(keydata));
|
||||
if (!result) return false;
|
||||
|
||||
} catch (e) {
|
||||
@@ -377,13 +375,11 @@ class WTVFavorites {
|
||||
}
|
||||
|
||||
updateShortcutKey(oldkey, newkey, folder, id) {
|
||||
var favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
const favoritefileout = this.favstore_dir + "KeyStore.zfav";
|
||||
if (!this.fs.existsSync(favoritefileout)) {
|
||||
this.createShortcutKey();
|
||||
}
|
||||
var keydata = {};
|
||||
|
||||
keydata = JSON.parse(this.fs.readFileSync(favoritefileout))
|
||||
const keydata = JSON.parse(this.fs.readFileSync(favoritefileout));
|
||||
switch(newkey) {
|
||||
case "F1":
|
||||
keydata.F1 = {
|
||||
@@ -435,7 +431,7 @@ class WTVFavorites {
|
||||
|
||||
try {
|
||||
// encode favorite into json
|
||||
var result = this.fs.writeFileSync(favoritefileout, JSON.stringify(keydata));
|
||||
const result = this.fs.writeFileSync(favoritefileout, JSON.stringify(keydata));
|
||||
if (!result) return false;
|
||||
|
||||
} catch (e) {
|
||||
|
||||
@@ -12,7 +12,7 @@ class WTVFlashrom {
|
||||
|
||||
|
||||
constructor(minisrv_config, service_vaults, service_name, use_zefie_server = true, bf0app_update = false, no_debug = false) {
|
||||
var { WTVShared } = require("./WTVShared.js");
|
||||
const { WTVShared } = require("./WTVShared.js");
|
||||
this.service_vaults = service_vaults;
|
||||
this.service_name = service_name;
|
||||
this.use_zefie_server = use_zefie_server;
|
||||
@@ -24,14 +24,12 @@ class WTVFlashrom {
|
||||
|
||||
async doLocalFlashROM(flashrom_file_path, request_path, callback, info_only = false) {
|
||||
// use local flashrom files;
|
||||
var self = this;
|
||||
const self = this;
|
||||
try {
|
||||
this.fs.readFile(flashrom_file_path, null, function (err, data) {
|
||||
if (err) {
|
||||
errpage = wtvshared.doErrorPage(400)
|
||||
var headers = errpage[0];
|
||||
data = err.toString();
|
||||
callback(data, headers);
|
||||
const errpage = self.wtvshared.doErrorPage(400);
|
||||
callback(err.toString(), errpage[0]);
|
||||
} else {
|
||||
if (info_only) {
|
||||
callback(self.getFlashromInfo(data, request_path));
|
||||
@@ -41,10 +39,8 @@ class WTVFlashrom {
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
var errpage = this.wtvshared.doErrorPage(404, "The service could not find the requested ROM.")
|
||||
var headers = errpage[0];
|
||||
var data = errpage[1];
|
||||
callback(data, headers);
|
||||
const errpage = this.wtvshared.doErrorPage(404, "The service could not find the requested ROM.")
|
||||
callback(errpage[1], errpage[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,16 +52,16 @@ class WTVFlashrom {
|
||||
|
||||
|
||||
getFlashromInfo(data, path) {
|
||||
var flashrom_info = new Array();
|
||||
var flashrom_magic = "96031889";
|
||||
var part_header = new Buffer.alloc(32);
|
||||
const flashrom_info = new Array();
|
||||
const flashrom_magic = "96031889";
|
||||
let part_header = new Buffer.alloc(32);
|
||||
data.copy(part_header, 0, 0, 32);
|
||||
flashrom_info.header_length = data.readUInt16BE(26);
|
||||
|
||||
flashrom_info.is_bootrom = (/\.brom$/).test(path);
|
||||
|
||||
// re-read entire header
|
||||
var part_header = new Buffer.alloc(flashrom_info.header_length);
|
||||
part_header = new Buffer.alloc(flashrom_info.header_length);
|
||||
data.copy(part_header, 0, 0, flashrom_info.header_length);
|
||||
|
||||
flashrom_info.magic = part_header.toString('hex', 0, 4);
|
||||
@@ -118,8 +114,8 @@ class WTVFlashrom {
|
||||
}
|
||||
|
||||
async sendToClient(data, request_path, callback) {
|
||||
var headers = "200 OK\n";
|
||||
var flashrom_info = this.getFlashromInfo(data, request_path)
|
||||
let headers = "200 OK\n";
|
||||
const flashrom_info = this.getFlashromInfo(data, request_path)
|
||||
if (flashrom_info.is_bootrom) headers += "Content-Type: binary/x-wtv-bootrom"; // maybe?
|
||||
else headers += "Content-Type: binary/x-wtv-flashblock";
|
||||
if (flashrom_info.next_rompath != null && this.bf0app_update) headers += "\nwtv-visit: " + flashrom_info.next_rompath;
|
||||
@@ -134,8 +130,8 @@ class WTVFlashrom {
|
||||
}
|
||||
|
||||
async getFlashRom(request_path, callback, length = 0) {
|
||||
var headers, flashrom_file_path = null;
|
||||
var self = this;
|
||||
let headers, data, flashrom_file_path = null;
|
||||
const self = this;
|
||||
Object.keys(self.service_vaults).forEach(function (g) {
|
||||
if (flashrom_file_path != null) return;
|
||||
flashrom_file_path = self.service_vaults[g] + "/" + self.service_name + "/" + request_path;
|
||||
@@ -143,7 +139,7 @@ class WTVFlashrom {
|
||||
});
|
||||
if (this.use_zefie_server && !flashrom_file_path) {
|
||||
// get flashrom files from flashrom.webtv.onl
|
||||
var options = {
|
||||
const options = {
|
||||
host: "flashrom.webtv.onl",
|
||||
path: "/" + request_path,
|
||||
timeout: 5000,
|
||||
@@ -156,7 +152,7 @@ class WTVFlashrom {
|
||||
}
|
||||
|
||||
const req = this.https.request(options, function (res) {
|
||||
var data_hex = '';
|
||||
let data_hex = '';
|
||||
res.setEncoding('hex');
|
||||
|
||||
res.on('data', d => {
|
||||
@@ -166,17 +162,17 @@ class WTVFlashrom {
|
||||
res.on('end', function () {
|
||||
if (self.minisrv_config.config.debug_flags.debug) console.log(` * minisrv FlashROM Server HTTP Status: ${res.statusCode} ${res.statusMessage}`)
|
||||
if (res.statusCode == 200) {
|
||||
var data = Buffer.from(data_hex, 'hex');
|
||||
data = Buffer.from(data_hex, 'hex');
|
||||
} else if (res.statusCode == 206) {
|
||||
var data = self.getFlashromInfo(Buffer.from(data_hex, 'hex'), request_path);
|
||||
data = self.getFlashromInfo(Buffer.from(data_hex, 'hex'), request_path);
|
||||
} else if (res.statusCode == 404) {
|
||||
var errpage = self.wtvshared.doErrorPage(404, "The service could not find the requested ROM on the minisrv FlashROM server.")
|
||||
const errpage = self.wtvshared.doErrorPage(404, "The service could not find the requested ROM on the minisrv FlashROM server.")
|
||||
headers = errpage[0];
|
||||
var data = errpage[1];
|
||||
data = errpage[1];
|
||||
} else {
|
||||
var errpage = self.wtvshared.doErrorPage(400)
|
||||
const errpage = self.wtvshared.doErrorPage(400)
|
||||
headers = errpage[0];
|
||||
var data = errpage[1];
|
||||
data = errpage[1];
|
||||
}
|
||||
if (!headers && res.statusCode != 206) {
|
||||
self.sendToClient(data, request_path, callback);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -21,7 +21,7 @@ class WTVNews {
|
||||
|
||||
initializeUsenet(host, port = 119, tls_options = null, username = null, password = null) {
|
||||
// use local self-signed cert for local server
|
||||
var newsie_options = {
|
||||
const newsie_options = {
|
||||
host: host,
|
||||
port: port,
|
||||
tlsPort: (tls_options !== null) ? true : false,
|
||||
@@ -69,10 +69,11 @@ class WTVNews {
|
||||
|
||||
listGroup(group, page = 0, limit = 100, raw_range = null) {
|
||||
// list of articles from group
|
||||
let range = {};
|
||||
return new Promise((resolve, reject) => {
|
||||
this.selectGroup(group).then((res) => {
|
||||
if (!raw_range) {
|
||||
var range = {
|
||||
range = {
|
||||
start: (limit * page) + res.group.low,
|
||||
}
|
||||
range.end = range.start + limit;
|
||||
@@ -133,9 +134,9 @@ class WTVNews {
|
||||
}
|
||||
|
||||
getArticle(articleID, get_next_last = true) {
|
||||
var articleID = parseInt(articleID);
|
||||
articleID = parseInt(articleID);
|
||||
return new Promise((resolve, reject) => {
|
||||
var promises = [];
|
||||
const promises = [];
|
||||
this.client.article(articleID).then((data) => {
|
||||
if (get_next_last) {
|
||||
// ask server for next article
|
||||
@@ -177,14 +178,14 @@ class WTVNews {
|
||||
}));
|
||||
|
||||
Promise.all(promises).then(() => {
|
||||
var self = this;
|
||||
const self = this;
|
||||
if (data.article.headers) Object.keys(data.article.headers).forEach((k) => {
|
||||
data.article.headers[k] = self.decodeCharset(data.article.headers[k])
|
||||
});
|
||||
resolve(data);
|
||||
});
|
||||
} else {
|
||||
var self = this;
|
||||
const self = this;
|
||||
if (data.article.headers) Object.keys(data.article.headers).forEach((k) => {
|
||||
data.article.headers[k] = self.decodeCharset(data.article.headers[k])
|
||||
});
|
||||
@@ -198,17 +199,17 @@ class WTVNews {
|
||||
}
|
||||
|
||||
decodeCharset(string) {
|
||||
var regex = /=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}=/;
|
||||
var decoded = null;
|
||||
var check = string.match(regex);
|
||||
const regex = /=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}=/;
|
||||
let decoded = null;
|
||||
const check = string.match(regex);
|
||||
if (check) {
|
||||
var match = check[0];
|
||||
var charset = check[1];
|
||||
var encoding = check[2];
|
||||
var encoded_text = check[3];
|
||||
const match = check[0];
|
||||
const charset = check[1];
|
||||
const encoding = check[2];
|
||||
const encoded_text = check[3];
|
||||
switch (encoding) {
|
||||
case "B":
|
||||
var buffer = new Buffer.from(encoded_text, 'base64')
|
||||
const buffer = new Buffer.from(encoded_text, 'base64')
|
||||
decoded = buffer.toString(charset).replace(/[^\x00-\x7F]/g, "");;
|
||||
break;
|
||||
|
||||
@@ -224,7 +225,7 @@ class WTVNews {
|
||||
getHeader(articleID) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.head(articleID).then((data) => {
|
||||
var self = this;
|
||||
const self = this;
|
||||
if (data.article.headers) Object.keys(data.article.headers).forEach((k) => {
|
||||
data.article.headers[k] = self.decodeCharset(data.article.headers[k])
|
||||
});
|
||||
@@ -237,7 +238,7 @@ class WTVNews {
|
||||
}
|
||||
|
||||
getHeaderFromMessage(message, header) {
|
||||
var response = null;
|
||||
const response = null;
|
||||
if (message.article.headers) {
|
||||
Object.keys(message.article.headers).forEach((k) => {
|
||||
if (k.toLowerCase() == header.toLowerCase()) {
|
||||
@@ -266,8 +267,8 @@ class WTVNews {
|
||||
|
||||
postToGroup(group, from_addr, msg_subject, msg_body, article = null, headers = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var promises = [];
|
||||
var messageid = null;
|
||||
const promises = [];
|
||||
let messageid = null;
|
||||
this.connectUsenet()
|
||||
.then(() => {
|
||||
if (article) {
|
||||
@@ -352,9 +353,9 @@ class WTVNews {
|
||||
|
||||
getHeaderObj(NGArticles) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var messages = [];
|
||||
var promises = [];
|
||||
for (var article in NGArticles) {
|
||||
const messages = [];
|
||||
const promises = [];
|
||||
for (const article in NGArticles) {
|
||||
if (article == "getCaseInsensitiveKey" || isNaN(article)) continue;
|
||||
promises.push(new Promise((resolve, reject) => {
|
||||
this.getHeader(NGArticles[article]).then((data) => {
|
||||
@@ -379,26 +380,26 @@ class WTVNews {
|
||||
|
||||
|
||||
parseAttachments(message) {
|
||||
var contype = this.getHeaderFromMessage(message, 'Content-Type');
|
||||
const contype = this.getHeaderFromMessage(message, 'Content-Type');
|
||||
if (contype) {
|
||||
var regex = /multipart\/mixed\; boundary=\"(.+)\"/i;
|
||||
var match = contype.match(regex);
|
||||
const regex = /multipart\/mixed\; boundary=\"(.+)\"/i;
|
||||
const match = contype.match(regex);
|
||||
if (match) {
|
||||
var boundary = "--" + match[1];
|
||||
var body = message.article.body.join("\n").split(boundary);
|
||||
var attachments = [];
|
||||
var i = 0;
|
||||
var message_body = '';
|
||||
var message_type = 'text/plain';
|
||||
const boundary = "--" + match[1];
|
||||
const body = message.article.body.join("\n").split(boundary);
|
||||
const attachments = [];
|
||||
let i = 0;
|
||||
let message_body = '';
|
||||
let message_type = 'text/plain';
|
||||
body.forEach((element) => {
|
||||
var section_type = null;
|
||||
var section = element.split("\n");
|
||||
let section_type = null;
|
||||
let section = element.split("\n");
|
||||
attachments[i] = {};
|
||||
section.forEach((line) => {
|
||||
this.debug('section_type', section_type, 'line', line);
|
||||
var section_header_match = line.match(/^Content\-/i)
|
||||
const section_header_match = line.match(/^Content\-/i)
|
||||
if (section_header_match) {
|
||||
var section_match = line.match(/^Content\-Type\: (.+)\;/i)
|
||||
const section_match = line.match(/^Content\-Type\: (.+)\;/i)
|
||||
if (section_match) {
|
||||
this.debug('section_match', section_match)
|
||||
section_type = section_match[1];
|
||||
@@ -436,13 +437,13 @@ class WTVNews {
|
||||
attachments: attachments
|
||||
}
|
||||
} else {
|
||||
var message_body = '';
|
||||
let message_body = '';
|
||||
if (message.article.body) message_body = message.article.body.join("\n")
|
||||
|
||||
return { text: message_body }
|
||||
}
|
||||
} else {
|
||||
var message_body = '';
|
||||
let message_body = '';
|
||||
if (message.article.body) message_body = message.article.body.join("\n")
|
||||
|
||||
return { text: message_body }
|
||||
@@ -451,21 +452,21 @@ class WTVNews {
|
||||
}
|
||||
|
||||
sortByResponse(messages) {
|
||||
var sorted = [];
|
||||
var message_id_roots = [];
|
||||
var message_relations = [];
|
||||
const sorted = [];
|
||||
const message_id_roots = [];
|
||||
const message_relations = [];
|
||||
Object.keys(messages).forEach((k) => {
|
||||
var messageId = messages[k].messageId;
|
||||
var ref = messages[k].headers.REFERENCES;
|
||||
const messageId = messages[k].messageId;
|
||||
const ref = messages[k].headers.REFERENCES;
|
||||
if (ref) {
|
||||
var res = message_id_roots.find(e => e.messageId == ref);
|
||||
const res = message_id_roots.find(e => e.messageId == ref);
|
||||
if (res) {
|
||||
// see if its attached to a root post
|
||||
if (message_relations[res.messageId]) message_relations[res.messageId].push({ "messageId": messageId, "index": k });
|
||||
else message_relations[res.messageId] = [{ "messageId": messageId, "index": k }];
|
||||
} else {
|
||||
// see if its related to a relation
|
||||
var found = false;
|
||||
let found = false;
|
||||
if (Object.keys(message_relations).length > 0) {
|
||||
Object.keys(message_relations).forEach((j) => {
|
||||
if (found) return;
|
||||
@@ -494,7 +495,7 @@ class WTVNews {
|
||||
|
||||
// If not found in relations, add as root (but check for duplicates first)
|
||||
if (!found) {
|
||||
var existingRoot = message_id_roots.find(e => e.messageId == messageId);
|
||||
const existingRoot = message_id_roots.find(e => e.messageId == messageId);
|
||||
if (!existingRoot) {
|
||||
message_id_roots.push({ "messageId": messageId, "index": k });
|
||||
}
|
||||
@@ -503,7 +504,7 @@ class WTVNews {
|
||||
}
|
||||
else {
|
||||
// Check for duplicates before adding as root
|
||||
var existingRoot = message_id_roots.find(e => e.messageId == messageId);
|
||||
const existingRoot = message_id_roots.find(e => e.messageId == messageId);
|
||||
if (!existingRoot) {
|
||||
message_id_roots.push({ "messageId": messageId, "index": k });
|
||||
}
|
||||
@@ -511,11 +512,11 @@ class WTVNews {
|
||||
});
|
||||
|
||||
// sort the relations, putting root articles first, followed by their relations
|
||||
var message_roots_sorted = [];
|
||||
const message_roots_sorted = [];
|
||||
Object.keys(message_id_roots).forEach((k) => {
|
||||
// sort relations by date
|
||||
var article = messages[message_id_roots[k].index];
|
||||
var article_date = Date.parse(article.headers.DATE);
|
||||
const article = messages[message_id_roots[k].index];
|
||||
const article_date = Date.parse(article.headers.DATE);
|
||||
message_roots_sorted.push({ "article": article, "relation": null, "date": article_date });
|
||||
});
|
||||
// Sort root articles newest to oldest
|
||||
@@ -524,13 +525,13 @@ class WTVNews {
|
||||
sorted.push(message_roots_sorted[k]);
|
||||
|
||||
// Use the correct messageId from the sorted root article
|
||||
var rootMessageId = message_roots_sorted[k].article.messageId;
|
||||
const rootMessageId = message_roots_sorted[k].article.messageId;
|
||||
if (message_relations[rootMessageId]) {
|
||||
var relations = [];
|
||||
const relations = [];
|
||||
Object.keys(message_relations[rootMessageId]).forEach((j) => {
|
||||
// sort relations by date
|
||||
var article = messages[message_relations[rootMessageId][j].index];
|
||||
var article_date = Date.parse(article.headers.DATE);
|
||||
const article = messages[message_relations[rootMessageId][j].index];
|
||||
const article_date = Date.parse(article.headers.DATE);
|
||||
relations.push({ "article": article, "relation": rootMessageId || null, "date": article_date })
|
||||
});
|
||||
relations.sort((a, b) => { return (a.date - b.date) });
|
||||
|
||||
@@ -1074,7 +1074,7 @@ class WTVTellyScript {
|
||||
* It handles directives like #ifdef, #ifndef, #if, #else, #endif.
|
||||
*/
|
||||
preprocess() {
|
||||
var definitions = this.preprocessor_definitions || {};
|
||||
const definitions = this.preprocessor_definitions || {};
|
||||
// Split input into lines (handling CRLF and LF)
|
||||
const lines = this.raw_data.split(/\r?\n/);
|
||||
const output = [];
|
||||
|
||||
Reference in New Issue
Block a user