/** * 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; /* 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; } 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; this.baselineClear(); return result; } /** * 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... if (! this.get(new PageObject(MAIL_TEMPLATE_FRAME),ext)) { this.page = new Page(); log(LOG_ERROR,'Echomail template missing :['+JSON.stringify(MAIL_TEMPLATE_FRAME)+'] ?'); 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"; } 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 'FROM': return msg.from.substr(0,length); case 'SUBJECT': return msg.subject.substr(0,length); case 'TO': return msg.to.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); //console.clear(null,false); 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; }