aboutsummaryrefslogtreecommitdiff
path: root/html/mail.html
blob: f363dfee35d245f410d7f7ecc2a76dbd51314ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<html>

<head>
    <title>mail</title>
    <script src="/src/bundle.js"></script>
    <style>
        .split {
            height: 100%;
            width: 50%;
            position: fixed;
            z-index: 1;
            top: 0;
            overflow-x: hidden;
            padding-top: 20px;
        }
    </style>
</head>

<body onload="load()">
    <script>
        const sid = makeid(20)
        window.sid = sid
        function setCookie(name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toUTCString();
            }
            document.cookie = name + "=" + (value || "") + expires + "; path=/";
        }
        function getCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
        function eraseCookie(name) {
            document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        }
        function makeid(length) {
            var result = '';
            var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            var charactersLength = characters.length;
            for (var i = 0; i < length; i++) {
                result += characters.charAt(Math.floor(Math.random() *
                    charactersLength));
            }
            return result;
        }
        function sendenc(location, content) {
            var promise = new Promise((resolve, reject) => {
                var xhr = new XMLHttpRequest();
                xhr.open("POST", window.location.href + location, true);
                xhr.setRequestHeader('Content-Type', 'application/json');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == XMLHttpRequest.DONE) {
                        console.log('recived')
                        pub = new TextDecoder().decode(kekw.decrypt(JSON.parse(xhr.responseText).data));
                        console.log('decrypted')
                        resolve(pub)
                    }
                }
                let out = {}
                Object.assign(out, { json: true, enc: true, sid: sid }, { data: nodersa(pub, 'pkcs8-public').encrypt({ data: content, date: new Date() }, 'base64') })
                xhr.send(JSON.stringify(out))
            })
            return (promise)
        }
        function sendnoenc(location, content) {
            var promise = new Promise((resolve, reject) => {
                var xhr = new XMLHttpRequest();
                xhr.open("POST", window.location.origin + location, true);
                xhr.setRequestHeader('Content-Type', 'application/json');
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == XMLHttpRequest.DONE) {
                        resolve(xhr.responseText);
                    }
                }
                let out = {}
                Object.assign(out, { json: true, enc: false }, content)
                xhr.send(JSON.stringify(out))
            })
            return (promise)
        }
        document.onkeypress = function (key) {
            key = key || window.event;
            if (key.key == "Enter") {
                submit()
            }
        };
        let emails = []
        function update() {
            //console.log('hi')
            sendenc('/get/update', { 'user': 'root', 'requested': 0, 'login_key': getCookie('login_key') }).then(res => {
                res = JSON.parse(res)
                console.log('parsed')
                emails = res.reverse()
            })
        }
        function init() {
            //console.log('hi')
            sendenc('/get/storage', { 'user': 'root', 'pass': 'password', 'requested': 0, 'login_key': getCookie('login_key') }).then(res => {
                res = JSON.parse(res)
                console.log('parsed')
                emails = res.reverse()
            })
        }
        let mypriv, mypub, pub, kekw
        let preview = -1
        async function lm(index) {
            console.log()
            document.getElementById('view').innerHTML = emails[index]['body[1]']
        }
        async function load() {

            kekw = await nodersa({ b: 512 })
            mypriv = await kekw.exportKey('pkcs1-private')
            mypub = await kekw.exportKey('pkcs8-public')
            pub = await sendnoenc('/pub.key', { sid: sid, pub: mypub })
            if (getCookie('login_key') == null) {
                window.location.href = '/'
            } else {
                init()
            }
            setInterval(() => {
                let ret = ''
                let evo = false

                for (let email of emails) {
                    let c = '#2C3333'
                    if (evo) {
                        c = '#395B64'
                    }
                    evo = !evo
                    ret += '<div onclick="lm(' + emails.indexOf(email) + ')" style="white-space: pre-wrap;color:#A5C9CA;border-radius:10px;max-width:40%;min-width:400px;padding:20px;background-color:' + c + ';">'
                    ret += '<tt><b><font size="4">sub:' + email.envelope.subject + '</font></b></br>frm:' + email.envelope.from[0].address + '</br><sub style="color:#E7F6F2;">' + email.envelope.date + '</sub></tt></br></div><div style="height:2px;"></div>'
                }

                document.getElementById('emails').innerHTML = ret
            }, 5000)
        }
        //<button onclick="update()">update mail</button>
    </script>
    <div id="emails"></div>
    <div id="box" class="split" style="white-space: pre-wrap;background-color:red;right:0;margin:40px;">
        <div id="view">
        </div>
    </div>
</body>


</html>