From 1920a38ef654522fc868706ffdad31ca88aa9fc9 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 20 Nov 2012 11:31:39 +0000 Subject: [PATCH] Initial server modules implementation + example --- server/client.js | 2 + server/clientcommands.js | 6 +++ server/kiwi.js | 14 +++++- server/plugins.js | 94 +++++++++++++++++++++++++++++++++++++++ server_modules/example.js | 14 ++++++ 5 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 server/plugins.js create mode 100644 server_modules/example.js diff --git a/server/client.js b/server/client.js index 36edf0c..60912be 100755 --- a/server/client.js +++ b/server/client.js @@ -46,6 +46,8 @@ var Client = function (websocket) { websocket.on('error', function () { websocketError.apply(that, arguments); }); + + global.plugins.emit('client:connected', {client:this}); }; util.inherits(Client, events.EventEmitter); diff --git a/server/clientcommands.js b/server/clientcommands.js index 779b44e..9118ba5 100644 --- a/server/clientcommands.js +++ b/server/clientcommands.js @@ -22,6 +22,12 @@ ClientCommands.prototype.run = function (command, args, irc_connection, callback var listeners = { PRIVMSG: function (args, irc_connection, callback) { + var data = {}; + data.args = args; + data.client = null; + + global.plugins.emit('client:commands:msg', data); + if (args.target && (args.msg)) { // TODO: Enable plugin support here again //obj = kiwi.kiwi_mod.run('msgsend', args, {websocket: websocket}); diff --git a/server/kiwi.js b/server/kiwi.js index 8151db4..fafc5cf 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -2,7 +2,8 @@ var fs = require('fs'), _ = require('lodash'), WebListener = require('./weblistener.js'), config = require('./configuration.js'), - rehash = require('./rehash.js'); + rehash = require('./rehash.js'), + plugins = require('./plugins'); @@ -55,6 +56,17 @@ if ((!global.config.servers) || (global.config.servers.length < 1)) { +// Create a plugin interface +global.plugins = new plugins.Publisher(); + +// Register as the active imterfac +plugins.registerPublisher(global.plugins); + +require('../server_modules/example.js'); + + + + // Holder for all the connected clients global.clients = { diff --git a/server/plugins.js b/server/plugins.js new file mode 100644 index 0000000..cb94427 --- /dev/null +++ b/server/plugins.js @@ -0,0 +1,94 @@ +var events = require('events'), + util = require('util'); + + +// Where events are bound to +var active_publisher; + + +// Create a publisher to allow event subscribing +function Publisher (obj) { + var EventPublisher = function pluginPublisher() {}; + util.inherits(EventPublisher, events.EventEmitter); + + return new EventPublisher(); +}; + + +// Register an already created Publisher() as the active instance +function registerPublisher (obj) { + active_publisher = obj; +} + + + +function Plugin (plugin_name) { + + // Holder for all the bound events by this plugin + var bound_events = {}; + + // Handy function to be a little more consistant with EventEmitter + this._events = function () { + return bound_events; + }; + + + // Keep track of this plugins events and bind + this.subscribe = function (event_name, fn) { + bound_events[event_name] = bound_events[event_name] || []; + bound_events[event_name].push(fn); + + global.plugins.on(event_name, fn); + }; + + + // Keep track of this plugins events and bind once + this.once = function (event_name, fn) { + bound_events[event_name] = bound_events[event_name] || []; + bound_events[event_name].push(fn); + + global.plugins.once(event_name, fn); + }; + + + // Remove any events by this plugin only + this.unsubscribe = function (event_name, fn) { + var idx; + + if (typeof event_name === 'undefined') { + // Remove all events + bound_events = []; + + } else if (typeof fn === 'undefined') { + // Remove all of 1 event type + delete bound_events[event_name]; + + } else { + // Remove a single event + callback + for (idx in (bound_events[event_name] || [])) { + if (bound_events[event_name][idx] === fn) { + delete bound_events[event_name][idx]; + } + } + } + + global.plugins.removeListener(event_name, fn); + }; + + + // Clean up anything used by this plugin + this.dispose = function () { + this.unsubscribe(); + }; +}; + + + +module.exports = { + // Objects + Plugin: Plugin, + Publisher: Publisher, + + // Methods + registerPublisher: registerPublisher +}; \ No newline at end of file diff --git a/server_modules/example.js b/server_modules/example.js new file mode 100644 index 0000000..9fa30cf --- /dev/null +++ b/server_modules/example.js @@ -0,0 +1,14 @@ +var kiwiPlugins = require('../server/plugins.js'); + +var plugin = new kiwiPlugins.Plugin('Example Plugin'); + + +plugin.subscribe('client:connected', function(data) { + console.log('Client connection:', data); +}); + + +plugin.subscribe('client:commands:msg', function(data) { + console.log('Client msg:', data.args.target, ': ', data.args.msg); + data.args.msg += ' - modified!'; +}); \ No newline at end of file -- 2.25.1