Onionr/static-data/www/mail/sendmail.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-02-10 22:43:42 +00:00
/*
Onionr - Private P2P Communication
2019-02-10 22:43:42 +00:00
This file handles the mail interface
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
var sendbutton = document.getElementById('sendMail')
messageContent = document.getElementById('draftText')
to = document.getElementById('draftID')
subject = document.getElementById('draftSubject')
friendPicker = document.getElementById('friendSelect')
2019-02-10 22:43:42 +00:00
2020-02-19 07:57:56 +00:00
function utf8Length(s) {
var size = encodeURIComponent(s).match(/%[89ABab]/g);
return s.length + (size ? size.length : 0);
}
function padString(string_data, round_nearest_byte_exponent = 3){
if (utf8Length(string_data) === 0){
string_data += '0'
}
let round_size = 10 ** round_nearest_byte_exponent
while (utf8Length(string_data) % round_size > 0){
string_data += '0'
}
return string_data
}
2019-10-04 21:49:35 +00:00
function sendMail(toData, message, subject){
2020-02-19 07:57:56 +00:00
let meta = {'subject': subject}
2020-02-19 07:57:56 +00:00
if (document.getElementById('messagePaddingSetting').checked){
message = padString(message)
}
if (document.getElementById('mailSignatureSetting').value !== false){
message += "\n"
message += document.getElementById('mailSignatureSetting').value
}
2020-02-19 07:57:56 +00:00
postData = {'message': message, 'to': toData, 'type': 'pm', 'encrypt': true, 'meta': JSON.stringify(meta)}
postData.forward = document.getElementById('forwardSecrecySetting').checked
2019-02-10 22:43:42 +00:00
postData = JSON.stringify(postData)
2019-03-05 03:16:33 +00:00
sendForm.style.display = 'none'
2020-02-19 07:57:56 +00:00
2019-02-10 22:43:42 +00:00
fetch('/insertblock', {
method: 'POST',
body: postData,
headers: {
"content-type": "application/json",
"token": webpass
}})
.then((resp) => resp.text()) // Transform the data into text
2019-02-10 22:43:42 +00:00
.then(function(data) {
2019-03-05 03:16:33 +00:00
sendForm.style.display = 'block'
2019-09-09 08:23:09 +00:00
PNotify.success({
text: 'Queued for sending!',
delay: 3500,
mouseReset: false
2019-09-09 08:23:09 +00:00
})
2019-10-04 21:49:35 +00:00
to.value = subject.value = messageContent.value = ""
friendPicker.value = ""
subject.value = ""
2019-02-10 22:43:42 +00:00
})
}
var friendPicker = document.getElementById('friendSelect')
friendPicker.onchange = function(){
to.value = friendPicker.value
}
2019-02-10 22:43:42 +00:00
sendForm.onsubmit = function(){
2019-09-23 22:56:05 +00:00
if (! to.value.includes("-") && to.value.length !== 56 && to.value.length !== 52){
2019-09-09 08:23:09 +00:00
PNotify.error({
text: 'User ID is not valid'
})
}
else{
sendMail(to.value, messageContent.value, subject.value)
}
return false
2019-02-10 22:43:42 +00:00
}