sbbs/load/funcs.js

542 lines
12 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: '+count);
}
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;
};
}
/*
// Cant enable this - problem with frame.js, line 451. c.open not a function
// Group By
if (!Array.prototype.groupby) {
Array.prototype.groupby = function(prop) {
return this.reduce(function(groups, item) {
const val = item[prop]
groups[val] = groups[val] || []
groups[val].push(item)
return groups
}, {})
};
}
if (!Array.prototype.min) {
Array.prototype.min = function() {
return this[0];
}
}
if (!Array.prototype.max) {
Array.prototype.max = function() {
return this.reverse()[0];
}
}
if (!Array.prototype.pluck) {
Array.prototype.pluck = function(item) {
var pluck = [];
for(var x in this) {
if (this[x][item])
pluck.push(this[x][item]);
}
return pluck;
}
}
*/
/**
* Convert ANSI into BIN for loading into a Frame
*
* @param ansi
*/
function ans2bin(ansi,frame) {
var x = new Graphic;
x.ANSI = ansi;
var o = 0; // offset into 'bin'
for (var yy = 0; yy < 22; yy++) {
for (var xx = 0; xx < 80; xx++) {
frame.setData(
xx,
yy,
x.BIN.substr(o,1),
x.BIN.substr(o+1,1).charCodeAt(0) || BG_BLACK
);
o = o+2;
}
}
}
// Dynamic Field Processing
// @note bbs.atcodes() cannot process modifiers, so this function is a replacement.
function atcode(field,length,pad,context) {
'use strict';
pad = pad ? pad : ' ';
var result = '';
switch(field) {
// Get the ECHOAREA FTN AREA_TAG
case 'msg_area_areatag':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = context.msgbase.cfg.area_tag;
break;
// Get the ECHOAREA Description
case 'msg_area_desc':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = context.msgbase.cfg.description;
break;
// Oldest message in msgarea
// Our oldest message, is the first message with a tag from the headers
case 'msg_area_msgoldest_date':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.list_tagged[0];
result = x ? x.date : '';
break;
case 'msg_area_msgoldest_page':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.list_tagged[0];
return x ? context.getMessagePage(x.number) : null;
// Newest message in msgarea
// Our newest message, is the last message with a tag from the headers
case 'msg_area_msgnewest_date':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.list_tagged[context.list_tagged.length-1];
result = x ? x.date : '';
break;
case 'msg_area_msgnewest_page':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.list_tagged[context.list_tagged.length-1];
return x ? context.getMessagePage(x.number) : null;
// First unread message
case 'msg_area_msgunread_date':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.newMsgs();
result = x.length ? x.shift().date : '';
break;
case 'msg_area_msgunread_page':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.newMsgs();
return x.length ? context.getMessagePage(x.shift().number) : null;
// First unread message to me
case 'msg_area_msgotome_date':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.newMsgsToMe();
result = x.length ? x.shift().date : '';
break;
case 'msg_area_msgtome_page':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
var x = context.newMsgsToMe();
return x.length ? context.getMessagePage(x.shift().number) : null;
// Count of unread messages
case 'msg_area_new':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = ''+context.newMsgs().length;
break;
// Count of unread messages to me
case 'msg_area_newtome':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = ''+context.newMsgsToMe().length;
break;
// Is this message area in my new scan list
case 'msg_area_newscan':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = (context.getUserStats().scan_ptr & SCAN_CFG_TOYOU) ? 'YES' : 'NO';
break;
// Is this message area in my new scan list
case 'msg_area_pending':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = ''+context.list_untagged.length;
break;
// Get the ECHOAREA Total Number of Messages
case 'msg_area_total':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = ''+context.msgbase.total_msgs;
break;
// Get the ECHOAREA Group Name
case 'msg_grp_name':
if (typeof context !== 'object') {
log(LOG_ERROR,'Unable to render ['+field+'], no context provided');
break;
}
result = context.zone_name;
break;
case 'nodeid':
result = getNodeID();
break;
default:
result = bbs.atcode(field);
}
if ((result === null) || (typeof result === 'undefined'))
result = '';
length = length ? length : result.length;
//log(LOG_DEBUG,' - result length ['+result.length+'] desired ('+length+')');
if (result.length < Math.abs(length))
result = (length < 0) ? padright(result,Math.abs(length),pad) : padleft(result,length,pad);
else if (result.length > Math.abs(length))
result = result.substr(0,Math.abs(length));
log(LOG_DEBUG,'- ATCODE ['+field+'] ('+length+'|"'+pad+'") returns ['+result+']');
return result;
}
/**
* 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
* @param abort
*/
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);
}
}
/**
* Returns the current node number
*
* @returns {string}
*/
function getNodeID() {
var regex = new RegExp('^'+SYSTEM_ZONE+':');
for each (var addr in system.fido_addr_list)
{
if (regex.test(addr)) {
addr = addr.replace(regex,'');
matches = addr.split('/',2);
break;
}
}
return padright(matches[0],3,'0')+padright(matches[1],3,'0')+padright(bbs.node_num,2,'0');
}
function getPageOwners() {
// 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 logoans = f.iniGetValue('prefix','logoans');
var logovtx = f.iniGetValue('prefix','logovtx');
var users = f.iniGetValue('prefix','user');
//log(LOG_DEBUG,'+ pageOwner: users='+JSON.stringify(users));
pageowners.push({prefix: 0,logoans: logoans,logovtx: logovtx,user:users});
f.iniGetSections('prefix:').forEach(function (prefix) {
var p = parseInt(prefix.substr(7));
var logoans = f.iniGetValue(prefix,'logoans','');
var logovtx = f.iniGetValue(prefix,'logovtx','');
var users = f.iniGetValue(prefix,'user','');
//log(LOG_DEBUG,'+ pageOwner: users='+JSON.stringify(users));
pageowners.push({prefix: p,logoans: logoans,logovtx: logovtx,user:users});
});
}
f.close();
// Sort the pageowners ascending
pageowners.sort(function(a,b) { return (a.prefix < b.prefix) ? 1 : ((b.prefix < a.prefix) ? -1 : 0); });
//log(LOG_DEBUG,'+ pageOwner: pageowners='+JSON.stringify(pageowners));
}
return pageowners;
}
function loadOptions(option) {
var f = new File(file_cfgname(system.mods_dir,'ansitex/ctrl/videotex.ini'));
if (! f.open('r')) {
return undefined;
}
val = f.iniGetObject(option);
f.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);
}
// Right Pad a string with char c
function padright(n,width,c) {
c = c || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(c) + n;
}
// Left Pad a string with char c
function padleft(n,width,c) {
c = c || '0';
n = n + '';
return n.length >= width ? n : n+new Array(width - n.length + 1).join(c);
}
/**
* 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) {
var BreakException = {};
var o = null;
try {
getPageOwners().forEach(function(owner) {
var p = owner.prefix.toString();
o = owner;
var re = new RegExp('^' + p, 'g');
if (page.toString().match(re)) {
//log(LOG_DEBUG,'= pageOwner: p='+p+',o: '+o);
throw BreakException;
}
});
} catch (e) {
if (e !== BreakException) throw e;
}
//log(LOG_DEBUG,'+ pageOwner: page='+page+', owner: '+JSON.stringify(o));
return o;
}
/**
* Can the user edit the frame
*
* @param page
* @param user
*/
function pageEditor(page) {
//log(LOG_DEBUG,'+ pageEditor: page='+page+', user #'+user.number);
var BreakException = {};
var pageditor = false;
try {
getPageOwners().forEach(function(owner) {
var p = owner.prefix.toString();
//log(LOG_DEBUG,' - pageEditor: '+JSON.stringify(owner));
frameusers = owner.user ? owner.user.toString().split(',') : [1];
log(LOG_DEBUG,' - pageEditor: p='+p+'('+p.length+') user ['+JSON.stringify(frameusers)+'] - :'+frameusers.indexOf(user.number.toString()));
var re = new RegExp('^' + p, 'g');
if (page.toString().match(re) && (frameusers.indexOf(user.number.toString()) !== -1)) {
pageditor = true;
throw BreakException;
}
});
} catch (e) {
if (e !== BreakException) throw e;
}
log(LOG_DEBUG,'+ pageEditor: page='+page+', editor: '+JSON.stringify(pageditor));
return pageditor;
}
/**
* This function returns a list of zones used by this system.
*/
function zones() {
var z = [];
var ftn = /([0-9]+):([0-9]+)\/([0-9]+)(\.([0-9]+))?/;
for(var g=0;g<system.fido_addr_list.length;g++) {
z.push(parseInt(system.fido_addr_list[g].match(ftn)[1]));
}
return z.sort(function(a,b) {
return (a>b);
});
}
this;