Skip to content
Snippets Groups Projects
Commit f39acd3f authored by mcmlxxix's avatar mcmlxxix
Browse files

more "genericification"

moved chatroom contents to own object,
moved input methods to inputline object,
added box init to give windows border & title (msgwndw.js)
fixed background coloring
parent 6a868e5f
Branches
Tags
No related merge requests found
...@@ -23,6 +23,7 @@ function Chat(key,engine) ...@@ -23,6 +23,7 @@ function Chat(key,engine)
engine=new ChatEngine(); engine=new ChatEngine();
engine.init(); engine.init();
} }
engine.redraw();
if(key) if(key)
{ {
engine.processKey(key); engine.processKey(key);
...@@ -61,76 +62,47 @@ load("scrollbar.js"); ...@@ -61,76 +62,47 @@ load("scrollbar.js");
load("graphic.js"); load("graphic.js");
load("str_cmds.js"); load("str_cmds.js");
load("nodedefs.js"); load("nodedefs.js");
load("msgwndw.js");
bbs.sys_status |= SS_PAUSEOFF; bbs.sys_status |= SS_PAUSEOFF;
oldpass=console.ctrl_key_passthru; oldpass=console.ctrl_key_passthru;
function ChatEngine(root) const flag_normal= "#";
{ const flag_global= "!";
const flag_normal= "#"; const flag_private= "%";
const flag_global= "!"; const flag_alert= 2;
const flag_private= "%"; const flag_notice= 1;
const flag_alert= 2; const flag_message= 0;
const flag_notice= 1;
const flag_message= 0; const local_color= "\1n\1g";
const remote_color= "\1n\1c";
const local_color= "\1n\1g"; const alert_color= "\1r\1h";
const remote_color= "\1n\1c"; const notice_color= "\1n\1y";
const alert_color= "\1r\1h"; const input_color= "\1n";
const notice_color= "\1n\1y"; const private_color= "\1n\1w";
const input_color= "\1n"; const global_color= "\1n\1m";
const private_color= "\1y\1h";
const global_color= "\1n\1m";
function ChatEngine(root)
{
//TODO: the only time this will be used is for storing chat history //TODO: the only time this will be used is for storing chat history
//maybe give ALL chat history files their own home, independent of the parent script //maybe give ALL chat history files their own home, independent of the parent script
var root_dir=(root?root:js.exec_dir); var root_dir=(root?root:js.exec_dir);
var stream=new ServiceConnection("chat"); var stream=new ServiceConnection("chat");
var scope=flag_normal; this.input_line=new InputLine();
var window=false; this.chat_room=new ChatRoom();
var message=new Object();
var fullscreen=true;
var scrollbar=false;
var background="\0010";
var ignore=[];
this.buffer="";
this.room="main";
this.columns=console.screen_columns;
this.rows=console.screen_rows;
this.x=1;
this.y=1;
this.input_line;
// USEFUL METHODS // USEFUL METHODS
this.init=function(room,c,r,x,y,bg,ix,iy,iw) //NOTE: DESTROYS BUFFER AND MESSAGE LIST this.init=function(room,c,r,x,y,ix,iy,iw,bg) //NOTE: DESTROYS BUFFER AND MESSAGE LIST
{ {
if(x) this.x=x; //top left corner x this.input_line=new InputLine();
if(y) this.y=y; //top left corner y this.input_line.init(ix,iy,iw,bg);
if(room) this.room=room; //room name (for lobby style interface) this.chat_room.init(room,x,y,c,r,bg);
if(bg) background=bg;
this.buffer="";
if(c && r) {
fullscreen=false;
this.columns=c;
this.rows=r;
if(this.columns>=console.screen_columns) this.columns=console.screen_columns-1;
scrollbar=new Scrollbar(this.x+this.columns,this.y,this.rows,"vertical","\1k\1h");
}
if(ix && iy) {
this.input_line=new InputLine(ix,iy,iw?iw:this.columns,bg);
fullscreen=false;
} else if(ix || iy) {
log("invalid input line parameters",LOG_WARNING);
return false;
}
if(!fullscreen) {
console.ctrlkey_passthru="+ACGKLOPQRTUVWXYZ_";
bbs.sys_status|=SS_MOFF;
window=new Graphic(this.columns,this.rows,undefined,undefined,true);
}
this.entryMessage(); this.entryMessage();
return true; }
this.initbox=function()
{
this.chat_room.initbox();
this.input_line.initbox();
} }
this.exit=function() this.exit=function()
{ {
...@@ -144,47 +116,21 @@ function ChatEngine(root) ...@@ -144,47 +116,21 @@ function ChatEngine(root)
this.exitMessage=function() this.exitMessage=function()
{ {
if(user.compare_ars("QUIET")) return false; if(user.compare_ars("QUIET")) return false;
var message=user.alias + " disconnected"; var message=new Message(user.alias + " disconnected",flag_notice);
this.send(message,flag_notice); this.send(message);
} }
this.entryMessage=function() this.entryMessage=function()
{ {
if(user.compare_ars("QUIET")) return false; if(user.compare_ars("QUIET")) return false;
var message=user.alias + " connected"; var message=new Message(user.alias + " connected",flag_notice);
this.send(message,flag_notice); this.send(message);
} }
this.resize=function(x,y,c,r,ix,iy,iw) //NOTE: DOES NOT DESTROY BUFFER OR MESSAGE LIST this.resize=function(x,y,c,r,ix,iy,iw) //NOTE: DOES NOT DESTROY BUFFER OR MESSAGE LIST
{ {
this.clearChatWindow(); this.input_line.init(ix,iy,iw);
if(x) this.x=x; this.chat_room.init(x,y,c,r);
if(y) this.y=y;
if(c) this.columns=columns;
if(r) this.rows=rows;
if(ix && iy) {
if(!this.input_line) {
this.input_line=new InputLine(ix,iy,iw?iw:this.columns,background);
} else {
this.input_line.x=ix;
this.input_line.y=iy;
this.input_line.width=iw?iw:this.columns;
}
} else {
if(this.input_line) {
this.input_line.x=this.x;
this.input_line.y=this.y+this.rows+1;
this.input_line.width=this.columns;
}
}
if(this.input_line) {
scrollbar=new Scrollbar(this.x+this.columns-1,this.y,this.rows,"vertical",DARKGRAY);
}
this.redraw(); this.redraw();
} }
this.ignoreUser=function(alias)
{
if(ignore[alias]==true) ignore[alias]=false;
else ignore[alias]==true;
}
this.getUserList=function() this.getUserList=function()
{ {
...@@ -192,47 +138,36 @@ function ChatEngine(root) ...@@ -192,47 +138,36 @@ function ChatEngine(root)
this.findUser=function(id) this.findUser=function(id)
{ {
//return stream.findUser(id); //return stream.findUser(id);
} return true;
this.clearChatWindow=function() //CLEARS THE ENTIRE CHAT WINDOW
{
clearBlock(this.x,this.y,this.columns,this.rows);
}
this.list=function(array,color) //DISPLAYS A TEMPORARY MESSAGE IN THE CHAT WINDOW (NOT STORED)
{
for(var i=0;i<array.length;i++) this.display(array[i],color);
}
this.notice=function(msg)
{
this.display(msg,notice_color);
}
this.alert=function(msg)
{
this.display(msg,alert_color);
} }
this.redraw=function() this.redraw=function()
{ {
this.clearChatWindow(); this.chat_room.draw();
this.display(); this.input_line.draw();
this.printBuffer();
} }
this.send=function(txt,level,scope,source,target) this.send=function(message)
{ {
if(!level) level=flag_message; if(!message.level) message.level=flag_message;
if(!scope) scope=flag_normal; if(!message.scope) message.scope=flag_normal;
var packet=new Message(txt,level,scope,source,target); if(!stream.send(message)) {
if(!stream.send(packet)) {
this.alert("message not sent."); this.alert("message not sent.");
} }
} }
this.receive=function() this.receive=function()
{ {
var notices=stream.getNotices();
while(notices.length) {
this.chat_room.notice(notices.shift());
}
var packet=stream.receive(); var packet=stream.receive();
this.processData(packet);
}
this.processData=function(packet)
{
if(packet && packet.txt) { if(packet && packet.txt) {
this.processData(packet); this.chat_room.process(packet);
} }
} }
//INTERNAL METHODS
this.processKey=function(key) //PROCESS ALL INCOMING KEYSTROKES this.processKey=function(key) //PROCESS ALL INCOMING KEYSTROKES
{ {
switch(key.toUpperCase()) switch(key.toUpperCase())
...@@ -267,233 +202,189 @@ function ChatEngine(root) ...@@ -267,233 +202,189 @@ function ChatEngine(root)
case KEY_DOWN: case KEY_DOWN:
case '\x02': /* CTRL-B KEY_HOME */ case '\x02': /* CTRL-B KEY_HOME */
case '\x05': /* CTRL-E KEY_END */ case '\x05': /* CTRL-E KEY_END */
this.showHistory(key); this.chat_room.scroll(key);
break; break;
case '\b': case '\b':
this.backSpace(); this.input_line.backspace();
break; break;
case '\r': case '\r':
case '\n': case '\n':
this.submitMessage(); this.submit();
break; break;
case '\x09': /* CTRL-I TAB */ case '\x09': /* CTRL-I TAB */
if(message.scope==flag_global) message.scope=flag_normal; this.input_line.toggle();
else message.scope=flag_global;
this.printBuffer();
break;
case ';':
this.bufferKey(key);
if(this.buffer.length==1 && fullscreen && user.compare_ars("SYSOP")) {
this.getStringCommand()
this.resetInput();
break;
}
break; break;
case '@': case '@':
if(!user.compare_ars("SYSOP") && !(bbs.sys_status&SS_TMPSYSOP)) break; if(!user.compare_ars("SYSOP") && !(bbs.sys_status&SS_TMPSYSOP)) break;
default: default:
this.bufferKey(key); this.input_line.bufferKey(key);
break; break;
} }
return true; return true;
} }
this.getStringCommand=function() this.submit=function()
{ {
var cmd=console.getstr(); var message=this.input_line.submit();
if(cmd.length>0) { if(message) {
str_cmds(cmd); this.send(message);
return true; this.chat_room.process(message);
} else return false; }
} }
this.submitMessage=function() }
function ChatRoom()
{
this.columns=console.screen_columns;
this.rows=console.screen_rows;
this.x=1;
this.y=1;
this.window;
this.scrollbar;
this.fullscreen=true;
this.ignored=[];
this.box=false;
this.room="Main";
this.bg="";
this.init=function(room,x,y,c,r,bg)
{ {
if(strlen(this.buffer)>0) { if(!(x || y || c || r)) {
this.send(this.buffer,message.level,message.scope,user.alias,message.target); this.fullscreen=true;
switch(message.scope) console.ctrlkey_passthru=oldpass;
{ bbs.sys_status&=~SS_MOFF;
case flag_global: return;
this.display(this.buffer,global_color,user.alias); }
break; if(x && y) {
case flag_private: this.x=x;
this.display(this.buffer,private_color,user.alias,message.target); this.y=y;
break; this.fullscreen=false;
case flag_normal: }
default: if(c && r) {
this.display(this.buffer,local_color,user.alias); this.columns=c;
break; this.rows=r;
} this.fullscreen=false;
} }
this.resetInput(); if(bg) this.bg=bg;
if(room) this.room=room;
if(this.box) this.initbox();
if(this.columns>=console.screen_columns) this.columns=console.screen_columns-1;
this.scrollbar=new Scrollbar(this.x+this.columns,this.y,this.rows,"vertical","\1k\1h");
this.window=new Graphic(this.columns,this.rows,getColor(this.bg));
console.ctrlkey_passthru="+ACGKLOPQRTUVWXYZ_";
bbs.sys_status|=SS_MOFF;
} }
this.backSpace=function() this.initbox=function()
{ {
if(this.buffer.length>0) { this.box=new Window(this.x-1,this.y-1,this.columns+2,this.rows+2);
if(fullscreen) { this.box.init("\1n\1cCHAT","\1n\1c" + this.room);
console.left();
console.cleartoeol();
this.buffer=this.buffer.substr(0,this.buffer.length-1);
} else if(this.buffer.length<=this.input_line.width) {
this.getInputPosition();
console.left();
console.putmsg(" ",P_SAVEATR);
this.buffer=this.buffer.substr(0,this.buffer.length-1);
} else {
this.buffer=this.buffer.substr(0,this.buffer.length-1);
this.printBuffer();
}
return true;
} else {
return false;
}
} }
this.getInputPosition=function() this.ignore=function(alias)
{ {
console.gotoxy(this.input_line.x+this.buffer.length,this.input_line.y); if(this.ignored[alias]==true) this.ignored[alias]=false;
else ignored[alias]==true;
} }
this.printBuffer=function() this.process=function(data)
{ {
if(!this.buffer.length) return false; if(this.ignored[data.source]==true) return false;
var color=""; switch(data.scope)
switch(message.scope) { {
case flag_global:
color=background + global_color;
break;
case flag_private: case flag_private:
color=background + private_color; if(data.target) {
break; if(data.target==user.alias) this.post(data.txt,private_color + "\1h",data.source);
case flag_normal: else this.post(data.txt,private_color,data.source + "\1h-" +private_color+ data.target);
default:
color=background + input_color;
break;
}
if(fullscreen) {
console.putmsg(color,P_SAVEATR);
console.write("\r" + this.buffer);
console.cleartoeol();
} else {
console.gotoxy(this.input_line);
console.putmsg(color,P_SAVEATR);
if(this.buffer.length>this.input_line.width) {
var overrun=(this.buffer.length-this.columns);
var truncated=this.buffer.substr(overrun);
var disp=truncated;
if(disp.indexOf('@')>=0) disp=disp.replace(/@/g,"?");
console.write(disp);
} else {
console.write(this.buffer);
} }
}
}
this.bufferKey=function(key) //ADD A KEY TO THE USER INPUT BUFFER
{
if(!key) return false;
var color="";
switch(message.scope) {
case flag_global:
color=background + global_color;
break; break;
case flag_private: case flag_global:
color=background + private_color; switch(data.source) {
case user.alias:
this.post(data.txt,global_color,data.source);
break;
default:
this.post(data.txt,global_color + "\1h",data.source);
break;
}
break; break;
case flag_normal: case flag_normal:
default: default:
color=background + input_color; if(!data.source) {
switch(data.level)
{
case flag_alert:
this.post(data.txt,alert_color);
break;
case flag_notice:
default:
this.post(data.txt,notice_color);
break;
}
} else if(data.source==user.alias) {
this.post(data.txt,local_color,data.source);
} else this.post(data.txt,remote_color,data.source);
break; break;
} }
if(!fullscreen) {
if(this.buffer.length>=this.input_line.width) {
this.buffer+=key;
this.printBuffer();
return;
} else {
this.getInputPosition();
}
}
this.buffer+=key;
console.putmsg(color,P_SAVEATR);
console.write(key);
} }
this.showHistory=function(key) //ACTIVATE MESSAGE HISTORY SCROLLBACK this.scroll=function(key)
{ {
if(!fullscreen && window.length>window.height) { if(!this.fullscreen && this.window.length>this.window.height) {
switch(key) switch(key)
{ {
case '\x02': /* CTRL-B KEY_HOME */ case '\x02': /* CTRL-B KEY_HOME */
window.home(); this.window.home();
break; break;
case '\x05': /* CTRL-E KEY_END */ case '\x05': /* CTRL-E KEY_END */
window.end(); this.window.end();
break; break;
case KEY_DOWN: case KEY_DOWN:
window.scroll(1); this.window.scroll(1);
break; break;
case KEY_UP: case KEY_UP:
window.scroll(-1); this.window.scroll(-1);
break; break;
} }
window.draw(this.x,this.y); this.window.draw(this.x,this.y);
scrollbar.draw(window.index,window.length,window.height); this.scrollbar.draw(this.window.index,this.window.length,this.window.height);
} }
} }
this.display=function(text,color,source,target) this.post=function(text,color,source,target)
{ {
if(text.indexOf('@')>=0) {
if(user.compare_ars("SYSOP") || bbs.sys_status&SS_TMPSYSOP) {
text=text.replace(/@/g,"?");
}
}
var msg; var msg;
if(source) { if(source) {
msg="\r" + color + source + "\1h: " + color + text + "\r\n"; msg="\r" + color + this.bg + source + "\1h: " + color + this.bg + text + "\r\n";
} else { } else {
msg="\r" + color + text + "\r\n"; msg="\r" + color + this.bg + text + "\r\n";
} }
if(fullscreen) { if(this.fullscreen) {
console.putmsg(msg,P_SAVEATR); console.putmsg(msg,P_SAVEATR);
this.printBuffer();
} else { } else {
window.putmsg(false,false,msg,undefined,true); this.window.putmsg(false,false,msg,undefined,true);
window.draw(this.x,this.y); this.draw();
if(window.length>window.height) scrollbar.draw(window.index,window.length,window.height);
} }
} }
this.processData=function(data) this.list=function(array,color) //DISPLAYS A TEMPORARY MESSAGE IN THE CHAT WINDOW (NOT STORED)
{ {
if(ignore[data.source]==true) return false; for(var i=0;i<array.length;i++) this.post(array[i],color);
switch(data.scope) }
{ this.notice=function(msg)
case flag_private: {
if(data.target==user.alias) this.display(data.txt,private_color + "\1h",data.source); this.post(msg,notice_color);
break;
case flag_global:
this.display(data.txt,global_color + "\1h",data.source);
break;
case flag_normal:
default:
if(!data.source) {
switch(data.level)
{
case flag_alert:
this.display(data.txt,alert_color);
break;
case flag_notice:
default:
this.display(data.txt,notice_color);
break;
}
} else if(data.source==user.alias) {
this.display(data.txt,local_color,data.source);
} else this.display(data.txt,remote_color,data.source);
break;
}
} }
this.resetInput=function() this.alert=function(msg)
{ {
if(!fullscreen) this.input_line.clear(); this.post(msg,alert_color);
else { }
console.left(this.buffer.length); this.draw=function()
console.cleartoeol(); {
if(!this.fullscreen) {
if(this.box) this.box.draw();
this.window.draw(this.x,this.y);
if(this.window.length>this.window.height) this.scrollbar.draw(this.window.index,this.window.length,this.window.height);
} }
this.buffer="";
message=new Message();
} }
} }
function Message(txt,level,scope,source,target) function Message(txt,level,scope,source,target)
{ {
...@@ -503,42 +394,190 @@ function Message(txt,level,scope,source,target) ...@@ -503,42 +394,190 @@ function Message(txt,level,scope,source,target)
this.source=source; this.source=source;
this.target=target; this.target=target;
} }
function InputLine(x,y,width,bg) function InputLine()
{ {
this.x=x; this.x=1;
this.y=y; this.y=1;
this.width=width; this.width;
this.bg=bg; this.bg="";
this.position; this.fg=input_color;
this.window;
this.buffer=""; this.buffer="";
this.scope=flag_normal;
this.target="";
this.box=false;
this.clear=function(bg) this.clear=function()
{ {
clearLine(this.width,this.x,this.y,bg?bg:this.bg); if(this.width) {
console.gotoxy(this);
console.putmsg(this.bg+format("%*s",this.width,""),P_SAVEATR);
} else {
console.write("\r");
console.cleartoeol();
}
console.gotoxy(this); console.gotoxy(this);
} }
this.init=function(x,y,w,bg,window) this.init=function(x,y,w,bg,fg)
{ {
this.x=x; if(x) this.x=x;
this.y=y; if(y) this.y=y;
this.width=w; if(w) this.width=w;
this.bg=bg; if(bg) this.bg=bg;
if(window) { if(fg) this.fg=fg;
this.window=new Window(x,y,w,3); if(this.box) this.initbox();
this.window.init("\1nINPUT"); this.reset();
this.x+=1; }
this.y+=1; this.initbox=function()
this.width-=2; {
this.box=new Window(this.x-1,this.y-1,this.width+2,3);
var color=this.fg;
var subtitle=false;
switch(this.scope) {
case flag_global:
color=global_color;
subtitle="GLOBAL";
break;
case flag_private:
color=private_color;
subtitle="PRIVATE";
break;
case flag_normal:
default:
break;
} }
this.box.init("\1n\1cINPUT",subtitle?color + subtitle:false);
}
this.submit=function()
{
if(strlen(this.buffer)<1) return false;
switch(this.buffer.charAt(0))
{
case ';':
if(this.buffer.length>1 && (user.compare_ars("SYSOP") || bbs.sys_status&SS_TMPSYSOP)) {
str_cmds(this.buffer.substr(1));
this.reset();
return false;
} else {
break;
}
case '/':
var target=getFirstWord(this.buffer.substr(1));
if(target.length>0) {
this.target=target;
this.buffer=removeSpaces(this.buffer.substr(target.length+1));
this.scope=flag_private;
}
default:
break;
}
var msg=new Message(this.buffer,flag_message,this.scope,user.alias,this.target);
this.reset();
this.clear();
return msg;
}
this.toggle=function()
{
if(this.scope==flag_global) this.scope=flag_normal;
else this.scope=flag_global;
if(this.box) this.initbox();
this.draw();
}
this.backspace=function()
{
if(this.buffer.length>0) {
if(!this.width>0) {
console.left();
console.cleartoeol();
this.buffer=this.buffer.substr(0,this.buffer.length-1);
} else if(this.buffer.length<=this.width) {
this.getxy();
console.left();
console.putmsg(" ",P_SAVEATR);
this.buffer=this.buffer.substr(0,this.buffer.length-1);
} else {
this.buffer=this.buffer.substr(0,this.buffer.length-1);
this.draw();
}
return true;
} else {
return false;
}
}
this.reset=function()
{
this.buffer="";
if(this.scope==flag_private) {
this.scope=flag_normal;
this.initbox();
}
this.target="";
}
this.getxy=function()
{
console.gotoxy(this.x+this.buffer.length,this.y);
}
this.bufferKey=function(key)
{
if(!key) return false;
if(this.width>0) {
if(this.buffer.length>=this.width) {
this.buffer+=key;
this.draw();
return;
} else {
this.getxy();
}
}
this.buffer+=key;
var color=this.fg;
switch(this.scope) {
case flag_global:
color=global_color;
break;
case flag_private:
color=private_color;
break;
case flag_normal:
default:
break;
}
console.putmsg(color+this.bg,P_SAVEATR);
console.write(key);
} }
this.draw=function() this.draw=function()
{ {
if(this.window) { if(this.box) this.box.draw();
this.window.draw(); if(this.buffer.length<1) {
this.clear();
return;
}
var color=this.fg;
switch(this.scope) {
case flag_global:
color=global_color;
break;
case flag_private:
color=private_color;
break;
case flag_normal:
default:
break;
}
if(this.width>0) {
console.putmsg(color + this.bg,P_SAVEATR);
console.gotoxy(this);
if(this.buffer.length>this.width) {
var overrun=(this.buffer.length-this.width);
var truncated=this.buffer.substr(overrun);
if(truncated.indexOf('@')>=0) truncated=truncated.replace(/@/g,"?");
console.write(truncated);
} else {
console.write(printPadded(this.buffer,this.width));
}
} else {
console.putmsg("\r" + color + this.bg,P_SAVEATR);
console.write(this.buffer);
} }
console.gotoxy(this.x,this.y);
console.write(printPadded(this.color + this.buffer,this.width));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment