Move control functions to control/ dir

This commit is contained in:
2023-12-24 17:46:37 +11:00
parent b90a058d47
commit 29f82592ac
4 changed files with 4 additions and 4 deletions

153
load/control/frameedit.js Normal file
View File

@@ -0,0 +1,153 @@
load('ansiedit.js');
load('frame.js');
load('tree.js');
load('scrollbar.js');
load('event-timer.js');
load('graphic.js');
var CONTROL_FRAMEEDIT = '1';
function edit(fo) {
log(LOG_DEBUG,'+ Control EDIT loaded');
var complete = false;
var inProperty = false;
Object.defineProperty(this,'getName', {
get: function() {
return 'Frame Edit';
}
});
Object.defineProperty(this,'isComplete', {
get: function() {
return complete;
}
});
log(LOG_DEBUG,' - Owner: '+JSON.stringify(fo.owner));
const frame = new Frame(1,1,console.screen_columns,console.screen_rows,BG_BLACK|LIGHTGRAY);
frame.gotoxy(1,1);
header = '\1n'+fo.pageownerlogo+' '.repeat(fo.settings.FRAME_HEADER-console.strlen(fo.pageownerlogo))+'\1n '+
'\1W\1H'+fo.page+' '.repeat(fo.settings.FRAME_PAGENUM-fo.page.length)+' '+
'\1G\1H'+' Edit';
frame.putmsg(header);
frame.open();
var editor = new ANSIEdit({
x: 1,
y: 2,
width: 80,
height: 23,
attr: WHITE,
//showPosition: true,
menuHeading: 'Frame Edit '+fo.page,
parentFrame: frame,
});
editor.open();
editor.menu.addItem('Properties',properties);
editor.menu.addItem('Exit',onexit);
editor.menu.addItem('Save & Exit',saveexit);
var x = new Graphic;
x.ANSI = fo.parse(base64_decode(fo.content));
log(LOG_DEBUG,' - Fields: '+JSON.stringify(fo.frame_fields));
const bin = x.BIN;
var o = 0; // offset into 'bin'
for (var yy = 0; yy < 22; yy++) {
for (var xx = 0; xx < 80; xx++) {
editor.putChar({
x : xx,
y : yy,
ch : bin.substr(o, 1),
attr : bin.substr(o + 1, 1).charCodeAt(0) || BG_BLACK
});
o = o + 2;
}
}
editor.cycle();
frame.cycle();
this.handle=function(read) {
if (! js.terminated) {
switch (ascii(read)) {
case 26:
if (inProperty) {
propFrame.close();
frame.top();
frame.cycle();
inProperty = false;
} else {
properties();
}
return '';
case 27:
if (inProperty) {
log(LOG_DEBUG, ' + FrameEdit properties(): ESC');
propFrame.close();
frame.top();
frame.cycle();
inProperty = false;
}
return '';
default:
if (inProperty) {
log(LOG_DEBUG, ' + FrameEdit properties(): read');
propFrame.putmsg(read);
propFrame.cycle();
} else {
editor.getcmd(read);
editor.cycle();
frame.cycle();
}
if (! complete)
return '';
}
}
editor.close();
frame.close();
fo.render();
return '';
}
function properties() {
inProperty = true;
log(LOG_DEBUG, '+ FrameEdit properties()');
frame.bottom();
propFrame = new Frame(1,2,40,14,BG_BLUE|WHITE,frame);
propFrame.gotoxy(1,1);
propFrame.putmsg('Properties!');
propFrame.open();
propFrame.cycle();
}
function save() {
fo.content = base64_encode(editor.exportAnsi().join(''));
fo.save();
}
function onexit() {
complete = true;
}
function saveexit() {
save();
onexit();
}
}
this;

198
load/control/register.js Normal file
View File

@@ -0,0 +1,198 @@
/**
* This handles user registration.
*
* The form must have the following fields:
* + UID The user's user id to login
* + EMAIL The users's email address - to receive tokens
* + FULLNAME The user's full name
* + PASS The users's preferred password
* + CITY The user's city
* + COUNTRY The user's country - 3 letter ISO code
* + PCODE THe user's postal code
*/
var CONTROL_REGISTER = '1';
var EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var cValChars='ACDEFHJKLMNPQRTUVWXY23456789!@$%&';
log(LOG_DEBUG,'+ Control REGISTER loaded');
function register() {
var code = '';
var complete = false;
var processed = false;
// Called before processing for a field
Object.defineProperty(this, 'getName', {
get: function () {
return 'Control-Registration';
}
});
Object.defineProperty(this, 'isComplete', {
get: function () {
return complete && processed;
}
});
this.handle=function(read) {
// Dont allow existing users to re-register
if (user.number) {
fo.baselineSend('ALREADY_MEMBER',false);
return read === '*' ? read : '';
}
log(LOG_DEBUG,'Control REGISTER handle() start. ('+read+')');
if (cf === undefined) {
log(LOG_DEBUG,' - CF not defined, returning');
return read;
}
log(LOG_DEBUG,'- Field '+cf.name+'('+JSON.stringify(cf)+')');
if ((cf.name === 'TOKEN') && (read === '#' || read === "\r")) {
if (cf.value === code) {
complete = true;
} else {
fo.baselineSend('INVALID_CODE',false);
fo.cursorOn(cf.c+cf.value.length,cf.r);
fo.attr(cf.attribute);
read = '';
}
}
log(LOG_DEBUG,'- Field Value ['+cf.value+'] ('+code+')');
return read;
}
// Make sure we got an email
this.prefield=function() {
log(LOG_DEBUG,'- prefield: Field '+cf.name+'('+JSON.stringify(cf)+')');
if (cf.name === 'TOKEN') {
if (! code.length) {
log(LOG_DEBUG,' - BASELINE '+cf.name+'('+JSON.stringify(cf)+')');
fo.baselineSend('TOKEN_EMAIL',false);
var email = fo.fieldValue('EMAIL');
var uid = fo.fieldValue('UID');
var name = fo.fieldValue('FULLNAME');
log(LOG_DEBUG,' - VALIDATE EMAIL TO ('+JSON.stringify(system.matchuserdata(U_NETMAIL,email))+')');
// Validate Email hasnt been used
// Validate USER_ID hasnt been used
if ((email.indexOf('@') === -1) || ! EMAIL_REGEX.test(email) || (system.matchuserdata(U_NETMAIL,email) !== 0)) {
fo.baselineSend('INVALID_EMAIL',false);
return;
}
if (! system.check_name(uid)) {
log(LOG_DEBUG,' - Cannot use user_id: ('+uid+')');
fo.baselineSend('INVALID_UID',false);
return;
}
var msgbase = new MsgBase('mail');
for (var i=0;i<6;i++) {
code += cValChars.substr(parseInt(Math.random()*cValChars.length),1);
}
var hdrs = new Object();
hdrs.to=name;
hdrs.to_net_type=netaddr_type(email);
if (hdrs.to_net_type !== NET_NONE) {
hdrs.to_net_addr = email;
} else {
fo.baselineSend('CANNOT_SEND_TOKEN',false);
return;
}
hdrs.from=system.name;
hdrs.from_net_addr='sysop@'+system.inet_addr;
hdrs.from_net_type=NET_INTERNET;
hdrs.subject='Registration TOKEN for '+system.name;
if (msgbase.open !== undefined && msgbase.open() === false) {
console.print("\r\n\1n\1h\1rERROR: \1y" + msgbase.last_error + "\1n \r\n");
console.pause();
msgbase.close();
bbs.hangup();
return;
}
var msg="CODE: " + code + "\n\n";
msg += 'Please use the above code to validate your login to '+system.name+'.';
if (! msgbase.save_msg(hdrs,msg)) {
console.print("\r\n\1n\1h\1rERROR: \1y" + msgbase.last_error + "\1n \r\n");
console.pause();
msgbase.close();
bbs.hangup();
return;
}
msgbase.close();
}
fo.baselineSend('TOKEN_SENT',false);
log(LOG_DEBUG,'SENT EMAIL TOKEN ('+code+') ['+JSON.stringify(hdrs)+']');
}
}
this.process=function() {
log(LOG_DEBUG,'Creating user: '+fo.fieldValue('EMAIL'));
try {
var newuser = system.new_user(fo.fieldValue('UID'));
} catch (e) {
fo.baselineSend('USER_EXISTS',false);
log(LOG_ERROR,"New user couldn't be created (user created while signing up)");
log(LOG_ERROR,JSON.stringify(e));
processed = true;
return this.isComplete;
}
if (typeof newuser === 'number') {
fo.baselineSend('USER_CREATE_ERROR',false);
log(LOG_ERROR,"New user couldn't be created (error code "+newuser+")");
processed = true;
return this.isComplete;
}
newuser.security.password = '';
if (bbs.login(newuser.alias,null)) {
user.number = newuser.number;
user.security.password = fo.fieldValue('PASS');
user.name = fo.fieldValue('FULLNAME');
user.handle = fo.fieldValue('UID');
user.location = fo.fieldValue('CITY')+', '+fo.fieldValue('COUNTRY');
user.zipcode = fo.fieldValue('PCODE');
user.netmail = fo.fieldValue('EMAIL');
user.comment = 'ANSItex registered user';
bbs.user_sync();
bbs.logon();
log(LOG_INFO,"Created user record #"+user.number+": "+user.alias);
action = ACTION_EXIT;
processed = true;
return this.isComplete;
} else {
fo.baselineSend('LOGIN_ERROR',false);
log(LOG_INFO,"bbs.login() failed");
user.comment = 'Initial login failed!';
newuser.settings |= USER_DELETED;
delete newuser;
processed = true;
return this.isComplete;
}
}
}
this;

