Skip to content

Commit

Permalink
Refactor Phishing Check Functionality for Better Organization and Rea…
Browse files Browse the repository at this point in the history
…dability (#9)

* Move file paths

* Formating
  • Loading branch information
Koon-Kiat authored Nov 23, 2024
1 parent 5b47ef7 commit 6c81d12
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 142 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
98 changes: 98 additions & 0 deletions server/static/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Office.onReady(() => {
// If needed, Office.js is ready to be called
});

function checkPhishing(event) {
Office.context.mailbox.getCallbackTokenAsync(
{ isRest: true },
async function (result) {
if (result.status === "succeeded") {
const accessToken = result.value;
const itemId = getItemRestId(); // Get the item's REST ID.
// Construct the REST URL to the current item. /$value returns the email data in EML
const eml =
Office.context.mailbox.restUrl +
"/v2.0/me/messages/" +
itemId +
"/$value";
const evaluateEmailURL =
"https://trisapple.pythonanywhere.com/evaluateEmail";

// Get Email Data in EML
const emlRequest = await fetch(eml, {
headers: {
Authorization: "Bearer " + accessToken,
},
});
const emlResponse = await emlRequest.text(); // Email in EML

// Send EML to our Python Anywhere Server in the request body. Determine if email is 'safe' or 'not safe'
const parseEML = await fetch(evaluateEmailURL, {
method: "POST",
body: emlResponse,
});
// Our server will return a response -> 'The email is safe' or 'The email is not safe'
const parseEMLResponse = await parseEML.text();

const message = {
type: Office.MailboxEnums.ItemNotificationMessageType
.InformationalMessage,
message: parseEMLResponse,
icon: "Icon.80x80",
persistent: true,
};

// Show a notification message
Office.context.mailbox.item.notificationMessages.replaceAsync(
"action",
message
);
event.completed(); // Indicate that the add-in command function is complete
} else {
// Handle the error.
const message = {
type: Office.MailboxEnums.ItemNotificationMessageType
.InformationalMessage,
message: "Unable to get callback token.",
icon: "Icon.80x80",
persistent: true,
};

// Show a notification message
Office.context.mailbox.item.notificationMessages.replaceAsync(
"action",
message
);
event.completed(); // Indicate that the add-in command function is complete
}
}
);
}

function getItemRestId() {
if (Office.context.mailbox.diagnostics.hostName === "OutlookIOS") {
// itemId is already REST-formatted.
return Office.context.mailbox.item.itemId;
} else {
// Convert to an item ID for API v2.0.
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}

function getGlobal() {
return typeof self !== "undefined"
? self
: typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: undefined;
}

const g = getGlobal();

// The add-in command functions need to be available in global scope
g.action = checkPhishing;
File renamed without changes.
File renamed without changes.
70 changes: 70 additions & 0 deletions server/static/taskpane.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Office.onReady((info) => {
function getItemRestId() {
if (Office.context.mailbox.diagnostics.hostName === "OutlookIOS") {
// itemId is already REST-formatted.
return Office.context.mailbox.item.itemId;
} else {
// Convert to an item ID for API v2.0.
return Office.context.mailbox.convertToRestId(
Office.context.mailbox.item.itemId,
Office.MailboxEnums.RestVersion.v2_0
);
}
}

if (info.host === Office.HostType.Outlook) {
document.getElementById("sideload-msg").style.display = "none";
document.getElementById("app-body").style.display = "flex";
document.getElementById("output").innerHTML = "Getting email data...";

Office.context.mailbox.getCallbackTokenAsync(
{ isRest: true },
async function (result) {
if (result.status === "succeeded") {
const accessToken = result.value;
const itemId = getItemRestId(); // Get the item's REST ID.
// Construct the REST URL to the current item. /$value returns the email data in EML
const eml =
Office.context.mailbox.restUrl +
"/v2.0/me/messages/" +
itemId +
"/$value";
const evaluateEmailURL =
"https://trisapple.pythonanywhere.com/evaluateEmail";

// Get Email Data in EML
const emlRequest = await fetch(eml, {
headers: {
Authorization: "Bearer " + accessToken,
},
});
const emlResponse = await emlRequest.text(); // Email in EML

document.getElementById("output").innerHTML =
"Determining if email is safe or phishing...";
// Send EML to our Python Anywhere Server in the request body. Determine if email is 'safe' or 'not safe'
const parseEML = await fetch(evaluateEmailURL, {
method: "POST",
body: emlResponse,
});
// Our server will return a response -> 'The email is safe' or 'The email is not safe'
const parseEMLResponse = await parseEML.text();

document.getElementById("output").innerHTML = parseEMLResponse;
if (parseEMLResponse == "The email is safe.") {
document.getElementById("output").style.color = "green";
document.getElementById("output").style.fontWeight = "bold";
}
if (parseEMLResponse == "The email is not safe.") {
document.getElementById("output").style.color = "red";
document.getElementById("output").style.fontWeight = "bold";
}
} else {
// Handle the error.
document.getElementById("output").innerHTML =
"Unable to get email data.";
}
}
);
}
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
82 changes: 0 additions & 82 deletions static/commands.js

This file was deleted.

60 changes: 0 additions & 60 deletions static/taskpane.js

This file was deleted.

0 comments on commit 6c81d12

Please sign in to comment.