sbbs/load/funcs.js
2020-07-18 00:36:49 +10:00

212 lines
4.3 KiB
JavaScript

// Array of page owners
pageowners = [];
// String repeat.
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
return rpt;
};
}
/**
* Turn off the cursor
*/
function cursorOff() {
ansi.send('ext_mode','clear','cursor');
console.gotoxy(0,24);
}
/**
* Find a message base by code
*
* @param code
* @returns {number | string|boolean}
*/
function findMsgBase(code) {
if (! code)
code = "tex_data";
// Find the actual sub-code
var sub_code;
for (var s in msg_area.sub) {
var sub = msg_area.sub[s];
if (sub.code.substr(-code.length).toLowerCase() == code)
return sub.code;
}
return false;
}
/**
* Return an argument from argv, or an error if it doesnt exit
*
* @param key
* @param error
*/
function getArg(key,error,abort) {
index = argv.indexOf(key);
if ((index !== -1) && (! (argv[index+1] === undefined || argv[index+1].match(/^-/)))) {
return argv[index+1];
}
if (abort) {
log(LOG_ERROR,error);
exit(1);
}
}
function loadOptions() {
ini = new File(file_cfgname(system.mods_dir,'ansitex/ctrl/videotex.ini'));
if (!ini.open("r")) {
return undefined;
}
val = ini.iniGetObject(null);
ini.close();
return val;
}
function msgBaseImport(msgbase,page,text) {
var msgbase = new MsgBase(findMsgBase(msgbase));
log(LOG_DEBUG,'Sending ['+page+'] to message base ['+msgbase.cfg.code+']');
var hdr = { to:'All', from:'Videotex', subject:'Frame: '+page };
var body = '';
body += text+"\r\n";
body += "--- " + js.exec_file + " " + '1.0' + "\r\n";
return msgbase.save_msg(hdr, body);
}
/**
* Return the frame as a string
*/
function pageStr(page) {
if (page.frame==null)
return null;
if (! page.index)
page.index = 'a';
return page.frame.toString()+page.index;
}
/**
* Read our videotex.ini configuration and determine who owns a page.
* If there is no prefix for the page, it is owned by the system '0'
*
* @param page
* @returns {undefined}
*/
function pageOwner(page) {
// Load the owner configuration into memory
if (! pageowners.length) {
var f = new File(file_cfgname(system.mods_dir,'ansitex/ctrl/videotex.ini'));
if (f.open("r")) {
var logo = f.iniGetValue('prefix','logo');
pageowners.push({prefix: 0,logo: logo});
f.iniGetSections('prefix:').forEach(function (prefix) {
var p = parseInt(prefix.substr(7));
var logo = f.iniGetValue(prefix,'logo','');
pageowners.push({prefix: p,logo: logo});
});
}
f.close();
// Sort the pageowners ascending
pageowners.sort(compare);
log(LOG_DEBUG,'+ pageOwner: pageowners='+JSON.stringify(pageowners));
}
var pageowner = o = null;
pageowners.forEach(function(owner) {
var p = owner.prefix.toString();
o = owner;
log(LOG_DEBUG,'- pageOwner: p='+p+'('+p.length+') ,o: '+o);
match = '';
var re = new RegExp('^' + p, 'g');
if (page.toString().match(re) && (p.length > match.length)) {
match = p;
pageowner = o;
log(LOG_DEBUG,'= pageOwner: p='+p+',o: '+o);
}
});
log(LOG_DEBUG,'+ pageOwner: page='+page+', owner: '+JSON.stringify(pageowner ? pageowner : o));
return pageowner ? pageowner : o;
}
function compare(a,b) {
return (a.prefix < b.prefix) ? 1 : ((b.prefix < a.prefix) ? -1 : 0);
}
/**
* Send a message to the baseline.
*
* @param text
* @param reposition
*/
function sendBaseline(text,reposition) {
console.pushxy();
console.gotoxy(0,24);
console.print(text);
console.cleartoeol();
if (! reposition) {
console.popxy();
}
}
this;