205
load/control/sqrllogin.js Normal file
View File

@@ -0,0 +1,205 @@
/**
* This handles user registration.
*
* The form must have the following fields:
* + UID The user's user id to login
* + EMAIL The users's email address - to receive tokens
* + FULLNAME The user's full name
* + PASS The users's preferred password
* + CITY The user's city
* + COUNTRY The user's country - 3 letter ISO code
* + PCODE THe user's postal code
*/
var CONTROL_SQRL = '1';
require('http.js','HTTPRequest');
load('ansitex/load/qrcode-make.js');
load('frame.js');
log(LOG_DEBUG,'+ Control SQRL-LOGIN loaded');
// @todo This should move to handle - since we have to press 2 twice to get out.
function sqrllogin() {
var complete = false;
var cancel = false;
var sqrl = loadOptions('sqrl');
log(LOG_DEBUG,'OPTIONS: '+JSON.stringify(sqrl));
var http = new HTTPRequest();
http.SetupGet(sqrl.auth_path,undefined,sqrl.auth_url);
http.request_headers.push('Accept: application/json');
try {
http.SendRequest();
http.ReadResponse();
log(LOG_INFO,'SQRL: '+JSON.stringify(http.body));
var data = http.body
.split('')
.map(function(x) {return x.charCodeAt(0)});
var qr = qrcodegen.QrCode.encodeBinary(data,qrcodegen.QrCode.Ecc.LOW);
var subframe = new Frame((viewdata ? fo.settings.FRAME_WIDTH : fo.settings.FRAME_WIDTH-qr.size-2),2,(viewdata ? qr.size/2 : qr.size+2),22,BG_BLACK|LIGHTGRAY);
fo.qrcode(qr,subframe);
fo.baselineSend('CANCEL_MSG',false);
// Loop and see if the user has logged in
var nut = http.body.substr(http.body.indexOf('nut='),68);
} catch (err) {
log(LOG_INFO,'SQRL Error: '+err+' '+JSON.stringify(sqrl));
cancel = true;
}
// Called before processing for a field
Object.defineProperty(this, 'getName', {
get: function () {
return 'SQRL-LOGIN';
}
});
Object.defineProperty(this, 'isComplete', {
get: function () {
return complete;
}
});
// Nothing to do here
this.handle=function(read) {
log(LOG_DEBUG,'Control SQRL-LOGIN handle() start. ('+read+')');
if (read === '2') {
log(LOG_INFO,'SQRL: Cancelled with 2');
cancel = true;
} else {
log(LOG_DEBUG,'SQRL read ['+read+']');
try {
http = new HTTPRequest();
http.SetupGet(sqrl.auth_post+'?'+nut,undefined,sqrl.auth_url);
http.request_headers.push('Accept: application/json');
log(LOG_DEBUG,'Checking NUT in ['+nut+']');
http.SendRequest();
http.ReadResponse();
switch (http.response_code) {
case 404:
log(LOG_DEBUG,'- NUT not Authorised yet.');
break;
case 200:
var result = JSON.parse(http.body);
if (result.isReady) {
log(LOG_INFO,'NUT: '+result.msg);
log(LOG_INFO,'NEXT: '+result.nextPage);
if (result.msg === 'SQRL authenticated') {
log(LOG_DEBUG,'Getting Authenticated User ['+result.nextPage+']');
http = new HTTPRequest();
http.SetupGet(result.nextPage,undefined,'');
http.request_headers.push('Accept: application/json');
http.SendRequest();
http.ReadResponse();
log(LOG_DEBUG,'Getting Authenticated User Response ['+http.response_code+']');
if (http.response_code === 200) {
var sqrluser = http.body.substr(0,40);
var username = 'S'+sqrluser.substr(0,24)
log(LOG_DEBUG,'Getting Authenticated sqrluser ['+JSON.stringify(sqrluser)+']');
// Look through our user base for an existing user
var uid = system.matchuser(username);
log(LOG_DEBUG,'Getting Authenticated UID ['+JSON.stringify(uid)+']');
if (! uid) {
log(LOG_DEBUG,'New User ['+username+'] with pass ('+sqrluser+')');
var user = system.new_user(username);
log(LOG_DEBUG,'New User ['+JSON.stringify(user.number)+']');
user.name = username;
user.security.password = sqrluser;
user.handle = username.substr(0,8);
user.location = 'Earth';
user.zipcode = '000';
user.netmail = username+'@'+system.inet_addr;
user.comment = 'ANSItex registered user - with SQRL';
bbs.user_sync();
} else {
user = new User(uid);
}
log(LOG_DEBUG,'Getting Authenticated USER ['+JSON.stringify(user.number)+']');
// Existing user, we'll exit here
if (bbs.login(user.name,null,user.security.password)) {
log(LOG_DEBUG,' - User:'+JSON.stringify(user.number));
bbs.logon();
log(LOG_DEBUG,' - SEND TO EXIT:');
complete = true;
action = ACTION_EXIT;
if (typeof subframe === 'object')
subframe.close();
break;
} else {
log(LOG_ERROR,'- Login Failed? ');
cancel = true;
}
} else {
log(LOG_ERROR,'- Unhandled User Details: '+http.response_code);
cancel = true;
}
} else {
log(LOG_ERROR,'- Unhandled isReady msg: '+result.msg);
cancel = true;
}
complete = true;
if (typeof subframe === 'object')
subframe.close();
} else {
log(LOG_ERROR,'- Unhandled isReady: '+result.isReady);
cancel = true;
}
break;
default:
log(LOG_ERROR,'- Unhandled response code: '+http.response_code);
// We are done
cancel = true;
}
} catch (err) {
log(LOG_INFO,'SQRL Error: '+err+' '+JSON.stringify(sqrl));
cancel = true;
}
}
if (cancel) {
log(LOG_INFO,'SQRL: Processing CANCEL ['+read+'].');
complete = true;
if (typeof subframe === 'object')
subframe.close();
action = ACTION_GOTO;
next_page = FRAME_LOGIN;
}
return read;
}
}
this;