sbbs/load/session.js

179 lines
4.7 KiB
JavaScript

/**
* 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;
/* Frame type settings */
this.settings = {};
/**
* Enable pulling out submitted value by its name
*
* @param key
* @returns {null|string|*}
* @deprecated I think this has been superseded, by page.js, but I need to investigate
*/
this.fieldValue = function(key) {
for each (var k in this.frame_fields) {
log(LOG_DEBUG,' - k:'+JSON.stringify(k));
if (k.fname === key) {
return k.fvalue;
}
}
return null;
}
this.get = function(page) {
if (!(page instanceof PageObject))
throw new Error('page must be a PageObject');
this.baselineSend('LOADING');
this.page = new Page();
this.page.get(page);
this.baselineClear();
}
/**
* Load a message frame
*
* @param pagenum
*/
this.loadMessage = function(pagenum,ext) {
log(LOG_ERROR,'Loading Message :['+pagenum+']');
// Load our message
var ma = new MsgAreas()
var area = ma.getArea(pagenum);
var msg = ma.getMessage(pagenum);
var msg_header;
if (! msg)
return false;
// @todo Search 1zzzzEE..., 1zzzz...
this.get(new PageObject(MAIL_TEMPLATE_FRAME),ext);
if (this.page.name.toString() === null) {
log(LOG_ERROR,'Echomail template missing :['+JSON.stringify(MAIL_TEMPLATE_FRAME)+'] ?');
msg_header = 'TO: '+msg.to.substr(0,72)+"\n\r";
msg_header += 'FROM: '+msg.from.substr(0,72)+"\n\r";
msg_header += 'DATE: '+msg.date.substr(0,72)+"\n\r";
msg_header += 'SUBJECT: '+msg.subject.substr(0,72)+"\n\r";
} else {
// @todo change this to use atcode()
msg_header = this.page.raw.replace(/@(.*)@/g,
function (str, code, offset, s) {
var length = code.split(':')[1];
switch(code.split(':')[0]) {
case 'DATE': return msg.date.substr(0,length);
case 'TO': return msg.to.substr(0,length);
case 'FROM': return msg.from.substr(0,length);
case 'SUBJECT': return msg.subject.substr(0,length);
}
}
);
}
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');
// Update the user's pointers
var stats = ma.getUserStats(this.page.name.frame);
// 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];
log(LOG_DEBUG,'- NEXT is: '+next.tags+', this is: '+msg.tags);
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);
this.page.key[1] = area.page(next.tags);
}
// Next new message
next = newmsgs[2];
if (next !== undefined) {
log(LOG_DEBUG,'- NEXT TO ME is: '+next.tags);
this.page.key[2] = area.page(next.tags);
}
}
// 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
x = area.MessagePrev(this.page.name.frame);
if (x)
this.page.key[4] = area.page(x.tags);
// Next Message
x = area.MessageNext(this.page.name.frame);
if (x)
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()+')');
return true;
}
// Render the page
this.render = function() {
this.gotoxy(0,0);
write(so.page.display().join(''));
}
}
/**
* Return a system message for a index
*
* @param index
* @returns {string|*}
* @see SessionProtocol()
*/
Session.prototype.getMessage = function(index) {
eval('var msg = this.settings.'+index);
return msg;
}