notes/cli/node/app.js

218 lines
4.2 KiB
JavaScript
Raw Normal View History

2023-07-11 14:48:03 +02:00
const axios = require("axios");
const fs = require("fs");
2023-08-24 17:19:03 +02:00
const openpgp = require("openpgp");
2023-07-11 14:48:03 +02:00
var cp = require("child_process");
var settings = JSON.parse(fs.readFileSync("settings.json", { encoding: "utf8", flag: "r" }));
2023-08-24 17:19:03 +02:00
var pgpkey = fs.readFileSync("key.acs", { encoding: "utf8", flag: "r" });
2023-07-12 18:07:32 +02:00
var intervalid = null;
var notes = null;
var currentnote = null;
2023-09-10 11:24:06 +02:00
var command = null;
function timestamp()
{
var utc = new Date();
var loc = new Date(utc - utc.getTimezoneOffset() * 60 * 1000);
return loc.toISOString().replace("T", " ").replace(/\..*/, "").replace(/:/g, ".");
}
2023-07-12 18:07:32 +02:00
2023-08-25 17:44:24 +02:00
function simplifystring(str)
{
return str.toLowerCase().normalize('NFD').replace(/\p{Diacritic}/gu, "");
}
2023-09-11 14:28:23 +02:00
function decrypt(str)
2023-08-24 17:19:03 +02:00
{
2023-09-11 14:28:23 +02:00
var keystring = pgpkey.split("-----END PGP PUBLIC KEY BLOCK-----")[1];
var key = null;
return openpgp.readKey({ armoredKey: keystring })
.then(privateKey =>
{
key = privateKey;
return openpgp.readMessage({ armoredMessage: str });
})
.then(message =>
{
return openpgp.decrypt({
message: message,
decryptionKeys: key })
})
.then(decrypted =>
{
var chunks = [];
for (const chunk of decrypted.data) {
chunks.push(chunk);
}
return chunks.join('');
});
2023-08-24 17:19:03 +02:00
}
2023-09-11 14:28:23 +02:00
function encrypt(str)
2023-08-24 17:19:03 +02:00
{
2023-09-11 14:28:23 +02:00
var keystring = pgpkey.split("-----BEGIN PGP PRIVATE KEY BLOCK-----")[0];
var key = null;
return openpgp.readKey({ armoredKey: keystring })
.then(publicKey =>
{
key = publicKey;
return openpgp.createMessage({ text: str });
})
.then(message =>
{
return openpgp.encrypt({
message: message,
encryptionKeys: key });
})
2023-08-24 17:19:03 +02:00
}
2023-09-11 14:28:23 +02:00
function saveifneeded()
{
var newcontent = fs.readFileSync("note.md", { encoding: "utf8", flag: "r" });
if (currentnote.content != newcontent)
{
currentnote.content = newcontent;
notes.splice(notes.indexOf(currentnote), 1);
notes.unshift(currentnote);
console.log("sending data file to server...");
2023-09-11 14:28:23 +02:00
encrypt(JSON.stringify(notes))
.then(encrypted =>
{
2023-09-11 14:28:23 +02:00
return axios.post(`${settings.url}/handler.php`,
{
action: "push",
password: settings.password,
data: encrypted
},
{
2023-09-11 14:28:23 +02:00
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
});
})
.then(res => {
console.log("...done.");
});
}
2023-09-10 11:24:06 +02:00
else if (!intervalid)
{
console.log("no change.");
}
}
2023-09-10 11:24:06 +02:00
function editnote()
{
2023-09-10 11:24:06 +02:00
fs.writeFileSync("note.md", currentnote.content);
2023-09-11 14:28:23 +02:00
cp.exec(`${settings.command} note.md`,
function (err, stdout, stderr)
{
2023-09-10 11:24:06 +02:00
clearInterval(intervalid);
intervalid = null;
saveifneeded();
});
intervalid = setInterval(saveifneeded, 10000);
}
// Run part
2023-09-10 11:24:06 +02:00
if (process.argv.length <= 2)
{
command = "list";
}
else
{
command = process.argv[2];
}
2023-07-11 14:48:03 +02:00
axios.post(`${settings.url}/handler.php`,
{
action: "fetch",
password: settings.password
},
{
headers:
{
"Content-type": "application/x-www-form-urlencoded"
}
})
2023-09-11 14:28:23 +02:00
.then(function(res)
2023-07-11 14:48:03 +02:00
{
2023-09-11 14:28:23 +02:00
return decrypt(res.data);
})
.then(json =>
{
notes = JSON.parse(json);
2023-09-10 11:24:06 +02:00
switch (command)
2023-07-11 14:48:03 +02:00
{
2023-09-10 11:24:06 +02:00
case "help":
case "-h":
case "--help":
var appcmd = "notes";
console.log(`list notes: ${appcmd} [list]`);
console.log(`edit a note: ${appcmd} [open|edit] <title|index>`);
console.log(`create a note: ${appcmd} new|create|add [<title>]`);
console.log(`display help: ${appcmd} help|-h|--help`);
break;
case "new":
case "create":
case "add":
var title = timestamp();
if (process.argv.length > 3)
2023-07-11 18:16:51 +02:00
{
2023-09-10 11:24:06 +02:00
title = process.argv[3];
}
if (notes.find(n => n.title == title))
2023-07-12 18:07:32 +02:00
{
2023-09-10 11:24:06 +02:00
console.log(`${title}: already exists`);
}
else
{
currentnote = {
title: title,
content: ""
}
2023-09-10 11:24:06 +02:00
notes.unshift(currentnote);
console.log(`Creating new note: ${title}`);
2023-09-11 09:57:00 +02:00
editnote();
2023-09-10 11:24:06 +02:00
}
break;
case "list":
for (var i = notes.length - 1; i >= 0; i--)
{
console.log(`[${i}] ${notes[i].title}`);
}
break;
2023-09-11 09:57:00 +02:00
2023-09-10 11:24:06 +02:00
default:
var arg = command;
if (arg === "open" || arg === "edit")
{
arg = process.argv[3];
}
if (isNaN(parseInt(arg)))
{
currentnote = notes.find(n => n.title == arg);
}
else
{
currentnote = notes[parseInt(arg)];
}
if (currentnote)
{
console.log(`Editing ${currentnote.title}`);
editnote();
}
else
{
console.log(`Note ${arg} not found`);
2023-09-11 09:57:00 +02:00
}
2023-09-10 11:24:06 +02:00
break;
}
2023-07-11 14:48:03 +02:00
});