1 var util
= require('util'),
2 events
= require('events'),
3 crypto
= require('crypto'),
5 State
= require('./irc/state.js'),
6 IrcConnection
= require('./irc/connection.js').IrcConnection
,
7 ClientCommands
= require('./clientcommands.js'),
8 WebsocketRpc
= require('./websocketrpc.js');
11 var Client = function (websocket
) {
14 events
.EventEmitter
.call(this);
15 this.websocket
= websocket
;
16 this.rpc
= new WebsocketRpc(this.websocket
);
19 this.real_address
= this.websocket
.meta
.real_address
;
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())
28 this.state
= new State(this);
35 // Handler for any commands sent from the client
36 this.client_commands
= new ClientCommands(this);
37 this.client_commands
.addRpcEvents(this, this.rpc
);
39 // Handles the kiwi.* RPC functions
40 this.attachKiwiCommands();
42 websocket
.on('close', function () {
43 websocketDisconnect
.apply(that
, arguments
);
45 websocket
.on('error', function () {
46 websocketError
.apply(that
, arguments
);
49 this.disposed
= false;
51 // Let the client know it's finished connecting
52 this.sendKiwiCommand('connected');
54 util
.inherits(Client
, events
.EventEmitter
);
56 module
.exports
.Client
= Client
;
59 // Callbacks SHALL accept 2 arguments, error and response, in that order.
60 // error MUST be null where the command is successul.
61 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
62 // response MAY be given even if error is truthy
64 Client
.prototype.sendIrcCommand = function (command
, data
, callback
) {
65 var c
= {command
: command
, data
: data
};
66 this.rpc
.call('irc', c
, callback
);
69 Client
.prototype.sendKiwiCommand = function (command
, data
, callback
) {
70 var c
= {command
: command
, data
: data
};
71 this.rpc
.call('kiwi', c
, callback
);
74 Client
.prototype.dispose = function () {
78 this.removeAllListeners();
83 Client
.prototype.attachKiwiCommands = function() {
86 this.rpc
.on('kiwi.connect_irc', function(callback
, command
) {
87 if (command
.hostname
&& command
.port
&& command
.nick
) {
90 // Get any optional parameters that may have been passed
92 options
.encoding
= command
.encoding
;
94 options
.password
= global
.config
.restrict_server_password
|| command
.password
;
97 (global
.config
.restrict_server
|| command
.hostname
),
98 (global
.config
.restrict_server_port
|| command
.port
),
99 (typeof global
.config
.restrict_server_ssl
!== 'undefined' ?
100 global
.config
.restrict_server_ssl
:
103 {hostname
: that
.websocket
.meta
.revdns
, address
: that
.websocket
.meta
.real_address
},
107 return callback('Hostname, port and nickname must be specified');
112 this.rpc
.on('kiwi.client_info', function(callback
, args
) {
113 // keep hold of selected parts of the client_info
115 build_version
: args
.build_version
.toString() || undefined
122 // Websocket has disconnected, so quit all the IRC connections
123 function websocketDisconnect() {
124 this.emit('disconnect');
130 // TODO: Should this close all the websocket connections too?
131 function websocketError() {