Server module refactor; Control module refactor;
[KiwiIRC.git] / server_modules / control.js
1 /**
2 * Server control via TCP socket
3 *
4 * Listens on localhost:8888 by default
5 */
6
7 var net = require('net'),
8 kiwiModules = require('../server/modules'),
9 rehash = require('../server/rehash.js'),
10 config = require('../server/configuration.js'),
11 _ = require('lodash');
12
13 var module = new kiwiModules.Module('Control');
14
15
16 /**
17 * The socket client
18 */
19 function SocketClient (socket) {
20 this.socket = socket;
21
22 this.remoteAddress = this.socket.remoteAddress;
23 console.log('Control connection from ' + this.socket.remoteAddress + ' opened');
24
25 this.bindEvents();
26
27 socket.write("\nHello, you are connected to the Kiwi server :)\n\n");
28 this.displayPrompt();
29 }
30
31 SocketClient.prototype.bindEvents = function() {
32 var that = this;
33
34 this.socket.on('data', function() { that.onData.apply(that, arguments); });
35 this.socket.on('close', function() { that.onClose.apply(that, arguments); });
36 };
37 SocketClient.prototype.unbindEvents = function() {
38 this.socket.removeAllListeners();
39 };
40
41
42
43 SocketClient.prototype.write = function(data, append) {
44 if (typeof append === 'undefined') append = '\n';
45 this.socket.write(data + append);
46 };
47 SocketClient.prototype.displayPrompt = function(prompt) {
48 prompt = prompt || 'Kiwi > ';
49 this.write(prompt, '');
50 };
51
52
53
54 SocketClient.prototype.onData = function(data) {
55 data = data.toString().trim();
56
57
58
59 try {
60 var data_split = data.split(' ');
61
62 if (typeof socket_commands[data_split[0]] === 'function') {
63 socket_commands[data_split[0]].call(this, data_split.slice(1));
64 } else {
65 this.write('Unrecognised command: ' + data);
66 }
67
68 } catch (err) {
69 console.log('[Control error] ' + err);
70 this.write('An error occured. Check the Kiwi server log for more details');
71 }
72
73 this.displayPrompt();
74 };
75
76
77 SocketClient.prototype.onClose = function() {
78 this.unbindEvents();
79 console.log('Control connection from ' + this.remoteAddress + ' closed');
80 };
81
82
83
84 /**
85 * Available commands
86 * Each function is run in context of the SocketClient
87 */
88 var socket_commands = {
89 module: function(data) {
90 switch(data[0]) {
91 case 'reload':
92 if (!data[1]) {
93 this.write('A module name must be specified');
94 return;
95 }
96
97 if (!kiwiModules.unload(data[1])) {
98 this.write('Module ' + (data[1] || '') + ' was not found');
99 return;
100 }
101
102 kiwiModules.load(data[1]);
103 this.write('Module ' + data[1] + ' reloaded');
104
105 break;
106
107 case 'list':
108 case 'ls':
109 default:
110 this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', '));
111 }
112
113 },
114
115 stats: function(data) {
116 this.write('Connected clients: ' + _.size(global.clients.clients).toString());
117 this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString());
118 },
119
120 rehash: function(data) {
121 rehash.rehashAll();
122 this.write('Rehashed');
123 },
124
125 reconfig: function(data) {
126 if (config.loadConfig()) {
127 this.write('New config file loaded');
128 } else {
129 this.write("No new config file was loaded");
130 }
131 },
132
133 quit: function(data) {
134 this.socket.destroy();
135 },
136 exit: function(data) {
137 this.socket.destroy();
138 }
139 };
140
141
142 /**
143 * Start the control socket server to serve connections
144 */
145 var server = net.createServer(function (socket) {
146 new SocketClient(socket);
147 });
148 server.listen(8888);