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

superseded by TBA

parent e6f0154b
Branches
Tags
No related merge requests found
// $Id$
/*
Javascript Modular Chat Engine
by Matt Johnson (MCMLXXIX) 2010
*/
load("nodedefs.js");
load("sbbsdefs.js");
load("synchronet-json.js");
var chat_settings=new ChatSettings();
function ChatSettings()
{
this.CONNECTION_ATTEMPTS= 5;
this.CONNECTION_INTERVAL= 10;
this.ALERT_COLOR= "\1r";
this.MOTD_COLOR= "\1c";
this.MSG_COLOR= "\1n";
this.HIGHLIGHT_COLOR= "\1b\1h";
this.TOPIC_COLOR= "\1y";
this.SNOTICE_COLOR= "\1g";
this.ERROR_COLOR= "\1r";
}
function ChatEngine(protocol,host,port)
{
var protocol=protocol;
var host=host;
var port=port;
var conn_attempts=0;
var last_conn_attempt=0;
this.disabled=false;
this.connect=true;
this.connected=false;
this.connecting=false;
this.connection_established=false;
this.registered=false;
this.registering=false;
this.channels=[];
this.nick=false;
this.init=function()
{
/* set queue naming convention */
var qname=format("%s_%d_%d",host,port,bbs.node_num);
var qfound=false;
/* scan current named queue list for an existing chat session */
var qlist=list_named_queues();
for each(var q in qlist) {
if(q == qname) {
qfound=true;
break;
}
}
/* if there is no existing chat session, load one into background */
if(!qfound) {
log(LOG_DEBUG,"initializing background socket");
load(true,"socket_bg.js",bbs.node_num,host,port);
}
/* initialize queue connection for background socket */
this.queue=new Queue(qname);
log("name: " + this.queue.name);
/* load protocol information */
switch(protocol.toUpperCase()) {
case "IRC":
load("irc_protocol.js");
IRC_init.apply(this);
break;
case "AIM":
break;
}
/* load user chat settings */
//var s_file=new File(system.ctrl_dir + "chat.ini");
//s_file.open('r',true);
//s_file.close();
}
this.synchronize=function(data)
{
switch(data.toLowerCase()) {
case "connecting":
this.server_chan.post(chat_settings.SNOTICE_COLOR + "connecting to " + host + "...");
break;
case "connected":
this.reset_counters();
this.connect=false;
this.connecting=false;
this.connected=true;
this.onConnect();
break;
case "failed":
this.server_chan.post(chat_settings.ERROR_COLOR + "connection failed");
this.connect=true;
this.connecting=false;
break;
case "disconnected":
this.server_chan.post(chat_settings.ERROR_COLOR + "disconnected");
this.reset_connection();
this.onDisconnect();
break;
}
}
this.enqueue=function(data,name)
{
//log("bg-->" + data);
this.queue.write(data,name);
}
this.cycle=function()
{
/* if chat has been disabled,
do not cycle events */
if(this.disabled) {
return false;
}
/* process synchronization data */
var sync_data=this.queue.read("sync");
if(sync_data) {
//log("bg<--" + sync_data);
this.synchronize(sync_data);
}
/* if we are not connected or in the process of connecting */
if(!this.connected && !this.connecting) {
/* if we are not connected and have exceeded our connection attempt limit,c
disable chat */
if(conn_attempts>chat_settings.CONNECTION_ATTEMPTS) {
this.server_chan.post("\1rconnection limit exceeded");
this.enqueue("disconnect","sync");
this.reset_connection();
this.disabled=true;
}
/* if we are scheduled to connect, establish socket connection */
if((time()-last_conn_attempt)>chat_settings.CONNECTION_INTERVAL) {
this.enqueue("connect","sync");
conn_attempts++;
this.connecting=true;
}
}
/* if our socket is connected */
else if(this.connected) {
/* perform any protocol cycle operations */
this.cycleProtocol();
/* decode and process chat data */
var data=false;
var raw_data=this.queue.read("data");
if(raw_data) {
//log("bg<--" + raw_data);
data=this.decode(raw_data);
}
if(data)
data=this.process(data);
return data;
}
}
this.send=function(data)
{
this.enqueue(this.encode(data),"data");
}
this.handle_command=function(text,target)
{
var data=this.parse(text,target);
if(!data) return false;
this.enqueue(this.encode(data),"data");
return data;
}
this.reset_counters=function()
{
this.conn_attempts=0;
this.last_conn_attempt=0;
}
this.reset_connection=function()
{
this.connection_established=false;
this.connected=false;
this.connect=true;
this.registered=false;
this.registering=false;
}
this.init();
}
// $Id$
/*
Javascript Modular Chat Engine
by Matt Johnson (MCMLXXIX) - 2010
NOTE: Do not load this file directly.
This script is meant to be loaded in
the background, preferrably by chatclient.js
or another script that can interact properly
*/
load("nodedefs.js");
var node=argv[0];
var host=argv[1];
var port=argv[2];
if( isNaN(node) ||
host==undefined ||
isNaN(port)) {
log(LOG_ERROR,"invalid client arguments");
exit();
}
const CONNECTION_TIMEOUT= 5;
const MAX_RECV= 2048;
var sock=false;
var disabled=false;
var outbound=[];
var parent=new Queue(format("%s_%d_%d",host,port,node));
function main()
{
try {
while(system.node_list[node-1].status == NODE_INUSE ||
system.node_list[node-1].status == NODE_QUIET) {
cycle();
mswait(10);
}
}
catch(e) {
log("ERROR: " + e);
}
disconnect();
exit();
}
function connect()
{
parent.write("connecting","sync");
sock=new Socket();
sock.connect(host,port,CONNECTION_TIMEOUT);
if(!sock.is_connected) {
parent.write("failed","sync");
sock=false;
}
else {
parent.write("connected","sync");
}
}
function cycle()
{
/* read any parent script control requests */
var syncdata=parent.read("sync");
if(syncdata) {
//log("parent<--" + syncdata);
sync(syncdata);
}
/* read any outbound chat data from the parent script */
var outdata=parent.read("data");
if(outdata) {
//log("parent<--" + outdata);
outbound.push(outdata);
}
/* if chat has been disabled,
do not run socket events */
if(disabled)
return;
/* if connection has not been initialized */
if(!sock)
return;
/* if active socket connection is broken */
if(!sock.is_connected) {
sock=false;
parent.write("disconnected","sync");
return;
}
/* if socket connection is established */
if(sock.data_waiting) {
var raw_data=sock.recvline(MAX_RECV);
if(raw_data != null) {
log(LOG_DEBUG,"sock<--" + raw_data);
parent.write(raw_data,"data");
}
}
/* send outbound socket data */
var data=outbound.shift();
if(data) {
if(sock.write(data))
log(LOG_DEBUG,"sock-->" + data);
else
outbound.unshift(data);
}
}
function sync(cmd)
{
switch(cmd.toLowerCase()) {
case "connect":
if(!sock.is_connected) {
disabled=false;
connect();
}
break;
case "disconnect":
disabled=true;
disconnect();
break;
}
}
function disconnect()
{
if(sock.is_connected) {
log(LOG_DEBUG,"terminating client connection");
while(outbound.length > 0)
sock.write(outbound.shift());
sock.close();
}
sock=false;
}
main();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment