sbbs/tools/save.js

120 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-12-09 06:19:33 +00:00
// ANSItex specific includes
2020-02-26 11:54:39 +00:00
load('ansitex/load/defs.js');
load('ansitex/load/funcs.js');
2019-10-03 04:08:48 +00:00
2019-10-20 11:31:15 +00:00
var options = loadOptions();
// Modes of operation:
// -s -p x -i y = To send a stored page via the msgbase
// {-s} -p x -i y -f file = To update (and optionally send) a page with new content
// -all details = to create a new page
2019-10-15 10:48:16 +00:00
// Import
send = (argv.indexOf('-s') >= 0);
// Page
p = getArg('-p','No page specified with -p',true);
// Index
i = getArg('-i','No index specified with -i',true);
// File to convert
file = getArg('-f','No file specified with -f',false);
frame = new TexFrame();
2020-03-26 06:22:46 +00:00
frame.load(pageStr({frame: p,index: i}));
if (! send || ! frame || file) {
2022-04-16 05:36:17 +00:00
if (frame.page === null) {
frame = new TexFrame();
2020-03-26 06:22:46 +00:00
frame.frame = p;
frame.index = i;
}
2019-10-15 10:48:16 +00:00
// Key
key = getArg('-k','No index specified with -k',false);
// Cost
cost = getArg('-c','No index specified with -c',false);
// Owner
owner = getArg('-o','No owner specified with -o',false);
// Owner
type = getArg('-t','No type specified with -t',false);
2019-10-03 04:08:48 +00:00
if (file) {
f = new File(file);
if (! f.exists || ! f.open('r')) {
log(LOG_ERROR,'! ERROR: Unable to open ['+file+']');
exit(1);
}
frame.content = base64_encode(f.read());
f.close();
2019-10-15 10:48:16 +00:00
}
2019-10-03 04:08:48 +00:00
if (owner) {
frame.owner = base64_encode(owner.replace(/\\1/g,"\1"));
}
2019-10-15 10:48:16 +00:00
if (key) {
frame.key = key.split(',').map(function(t){return parseInt(t)});
2022-04-16 05:36:17 +00:00
if (frame.key.length !== 10) {
2019-10-15 10:48:16 +00:00
log(LOG_ERROR,'! ERROR: Must specify 10 keys with -k');
exit(1);
}
}
2019-10-03 04:08:48 +00:00
2019-10-15 10:48:16 +00:00
// Public
if (argv.indexOf('-P') >= 0)
frame.isPublic = true;
if (argv.indexOf('-A') >= 0)
frame.isAccessible = true;
2019-10-15 10:48:16 +00:00
if (cost)
frame.cost = cost;
2019-10-03 04:08:48 +00:00
if (type)
frame.type = type; // @todo validate this is a valid type.
2019-10-15 10:48:16 +00:00
// Date
frame.date = new Date().toISOString();
2019-10-03 04:08:48 +00:00
// Final validation
if (! frame.owner) {
log(LOG_ERROR,'! ERROR: No owner specified ['+file+']');
exit(1);
}
2019-10-15 10:48:16 +00:00
// Store the frame in file
2020-07-17 14:36:49 +00:00
frame.save();
2019-10-03 04:08:48 +00:00
}
2019-10-15 10:48:16 +00:00
// @NOTE: We need to use a binary signature then base64 encode it, as mailers may strip 0x0a while messages are in transit.
2022-04-15 11:35:01 +00:00
if (send === 1 && options.gpg_key) {
2019-10-20 11:31:15 +00:00
if (! file) {
2020-02-26 11:54:39 +00:00
file = system.mods_dir+'ansitex/text/'+frame.page+'.tex';
2019-10-20 11:31:15 +00:00
}
2019-10-15 10:48:16 +00:00
if (! file_exists(file)) {
log(LOG_ERROR,'! ERROR: File '+file+' doesnt exist?');
exit(1);
}
if (file_exists(file+'.asc'))
file_remove(file+'.asc')
2019-10-15 10:48:16 +00:00
2020-02-26 11:54:39 +00:00
result = system.exec('gpg --homedir '+system.mods_dir+'ansitex/keys --clearsign --batch --local-user '+options.gpg_key+' -s '+file);
w = new File(file+'.asc');
2019-10-15 10:48:16 +00:00
if (w.open('r')) {
msg = base64_encode(w.read());
} else {
log(LOG_ERROR,'! ERROR: Unable to send with GPG for '+frame.page + ' Error: '+w.error);
exit(1);
}
w.close();
2019-10-03 04:08:48 +00:00
2019-10-20 11:31:15 +00:00
msgBaseImport(null,frame.page,msg);
2019-10-15 10:48:16 +00:00
printf('GPG Result: %s',result);
2020-02-26 11:54:39 +00:00
}