Server module hooks for IRC events
[KiwiIRC.git] / server / client.js
1 var util = require('util'),
2 events = require('events'),
3 crypto = require('crypto'),
4 _ = require('lodash'),
5 State = require('./irc/state.js'),
6 IrcConnection = require('./irc/connection.js').IrcConnection,
7 ClientCommands = require('./clientcommands.js'),
8 WebsocketRpc = require('./websocketrpc.js');
9
10
11 var Client = function (websocket) {
12 var that = this;
13
14 events.EventEmitter.call(this);
15 this.websocket = websocket;
16 this.rpc = new WebsocketRpc(this.websocket);
17
18 // Clients address
19 this.real_address = this.websocket.meta.real_address;
20
21 // A hash to identify this client instance
22 this.hash = crypto.createHash('sha256')
23 .update(this.real_address)
24 .update('' + Date.now())
25 .update(Math.floor(Math.random() * 100000).toString())
26 .digest('hex');
27
28 this.state = new State(this);
29
30 this.buffer = {
31 list: [],
32 motd: ''
33 };
34
35 // Handler for any commands sent from the client
36 this.client_commands = new ClientCommands(this);
37
38 this.rpc.on('irc', function (response, data) {
39 handleClientMessage.call(that, data, response);
40 });
41 this.rpc.on('kiwi', function (response, data) {
42 kiwiCommand.call(that, data, response);
43 });
44 websocket.on('close', function () {
45 websocketDisconnect.apply(that, arguments);
46 });
47 websocket.on('error', function () {
48 websocketError.apply(that, arguments);
49 });
50
51 this.disposed = false;
52 };
53 util.inherits(Client, events.EventEmitter);
54
55 module.exports.Client = Client;
56
57 // Callback API:
58 // Callbacks SHALL accept 2 arguments, error and response, in that order.
59 // error MUST be null where the command is successul.
60 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
61 // response MAY be given even if error is truthy
62
63 Client.prototype.sendIrcCommand = function (command, data, callback) {
64 var c = {command: command, data: data};
65 this.rpc.call('irc', c, callback);
66 };
67
68 Client.prototype.sendKiwiCommand = function (command, data, callback) {
69 var c = {command: command, data: data};
70 this.rpc.call('kiwi', c, callback);
71 };
72
73 Client.prototype.dispose = function () {
74 this.disposed = true;
75 this.rpc.dispose();
76 this.emit('dispose');
77 this.removeAllListeners();
78 };
79
80 function handleClientMessage(msg, callback) {
81 var that = this,
82 server;
83
84 // Make sure we have a server number specified
85 if ((msg.server === null) || (typeof msg.server !== 'number')) {
86 return (typeof callback === 'function') ? callback('server not specified') : undefined;
87 } else if (!this.state.irc_connections[msg.server]) {
88 return (typeof callback === 'function') ? callback('not connected to server') : undefined;
89 }
90
91 // The server this command is directed to
92 server = this.state.irc_connections[msg.server];
93
94 if (typeof callback !== 'function') {
95 callback = null;
96 }
97
98 try {
99 msg.data = JSON.parse(msg.data);
100 } catch (e) {
101 kiwi.log('[handleClientMessage] JSON parsing error ' + msg.data);
102 return;
103 }
104
105 // Run the client command
106 global.modules.emit('client command', {
107 command: msg.data,
108 server: server
109 })
110 .done(function() {
111 that.client_commands.run(msg.data.method, msg.data.args, server, callback);
112 });
113 }
114
115
116
117
118 function kiwiCommand(command, callback) {
119 if (typeof callback !== 'function') {
120 callback = function () {};
121 }
122
123 switch (command.command) {
124 case 'connect':
125 if (command.hostname && command.port && command.nick) {
126 var options = {};
127
128 // Get any optional parameters that may have been passed
129 if (command.encoding)
130 options.encoding = command.encoding;
131
132 options.password = global.config.restrict_server_password || command.password;
133
134 this.state.connect(
135 (global.config.restrict_server || command.hostname),
136 (global.config.restrict_server_port || command.port),
137 (typeof global.config.restrict_server_ssl !== 'undefined' ?
138 global.config.restrict_server_ssl :
139 command.ssl),
140 command.nick,
141 {hostname: this.websocket.meta.revdns, address: this.websocket.meta.real_address},
142 options,
143 callback);
144 } else {
145 return callback('Hostname, port and nickname must be specified');
146 }
147 break;
148 default:
149 callback();
150 }
151 }
152
153
154 // Websocket has disconnected, so quit all the IRC connections
155 function websocketDisconnect() {
156 this.emit('disconnect');
157
158 this.dispose();
159 }
160
161
162 // TODO: Should this close all the websocket connections too?
163 function websocketError() {
164 this.dispose();
165 }