1 var net
= require('net'),
3 events
= require('events'),
4 util
= require('util');
6 var IRCConnection = function (hostname
, port
, ssl
, nick
, user
, pass
, webirc
) {
8 events
.EventEmitter
.call(this);
11 this.socket
= tls
.connect(port
, hostname
, {}, connect_handler
);
13 this.socket
= net
.createConnection(port
, hostname
);
14 this.socket
.on('connect', function () {
15 connect_handler
.apply(that
, arguments
);
19 this.socket
.on('error', function () {
20 var a
= Array
.prototype.slice
.call(arguments
);
22 that
.emit
.apply(this, a
);
25 this.socket
.setEncoding('utf-8');
27 this.socket
.on('data', function () {
28 parse
.apply(that
, arguments
);
31 this.socket
.on('close', function () {
35 this.connected
= false;
36 this.registered
= false;
40 this.options
= Object
.create(null);
44 this.hold_last
= false;
47 util
.inherits(IRCConnection
, events
.EventEmitter
);
49 IRCConnection
.prototype.write = function (data
, callback
) {
50 console
.log('S<--', data
);
51 write
.call(this, data
+ '\r\n', 'utf-8', callback
);
54 IRCConnection
.prototype.end = function (data
, callback
) {
55 console
.log('S<--', data
);
56 console
.log('Closing docket');
57 end
.call(this, data
+ '\r\n', 'utf-8', callback
);
60 var write = function (data
, encoding
, callback
) {
61 this.socket
.write(data
, encoding
, callback
);
64 var end = function (data
, encoding
, callback
) {
65 this.socket
.end(data
, encoding
, callback
);
68 module
.exports
.IRCConnection
= IRCConnection
;
70 var connect_handler = function () {
72 this.write('WEBIRC ' + this.webirc
.pass
+ ' KiwiIRC ' + this.user
.hostname
+ ' ' + this.user
.address
);
75 this.write('PASS ' + this.password
);
77 //this.write('CAP LS');
78 this.write('NICK ' + this.nick
);
79 this.write('USER kiwi_' + this.nick
.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + this.nick
);
81 this.connected
= true;
82 console
.log("IRCConnection.emit('connected')");
83 this.emit('connected');
86 parse_regex
= /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
87 //alt_regex = /(?::(([0-9a-z][\x2d0-9a-z]*[0-9a-z]*(?:\x2e[0-9a-z][\x2d0-9a-z]*[0-9a-z]*)*|[\x5b-\x7d][\x2d0-9\x5b-\x7d]{0,8})(?:(?:!([\x01-\t\v\f\x0e-\x1f!-\x3f\x5b-\xff]+))?@([0-9a-z][\x2d0-9a-z]*[0-9a-z]*(?:\x2e[0-9a-z][\x2d0-9a-z]*[0-9a-z]*)*|\d{1,3}\x2e\d{1,3}\x2e\d{1,3}\x2e\d{1,3}|[0-9a-f]+(?::[0-9a-f]+){7}|0:0:0:0:0:(?:0|ffff):\d{1,3}\x2e\d{1,3}\x2e\d{1,3}\x2e\d{1,3}))?)\x20)?([a-z]+|\d{3})((?:\x20[\x01-\t\v\f\x0e-\x1f!-9;-@\x5b-\xff][\x01-\t\v\f\x0e-\x1f!-@\x5b-\xff]*){0,14}(?:\x20:[\x01-\t\v\f\x0e-@\x5b-\xff]*)?|(?:\x20[\x01-\t\v\f\x0e-\x1f!-9;-@\x5b-\xff][\x01-\t\v\f\x0e-\x1f!-@\x5b-\xff]*){14}(?:\x20:?[\x01-\t\v\f\x0e-@\x5b-\xff]*)?)?/i;
89 var parse = function (data
) {
95 if ((this.hold_last
) && (this.held_data
!== '')) {
96 data
= this.held_data
+ data
;
97 this.hold_last
= false;
100 if (data
.substr(-1) !== '\n') {
101 this.hold_last
= true;
103 data
= data
.split("\n");
104 for (i
= 0; i
< data
.length
; i
++) {
106 if ((this.hold_last
) && (i
=== data
.length
- 1)) {
107 this.held_data
= data
[i
];
111 // We have a complete line of data, parse it!
112 msg
= parse_regex
.exec(data
[i
].replace(/^\r+|\r+$/, ''));
113 //msg2 = alt_regex.exec(data[i].replace(/^\r+|\r+$/, ''));
114 console
.log('S-->', data
[i
]);
115 console
.log('Matches', msg
);
121 hostname
: msg
[4] || '',
123 params
: msg
[6] || '',
124 trailing
: (msg
[7]) ? msg
[7].trim() : ''
126 msg
.params
= msg
.params
.split(' ');
128 console
.log('Parsed', msg
);
130 this.emit('irc_' + msg
.command
.toUpperCase(), msg
);
132 console
.log("Malformed IRC line: " + data
[i
].replace(/^\r+|\r+$/, ''));