1 var util
= require('util'),
2 events
= require('events'),
3 _
= require('underscore'),
4 IRCConnection
= require('./irc-connection.js').IRCConnection
,
5 IRCCommands
= require('./irc-commands.js'),
6 ClientCommandset
= require('./client-commands.js').ClientCommandset
;
8 var Client = function (websocket
) {
11 events
.EventEmitter
.call(this);
12 this.websocket
= websocket
;
14 this.IRC_connections
= [];
15 this.next_connection
= 0;
22 // Handler for any commands sent from the client
23 this.client_commands
= new ClientCommandset(this);
25 websocket
.on('irc', function () {
26 handleClientMessage
.apply(c
, arguments
);
28 websocket
.on('kiwi', function () {
29 kiwi_command
.apply(c
, arguments
);
31 websocket
.on('disconnect', function () {
32 disconnect
.apply(c
, arguments
);
34 websocket
.on('error', function () {
35 error
.apply(c
, arguments
);
38 util
.inherits(Client
, events
.EventEmitter
);
40 module
.exports
.Client
= Client
;
43 // Callbacks SHALL accept 2 arguments, error and response, in that order.
44 // error MUST be null where the command is successul.
45 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
46 // response MAY be given even if error is truthy
48 Client
.prototype.sendIRCCommand = function (command
, data
, callback
) {
49 var c
= {command
: command
, data
: data
};
50 console
.log('C<--', c
);
51 this.websocket
.emit('irc', c
, callback
);
54 Client
.prototype.sendKiwiCommand = function (command
, callback
) {
55 this.websocket
.emit('kiwi', command
, callback
);
58 var handleClientMessage = function (msg
, callback
) {
59 var server
, args
, obj
, channels
, keys
;
61 // Make sure we have a server number specified
62 if ((msg
.server
=== null) || (typeof msg
.server
!== 'number')) {
63 return callback('server not specified');
64 } else if (!this.IRC_connections
[msg
.server
]) {
65 return callback('not connected to server');
68 // The server this command is directed to
69 server
= this.IRC_connections
[msg
.server
];
71 if (typeof callback
!== 'function') {
76 msg
.data
= JSON
.parse(msg
.data
);
78 kiwi
.log('[handleClientMessage] JSON parsing error ' + msg
.data
);
82 // Run the client command
83 this.client_commands
.run(msg
.data
.method
, msg
.data
.args
, server
, callback
);
89 var kiwi_command = function (command
, callback
) {
91 console
.log(typeof callback
);
92 if (typeof callback
!== 'function') {
93 callback = function () {};
95 switch (command
.command
) {
97 if ((command
.hostname
) && (command
.port
) && (command
.nick
)) {
98 var con
= new IRCConnection(command
.hostname
, command
.port
, command
.ssl
,
99 command
.nick
, {hostname
: this.websocket
.handshake
.revdns
, address
: this.websocket
.handshake
.address
.address
},
100 command
.password
, null);
102 var con_num
= this.next_connection
++;
103 this.IRC_connections
[con_num
] = con
;
105 var binder
= new IRCCommands
.Binder(con
, con_num
, this);
106 binder
.bind_irc_commands();
108 con
.on('connected', function () {
109 console
.log("con.on('connected')");
110 return callback(null, con_num
);
113 con
.on('error', function (err
) {
114 this.websocket
.sendKiwiCommand('error', {server
: con_num
, error
: err
});
117 con
.on('close', function () {
118 that
.IRC_connections
[con_num
] = null;
121 return callback('Hostname, port and nickname must be specified');
129 var extension_command = function (command
, callback
) {
130 if (typeof callback
=== 'function') {
131 callback('not implemented');
135 var disconnect = function () {
136 _
.each(this.IRC_connections
, function (irc_connection
, i
, cons
) {
137 if (irc_connection
) {
138 irc_connection
.end('QUIT :Kiwi IRC');
142 this.emit('destroy');
145 var error = function () {
146 this.emit('destroy');