707ca54052936cd98a69c23ce16218118df7a24c
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
.kiwi
.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);
38 this.rpc
.on('irc', function (response
, data
) {
39 handleClientMessage
.call(that
, data
, response
);
41 this.rpc
.on('kiwi', function (response
, data
) {
42 kiwiCommand
.call(that
, data
, response
);
44 websocket
.on('close', function () {
45 websocketDisconnect
.apply(that
, arguments
);
47 websocket
.on('error', function () {
48 websocketError
.apply(that
, arguments
);
51 this.disposed
= false;
53 util
.inherits(Client
, events
.EventEmitter
);
55 module
.exports
.Client
= Client
;
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
63 Client
.prototype.sendIrcCommand = function (command
, data
, callback
) {
64 var c
= {command
: command
, data
: data
};
65 this.rpc
.call('irc', c
, callback
);
68 Client
.prototype.sendKiwiCommand = function (command
, data
, callback
) {
69 var c
= {command
: command
, data
: data
};
70 this.rpc
.call('kiwi', c
, callback
);
73 Client
.prototype.dispose = function () {
76 this.removeAllListeners();
79 function handleClientMessage(msg
, callback
) {
82 // Make sure we have a server number specified
83 if ((msg
.server
=== null) || (typeof msg
.server
!== 'number')) {
84 return (typeof callback
=== 'function') ? callback('server not specified') : undefined;
85 } else if (!this.state
.irc_connections
[msg
.server
]) {
86 return (typeof callback
=== 'function') ? callback('not connected to server') : undefined;
89 // The server this command is directed to
90 server
= this.state
.irc_connections
[msg
.server
];
92 if (typeof callback
!== 'function') {
97 msg
.data
= JSON
.parse(msg
.data
);
99 kiwi
.log('[handleClientMessage] JSON parsing error ' + msg
.data
);
103 // Run the client command
104 this.client_commands
.run(msg
.data
.method
, msg
.data
.args
, server
, callback
);
110 function kiwiCommand(command
, callback
) {
111 if (typeof callback
!== 'function') {
112 callback = function () {};
115 switch (command
.command
) {
117 if (command
.hostname
&& command
.port
&& command
.nick
) {
120 // Get any optional parameters that may have been passed
121 if (command
.encoding
)
122 options
.encoding
= command
.encoding
;
124 options
.password
= global
.config
.restrict_server_password
|| command
.password
;
127 (global
.config
.restrict_server
|| command
.hostname
),
128 (global
.config
.restrict_server_port
|| command
.port
),
129 (typeof global
.config
.restrict_server_ssl
!== 'undefined' ?
130 global
.config
.restrict_server_ssl
:
133 {hostname
: this.websocket
.kiwi
.revdns
, address
: this.websocket
.kiwi
.real_address
},
137 return callback('Hostname, port and nickname must be specified');
146 // Websocket has disconnected, so quit all the IRC connections
147 function websocketDisconnect() {
148 this.emit('disconnect');
154 // TODO: Should this close all the websocket connections too?
155 function websocketError() {