diff --git a/broker/assets/script.js b/broker/assets/script.js index a1ece3c..5e1197c 100644 --- a/broker/assets/script.js +++ b/broker/assets/script.js @@ -1,278 +1,280 @@ // SPDX-FileCopyrightText: 2023 g10 code GmbH // SPDX-Contributor: Carl Schwan // SPDX-License-Identifier: GPL-2.0-or-later function downloadViaRest(callback) { const context = { isRest: true }; Office.context.mailbox.getCallbackTokenAsync(context, (tokenResults) => { if (tokenResults.status === Office.AsyncResultStatus.Failed) { console.error('Failed to get rest api auth token'); return; } const request = '' + '' + ' ' + ' ' + ' ' + ' ' + ' ' + ' ' + ' IdOnly' + ' true' + ' ' + ' ' + ' ' + ' ' + ''; Office.context.mailbox.makeEwsRequestAsync(request, (asyncResult) => { const parser = new DOMParser(); xmlDoc = parser.parseFromString(asyncResult.value, "text/xml"); const mimeContent = xmlDoc.getElementsByTagName('t:MimeContent')[0].innerHTML; callback(atob(mimeContent)); }); }); } async function view(content) { const response = await fetch('https://localhost:5656/view', { method: 'POST', body: content, headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } async function info(content) { const response = await fetch('https://localhost:5656/info', { method: 'POST', body: content, headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } async function newEmail(content) { const response = await fetch('https://localhost:5656/new', { method: 'POST', headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } async function reply(content) { const response = await fetch('https://localhost:5656/reply', { method: 'POST', body: content, headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } async function forward(content) { const response = await fetch('https://localhost:5656/forward', { method: 'POST', body: content, headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } async function openDraft(id) { const response = await fetch(`https://localhost:5656/draft/${id}`, { method: 'POST', headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } async function deleteDraft(id) { const response = await fetch(`https://localhost:5656/draft/${id}`, { method: 'DELETE', headers: { 'X-EMAIL': Office.context.mailbox.userProfile.emailAddress, 'X-NAME': Office.context.mailbox.userProfile.displayName, }, }); const json = await response.json(); return json; } function showError(errorMessage) { const errorElement = document.getElementById('error'); errorElement.innerHTML = errorMessage; errorElement.classList.remove('d-none'); } function hideError() { const errorElement = document.getElementById('error'); errorElement.classList.add('d-none'); } Office.onReady(). then(()=> { downloadViaRest(async (content) => { const status = await info(content) const statusText = document.getElementById('status-text'); if (status.encrypted || status.signed) { const decryptButton = document.getElementById('decrypt-button'); decryptButton.classList.remove('d-none'); if (status.encrypted) { decryptButton.innerText = "Decrypt"; statusText.innerText = status.signed ? "This mail is encrypted and signed." : "This mail is encrypted."; } else if (status.signed) { decryptButton.innerText = "Show signature"; statusText.innerText = "This mail is signed"; } decryptButton.addEventListener('click', (event) => { view(content); }); + } else { + statusText.innerText = "This mail is not encrypted nor signed."; } document.getElementById('reply-button').addEventListener('click', (event) => { reply(content); }); document.getElementById('forward-button').addEventListener('click', (event) => { forward(content); }); document.getElementById('new-button').addEventListener('click', (event) => { newEmail(); }); if (status.drafts.length === 0) { document.getElementById('no-draft').classList.remove('d-none'); } else { const draftsContainer = document.getElementById('drafts'); status.drafts.forEach(draft => { const draftElementContainer = document.createElement('li'); const draftElement = document.createElement('button'); draftElement.classList.add('btn', 'w-100', 'd-flex', 'flex-row', 'align-items-center'); draftElement.addEventListener('click', (event) => { openDraft(draft.id); }); const date = new Date(draft.last_modification * 1000); let todaysDate = new Date(); let lastModification = ''; if ((new Date(date)).setHours(0, 0, 0, 0) == todaysDate.setHours(0, 0, 0, 0)) { lastModification = date.toLocaleTimeString([], { hour: 'numeric', minute: 'numeric', }); } else { lastModification = date.toLocaleDateString(); } const content = document.createTextNode('Last Modified: ' + lastModification); draftElement.appendChild(content); const deleteDraftButton = document.createElement('button'); deleteDraftButton.classList.add('btn', 'btn-danger', 'ms-auto', 'py-1'); deleteDraftButton.addEventListener('click', (event) => { deleteDraft(draft.id); draftElement.remove(); }); const deleteDraftButtonContent = document.createTextNode('X'); deleteDraftButton.appendChild(deleteDraftButtonContent); draftElement.appendChild(deleteDraftButton); draftElementContainer.appendChild(draftElement); draftsContainer.appendChild(draftElementContainer); }); } function webSocketConnect() { // Create WebSocket connection. const socket = new WebSocket("wss://localhost:5657"); // Connection opened socket.addEventListener("open", (event) => { hideError(); socket.send(JSON.stringify({ command: "register", arguments: { emails: [Office.context.mailbox.userProfile.emailAddress], type: 'webclient', }, })); }); socket.addEventListener("close", (event) => { showError('Native client was disconnected'); setTimeout(function() { webSocketConnect(); }, 1000); }); socket.addEventListener("error", (event) => { showError('Native client received an error'); setTimeout(function() { webSocketConnect(); }, 1000); }); // Listen for messages socket.addEventListener("message", ({ data }) => { const message = JSON.parse(data); console.log("Message from server ", message); switch (message.type) { case 'ews': Office.context.mailbox.makeEwsRequestAsync(message.payload, (asyncResult) => { console.log('Email sent') // let the client known that the email was sent socket.send(JSON.stringify({ command: 'email-sent', arguments: { id: message.id, email: Office.context.mailbox.userProfile.emailAddress, } })); }); break; case 'disconnection': showError('Native client was disconnected'); break; case 'connection': hideError(); break; } }); } webSocketConnect(); }); });