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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* global $, APP, config, Strophe*/
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  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.ping")(XMPP, XMPP.eventEmitter);
  24. require("./strophe.rayo")();
  25. require("./strophe.logger")();
  26. }
  27. //!!!!!!!!!! FIXME: ...
  28. ///**
  29. // * If given <tt>localStream</tt> is video one this method will advertise it's
  30. // * video type in MUC presence.
  31. // * @param localStream new or modified <tt>LocalStream</tt>.
  32. // */
  33. //function broadcastLocalVideoType(localStream) {
  34. // if (localStream.videoType)
  35. // XMPP.addToPresence('videoType', localStream.videoType);
  36. //}
  37. //
  38. //function registerListeners() {
  39. // RTC.addStreamListener(
  40. // broadcastLocalVideoType,
  41. // StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  42. // );
  43. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  44. // XMPP.addToPresence("devices", devices);
  45. // });
  46. //}
  47. function XMPP(options) {
  48. this.eventEmitter = new EventEmitter();
  49. this.connection = null;
  50. this.disconnectInProgress = false;
  51. this.forceMuted = false;
  52. this.options = options;
  53. initStrophePlugins(this);
  54. // registerListeners();
  55. this.connection = createConnection(options.bosh);
  56. }
  57. XMPP.prototype.getConnection = function(){ return connection; };
  58. XMPP.prototype._connect = function (jid, password) {
  59. var self = this;
  60. // connection.connect() starts the connection process.
  61. //
  62. // As the connection process proceeds, the user supplied callback will
  63. // be triggered multiple times with status updates. The callback should
  64. // take two arguments - the status code and the error condition.
  65. //
  66. // The status code will be one of the values in the Strophe.Status
  67. // constants. The error condition will be one of the conditions defined
  68. // in RFC 3920 or the condition ‘strophe-parsererror’.
  69. //
  70. // The Parameters wait, hold and route are optional and only relevant
  71. // for BOSH connections. Please see XEP 124 for a more detailed
  72. // explanation of the optional parameters.
  73. //
  74. // Connection status constants for use by the connection handler
  75. // callback.
  76. //
  77. // Status.ERROR - An error has occurred (websockets specific)
  78. // Status.CONNECTING - The connection is currently being made
  79. // Status.CONNFAIL - The connection attempt failed
  80. // Status.AUTHENTICATING - The connection is authenticating
  81. // Status.AUTHFAIL - The authentication attempt failed
  82. // Status.CONNECTED - The connection has succeeded
  83. // Status.DISCONNECTED - The connection has been terminated
  84. // Status.DISCONNECTING - The connection is currently being terminated
  85. // Status.ATTACHED - The connection has been attached
  86. var anonymousConnectionFailed = false;
  87. var connectionFailed = false;
  88. var lastErrorMsg;
  89. this.connection.connect(jid, password, function (status, msg) {
  90. logger.log('Strophe status changed to',
  91. Strophe.getStatusString(status), msg);
  92. if (status === Strophe.Status.CONNECTED) {
  93. if (self.options.useStunTurn) {
  94. self.connection.jingle.getStunAndTurnCredentials();
  95. }
  96. logger.info("My Jabber ID: " + self.connection.jid);
  97. self.connection.ping.startInterval(
  98. Strophe.getDomainFromJid(self.connection.jid));
  99. if (password)
  100. authenticatedUser = true;
  101. if (self.connection && self.connection.connected &&
  102. Strophe.getResourceFromJid(self.connection.jid)) {
  103. // .connected is true while connecting?
  104. // self.connection.send($pres());
  105. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  106. Strophe.getResourceFromJid(self.connection.jid));
  107. }
  108. } else if (status === Strophe.Status.CONNFAIL) {
  109. if (msg === 'x-strophe-bad-non-anon-jid') {
  110. anonymousConnectionFailed = true;
  111. }
  112. else
  113. {
  114. connectionFailed = true;
  115. }
  116. lastErrorMsg = msg;
  117. } else if (status === Strophe.Status.DISCONNECTED) {
  118. // Stop ping interval
  119. self.connection.ping.stopInterval();
  120. self.disconnectInProgress = false;
  121. if (anonymousConnectionFailed) {
  122. // prompt user for username and password
  123. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  124. JitsiConnectionErrors.PASSWORD_REQUIRED);
  125. } else if(connectionFailed) {
  126. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  127. JitsiConnectionErrors.OTHER_ERROR,
  128. msg ? msg : lastErrorMsg);
  129. } else {
  130. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  131. msg ? msg : lastErrorMsg);
  132. }
  133. } else if (status === Strophe.Status.AUTHFAIL) {
  134. // wrong password or username, prompt user
  135. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  136. JitsiConnectionErrors.PASSWORD_REQUIRED);
  137. }
  138. });
  139. }
  140. XMPP.prototype.connect = function (jid, password) {
  141. if(!jid) {
  142. var configDomain = this.options.hosts.anonymousdomain || this.options.hosts.domain;
  143. // Force authenticated domain if room is appended with '?login=true'
  144. if (this.options.hosts.anonymousdomain &&
  145. window.location.search.indexOf("login=true") !== -1) {
  146. configDomain = this.options.hosts.domain;
  147. }
  148. jid = configDomain || window.location.hostname;
  149. }
  150. return this._connect(jid, password);
  151. };
  152. XMPP.prototype.createRoom = function (roomName, options, useNicks, nick) {
  153. var roomjid = roomName + '@' + this.options.hosts.muc;
  154. if (useNicks) {
  155. if (nick) {
  156. roomjid += '/' + nick;
  157. } else {
  158. roomjid += '/' + Strophe.getNodeFromJid(this.connection.jid);
  159. }
  160. } else {
  161. var tmpJid = Strophe.getNodeFromJid(this.connection.jid);
  162. if(!authenticatedUser)
  163. tmpJid = tmpJid.substr(0, 8);
  164. roomjid += '/' + tmpJid;
  165. }
  166. return this.connection.emuc.createRoom(roomjid, null, options);
  167. }
  168. XMPP.prototype.addListener = function(type, listener) {
  169. this.eventEmitter.on(type, listener);
  170. };
  171. XMPP.prototype.removeListener = function (type, listener) {
  172. this.eventEmitter.removeListener(type, listener);
  173. };
  174. //FIXME: this should work with the room
  175. XMPP.prototype.leaveRoom = function (jid) {
  176. var handler = this.connection.jingle.jid2session[jid];
  177. if (handler && handler.peerconnection) {
  178. // FIXME: probably removing streams is not required and close() should
  179. // be enough
  180. if (RTC.localAudio) {
  181. handler.peerconnection.removeStream(
  182. RTC.localAudio.getOriginalStream(), true);
  183. }
  184. if (RTC.localVideo) {
  185. handler.peerconnection.removeStream(
  186. RTC.localVideo.getOriginalStream(), true);
  187. }
  188. handler.peerconnection.close();
  189. }
  190. this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE);
  191. this.connection.emuc.doLeave(jid);
  192. };
  193. /**
  194. * Sends 'data' as a log message to the focus. Returns true iff a message
  195. * was sent.
  196. * @param data
  197. * @returns {boolean} true iff a message was sent.
  198. */
  199. XMPP.prototype.sendLogs = function (data) {
  200. if(!this.connection.emuc.focusMucJid)
  201. return false;
  202. var deflate = true;
  203. var content = JSON.stringify(data);
  204. if (deflate) {
  205. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  206. }
  207. content = Base64.encode(content);
  208. // XEP-0337-ish
  209. var message = $msg({to: this.connection.emuc.focusMucJid, type: 'normal'});
  210. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  211. id: 'PeerConnectionStats'});
  212. message.c('message').t(content).up();
  213. if (deflate) {
  214. message.c('tag', {name: "deflated", value: "true"}).up();
  215. }
  216. message.up();
  217. this.connection.send(message);
  218. return true;
  219. };
  220. // Gets the logs from strophe.jingle.
  221. XMPP.prototype.getJingleLog = function () {
  222. return this.connection.jingle ? this.connection.jingle.getLog() : {};
  223. };
  224. // Gets the logs from strophe.
  225. XMPP.prototype.getXmppLog = function () {
  226. return this.connection.logger ? this.connection.logger.log : null;
  227. };
  228. XMPP.prototype.dial = function (to, from, roomName,roomPass) {
  229. this.connection.rayo.dial(to, from, roomName,roomPass);
  230. };
  231. XMPP.prototype.setMute = function (jid, mute) {
  232. this.connection.moderate.setMute(jid, mute);
  233. };
  234. XMPP.prototype.eject = function (jid) {
  235. this.connection.moderate.eject(jid);
  236. };
  237. XMPP.prototype.getSessions = function () {
  238. return this.connection.jingle.sessions;
  239. };
  240. XMPP.prototype.disconnect = function () {
  241. if (this.disconnectInProgress || !this.connection || !this.connection.connected)
  242. {
  243. this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
  244. return;
  245. }
  246. this.disconnectInProgress = true;
  247. this.connection.disconnect();
  248. };
  249. module.exports = XMPP;