|
| 1 | +let refreshInterval; |
| 2 | + |
| 3 | +async function loadEmailAccounts() { |
| 4 | + try { |
| 5 | + const response = await fetch('/api/email-accounts'); |
| 6 | + const accounts = await response.json(); |
| 7 | + const select = document.getElementById('destination'); |
| 8 | + |
| 9 | + select.innerHTML = '<option value="">Select destination email</option>'; |
| 10 | + accounts.forEach(email => { |
| 11 | + const option = document.createElement('option'); |
| 12 | + option.value = email; |
| 13 | + option.textContent = email; |
| 14 | + select.appendChild(option); |
| 15 | + }); |
| 16 | + } catch (error) { |
| 17 | + console.error('Error loading email accounts:', error); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function loadForwarders() { |
| 22 | + try { |
| 23 | + const response = await fetch('/api/forwarders'); |
| 24 | + const forwarders = await response.json(); |
| 25 | + const list = document.getElementById('forwardersList'); |
| 26 | + |
| 27 | + if (forwarders.length === 0) { |
| 28 | + list.innerHTML = '<p>No forwarders configured</p>'; |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + list.innerHTML = forwarders.map(f => ` |
| 33 | + <div class="forwarder-item"> |
| 34 | + <div> |
| 35 | + <strong>${f.alias}</strong> → ${f.destinations.join(', ')} |
| 36 | + </div> |
| 37 | + <button class="delete-btn" onclick="deleteForwarder('${f.alias}')">Delete</button> |
| 38 | + </div> |
| 39 | + `).join(''); |
| 40 | + } catch (error) { |
| 41 | + console.error('Error loading forwarders:', error); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +async function createForwarder(event) { |
| 46 | + event.preventDefault(); |
| 47 | + |
| 48 | + const alias = document.getElementById('alias').value; |
| 49 | + const destination = document.getElementById('destination').value; |
| 50 | + |
| 51 | + try { |
| 52 | + const response = await fetch('/api/forwarders', { |
| 53 | + method: 'POST', |
| 54 | + headers: { |
| 55 | + 'Content-Type': 'application/json', |
| 56 | + }, |
| 57 | + body: JSON.stringify({ alias, destination }) |
| 58 | + }); |
| 59 | + |
| 60 | + const result = await response.json(); |
| 61 | + if (result.success) { |
| 62 | + document.getElementById('createForwarderForm').reset(); |
| 63 | + loadForwarders(); |
| 64 | + } else { |
| 65 | + alert('Failed to create forwarder'); |
| 66 | + } |
| 67 | + } catch (error) { |
| 68 | + console.error('Error creating forwarder:', error); |
| 69 | + alert('Error creating forwarder'); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function deleteForwarder(alias) { |
| 74 | + if (!confirm(`Delete forwarder ${alias}?`)) return; |
| 75 | + |
| 76 | + try { |
| 77 | + const response = await fetch(`/api/forwarders/${alias}`, { |
| 78 | + method: 'DELETE' |
| 79 | + }); |
| 80 | + |
| 81 | + const result = await response.json(); |
| 82 | + if (result.success) { |
| 83 | + loadForwarders(); |
| 84 | + } else { |
| 85 | + alert('Failed to delete forwarder'); |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + console.error('Error deleting forwarder:', error); |
| 89 | + alert('Error deleting forwarder'); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +async function toggle2FA() { |
| 94 | + const action = confirm('Toggle 2FA status?'); |
| 95 | + if (!action) return; |
| 96 | + |
| 97 | + try { |
| 98 | + const response = await fetch('/setup-2fa', { |
| 99 | + method: 'POST', |
| 100 | + headers: { |
| 101 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 102 | + }, |
| 103 | + body: 'enable=true' |
| 104 | + }); |
| 105 | + |
| 106 | + if (response.redirected) { |
| 107 | + window.location.reload(); |
| 108 | + } else { |
| 109 | + const data = await response.json(); |
| 110 | + if (data.qr_code) { |
| 111 | + document.getElementById('qrCode').innerHTML = |
| 112 | + `<img src="data:image/png;base64,${data.qr_code}" alt="QR Code">`; |
| 113 | + document.getElementById('totpSecret').textContent = data.secret; |
| 114 | + document.getElementById('qrModal').style.display = 'flex'; |
| 115 | + } |
| 116 | + } |
| 117 | + } catch (error) { |
| 118 | + console.error('Error toggling 2FA:', error); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function closeModal() { |
| 123 | + document.getElementById('qrModal').style.display = 'none'; |
| 124 | + window.location.reload(); |
| 125 | +} |
| 126 | + |
| 127 | +// Initialize |
| 128 | +document.addEventListener('DOMContentLoaded', () => { |
| 129 | + if (document.getElementById('createForwarderForm')) { |
| 130 | + document.getElementById('createForwarderForm').addEventListener('submit', createForwarder); |
| 131 | + loadEmailAccounts(); |
| 132 | + loadForwarders(); |
| 133 | + |
| 134 | + // Refresh every 60 seconds |
| 135 | + refreshInterval = setInterval(loadForwarders, 60000); |
| 136 | + } |
| 137 | +}); |
0 commit comments