Project

General

Profile

[Plugin] C# API to Javascript wrapper

Added by Per Amundsen 3 months ago

This is just a test to allow using javascript to listen/respond to AdiIRC API events.

Uses jint to provide .NET javascript support.

Installing

Download and unpack the attached JSWrapper.zip to the $adiircdir\Plugins folder.
Open Plugins Dialog and load JSWrapper.dll.

Using

Put the javascript files in the $adiircdir\Plugins\js folder, file extension must end with .js, the plugin monitors and updates the folder in real time.

The plugin will call all javascript files with the function name matching the API call e.g OnJoin() (Use plugin docs as reference)

To eat the text or eat the entire event, use EatData.EatNone, EatData.EatText, EatData.EatAll as the return value in the function.

Properties for all objects needs more testing.

Feel free to use/edit/distribute the wrapper, it's public domain.

Global Variables

These variables is always available to the script.

plugin - The c# plugin.
myHost - The plugin host API.
myTools - The plugin tools API.
scriptFile - Full path to the javascript file.

Event Variables

Available variables will be in camelCase version from the docs e.g Server is server, QuitMessage is quitMessage, if unsure just check the JSWrapper source code.

These variables will have a value or NULL depending on the event called.

server - IServer
channel - IChannel
user - IUser
window - object, can be either IServer, IChannel, IUser or ICustomWindow.
quitMessage - string.
partMessage - string.
newNick - string.
mode - string.
message - string.
kickReason - string.
newTopic - string.
notice - string.
data - string.
byUser - string.
editbox - IEditbox.
bytes - byte[] array.
menuItems ToolstripMenuItems[] array. (Not tested)
keyPressEventArgs - KeyPressEventArgs.

Examples

// set a global variable
var joined = 0;

function OnJoin() {
  // print a message to the server window.
  myHost.NotifyUser(server, user.Nick + " just joined " + channel.Name);

  // don't do anything to the original message.
  return EatData.EatNone;
}

function OnPart() { 
  // eat/hide the part message..
  return EatData.EatText;
}

function OnUserJoin() {
  // update the global variable.
  joined = joined + 1;

  // print number of times joined a channel while the script was running.
  myHost.NotifyUser(server, "I have joined a channel " + joined + " times");
}

function OnLoad() {
  // print a message when the script is loaded.
  myHost.NotifyUser(myHost.ActiveWindow, "Loaded script " + scriptFile);

  // add a hook for the command /testjs
  myHost.HookCommand(plugin, "/testjs", "/testjs", "Command for testing HookCommand from javascript");

  // add a hook for the command /testeval
  myHost.HookCommand(plugin, "/testeval", "/testeval", "Command for testing msl evaluation from javascript");

  // add a hook for the command /listnicks
  myHost.HookCommand(plugin, "/listnicks", "/listnicks", "Command for testing list enumeration");

  // add a hook for the command /testcmd
  myHost.HookCommand(plugin, "/testcmd", "/testcmd", "Command for testing sending commands");
}

function OnUnLoad() {
  // unhook the commands
  myHost.UnHookCommand(plugin, "/testjs");
  myHost.UnHookCommand(plugin, "/testeval");
  myHost.UnHookCommand(plugin, "/listnicks");
  myHost.UnHookCommand(plugin, "/testcmd");

  // print a message when the script is unloaded.
  myHost.NotifyUser(myHost.ActiveWindow, "Unloaded script " + scriptFile);  
}

function OnCommand() {
  if (command == "/testjs") {

    // if the typed command was /testjs, print a message.
    myHost.NotifyUser(window, "You typed /testjs " + args);

  } else if (command == "/testeval") {

    // if the typed command was /testeval, try to evaluate "$me" 
    myHost.NotifyUser(window, "$me evaluates to " + myHost.Evaluate(myHost.ActiveWindow, "$me", ""));

  } else if (command == "/listnicks") {

    // if the typed command was /testnicks, print all nicks in the channel
    for (var i = 0; i < window.GetUsers.Count; i++) {
      myHost.NotifyUser(myHost.ActiveWindow, window.GetUsers[i].Nick);
    }

  } else if (command == "/testcmd") {

   // send the command "/echo -ag Hello World" to the active window.
    myHost.SendCommand(myHost.ActiveWindow, "/echo", "-ag Hello World");
  }
}

Changelog

  • v1.2
    Fixed monitoring folder only monitored changes.
    Added scriptFile variable which returns the full path to the js script.
    Added plugin variable which returns a reference to the c# plugin instance.
    Added OnLoad() and OnUnLoad() events which triggers when a js script is modified/added/removed/renamed.
  • v1.1
    Js scripts now retains global variables during it's lifetime, any global functions is run once on load.
    tools renamed to myTools.
  • v1.0
    Initial release