sbbs/load/session.js

188 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2022-12-09 06:19:33 +00:00
/**
* This handles session specific aspects of each frame type, eg: sending to the baseline, clearing, accepting
* input, moving around the frame (and windows within one if any)
*
* @constructor
*/
function Session() {
'use strict';
// Our page object
this.page = undefined;
this.previous = undefined;
2022-12-09 06:19:33 +00:00
/* Frame type settings */
this.settings = {};
/**
* Enable pulling out submitted value by its name
*
* @param key
* @returns {null|string|*}
*/
this.fieldValue = function(key) {
for each (var k in this.page.input_fields) {
if (k.name === key)
return k.value;
2022-12-09 06:19:33 +00:00
}
return null;
}
this.get = function(page) {
if (!(page instanceof PageObject))
throw new Error('page must be a PageObject');
this.baselineSend('LOADING');
// Just in case our new page doesnt exist
this.previous = this.page;
this.page = new Page();
var result = this.page.get(page);
// If our new page doesnt exist, reset our page back to the current
if (! result)
this.page = this.previous;
this.previous = undefined;
2022-12-09 06:19:33 +00:00
this.baselineClear();
return result;
2022-12-09 06:19:33 +00:00
}
/**
* Load a message frame
*
2023-12-27 11:24:20 +00:00
* @param pagenum
2022-12-09 06:19:33 +00:00
*/
2023-12-27 11:24:20 +00:00
this.loadMessage = function(pagenum,ext) {
log(LOG_ERROR,'Loading Message :['+pagenum+']');
2022-12-09 06:19:33 +00:00
// Load our message
var ma = new MsgAreas()
2023-12-27 11:24:20 +00:00
var area = ma.getArea(pagenum);
var msg = ma.getMessage(pagenum);
2022-12-09 06:19:33 +00:00
var msg_header;
if (! msg)
2023-12-27 11:24:20 +00:00
return false;
2022-12-09 06:19:33 +00:00
// @todo Search 1zzzzEE..., 1zzzz...
if (! this.get(new PageObject(MAIL_TEMPLATE_FRAME),ext)) {
this.page = new Page();
2023-12-27 11:24:20 +00:00
log(LOG_ERROR,'Echomail template missing :['+JSON.stringify(MAIL_TEMPLATE_FRAME)+'] ?');
2022-12-09 06:19:33 +00:00
msg_header = 'TO: '+msg.to.substr(0,72)+"\r\n";
msg_header += 'FROM: '+msg.from.substr(0,72)+"\r\n";
msg_header += 'DATE: '+msg.date.substr(0,72)+"\r\n";
msg_header += 'SUBJECT: '+msg.subject.substr(0,72)+"\r\n";
2022-12-09 06:19:33 +00:00
} else {
// @todo change this to use atcode()
2023-12-27 11:24:20 +00:00
msg_header = this.page.raw.replace(/@(.*)@/g,
2022-12-09 06:19:33 +00:00
function (str, code, offset, s) {
var length = code.split(':')[1];
switch(code.split(':')[0]) {
case 'DATE': return msg.date.substr(0,length);
case 'FROM': return msg.from.substr(0,length);
case 'SUBJECT': return msg.subject.substr(0,length);
case 'TO': return msg.to.substr(0,length);
2022-12-09 06:19:33 +00:00
}
}
);
}
2023-12-27 11:24:20 +00:00
this.page.name = new PageObject(pagenum,'a');
this.page.owner = 1;
// @todo Keys should map to next/previous/send, etc as indicated in the template frame.
this.page.key = [this.page.name.frame.substr(0,7)+'1',null,null,null,null,null,null,null,null,null];
// @todo validate that FRAME_TYPE_MESSAGE is a message template
this.page.type = FRAME_TYPE_MESSAGE;
// @todo Take the cost from the template
this.page.cost = 5;
// @todo Take the key values from the template
this.page.__properties__.isAccessible = true;
this.page.__properties__.isPublic = true;
this.page.preload(msg_header+msg.content,'txt');
2022-12-09 06:19:33 +00:00
// Update the user's pointers
2023-12-27 11:24:20 +00:00
var stats = ma.getUserStats(this.page.name.frame);
2022-12-09 06:19:33 +00:00
// if this message is to the user, and the msg number > scan_ptr and it is the next message on the user's new mail list
var newmsgs = area.newMsgsToMe();
var next;
log(LOG_DEBUG,'User has: '+newmsgs.length-1+' msgs to read to ME');
if (newmsgs.length) {
next = newmsgs[1];
2023-12-27 11:24:20 +00:00
log(LOG_DEBUG,'- NEXT is: '+next.tags+', this is: '+msg.tags);
2022-12-09 06:19:33 +00:00
if (next && (next.tags === msg.tags)) {
log(LOG_DEBUG,'- Updating scan_ptr to: '+next.number);
stats.scan_ptr = next.number;
}
// Last message
next = newmsgs[0];
if (next !== undefined) {
log(LOG_DEBUG,'- LAST TO ME is: '+next.tags);
2023-12-27 11:24:20 +00:00
this.page.key[1] = area.page(next.tags);
2022-12-09 06:19:33 +00:00
}
// Next new message
next = newmsgs[2];
if (next !== undefined) {
log(LOG_DEBUG,'- NEXT TO ME is: '+next.tags);
2023-12-27 11:24:20 +00:00
this.page.key[2] = area.page(next.tags);
2022-12-09 06:19:33 +00:00
}
}
// if this message is the next message, update last_read
newmsgs = area.newMsgs();
log(LOG_DEBUG,'User has: '+newmsgs.length+' msgs to read');
if (newmsgs.length) {
next = newmsgs[0];
log(LOG_DEBUG,'- NEXT is: '+next.tags+', this is: '+msg.tags);
if (next.tags === msg.tags) {
log(LOG_DEBUG,'- Updating last_read to: '+next.number);
stats.last_read = next.number;
}
}
// Previous Message
2023-12-27 11:24:20 +00:00
x = area.MessagePrev(this.page.name.frame);
2022-12-09 06:19:33 +00:00
if (x)
2023-12-27 11:24:20 +00:00
this.page.key[4] = area.page(x.tags);
2022-12-09 06:19:33 +00:00
// Next Message
2023-12-27 11:24:20 +00:00
x = area.MessageNext(this.page.name.frame);
2022-12-09 06:19:33 +00:00
if (x)
2023-12-27 11:24:20 +00:00
this.page.key[6] = area.page(x.tags);
log(LOG_DEBUG,'Built frame: ['+this.page.name.frame+']['+this.page.name.index+'] ('+this.page.name.toString()+')');
2022-12-09 06:19:33 +00:00
2023-12-27 11:24:20 +00:00
return true;
2022-12-09 06:19:33 +00:00
}
// Render the page
this.render = function() {
this.gotoxy(0,0);
//console.clear(null,false);
2022-12-09 06:19:33 +00:00
write(so.page.display().join(''));
}
}
/**
2023-12-27 11:24:20 +00:00
* Return a system message for a index
2022-12-09 06:19:33 +00:00
*
* @param index
* @returns {string|*}
2023-12-27 11:24:20 +00:00
* @see SessionProtocol()
2022-12-09 06:19:33 +00:00
*/
Session.prototype.getMessage = function(index) {
eval('var msg = this.settings.'+index);
return msg;
}