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'),
9 Stats
= require('./stats.js');
12 var Client = function (websocket
, opts
) {
15 Stats
.incr('client.created');
17 events
.EventEmitter
.call(this);
18 this.websocket
= websocket
;
20 // Keep a record of how this client connected
21 this.server_config
= opts
.server_config
;
23 this.rpc
= new WebsocketRpc(this.websocket
);
24 this.rpc
.on('all', function(func_name
, return_fn
) {
25 if (typeof func_name
=== 'string' && typeof return_fn
=== 'function') {
26 Stats
.incr('client.command');
27 Stats
.incr('client.command.' + func_name
);
32 this.real_address
= this.websocket
.meta
.real_address
;
34 // A hash to identify this client instance
35 this.hash
= crypto
.createHash('sha256')
36 .update(this.real_address
)
37 .update('' + Date
.now())
38 .update(Math
.floor(Math
.random() * 100000).toString())
41 this.state
= new State(this);
48 // Handler for any commands sent from the client
49 this.client_commands
= new ClientCommands(this);
50 this.client_commands
.addRpcEvents(this, this.rpc
);
52 // Handles the kiwi.* RPC functions
53 this.attachKiwiCommands();
55 websocket
.on('close', function () {
56 websocketDisconnect
.apply(that
, arguments
);
58 websocket
.on('error', function () {
59 websocketError
.apply(that
, arguments
);
62 this.disposed
= false;
64 // Let the client know it's finished connecting
65 this.sendKiwiCommand('connected');
67 util
.inherits(Client
, events
.EventEmitter
);
69 module
.exports
.Client
= Client
;
72 // Callbacks SHALL accept 2 arguments, error and response, in that order.
73 // error MUST be null where the command is successul.
74 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
75 // response MAY be given even if error is truthy
77 Client
.prototype.sendIrcCommand = function (command
, data
, callback
) {
78 var c
= {command
: command
, data
: data
};
79 this.rpc('irc', c
, callback
);
82 Client
.prototype.sendKiwiCommand = function (command
, data
, callback
) {
83 var c
= {command
: command
, data
: data
};
84 this.rpc('kiwi', c
, callback
);
87 Client
.prototype.dispose = function () {
88 Stats
.incr('client.disposed');
93 this.removeAllListeners();
98 Client
.prototype.attachKiwiCommands = function() {
101 this.rpc
.on('kiwi.connect_irc', function(callback
, command
) {
102 if (command
.hostname
&& command
.port
&& command
.nick
) {
105 // Get any optional parameters that may have been passed
106 if (command
.encoding
)
107 options
.encoding
= command
.encoding
;
109 options
.password
= global
.config
.restrict_server_password
|| command
.password
;
112 (global
.config
.restrict_server
|| command
.hostname
),
113 (global
.config
.restrict_server_port
|| command
.port
),
114 (typeof global
.config
.restrict_server_ssl
!== 'undefined' ?
115 global
.config
.restrict_server_ssl
:
118 {hostname
: that
.websocket
.meta
.revdns
, address
: that
.websocket
.meta
.real_address
},
122 return callback('Hostname, port and nickname must be specified');
127 this.rpc
.on('kiwi.client_info', function(callback
, args
) {
128 // keep hold of selected parts of the client_info
130 build_version
: args
.build_version
.toString() || undefined
137 // Websocket has disconnected, so quit all the IRC connections
138 function websocketDisconnect() {
139 this.emit('disconnect');
145 // TODO: Should this close all the websocket connections too?
146 function websocketError() {