/usr/lib/nodejs/node-xmpp/xmpp/connection.js is in libnode-node-xmpp 0.2.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | var net = require('net');
var EventEmitter = require('events').EventEmitter;
var sys = require('sys');
var ltx = require('ltx');
var StreamParser = require('./stream_parser');
var starttls = require('../starttls');
var NS_XMPP_TLS = exports.NS_XMPP_TLS = 'urn:ietf:params:xml:ns:xmpp-tls';
var NS_STREAM = exports.NS_STREAM = 'http://etherx.jabber.org/streams';
var NS_XMPP_STREAMS = 'urn:ietf:params:xml:ns:xmpp-streams';
/**
Base class for connection-based streams.
The socket parameter is optional for incoming connections.
A note on events: this base class will emit 'rawStanza' and leaves
'stanza' to Client & Component. Therefore we won't confuse the
user with stanzas before authentication has finished.
*/
function Connection(socket) {
EventEmitter.call(this);
this.charset = 'UTF-8';
this.xmlns = { stream: NS_STREAM };
this.socket = socket || new net.Stream();
this.setupStream();
this.mixins = [];
}
sys.inherits(Connection, EventEmitter);
exports.Connection = Connection;
// Defaults
Connection.prototype.charset = 'UTF-8';
Connection.prototype.allowTLS = true;
/**
Used by both the constructor and by reinitialization in setSecure().
*/
Connection.prototype.setupStream = function() {
var self = this;
this.socket.addListener('data', function(data) {
self.onData(data);
});
this.socket.addListener('end', function() {
self.onEnd();
});
var proxyEvent = function(event) {
self.socket.addListener(event, function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(event);
self.emit.apply(self, args);
});
};
proxyEvent('data'); // let them sniff unparsed XML
proxyEvent('drain');
proxyEvent('close');
};
/** Climbs the stanza up if a child was passed,
but you can send strings and buffers too.
*/
Connection.prototype.send = function(stanza) {
if (!this.socket.writable) {
this.socket.end();
return;
}
if (stanza.root) {
var el = this.rmStreamNs(stanza.root());
var socket = this.socket;
el.write(function(s) { socket.write(s); });
return el;
} else {
this.socket.write(stanza);
return stanza;
}
};
Connection.prototype.startParser = function() {
var self = this;
this.parser = new StreamParser.StreamParser(this.charset, this.maxStanzaSize);
this.parser.addListener('start', function(attrs) {
self.streamAttrs = attrs;
/* We need those xmlns often, store them extra */
self.streamNsAttrs = {};
for(var k in attrs) {
if (k == 'xmlns' ||
k.substr(0, 6) == 'xmlns:')
self.streamNsAttrs[k] = attrs[k];
}
/* Notify in case we don't wait for <stream:features/>
(Component or non-1.0 streams)
*/
self.emit('streamStart', attrs);
});
this.parser.addListener('stanza', function(stanza) {
self.onStanza(self.addStreamNs(stanza));
});
this.parser.addListener('error', function(e) {
self.error(e.condition || 'internal-server-error', e.message);
});
this.parser.addListener('end', function() {
self.stopParser();
self.end();
});
};
Connection.prototype.stopParser = function() {
delete this.parser;
};
Connection.prototype.startStream = function() {
var attrs = {};
for(var k in this.xmlns) {
if (this.xmlns.hasOwnProperty(k)) {
if (!k)
attrs.xmlns = this.xmlns[k];
else
attrs['xmlns:' + k] = this.xmlns[k];
}
}
if (this.xmppVersion)
attrs.version = this.xmppVersion;
if (this.streamTo)
attrs.to = this.streamTo;
if (this.streamId)
attrs.id = this.streamId;
var el = new ltx.Element('stream:stream', attrs);
// make it non-empty to cut the closing tag
el.t(' ');
var s = el.toString();
this.send(s.substr(0, s.indexOf(' </stream:stream>')));
this.streamOpened = true;
};
Connection.prototype.onData = function(data) {
if (this.parser)
this.parser.write(data);
};
Connection.prototype.setSecure = function(credentials) {
var self = this;
this.stopParser();
// Remove old event listeners
this.socket.removeAllListeners('data');
this.socket.removeAllListeners('end');
this.socket.removeAllListeners('drain');
this.socket.removeAllListeners('close');
var ct = starttls(this.socket, credentials || this.credentials, function() {
self.startParser();
self.startStream();
});
// The socket is now the cleartext stream
this.socket = ct;
// Attach new listeners on the cleartext stream
this.setupStream();
};
/**
* This is not an event listener, but takes care of the TLS handshake
* before 'rawStanza' events are emitted to the derived classes.
*/
Connection.prototype.onStanza = function(stanza) {
if (stanza.is('error', NS_STREAM)) {
/* TODO: extract error text */
this.emit('error', stanza);
} else if (stanza.is('features', NS_STREAM) &&
this.allowTLS &&
stanza.getChild('starttls', NS_XMPP_TLS)) {
/* Signal willingness to perform TLS handshake */
this.send(new ltx.Element('starttls', { xmlns: NS_XMPP_TLS }));
} else if (this.allowTLS &&
stanza.is('proceed', NS_XMPP_TLS)) {
/* Server is waiting for TLS handshake */
this.setSecure();
} else {
this.emit('rawStanza', stanza);
}
};
/**
* Add stream xmlns to a stanza, so the user can check for
* 'jabber:client' etc.
*/
Connection.prototype.addStreamNs = function(stanza) {
for(var k in this.streamNsAttrs) {
if (!stanza.attrs[k])
stanza.attrs[k] = this.streamNsAttrs[k];
}
return stanza;
};
/**
* Remove superfluous xmlns that were aleady declared in
* our <stream:stream>
*/
Connection.prototype.rmStreamNs = function(stanza) {
for(var k in this.xmlns) {
var attr = k ? 'xmlns:' + k : 'xmlns';
if (stanza.attrs[attr] == this.xmlns[k])
delete stanza.attrs[attr];
}
return stanza;
};
/**
* Connection has been ended by remote, we will not get any incoming
* 'data' events. Alternatively, used for 'error' event.
*/
Connection.prototype.onEnd = function() {
this.stopParser();
this.socket.end();
};
/**
* XMPP-style end connection for user
*/
Connection.prototype.end = function() {
if (this.socket.writable) {
if (this.streamOpened) {
this.socket.write('</stream:stream>');
delete this.streamOpened;
} else {
this.socket.end();
}
}
};
/**
* End connection with stream error.
* Emits 'error' event too.
*
* @param {String} condition XMPP error condition, see RFC3920 4.7.3. Defined Conditions
* @param {String} text Optional error message
*/
Connection.prototype.error = function(condition, message) {
this.emit('error', new Error(message));
if (!this.socket.writable)
return;
if(!this.streamOpened)
this.startStream(); /* RFC 3920, 4.7.1 stream-level errors rules */
var e = new ltx.Element('stream:error');
e.c(condition, { xmlns: NS_XMPP_STREAMS });
if (message)
e.c('text', { xmlns: NS_XMPP_STREAMS,
'xml:lang': 'en' }).
t(message);
this.send(e);
this.end();
};
/**
* Adds a mixin to this Connection for implementing higher-level functionality.
*
* A mixin is a module that exports a single function, taking the
* Connection as its value. The module can perform its own stanza-handling,
* event emission and other functionality. Note that mixins can only be
* added once.
*/
Connection.prototype.addMixin = function(mixin, mixinArgs) {
if(this.mixins.indexOf(mixin) == -1) {
mixin(this, mixinArgs);
this.mixins = this.mixins.concat(mixin);
}
};
|