server Namespace Ape

Summary

File
<./src/Ape_Server/modules/libape-spidermonkey.c>

Ape Namespace Detail

Ape
The great APE Object in the global namespace.
File:
<./src/Ape_Server/modules/libape-spidermonkey.c>

Summary

Own
Attributes Type Name Description
<static>   Ape apesocket Standard apesocket object.

<static> Ape apesocket

Standard apesocket object.

Summary

Own
Methods Attributes Methods Name Methods Description
<static>   Ape. addEvent (name, fn) Listen to an ape event an start a callback function
<static>   Ape. clearInterval (timeoutId) Cancel a timeout created by setInterval.
<static>   Ape. clearTimeout (timeoutId) Cancel a timeout created by setTimeout.
<static>   Ape. config (configFilename, key) Get a configuration value.
<static>   Ape. getChannelByName () Get the channel by its name.
<static>   Ape. getChannelByPubid () Get the channel by its pubid.
<static>   Ape. getHostByName (hostname) Get the ip address of a host.
<static>   Ape. getPipe (pubid) Get a pipe object.
<static>   Ape. log (message) Log a message in the console (daemon = false).
<static>   Ape. mainConfig (section, key) Get a configuration value from the main configfile (bin/ape.conf).
<static>   Ape. registerCmd (name, require_sess_id, fn, params, info) Register a new server command.
<static>   Ape. registerHookBadCmd (fn, params, info, raw) Catch all commands that are not registered.
<static>   Ape. registerHookCmd (name, fn, params, info) Register a hook on a server command.
<static>   Ape. rmChan () Remove a channel object.
<static>   Ape. setInterval (fn, delay) Calls a function repeatedly, with a fixed time delay between each call to that function.
<static>   Ape. setTimeout (fn, delay) Execute a function after specified delay.
<static>   Ape. xorize (string1, string2) Apply a 'XOR' between two string (or binary)

<static> {void} Ape. addEvent (name, fn)

Listen to an ape event an start a callback function
Parameters:
{string} name The event name to listen to
{function} fn This function will be called when the event is triggered
Returns:
{void}
Examples
Ape.addEvent('init', function(){
	Ape.log('Ape is ready !');
});
Ape.addEvent("adduser", function(user) {
	Ape.log("New user :)");
});
Ape.addEvent("join", function(user, channel) {
	Ape.log("New user has joined the channel ("+channel.getProperty('name')+") :)");
});
//Note that all objects passed to Events are persistent.
//This means that you can store private data inside user, channel, ...
Ape.addEvent("adduser", function(user) {
	Ape.log("New user.");
	user.foo = "bar";
});
Ape.addEvent("join", function(user, channel) {
	Ape.log(user.foo+" joined the channel ("+channel.getProperty('name')+").");
});
See:
Ape.init
Ape.addUser
Ape.delUser
Ape.beforeJoin
Ape.join
Ape.afterJoin
Ape.left
Ape.mkChan
Ape.rmChan
Ape.stop

<static> {void} Ape. clearInterval (timeoutId)

Cancel a timeout created by setInterval.
Parameters:
{integer} timeoutId
Returns:
{void}
Example
var timeoutID = Ape.setInterval(function(a, b) {
	Ape.log("Foo : " + a + " Bar : " + b);
}, 3000, "foo", "bar");
Ape.clearInterval(timeoutID);
See:

<static> {void} Ape. clearTimeout (timeoutId)

Cancel a timeout created by setTimeout.
Parameters:
{integer} timeoutId
Returns:
{void}
Example
var timeoutID = Ape.setTimeout(function(a, b) {
	Ape.log("Foo : " + a + " Bar : " + b);
}, 3000, "foo", "bar");
Ape.clearTimeout(timeoutID);
See:

<static> {string} Ape. config (configFilename, key)

Get a configuration value.
Parameters:
{string} configFilename Configuration filename
{string} key Configuration key
Returns:
{string} The string as defined in the configuration file for the configuration key, or an emtpty string.
Example
var bar = Ape.config('settings.conf', 'foo');
See:

<static> Ape. getChannelByName ()

