Channel Info+admin window; RPL_CHANNEL_URL; Ban list; CSS forms refactor
[KiwiIRC.git] / server / controlinterface.js
CommitLineData
4eba7612
D
1var _ = require('lodash'),
2 rehash = require('./rehash.js'),
3 config = require('../server/configuration.js'),
4 kiwiModules = require('../server/modules');
5
6
7
8var ControlInterface = module.exports = function(stream_in, stream_out, opts) {
9 stream_out = stream_out || stream_in;
10 this.stream_out = stream_out;
11 this.stream_in = stream_in;
12
13 opts = opts || {};
14 this.prompt = (typeof opts.prompt === 'string') ?
15 opts.prompt :
16 'Kiwi > ';
17
18 this._custom_commands = {};
19
20 this._onData = this.onData.bind(this);
21 stream_in.on('data', this._onData);
22
23 this.displayPrompt();
24};
25
26
27
28ControlInterface.prototype.dispose = function() {
29 this.stream_in.removeListener('data', this._onData);
30 this.stream_in = null;
31 this.stream_out = null;
32};
33
34
35
36ControlInterface.prototype.write = function(data, append) {
37 if (typeof append === 'undefined') append = '\n';
38 try {
39 this.stream_out.write(data + append);
40 } catch(err){}
41};
42
43
44
45ControlInterface.prototype.displayPrompt = function(prompt) {
46 prompt = prompt || this.prompt;
47 this.write(prompt, '');
48};
49
50
51
52ControlInterface.prototype.onData = function(buffered) {
53 var data = buffered.toString().trim(),
54 data_parts = data.split(' '),
55 cmd = data_parts[0] || null;
56
57 if (typeof this._custom_commands[cmd] === 'function') {
58 this._custom_commands[cmd].call(this, data_parts.slice(1), data);
59
60 } else if (typeof commands[cmd] === 'function') {
61 commands[cmd].call(this, data_parts.slice(1), data);
62
63 } else {
64 this.write('Unrecognised command: ' + cmd);
65 }
66
67 this.displayPrompt();
68};
69
70
71
72ControlInterface.prototype.addCommand = function(command, fn) {
73 this._custom_commands[command] = fn;
74};
75
76
77
78var commands = {};
79commands.stats = function(args, raw) {
80 this.write('Connected clients: ' + _.size(global.clients.clients).toString());
81 this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString());
82};
83
84
85commands.reconfig = function(args, raw) {
86 if (config.loadConfig()) {
40966766 87 this.write('New config file loaded');
4eba7612 88 } else {
40966766 89 this.write("No new config file was loaded");
4eba7612
D
90 }
91};
92
93
94commands.rehash = function(args, raw) {
95 rehash.rehashAll();
40966766 96 this.write('Rehashed');
4eba7612
D
97};
98
99
100commands.jumpserver = function(args, raw) {
40966766
D
101 var that = this,
102 num_clients = _.size(global.clients.clients),
4eba7612
D
103 packet = {}, args_idx;
104
105 if (num_clients === 0) {
40966766 106 this.write('No connected clients');
4eba7612
D
107 return;
108 }
109
110 // For each word in the line minus the last, add it to the packet
111 for(args_idx=0; args_idx<args.length-1; args_idx++){
112 packet[args[args_idx]] = true;
113 }
114
115 packet.kiwi_server = args[args_idx];
116
7b8d3350
D
117 if (!packet.kiwi_server) {
118 this.write('No Kiwi server specified');
119 return;
120 }
121
40966766 122 this.write('Broadcasting jumpserver to ' + num_clients.toString() + ' clients..');
4eba7612 123 global.clients.broadcastKiwiCommand('jumpserver', packet, function() {
40966766 124 that.write('Broadcast complete.');
4eba7612
D
125 });
126};
127
128
129commands.module = function(args, raw) {
130 switch(args[0]) {
131 case 'reload':
132 if (!args[1]) {
133 this.write('A module name must be specified');
134 return;
135 }
136
137 if (!kiwiModules.unload(args[1])) {
138 this.write('Module ' + (args[1] || '') + ' is not loaded');
139 return;
140 }
141
142 if (!kiwiModules.load(args[1])) {
143 this.write('Error loading module ' + (args[1] || ''));
144 }
145 this.write('Module ' + args[1] + ' reloaded');
146
147 break;
148
149 case 'list':
150 case 'ls':
151 default:
152 var module_names = [];
153 kiwiModules.getRegisteredModules().forEach(function(module) {
154 module_names.push(module.module_name);
155 });
156 this.write('Loaded modules: ' + module_names.join(', '));
157 }
158};
159
160
161commands.hello = function(args, raw) {
162 this.write('Hello, beautiful :)');
163};