You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmpp.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* global $, APP, config, Strophe*/
  2. var Moderator = require("./moderator");
  3. var EventEmitter = require("events");
  4. var Pako = require("pako");
  5. var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
  6. var RTCEvents = require("../../service/RTC/RTCEvents");
  7. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  8. var JitsiConnectionErrors = require("../../JitsiConnectionErrors");
  9. var JitsiConnectionEvents = require("../../JitsiConnectionEvents");
  10. var RTC = require("../RTC/RTC");
  11. var authenticatedUser = false;
  12. function createConnection(bosh) {
  13. bosh = bosh || '/http-bind';
  14. return new Strophe.Connection(bosh);
  15. };
  16. //!!!!!!!!!! FIXME: ...
  17. function initStrophePlugins(XMPP)
  18. {
  19. require("./strophe.emuc")(XMPP);
  20. require("./strophe.jingle")(XMPP, XMPP.eventEmitter);
  21. // require("./strophe.moderate")(XMPP, eventEmitter);
  22. require("./strophe.util")();
  23. require("./strophe.rayo")();
  24. require("./strophe.logger")();
  25. }
  26. //!!!!!!!!!! FIXME: ...
  27. ///**
  28. // * If given <tt>localStream</tt> is video one this method will advertise it's
  29. // * video type in MUC presence.
  30. // * @param localStream new or modified <tt>LocalStream</tt>.
  31. // */
  32. //function broadcastLocalVideoType(localStream) {
  33. // if (localStream.videoType)
  34. // XMPP.addToPresence('videoType', localStream.videoType);
  35. //}
  36. //
  37. //function registerListeners() {
  38. // RTC.addStreamListener(
  39. // broadcastLocalVideoType,
  40. // StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  41. // );
  42. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  43. // XMPP.addToPresence("devices", devices);
  44. // });
  45. //}
  46. function XMPP(options) {
  47. this.eventEmitter = new EventEmitter();
  48. this.connection = null;
  49. this.disconnectInProgress = false;
  50. this.forceMuted = false;
  51. this.options = options;
  52. initStrophePlugins(this);
  53. // registerListeners();
  54. this.connection = createConnection(options.bosh);
  55. }
  56. XMPP.prototype.getConnection = function(){ return connection; };
  57. XMPP.prototype._connect = function (jid, password) {
  58. var self = this;
  59. // connection.connect() starts the connection process.
  60. //
  61. // As the connection process proceeds, the user supplied callback will
  62. // be triggered multiple times with status updates. The callback should
  63. // take two arguments - the status code and the error condition.
  64. //
  65. // The status code will be one of the values in the Strophe.Status
  66. // constants. The error condition will be one of the conditions defined
  67. // in RFC 3920 or the condition ‘strophe-parsererror’.
  68. //
  69. // The Parameters wait, hold and route are optional and only relevant
  70. // for BOSH connections. Please see XEP 124 for a more detailed
  71. // explanation of the optional parameters.
  72. //
  73. // Connection status constants for use by the connection handler
  74. // callback.
  75. //
  76. // Status.ERROR - An error has occurred (websockets specific)
  77. // Status.CONNECTING - The connection is currently being made
  78. // Status.CONNFAIL - The connection attempt failed
  79. // Status.AUTHENTICATING - The connection is authenticating
  80. // Status.AUTHFAIL - The authentication attempt failed
  81. // Status.CONNECTED - The connection has succeeded
  82. // Status.DISCONNECTED - The connection has been terminated
  83. // Status.DISCONNECTING - The connection is currently being terminated
  84. // Status.ATTACHED - The connection has been attached
  85. var anonymousConnectionFailed = false;
  86. var connectionFailed = false;
  87. var lastErrorMsg;
  88. this.connection.connect(jid, password, function (status, msg) {
  89. console.log('Strophe status changed to',
  90. Strophe.getStatusString(status), msg);
  91. if (status === Strophe.Status.CONNECTED) {
  92. if (self.options.useStunTurn) {
  93. self.connection.jingle.getStunAndTurnCredentials();
  94. }
  95. console.info("My Jabber ID: " + self.connection.jid);
  96. if (password)
  97. authenticatedUser = true;
  98. if (self.connection && self.connection.connected &&
  99. Strophe.getResourceFromJid(self.connection.jid)) {
  100. // .connected is true while connecting?
  101. // self.connection.send($pres());
  102. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  103. Strophe.getResourceFromJid(self.connection.jid));
  104. }
  105. } else if (status === Strophe.Status.CONNFAIL) {
  106. if (msg === 'x-strophe-bad-non-anon-jid') {
  107. anonymousConnectionFailed = true;
  108. }
  109. else
  110. {
  111. connectionFailed = true;
  112. }
  113. lastErrorMsg = msg;
  114. } else if (status === Strophe.Status.DISCONNECTED) {
  115. self.disconnectInProgress = false;
  116. if (anonymousConnectionFailed) {
  117. // prompt user for username and password
  118. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  119. JitsiConnectionErrors.PASSWORD_REQUIRED);
  120. } else if(connectionFailed) {
  121. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  122. JitsiConnectionErrors.OTHER_ERROR,
  123. msg ? msg : lastErrorMsg);
  124. } else {
  125. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  126. msg ? msg : lastErrorMsg);
  127. }
  128. } else if (status === Strophe.Status.AUTHFAIL) {
  129. // wrong password or username, prompt user
  130. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  131. JitsiConnectionErrors.PASSWORD_REQUIRED);
  132. }
  133. });
  134. }
  135. XMPP.prototype.connect = function (jid, password) {
  136. if(!jid) {
  137. var configDomain = this.options.hosts.anonymousdomain || this.options.hosts.domain;
  138. // Force authenticated domain if room is appended with '?login=true'
  139. if (this.options.hosts.anonymousdomain &&
  140. window.location.search.indexOf("login=true") !== -1) {
  141. configDomain = this.options.hosts.domain;
  142. }
  143. jid = configDomain || window.location.hostname;
  144. }
  145. return this._connect(jid, password);
  146. };
  147. XMPP.prototype.createRoom = function (roomName, useNicks, nick) {
  148. var roomjid = roomName + '@' + this.options.hosts.muc;
  149. if (useNicks) {
  150. if (nick) {
  151. roomjid += '/' + nick;
  152. } else {
  153. roomjid += '/' + Strophe.getNodeFromJid(this.connection.jid);
  154. }
  155. } else {
  156. var tmpJid = Strophe.getNodeFromJid(this.connection.jid);
  157. if(!authenticatedUser)
  158. tmpJid = tmpJid.substr(0, 8);
  159. roomjid += '/' + tmpJid;
  160. }
  161. return this.connection.emuc.createRoom(roomjid, null, this.eventEmitter);
  162. }
  163. XMPP.prototype.addListener = function(type, listener) {
  164. this.eventEmitter.on(type, listener);
  165. };
  166. XMPP.prototype.removeListener = function (type, listener) {
  167. this.eventEmitter.removeListener(type, listener);
  168. };
  169. XMPP.prototype.leaveRoom = function (jid) {
  170. var handler = this.connection.jingle.jid2session[jid];
  171. if (handler && handler.peerconnection) {
  172. // FIXME: probably removing streams is not required and close() should
  173. // be enough
  174. if (RTC.localAudio) {
  175. handler.peerconnection.removeStream(
  176. RTC.localAudio.getOriginalStream(), true);
  177. }
  178. if (RTC.localVideo) {
  179. handler.peerconnection.removeStream(
  180. RTC.localVideo.getOriginalStream(), true);
  181. }
  182. handler.peerconnection.close();
  183. }
  184. this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE);
  185. this.connection.emuc.doLeave(jid);
  186. };
  187. XMPP.prototype.isConferenceInProgress = function () {
  188. return this.connection && this.connection.jingle.activecall &&
  189. this.connection.jingle.activecall.peerconnection;
  190. };
  191. XMPP.prototype.switchStreams = function (stream, oldStream, callback, isAudio) {
  192. if (this.isConferenceInProgress()) {
  193. // FIXME: will block switchInProgress on true value in case of exception
  194. this.connection.jingle.activecall.switchStreams(stream, oldStream, callback, isAudio);
  195. } else {
  196. // We are done immediately
  197. console.warn("No conference handler or conference not started yet");
  198. callback();
  199. }
  200. };
  201. XMPP.prototype.sendVideoInfoPresence = function (mute) {
  202. if(!this.connection)
  203. return;
  204. this.connection.emuc.addVideoInfoToPresence(mute);
  205. this.connection.emuc.sendPresence();
  206. };
  207. XMPP.prototype.setVideoMute = function (mute, callback, options) {
  208. if(!this.connection)
  209. return;
  210. var self = this;
  211. var localCallback = function (mute) {
  212. self.sendVideoInfoPresence(mute);
  213. return callback(mute);
  214. };
  215. if(this.connection.jingle.activecall)
  216. {
  217. this.connection.jingle.activecall.setVideoMute(
  218. mute, localCallback, options);
  219. }
  220. else {
  221. localCallback(mute);
  222. }
  223. };
  224. XMPP.prototype.setAudioMute = function (mute, callback) {
  225. if (!(this.connection && RTC.localAudio)) {
  226. return false;
  227. }
  228. if (this.forceMuted && !mute) {
  229. console.info("Asking focus for unmute");
  230. this.connection.moderate.setMute(this.connection.emuc.myroomjid, mute);
  231. // FIXME: wait for result before resetting muted status
  232. this.forceMuted = false;
  233. }
  234. if (mute == RTC.localAudio.isMuted()) {
  235. // Nothing to do
  236. return true;
  237. }
  238. RTC.localAudio.setMute(mute);
  239. this.sendAudioInfoPresence(mute, callback);
  240. return true;
  241. };
  242. XMPP.prototype.sendAudioInfoPresence = function(mute, callback) {
  243. if(this.connection) {
  244. this.connection.emuc.addAudioInfoToPresence(mute);
  245. this.connection.emuc.sendPresence();
  246. }
  247. callback();
  248. return true;
  249. };
  250. /**
  251. * Sends 'data' as a log message to the focus. Returns true iff a message
  252. * was sent.
  253. * @param data
  254. * @returns {boolean} true iff a message was sent.
  255. */
  256. XMPP.prototype.sendLogs = function (data) {
  257. if(!this.connection.emuc.focusMucJid)
  258. return false;
  259. var deflate = true;
  260. var content = JSON.stringify(data);
  261. if (deflate) {
  262. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  263. }
  264. content = Base64.encode(content);
  265. // XEP-0337-ish
  266. var message = $msg({to: this.connection.emuc.focusMucJid, type: 'normal'});
  267. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  268. id: 'PeerConnectionStats'});
  269. message.c('message').t(content).up();
  270. if (deflate) {
  271. message.c('tag', {name: "deflated", value: "true"}).up();
  272. }
  273. message.up();
  274. this.connection.send(message);
  275. return true;
  276. };
  277. // Gets the logs from strophe.jingle.
  278. XMPP.prototype.getJingleLog = function () {
  279. return this.connection.jingle ? this.connection.jingle.getLog() : {};
  280. };
  281. // Gets the logs from strophe.
  282. XMPP.prototype.getXmppLog = function () {
  283. return this.connection.logger ? this.connection.logger.log : null;
  284. };
  285. XMPP.prototype.dial = function (to, from, roomName,roomPass) {
  286. this.connection.rayo.dial(to, from, roomName,roomPass);
  287. };
  288. XMPP.prototype.setMute = function (jid, mute) {
  289. this.connection.moderate.setMute(jid, mute);
  290. };
  291. XMPP.prototype.eject = function (jid) {
  292. this.connection.moderate.eject(jid);
  293. };
  294. XMPP.prototype.getJidFromSSRC = function (ssrc) {
  295. if (!this.isConferenceInProgress())
  296. return null;
  297. return this.connection.jingle.activecall.getSsrcOwner(ssrc);
  298. };
  299. XMPP.prototype.getSessions = function () {
  300. return this.connection.jingle.sessions;
  301. };
  302. XMPP.prototype.removeStream = function (stream) {
  303. if (!this.isConferenceInProgress())
  304. return;
  305. this.connection.jingle.activecall.peerconnection.removeStream(stream);
  306. };
  307. XMPP.prototype.disconnect = function () {
  308. if (this.disconnectInProgress || !this.connection || !this.connection.connected)
  309. {
  310. this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
  311. return;
  312. }
  313. this.disconnectInProgress = true;
  314. this.connection.disconnect();
  315. };
  316. module.exports = XMPP;