Server using correct webirc pass / user pass
[KiwiIRC.git] / server / irc-connection.js
1 var net = require('net'),
2 tls = require('tls'),
3 events = require('events'),
4 util = require('util');
5
6 var IRCConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
7 var that = this;
8 events.EventEmitter.call(this);
9
10 if (ssl) {
11 this.socket = tls.connect(port, hostname, {}, connect_handler);
12 } else {
13 this.socket = net.createConnection(port, hostname);
14 this.socket.on('connect', function () {
15 connect_handler.apply(that, arguments);
16 });
17 }
18
19 this.socket.on('error', function () {
20 var a = Array.prototype.slice.call(arguments);
21 a.unshift('error');
22 that.emit.apply(this, a);
23 });
24
25 this.socket.setEncoding('utf-8');
26
27 this.socket.on('data', function () {
28 parse.apply(that, arguments);
29 });
30
31 this.socket.on('close', function () {
32 that.emit('close');
33 });
34
35 this.connected = false;
36 this.registered = false;
37 this.nick = nick;
38 this.user = user;
39 this.ssl = !(!ssl);
40 this.options = Object.create(null);
41
42 this.webirc = webirc;
43 this.password = pass;
44 this.hold_last = false;
45 this.held_data = '';
46 };
47 util.inherits(IRCConnection, events.EventEmitter);
48
49 IRCConnection.prototype.write = function (data, callback) {
50 console.log('S<--', data);
51 write.call(this, data + '\r\n', 'utf-8', callback);
52 };
53
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);
58 };
59
60 var write = function (data, encoding, callback) {
61 this.socket.write(data, encoding, callback);
62 };
63
64 var end = function (data, encoding, callback) {
65 this.socket.end(data, encoding, callback);
66 };
67
68 module.exports.IRCConnection = IRCConnection;
69
70 var connect_handler = function () {
71 if (this.webirc) {
72 this.write('WEBIRC ' + this.webirc.pass + ' KiwiIRC ' + this.user.hostname + ' ' + this.user.address);
73 }
74 if (this.password) {
75 this.write('PASS ' + this.password);
76 }
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);
80
81 this.connected = true;
82 console.log("IRCConnection.emit('connected')");
83 this.emit('connected');
84 };
85
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;
88
89 var parse = function (data) {
90 var i,
91 msg,
92 msg2,
93 trm;
94
95 if ((this.hold_last) && (this.held_data !== '')) {
96 data = this.held_data + data;
97 this.hold_last = false;
98 this.held_data = '';
99 }
100 if (data.substr(-1) !== '\n') {
101 this.hold_last = true;
102 }
103 data = data.split("\n");
104 for (i = 0; i < data.length; i++) {
105 if (data[i]) {
106 if ((this.hold_last) && (i === data.length - 1)) {
107 this.held_data = data[i];
108 break;
109 }
110
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);
116 if (msg) {
117 msg = {
118 prefix: msg[1],
119 nick: msg[2],
120 ident: msg[3],
121 hostname: msg[4] || '',
122 command: msg[5],
123 params: msg[6] || '',
124 trailing: (msg[7]) ? msg[7].trim() : ''
125 };
126 msg.params = msg.params.split(' ');
127
128 console.log('Parsed', msg);
129
130 this.emit('irc_' + msg.command.toUpperCase(), msg);
131 } else {
132 console.log("Malformed IRC line: " + data[i].replace(/^\r+|\r+$/, ''));
133 }
134 }
135 }
136 };