764d600ece700e61b7a5729f8b9b84e4e6e528c7
1 var util
= require('util'),
2 events
= require('events'),
3 _
= require('underscore'),
4 config
= require('./configuration.js'),
5 IrcConnection
= require('./irc/connection.js').IrcConnection
,
6 IrcCommands
= require('./irc/commands.js'),
7 ClientCommands
= require('./clientcommands.js');
10 var Client = function (websocket
) {
13 events
.EventEmitter
.call(this);
14 this.websocket
= websocket
;
16 this.irc_connections
= [];
17 this.next_connection
= 0;
24 // Handler for any commands sent from the client
25 this.client_commands
= new ClientCommands(this);
27 websocket
.on('irc', function () {
28 handleClientMessage
.apply(that
, arguments
);
30 websocket
.on('kiwi', function () {
31 kiwiCommand
.apply(that
, arguments
);
33 websocket
.on('disconnect', function () {
34 websocketDisconnect
.apply(that
, arguments
);
36 websocket
.on('error', function () {
37 websocketError
.apply(that
, arguments
);
40 util
.inherits(Client
, events
.EventEmitter
);
42 module
.exports
.Client
= Client
;
45 // Callbacks SHALL accept 2 arguments, error and response, in that order.
46 // error MUST be null where the command is successul.
47 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
48 // response MAY be given even if error is truthy
50 Client
.prototype.sendIrcCommand = function (command
, data
, callback
) {
51 var c
= {command
: command
, data
: data
};
52 this.websocket
.emit('irc', c
, callback
);
55 Client
.prototype.sendKiwiCommand = function (command
, data
, callback
) {
56 var c
= {command
: command
, data
: data
};
57 this.websocket
.emit('kiwi', c
, callback
);
60 function handleClientMessage(msg
, callback
) {
61 var server
, args
, obj
, channels
, keys
;
63 // Make sure we have a server number specified
64 if ((msg
.server
=== null) || (typeof msg
.server
!== 'number')) {
65 return (typeof callback
=== 'function') ? callback('server not specified') : undefined;
66 } else if (!this.irc_connections
[msg
.server
]) {
67 return (typeof callback
=== 'function') ? callback('not connected to server') : undefined;
70 // The server this command is directed to
71 server
= this.irc_connections
[msg
.server
];
73 if (typeof callback
!== 'function') {
78 msg
.data
= JSON
.parse(msg
.data
);
80 kiwi
.log('[handleClientMessage] JSON parsing error ' + msg
.data
);
84 // Run the client command
85 this.client_commands
.run(msg
.data
.method
, msg
.data
.args
, server
, callback
);
91 function kiwiCommand(command
, callback
) {
94 if (typeof callback
!== 'function') {
95 callback = function () {};
97 switch (command
.command
) {
99 if (command
.hostname
&& command
.port
&& command
.nick
) {
100 var con
= new IrcConnection(command
.hostname
, command
.port
, command
.ssl
,
101 command
.nick
, {hostname
: this.websocket
.handshake
.revdns
, address
: this.websocket
.handshake
.address
.address
},
104 var con_num
= this.next_connection
++;
105 this.irc_connections
[con_num
] = con
;
107 var irc_commands
= new IrcCommands(con
, con_num
, this);
108 irc_commands
.bindEvents();
110 con
.on('connected', function () {
111 console
.log("con.on('connected')");
112 return callback(null, con_num
);
115 con
.on('error', function (err
) {
116 // TODO: Once multiple servers implemented, specify which server failed
117 //that.sendKiwiCommand('error', {server: con_num, error: err});
118 return callback(err
.code
, null);
121 con
.on('close', function () {
122 that
.irc_connections
[con_num
] = null;
125 return callback('Hostname, port and nickname must be specified');
134 // Websocket has disconnected, so quit all the IRC connections
135 function websocketDisconnect() {
136 _
.each(this.irc_connections
, function (irc_connection
, i
, cons
) {
137 if (irc_connection
) {
138 irc_connection
.end('QUIT :' + (config
.get().quit_message
|| ''));
142 this.emit('destroy');
146 // TODO: Should this close all the websocket connections too?
147 function websocketError() {
148 this.emit('destroy');