The integration has two goal:
1. Scan the email not only when received but also the past emails
2. Don't waste CPU time and IO disk to scan by outside, but scan the emails from inside the Thunderbird database
The emails are sent by websocket to the user-mode application
note: in server evrioments without GUI the listening application is not open and so is also closed the websocket listener
browser.messageDisplay.onMessageDisplayed.addListener(openMailListener);
async function openMailListener(tab, message) {
console.log(`Message2 open in tab ${tab.id}: ${message.subject}`);
let attachments = await browser.messages.listAttachments(message.id);
for (let att of attachments) {
let file = await browser.messages.getAttachmentFile(
message.id,
att.partName
);
/**
* Get the Attachments
* and encode with base64
*/
var encoded = "";
var isreturned = false;
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
encoded = reader.result;
isreturned = true;
//console.log("ed:"+ encoded);
};
reader.onerror = function (error) {
console.log('Error: ', error);
isreturned = true;
};
while(!isreturned){
console.log("waiting");
await sleep(100);
}
//console.log("end:"+encoded);
/**
* Check if is a virus by asking MutltiAntivirus
*
*/
var isvirus= false;
var isreturned = false;
try{
let ws = new WebSocket("ws://127.0.0.1:51001");
ws.onopen = function(e) {
ws.send(""+encoded+" ");
};
ws.onmessage = function(e) {
isreturned = true;
//console.log( e.data);
isvirus = e.data.includes("true ");
return isvirus;
};
ws.onerror = function (e) {
console.log(e.data);
isreturned = true;
};
while(!isreturned){
console.log("waiting");
await sleep(100);
}
}catch(evt){
console.log(evt.data);
return false;
}
/**
* Remove virus
* Todo: selct by preferences what action permorm
*/
if(isvirus){
await browser.messages.delete(message.id,true);
console.log("virus removed");
}else{
console.log("is not a virus");
}
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
browser.messages.onNewMailReceived.addListener(newMailListener);
async function newMailListener(folder, messages) {
console.log(`Mail arrived `);
for (let message of messages.messages) {
console.log(`Arrived message ${message.id}: ${message.subject}`);
//let realMessage = context.extension.messageManager.get(message.id);
/*
let mparts = browser.messages.getFull(message.id);
console.log(`Arrived message ${mparts.contentType}: ${mparts.body}`);
for (let part of mparts.parts) {
console.log(`Arrived message ${part.contentType}`);
}
*/
let attachments = await messenger.messages.listAttachments(message.id);
for (let attachment of attachments) {
console.log(`Arrived message ${attachment.contentType.toLowerCase()}`);
let reader = new FileReader();
attachment.url = await new Promise((resolve) => {
reader.onload = (e) => {
resolve(e.target.result);
};
reader.readAsDataURL(file);
});
}
}
}
If you want to integrate your application with MultiAntivirus plese contact us, we will help you for the integration