sbbs/load/msgbases.js

247 lines
5.6 KiB
JavaScript
Raw Normal View History

const PAGE_LENGTH = 4; // The size of our page tag.
const PAGE_LAST_KEY = 'last_page';
const MAX_PAGE_NUM = 9999;
// Our message bases
function MsgAreas() {
'use strict';
this.areas = [];
this.areas_excluded = [];
var zone_id;
var zone_name;
var ma;
for(var g in msg_area.grp_list) {
if (msg_area.grp_list[g].name.indexOf(':') !== -1) {
zone_id = msg_area.grp_list[g].name.split(':')[0];
zone_name = msg_area.grp_list[g].name.split(':')[1];
for (var a in msg_area.grp_list[g].sub_list) {
if (msg_area.grp_list[g].sub_list[a].name.indexOf(':') !== -1) {
ma = new MsgArea();
ma.zone_id = zone_id;
ma.zone_name = zone_name;
ma.area_id = msg_area.grp_list[g].sub_list[a].name.split(':')[0];
ma.area_name = msg_area.grp_list[g].sub_list[a].name.split(':')[1];
ma.code = msg_area.grp_list[g].sub_list[a].code;
this.areas.push(ma);
} else {
this.areas_excluded.push(zone_name+':'+msg_area.grp_list[g].sub_list[a].name);
}
}
} else {
zone_name = msg_area.grp_list[g].name;
for (var a in msg_area.grp_list[g].sub_list) {
this.areas_excluded.push(zone_name+':'+msg_area.grp_list[g].sub_list[a].name);
}
}
}
}
function MsgArea() {
this.zone_id = undefined;
this.zone_name = undefined;
this.area_id = undefined;
this.area_name = undefined;
this.msgbase = undefined;
this.headers = undefined;
this.tagged_list = undefined;
this.untagged_list = undefined;
this.grp_number = undefined;
this.subnum = undefined;
Object.defineProperty(this,'code',{
set: function(code) {
this.msgbase = new MsgBase(code);
if (! this.msgbase.open()) {
writeln(code+' cannot be opened:'+this.msgbase.error);
exit(2);
}
this.headers = this.msgbase.get_all_msg_headers(false,false);
this.msgbase.close();
}
});
// Get Area's full name
Object.defineProperty(this,'full_name',{
get: function() {
return this.zone_name+':'+this.area_name;
}
});
// Total Messages
Object.defineProperty(this,'list',{
get: function() {
return this.headers ? Object.keys(this.headers) : [];
}
});
// Total tagged messages
Object.defineProperty(this,'list_tagged',{
get: function() {
if (this.tagged_list === undefined) {
this.tagged_list = [];
if (! this.headers)
return this.tagged_list;
for(var x in this.headers) {
if (this.headers[x].tags && (this.headers[x].tags.length === PAGE_LENGTH)) {
this.tagged_list.push(this.headers[x]);
write(); // @todo This is needed for this to work?
}
}
}
return this.tagged_list;
}
});
// List untagged messages
Object.defineProperty(this,'list_untagged',{
get: function() {
if (this.untagged_list === undefined) {
this.untagged_list = [];
if (! this.headers)
return this.untagged_list;
for(var x in this.headers) {
if ((! this.headers[x].tags) || (this.headers[x].tags.length !== PAGE_LENGTH)) {
this.untagged_list.push(this.headers[x]);
write(); // @todo This is needed for this to work?
}
}
}
return this.untagged_list;
}
});
// Get Next page number
Object.defineProperty(this,'page_next',{
get: function() {
var f = new File(file_cfgname(system.mods_dir,'ansitex/ctrl/videotex.ini'));
if (! f.open('r')) {
writeln('Unable to open ini file');
exit(2);
}
var page = f.iniGetValue('zone:'+this.page_prefix,PAGE_LAST_KEY)
f.close();
return page ? page : '0'.repeat(PAGE_LENGTH);
},
set: function(page) {
var f = new File(file_cfgname(system.mods_dir,'ansitex/ctrl/videotex.ini'));
if (! f.open('r+')) {
writeln('Unable to open ini file');
exit(2);
}
f.iniSetValue('zone:'+this.page_prefix,PAGE_LAST_KEY,(''+page).padStart(4,'0'));
f.close();
}
});
// Our page prefix for this msg area
Object.defineProperty(this,'page_prefix',{
get: function() {
return ''+this.zone_id+this.area_id;
},
});
}
/**
* Get a specific message with a tag
*/
MsgArea.prototype.getMessage = function(page) {
var msg = undefined;
for(var x in this.headers) {
if (this.headers[x].tags === page) {
msg = this.headers[x];
break;
}
write(); // @todo This is needed for this to work?
}
if (! msg)
return undefined;
if (! this.msgbase.open()) {
writeln(code+' cannot be opened:'+this.msgbase.error);
return undefined;
}
msg.grp_number = this.msgbase.cfg.grp_number;
var cfg = this.msgbase.cfg;
msg.subnum = msg_area.grp_list[cfg.grp_number].sub_list.filter(function(x) { return x.number === cfg.number; }).pop().index;
msg.content = this.msgbase.get_msg_body(false,msg.number,false,false,true,true);
this.msgbase.close();
return msg;
}
/**
* Tag messages with a frame number
*
* @returns {boolean}
*/
MsgArea.prototype.tag_msgs = function() {
const MAX_PAGE_NUM = 9999;
var msgs = this.list_untagged;
// See if we need to something
if (! msgs.length)
return;
if (! this.msgbase.open()) {
writeln(code+' cannot be opened:'+this.msgbase.error);
return false;
}
var page_next = this.page_next;
for(var x in msgs) {
msgs[x].tags = (''+(page_next)).padStart(4,'0');
if(! this.msgbase.put_msg_header(msgs[x].number,msgs[x])) {
writeln('ERROR:'+this.msgbase.error);
} else {
page_next++;
if (page_next > MAX_PAGE_NUM)
page_next = 0;
}
}
this.msgbase.close();
this.page_next = page_next;
return true;
}
MsgAreas.prototype.getMessage = function(page) {
if (page === undefined)
return undefined;
var zone = page.substr(0,4);
var echo = page.substr(4,2);
var page = page.substr(6);
area = this.areas.filter(function(x) {
return x.zone_id === zone && x.area_id === echo;
})[0]
return area ? area.getMessage(page) : undefined;
}