Get the channel by its name.
Example
var channel = Ape.getChannelByName('foochannel');
channel.setProperty('foo', 'bar');
channel.myprivate = {'my':'private'}; // Can be a string or whatever you want
channel.pipe.sendRaw('FOORAW', {'John':'Doe'});
Ape.addEvent('beforeJoin', function(user, channel) {
	Ape.log('My private : ' + channel.myprivate);
});
See:

<static> Ape. getChannelByPubid ()

Get the channel by its pubid.
Example
var channel = Ape.getChannelByPubid(pubid);
See:

<static> {string} Ape. getHostByName (hostname)

Get the ip address of a host.
Parameters:
{string} hostname
Returns:
{string} The ip address af the hostname or NULL
Example
var content = Ape.getHostByName("www.ape-project");

<static> {Ape.pipe} Ape. getPipe (pubid)

Get a pipe object.
Parameters:
{string} pubid The pubid of the pipe
Returns:
Example
Ape.registerCmd("foocmd", true, function(params, info) {
	Ape.log("The user ip : ("+info.ip+"), foo : " + params.foo);
	Ape.getPipe(params.pubid).sendRaw("CUSTOM_RAW", {"foo":"bar"});
});
See:

<static> {void} Ape. log (message)

Log a message in the console (daemon = false).
Parameters:
{string} message The message that should be logged
Returns:
{void}
Examples
Ape.log('foo');// foo
Ape.log(pipe); // [object Object]
Ape.log(JSON.stringify(var)); // '{}';

<static> {string} Ape. mainConfig (section, key)

Get a configuration value from the main configfile (bin/ape.conf).
Parameters:
{string} section Configuration section
{string} key Configuration key
Returns:
{string} The string as defined in the configuration file for the configuration key, or an emtpty string.
Example
var bar = Ape.mainConfig('Server', 'daemon');
See:

<static> {void} Ape. registerCmd (name, require_sess_id, fn, params, info)

Register a new server command.
Parameters:
{string} name The CMD name to listen to
{boolean} require_sess_id Does this command requires a connected user (is sessid required) ?
{function} fn This function will be called when an user uses the CMD command.
{object} params The list of parameters sent by the client.
{object} info Contains information about the client:
{string} info.host Host sent on HTTP headers.
{sockClient} info.client The client socket's object.
{integer} info.chl The challenge number.
{string} info.ip The client's IP.
{user} info.user User object (if logged in).
{subuser} info.subuser User object (if logged in).
{Array} info.http HTTP headers.
Returns:
{void}
Examples
Ape.registerCmd("foocmd", true, function(params, info) {
	Ape.log("The user ip : ("+info.ip+"), foo : " + params.foo);
});
// You can return error in two ways:
// By returning 0 to return a "BAD_PARAMS"
// By returning an array [code, error] for custom errors.
Ape.registerCmd("foocmd", true, function(params, info) {
	if (!$defined(params.john)) return 0; // send a "BAD_PARAMS" RAW to the user
	if (params.john != "doe") return ["209", "NOT_A_JOHN_DOE"];
	return 1;
});
//Return an object to send a raw as response.
Ape.registerCmd( "gettime", true, function(params, info) ) {
	return {
		name: "TIME",//The raw's name
		data: { time: new Date().getTime() }//The raw's data (must be an object)
	}
}
See:

<static> {void} Ape. registerHookBadCmd (fn, params, info, raw)

Catch all commands that are not registered.
Parameters:
{function} fn This function will be called when an user uses the CMD command.
{object} params The list of parameters sent by the client.
{object} info Contains information about the client:
{string} info.host Host sent on HTTP headers.
{sockClient} info.client The client socket's object.
{integer} info.chl The challenge number.
{string} info.ip The client's IP.
{user} info.user User object (if logged in).
{subuser} info.subuser User object (if logged in).
{Array} info.http HTTP headers.
{string} raw the raw received
Returns:
{void}
Examples
//log
Ape.registerHookBadCmd(function(params, info, raw){
		Ape.log("Bad RAW received ("+raw+").");
		//We return nothing so client will receive a BAD_CMD error
});
//Forward all
Ape.registerHookBadCmd(function(params, info, raw) {
	if(!params.pipe) {
		return 0;//We return 0 so client will receive BAD_PARAMS error
	}
	var pipe = Ape.getPipe(params.pipe);
	if (!pipe) {
		return [109, 'UNKNOWN_PIPE']; //Client will receive UNKNOWN_PIPE error
	}
	pipe.sendRaw(raw, params, {from: info.user});
});
See:

<static> {void} Ape. registerHookCmd (name, fn, params, info)

Register a hook on a server command.

APE server commes with the following pre-registered CMD's

  • CONNECT Returns a RAW IDENT set a sessid on the user an sends a RAW LOGIN
  • SCRIPT Sends the files thaw were requested in the json array
  • CHECK (sessid needed) does nothing
  • SEND (sessid needed) send a Raw Data to the requested pipe
  • QUIT (sessid needed) sends a Raw quit and makes the user quit
  • JOIN (sessid needed) Joins the user on the channel if possible. If the channel does not exist, it is created
  • LEFT (sessid needed) makes a user leave a channel or sends error {103 UNKNOWN, 104 NOT_IN_CHANNEL}
  • SESSION (sessid needed) sets or gets session propertyies and returns with Raw SESSIONS or sends a error (203 SESSION_ERROR BAD_PARAMS)
Parameters:
{string} name The CMD name to listen to
{function} fn This function will be called when an user uses the CMD command.
{object} params The list of parameters sent by the client.
{object} info Contains information about the client:
{string} info.host Host sent on HTTP headers.
{sockClient} info.client The client socket's object.
{integer} info.chl The challenge number.
{string} info.ip The client's IP.
{user} info.user User object (if logged in).
{subuser} info.subuser User object (if logged in).
{Array} info.http HTTP headers.
Returns:
{void}
Example
Ape.registerHookCmd("foocmd", function(params, info) {
	if (!$defined(params.john)) return 0;
	return 1;
});
See:

<static> Ape. rmChan ()

Remove a channel object.

Remove a channel and send a left event to all user on the channel.

Example
Ape.rmChan('my_channel');
See:

<static> {integer} Ape. setInterval (fn, delay)

Calls a function repeatedly, with a fixed time delay between each call to that function.
Parameters:
{function} fn
{integer} delay
{mixed} ... Optional param1, param2, ..
Returns:
{integer} The interval id that can be used in clearInterval
Example
var timeoutID = Ape.setInterval(function(a, b) {
	Ape.log("Foo : " + a + " Bar : " + b);
}, 3000, "foo", "bar");
Ape.clearInterval(timeoutID);
See:

<static> {integer} Ape. setTimeout (fn, delay)

Execute a function after specified delay.
Parameters:
{function} fn
{integer} delay
{mixed} ... Optional param1, param2, ..
Returns:
{integer} The interval id that can be used in clearTimeout
Example
var timeoutID = Ape.setTimeout(function(a, b) {
	Ape.log("Foo : " + a + " Bar : " + b);
}, 3000, "foo", "bar");
Ape.clearInterval(timeoutID);
See:

<static> {string} Ape. xorize (string1, string2)

Apply a 'XOR' between two string (or binary)
Parameters:
{string} string1
{string} string2
Returns:
{string} The xor-ed string
Examples
var result = Ape.xorize("key1", "key2");
for (i = 0; i < key1_len; i++) {
	returned[i] = key1[i] ^ key2[i];
}

Summary

Own
Attributes Name Description
<static>   Ape. addUser (user) This is event is fired wen a user successfully connects to the server
<static>   Ape. afterJoin (user, channel) This is event is fired just after an user joins a channel.
<static>   Ape. beforeJoin (user, channel) This is event is fired just before an user joins a channel, can be useful to add a public property to the user that will be sent to all users on the channel.
<static>   Ape. delUser (user) This event is fired when an user is deleted from the server (timeout, disconnect etc.
<static>   Ape. init () This event is fired when the server is ready
<static>   Ape. join (user, channel) This is event is fired when an user joins a channel.
<static>   Ape. left (user, channel) This is event is fired when a user leaves a channel
<static>   Ape. mkChan (channel) This event is fired when a channel is created
<static>   Ape. rmChan (channel) This event is fired when a channel is destroyed
<static>   Ape. stop () This event is fired when the server is stopped (plugin unload)

<static> {void} Ape. addUser (user)

This is event is fired wen a user successfully connects to the server
Parameters:
{user} user The user who joined the channel
Returns:
{void}
Example
Ape.addEvent('init', function() {});
Ape.registerHookCmd("connect", function(params, cmd) {
	if (!$defined(params) || !$defined(params.name)) return 0;
	cmd.user.setProperty('name', params.name);
	return 1;
	});
Ape.addEvent("addUser", function(user) {
	oceania.habitants++;
	var bigBro = Ape.getUserByPubid('1984');
	if ( bigBro && user.getProperty('name') == 'Smith, Winston') {
		user.join(room_101);
		bigBro.pipe.sendRaw('ACCEPTANCE', {'fear': 'rats'});
		}
	});
See:
Ape.addEvent
Ape.delUser
Ape.connect

<static> {void} Ape. afterJoin (user, channel)

This is event is fired just after an user joins a channel.
Parameters:
{user} user The user who joined the channel
{channel} channel The joined channel
Returns:
{void}
Example
Ape.addEvent("afterJoin", function(user, channel) {
	if( user.getProperty('name') == 'Smith, Winston') {
		user.save.inLoveWithJulia = true;
	}
});
See:
Ape.addEvent
Ape.join
Ape.left
Ape.beforeJoin

<static> {void} Ape. beforeJoin (user, channel)

This is event is fired just before an user joins a channel, can be useful to add a public property to the user that will be sent to all users on the channel.
Parameters:
{user} user The user who joined the channel
{channel} channel The joined channel
Returns:
{void}
Example
Ape.addEvent("beforeJoin", function(user, channel) {
	if ( user.safe.role == 'captain' &&  channel.getProperty('name') == 'bridge') {
		user.setProperty('captianOnDeck',true);
	}
});
See:
Ape.addEvent
Ape.join
Ape.left
Ape.afterJoin

<static> {void} Ape. delUser (user)

This event is fired when an user is deleted from the server (timeout, disconnect etc...)
Parameters:
{user} user The user who left the ape
Returns:
{void}
Example
Ape.addEvent("delUser", function(user) {
	oceania.habitants--;
	});
See:
Ape.addEvent
Ape.addUser

<static> {void} Ape. init ()

This event is fired when the server is ready
Returns:
{void}
Example
Ape.addEvent("init", function() {
	var eurasia = {};
	var eastasia = {};
	var oceania = {'habitants': 1984} ;
	var room_101 = APE.mkChan('room_101');
	});
See:
Ape.addEvent
Ape.sto

<static> {void} Ape. join (user, channel)

This is event is fired when an user joins a channel.
Parameters:
{user} user The user who joined the channel
{channel} channel The joined channel
Returns:
{void}
Example
Ape.addEvent("join", function(user, channel) {
	user.pipe.sendRaw('WELCOME', {});
});
See:
Ape.addEvent
Ape.join
Ape.left
Ape.beforeJoin

<static> {void} Ape. left (user, channel)

This is event is fired when a user leaves a channel
Parameters:
{user} user The user who leaves the channel
{channel} channel The abandoned channel
Returns:
{void}
Example
Ape.addEvent("left", function(user, channel) {
	if( channel.getProperty('name')== 'room_101' && user.getProperty('name') == 'Smith, Winston') {
		user.save.inLoveWithJulia = false;
	}
});
See:
Ape.addEvent
Ape.join
Ape.left
Ape.beforeJoin
Ape.afterJoin

<static> {void} Ape. mkChan (channel)

This event is fired when a channel is created
Parameters:
{channnel} channel Channel that is created
Returns:
{void}
Example
Ape.addEvent("mkChan", function(channel) {
	if(channel.getProperty('name') == 'room_101'){
		oceania.enemy= eurasia ;
		eastasia.enemy = null;
		eurasia.enemy = oceania;
	}
});
See:

<static> {void} Ape. rmChan (channel)

This event is fired when a channel is destroyed
Parameters:
{channnel} channel Channel that is destroyed
Returns:
{void}
Example
Ape.addEvent("rmChan", function(channel) {
	if(channel.getProperty('name') == 'room_101'){
		oceania.enemy= eastasia ;
		eastasia.enemy = oceania;
		eurasia.enemy = null;
	}
});
See:
Ape.addEvent
Ape.mkChan

<static> {void} Ape. stop ()

This event is fired when the server is stopped (plugin unload)
Returns:
{void}
Example

												
See:
Ape.addEvent
Ape.init