Script Type Examples

Example 1. Server Start

This is an example script which is intended to run with a "Server Start" Script Type. It demonstrates how to retrieve persisted variables within the system on startup.

function run()

{

    // Display a message in the log/server window.

    ScriptSession.logInfo("Server Started!");

    

    // Display persisted value when server was last stopped.

    var dtLast = ScriptSession.getVariable("LastStopped");

    ScriptSession.logInfo("Last Stopped: " + dtLast.toString());

}

Note:

Example 2. Server Stopped

This is an example script which is intended to run with a "Server Stopped" Script Type. It demonstrates how to persist variables for future server starts.

importClass (java.util.Calendar);

 

function run()

{

    // Display a message in the log/server window.

    ScriptSession.logInfo("Server stopped!");

    

    // Save the current time as last server start.

    var now = Calendar.getInstance().getTime();

    ScriptSession.persistVariable("LastStopped", now);

}

Note:

Example 3. User Login

This is an example script which is intended to run with a "User Login" Script Type. It demonstrates how to retrieve and evaluate  the username of the user authenticated.

function run()

{

    // Produce warning if user meets certain criteria.

    var usr = ScriptSession.getUserName();

 

    if (usr == "someuser")

        ScriptSession.logWarn("Warning: User " + usr + " logged into system" );

}

Note:

Example 4. User Logout

This is an example script which is intended to run with a "User Logout" Script Type. It demonstrates how to determine the user logging out in this scenario.

function run()

{    

    // Retrieve username.

    var usr = ScriptSession.getUserName();

 

    // Perform some kind of clean-up after a user logs off.

    doCleanUp(usr);

}

Note:

Example 5. Authentication

This is an example script which is intended to run with a "Authentication" Script Type, run before the user logs in. It arbitrarily checks if the username and password match - if they do, continue with the login, if not, invalidate the login attempt. Also displays username/password in the server log, as well as retrieves user IP address.

function authenticate(name, password)

{

    // Display user name and password in log/server window.

    ScriptSession.logInfo("Checking credentials for " + name + " [" + password + "]");

    ScriptSession.logInfo(name + " IP Address is: " + ScriptSession.getUserRemoteHostAddress());

 

    // Check if username and password are equal, if not, invalidate.

    if (name == password)

        return true;

 

    return false;

}

Note: