sbbs/mods/ansitex_save.js
2019-10-15 21:48:16 +11:00

115 lines
2.6 KiB
JavaScript

load('texdefs.js');
load('texfuncs.js');
// Import
send = getArg('-s','Use -s 1 to send',false);
file = getArg('-S','Use -S filename',false);
frame = '';
if (! send || ! file) {
// File to convert
file = getArg('-f','No file specified with -f',true);
// Page
p = getArg('-p','No page specified with -p',true);
// Index
i = getArg('-i','No index specified with -i',false);
// 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',true);
// Import
send = getArg('-s','Use -s 1 to send',false);
f = new File(file);
if (! f.exists || ! f.open('r')) {
log(LOG_ERROR,'! ERROR: Unable to open ['+file+']');
exit(1);
}
frame = new Frame(p,i);
frame.owner = base64_encode(owner.replace(/\\1/g,"\1"));
frame.content = base64_encode(f.read());
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);
}
}
// Public
frame.isPublic = (argv.indexOf('-P') >= 0);
frame.isAccessible = (argv.indexOf('-A') >= 0);
if (cost)
frame.cost = cost;
// Date
frame.date = new Date().toISOString();
// Store the frame in file
file = system.text_dir+'ansitex/'+frame.page+'.tex';
w = new File(file);
if (! w.open('w')) {
log(LOG_ERROR,'! ERROR: Unable to create TEX file for '+frame.page);
exit(1);
}
w.write(JSON.stringify(frame));
w.close();
printf('Saved file: %s.tex',frame.page);
}
// @NOTE: We need to use a binary signature then base64 encode it, as mailers may strip 0x0a while messages are in transit.
if (send == 1) {
if (! file_exists(file)) {
log(LOG_ERROR,'! ERROR: File '+file+' doesnt exist?');
exit(1);
}
if (! frame) {
f = new File(file);
if (! f.open('r')) {
log(LOG_ERROR,'! ERROR: Unable to open '+file);
exit(1);
}
try {
frame = JSON.parse(f.read());
x = new Frame(0);
frame.render = x.render;
// @todo Figure out how to delete this duplicate code
Object.defineProperty(frame,'page', {
get: function() {return this.frame+this.index}
});
} catch (error) {
log(LOG_ERROR,error);
exit(1);
}
f.close();
}
if (file_exists(file+'.gpg'))
file_remove(file+'.gpg')
result = system.exec('gpg --homedir /opt/sbbs/mods/keys --batch --local-user 516@videotex -s '+file);
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();
msgBaseExport(null,frame.page,msg);
printf('GPG Result: %s',result);
}