sbbs/mods/ansitex_save.js

115 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-10-03 04:08:48 +00:00
load('texdefs.js');
load('texfuncs.js');
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 = getFrame({frame: p,index: i});
if (! send || ! frame || file) {
if (! frame) {
frame = new Frame(p,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)});
if (frame.key.length != 10) {
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 = 1;
if (argv.indexOf('-A') >= 0)
frame.isAccessible = 1;
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
2019-10-22 10:57:25 +00:00
saveFrame(frame);
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.
2019-10-20 11:31:15 +00:00
if (send == 1 && options.gpg_key) {
if (! file) {
file = system.text_dir+'ansitex/'+frame.page+'.tex';
}
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+'.gpg'))
file_remove(file+'.gpg')
2019-10-20 11:31:15 +00:00
result = system.exec('gpg --homedir /opt/sbbs/mods/keys --batch --local-user '+options.gpg_key+' -s '+file);
2019-10-15 10:48:16 +00:00
w = new File(file+'.gpg');
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);
}