Initial server modules implementation + example
authorDarren <darren@darrenwhitlen.com>
Tue, 20 Nov 2012 11:31:39 +0000 (11:31 +0000)
committerDarren <darren@darrenwhitlen.com>
Tue, 20 Nov 2012 11:31:39 +0000 (11:31 +0000)
server/client.js
server/clientcommands.js
server/kiwi.js
server/plugins.js [new file with mode: 0644]
server_modules/example.js [new file with mode: 0644]

index 36edf0c0254088b4a797f7c90b48cd172a24bb31..60912be9e4cfbe92ab0015eb4a4fe0d438354224 100755 (executable)
@@ -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);
 
index 779b44e717ab722a4bfeb10aa4c690c04f62089b..9118ba5bde879935b7a07b10f59c3c4523c098e8 100644 (file)
@@ -22,6 +22,12 @@ ClientCommands.prototype.run = function (command, args, irc_connection, callback
 \r
 var listeners = {\r
     PRIVMSG: function (args, irc_connection, callback) {\r
+        var data = {};\r
+        data.args = args;\r
+        data.client = null;\r
+\r
+        global.plugins.emit('client:commands:msg', data);\r
+\r
         if (args.target && (args.msg)) {\r
             // TODO: Enable plugin support here again\r
             //obj = kiwi.kiwi_mod.run('msgsend', args, {websocket: websocket});\r
index 8151db4aa9459a8c547a8ecc8e41735a80b189ed..fafc5cfd574d64c1a8d4253207ca9c596071f342 100755 (executable)
@@ -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 (file)
index 0000000..cb94427
--- /dev/null
@@ -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 (file)
index 0000000..9fa30cf
--- /dev/null
@@ -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