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');
10 var Client = function (websocket
) {
13 events
.EventEmitter
.call(this);
14 this.websocket
= websocket
;
17 this.real_address
= this.websocket
.handshake
.real_address
;
19 // A hash to identify this client instance
20 this.hash
= crypto
.createHash('sha256')
21 .update(this.real_address
)
22 .update('' + Date
.now())
23 .update(Math
.floor(Math
.random() * 100000).toString())
26 this.state
= new State(this);
33 // Handler for any commands sent from the client
34 this.client_commands
= new ClientCommands(this);
36 websocket
.on('irc', function () {
37 handleClientMessage
.apply(that
, arguments
);
39 websocket
.on('kiwi', function () {
40 kiwiCommand
.apply(that
, arguments
);
42 websocket
.on('disconnect', function () {
43 websocketDisconnect
.apply(that
, arguments
);
45 websocket
.on('error', function () {
46 websocketError
.apply(that
, arguments
);
49 this.disposed
= false;
51 util
.inherits(Client
, events
.EventEmitter
);
53 module
.exports
.Client
= Client
;
56 // Callbacks SHALL accept 2 arguments, error and response, in that order.
57 // error MUST be null where the command is successul.
58 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
59 // response MAY be given even if error is truthy
61 Client
.prototype.sendIrcCommand = function (command
, data
, callback
) {
62 var c
= {command
: command
, data
: data
};
63 this.websocket
.emit('irc', c
, callback
);
66 Client
.prototype.sendKiwiCommand = function (command
, data
, callback
) {
67 var c
= {command
: command
, data
: data
};
68 this.websocket
.emit('kiwi', c
, callback
);
71 Client
.prototype.dispose = function () {
74 this.removeAllListeners();
77 function handleClientMessage(msg
, callback
) {
80 // Make sure we have a server number specified
81 if ((msg
.server
=== null) || (typeof msg
.server
!== 'number')) {
82 return (typeof callback
=== 'function') ? callback('server not specified') : undefined;
83 } else if (!this.state
.irc_connections
[msg
.server
]) {
84 return (typeof callback
=== 'function') ? callback('not connected to server') : undefined;
87 // The server this command is directed to
88 server
= this.state
.irc_connections
[msg
.server
];
90 if (typeof callback
!== 'function') {
95 msg
.data
= JSON
.parse(msg
.data
);
97 kiwi
.log('[handleClientMessage] JSON parsing error ' + msg
.data
);
101 // Run the client command
102 this.client_commands
.run(msg
.data
.method
, msg
.data
.args
, server
, callback
);
108 function kiwiCommand(command
, callback
) {
109 if (typeof callback
!== 'function') {
110 callback = function () {};
113 switch (command
.command
) {
115 if (command
.hostname
&& command
.port
&& command
.nick
) {
118 // Get any optional parameters that may have been passed
119 if (command
.encoding
)
120 options
.encoding
= command
.encoding
;
122 options
.password
= global
.config
.restrict_server_password
|| command
.password
;
125 (global
.config
.restrict_server
|| command
.hostname
),
126 (global
.config
.restrict_server_port
|| command
.port
),
127 (typeof global
.config
.restrict_server_ssl
!== 'undefined' ?
128 global
.config
.restrict_server_ssl
:
131 {hostname
: this.websocket
.handshake
.revdns
, address
: this.websocket
.handshake
.real_address
},
135 return callback('Hostname, port and nickname must be specified');
144 // Websocket has disconnected, so quit all the IRC connections
145 function websocketDisconnect() {
146 this.emit('disconnect');
152 // TODO: Should this close all the websocket connections too?
153 function websocketError() {