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 IrcCommands
= require('./irc/commands.js'),
8 ClientCommands
= require('./clientcommands.js');
11 var Client = function (websocket
) {
14 events
.EventEmitter
.call(this);
15 this.websocket
= websocket
;
18 this.real_address
= this.websocket
.handshake
.real_address
;
20 // A hash to identify this client instance
21 this.hash
= crypto
.createHash('sha256')
22 .update(this.real_address
)
23 .update('' + Date
.now())
24 .update(Math
.floor(Math
.random() * 100000).toString())
27 this.state
= new State(this);
34 // Handler for any commands sent from the client
35 this.client_commands
= new ClientCommands(this);
37 websocket
.on('irc', function () {
38 handleClientMessage
.apply(that
, arguments
);
40 websocket
.on('kiwi', function () {
41 kiwiCommand
.apply(that
, arguments
);
43 websocket
.on('disconnect', function () {
44 websocketDisconnect
.apply(that
, arguments
);
46 websocket
.on('error', function () {
47 websocketError
.apply(that
, arguments
);
50 util
.inherits(Client
, events
.EventEmitter
);
52 module
.exports
.Client
= Client
;
55 // Callbacks SHALL accept 2 arguments, error and response, in that order.
56 // error MUST be null where the command is successul.
57 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
58 // response MAY be given even if error is truthy
60 Client
.prototype.sendIrcCommand = function (command
, data
, callback
) {
61 var c
= {command
: command
, data
: data
};
62 this.websocket
.emit('irc', c
, callback
);
65 Client
.prototype.sendKiwiCommand = function (command
, data
, callback
) {
66 var c
= {command
: command
, data
: data
};
67 this.websocket
.emit('kiwi', c
, callback
);
70 Client
.prototype.dispose = function () {
72 this.removeAllListeners();
75 function handleClientMessage(msg
, callback
) {
76 var server
, args
, obj
, channels
, keys
;
78 // Make sure we have a server number specified
79 if ((msg
.server
=== null) || (typeof msg
.server
!== 'number')) {
80 return (typeof callback
=== 'function') ? callback('server not specified') : undefined;
81 } else if (!this.state
.irc_connections
[msg
.server
]) {
82 return (typeof callback
=== 'function') ? callback('not connected to server') : undefined;
85 // The server this command is directed to
86 server
= this.state
.irc_connections
[msg
.server
];
88 if (typeof callback
!== 'function') {
93 msg
.data
= JSON
.parse(msg
.data
);
95 kiwi
.log('[handleClientMessage] JSON parsing error ' + msg
.data
);
99 // Run the client command
100 this.client_commands
.run(msg
.data
.method
, msg
.data
.args
, server
, callback
);
106 function kiwiCommand(command
, callback
) {
109 if (typeof callback
!== 'function') {
110 callback = function () {};
112 switch (command
.command
) {
114 if (command
.hostname
&& command
.port
&& command
.nick
) {
117 if (global
.config
.restrict_server
) {
119 global
.config
.restrict_server
,
120 global
.config
.restrict_server_port
,
121 global
.config
.restrict_server_ssl
,
123 {hostname
: this.websocket
.handshake
.revdns
, address
: this.websocket
.handshake
.real_address
},
124 global
.config
.restrict_server_password
,
133 {hostname
: this.websocket
.handshake
.revdns
, address
: this.websocket
.handshake
.real_address
},
138 return callback('Hostname, port and nickname must be specified');
147 // Websocket has disconnected, so quit all the IRC connections
148 function websocketDisconnect() {
149 this.emit('disconnect');
155 // TODO: Should this close all the websocket connections too?
156 function websocketError() {