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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 RTCEvents = require("../../service/RTC/RTCEvents");
  6. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  7. var JitsiConnectionErrors = require("../../JitsiConnectionErrors");
  8. var JitsiConnectionEvents = require("../../JitsiConnectionEvents");
  9. var RTC = require("../RTC/RTC");
  10. var authenticatedUser = false;
  11. function createConnection(bosh) {
  12. bosh = bosh || '/http-bind';
  13. return new Strophe.Connection(bosh);
  14. };
  15. //!!!!!!!!!! FIXME: ...
  16. function initStrophePlugins(XMPP)
  17. {
  18. require("./strophe.emuc")(XMPP);
  19. require("./strophe.jingle")(XMPP, XMPP.eventEmitter);
  20. // require("./strophe.moderate")(XMPP, eventEmitter);
  21. require("./strophe.util")();
  22. require("./strophe.ping")(XMPP, XMPP.eventEmitter);
  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. logger.log("(TIME) Strophe " + Strophe.getStatusString(status) +
  90. (msg ? "[" + msg + "]" : "") + "\t:" + window.performance.now());
  91. if (status === Strophe.Status.CONNECTED) {
  92. if (self.options.useStunTurn) {
  93. self.connection.jingle.getStunAndTurnCredentials();
  94. }
  95. logger.info("My Jabber ID: " + self.connection.jid);
  96. // Schedule ping ?
  97. var pingJid = self.connection.domain;
  98. self.connection.ping.hasPingSupport(
  99. pingJid,
  100. function (hasPing) {
  101. if (hasPing)
  102. self.connection.ping.startInterval(pingJid);
  103. else
  104. logger.warn("Ping NOT supported by " + pingJid);
  105. }
  106. );
  107. if (password)
  108. authenticatedUser = true;
  109. if (self.connection && self.connection.connected &&
  110. Strophe.getResourceFromJid(self.connection.jid)) {
  111. // .connected is true while connecting?
  112. // self.connection.send($pres());
  113. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  114. Strophe.getResourceFromJid(self.connection.jid));
  115. }
  116. } else if (status === Strophe.Status.CONNFAIL) {
  117. if (msg === 'x-strophe-bad-non-anon-jid') {
  118. anonymousConnectionFailed = true;
  119. }
  120. else
  121. {
  122. connectionFailed = true;
  123. }
  124. lastErrorMsg = msg;
  125. } else if (status === Strophe.Status.DISCONNECTED) {
  126. // Stop ping interval
  127. self.connection.ping.stopInterval();
  128. self.disconnectInProgress = false;
  129. if (anonymousConnectionFailed) {
  130. // prompt user for username and password
  131. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  132. JitsiConnectionErrors.PASSWORD_REQUIRED);
  133. } else if(connectionFailed) {
  134. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  135. JitsiConnectionErrors.OTHER_ERROR,
  136. msg ? msg : lastErrorMsg);
  137. } else {
  138. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  139. msg ? msg : lastErrorMsg);
  140. }
  141. } else if (status === Strophe.Status.AUTHFAIL) {
  142. // wrong password or username, prompt user
  143. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  144. JitsiConnectionErrors.PASSWORD_REQUIRED);
  145. }
  146. });
  147. }
  148. XMPP.prototype.connect = function (jid, password) {
  149. if(!jid) {
  150. var configDomain = this.options.hosts.anonymousdomain || this.options.hosts.domain;
  151. // Force authenticated domain if room is appended with '?login=true'
  152. if (this.options.hosts.anonymousdomain &&
  153. window.location.search.indexOf("login=true") !== -1) {
  154. configDomain = this.options.hosts.domain;
  155. }
  156. jid = configDomain || window.location.hostname;
  157. }
  158. return this._connect(jid, password);
  159. };
  160. XMPP.prototype.createRoom = function (roomName, options, useNicks, nick) {
  161. var roomjid = roomName + '@' + this.options.hosts.muc;
  162. if (useNicks) {
  163. if (nick) {
  164. roomjid += '/' + nick;
  165. } else {
  166. roomjid += '/' + Strophe.getNodeFromJid(this.connection.jid);
  167. }
  168. } else {
  169. var tmpJid = Strophe.getNodeFromJid(this.connection.jid);
  170. if(!authenticatedUser)
  171. tmpJid = tmpJid.substr(0, 8);
  172. roomjid += '/' + tmpJid;
  173. }
  174. return this.connection.emuc.createRoom(roomjid, null, options);
  175. }
  176. XMPP.prototype.addListener = function(type, listener) {
  177. this.eventEmitter.on(type, listener);
  178. };
  179. XMPP.prototype.removeListener = function (type, listener) {
  180. this.eventEmitter.removeListener(type, listener);
  181. };
  182. //FIXME: this should work with the room
  183. XMPP.prototype.leaveRoom = function (jid) {
  184. var handler = this.connection.jingle.jid2session[jid];
  185. if (handler && handler.peerconnection) {
  186. // FIXME: probably removing streams is not required and close() should
  187. // be enough
  188. if (RTC.localAudio) {
  189. handler.peerconnection.removeStream(
  190. RTC.localAudio.getOriginalStream(), true);
  191. }
  192. if (RTC.localVideo) {
  193. handler.peerconnection.removeStream(
  194. RTC.localVideo.getOriginalStream(), true);
  195. }
  196. handler.peerconnection.close();
  197. }
  198. this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE);
  199. this.connection.emuc.doLeave(jid);
  200. };
  201. /**
  202. * Sends 'data' as a log message to the focus. Returns true iff a message
  203. * was sent.
  204. * @param data
  205. * @returns {boolean} true iff a message was sent.
  206. */
  207. XMPP.prototype.sendLogs = function (data) {
  208. if(!this.connection.emuc.focusMucJid)
  209. return false;
  210. var deflate = true;
  211. var content = JSON.stringify(data);
  212. if (deflate) {
  213. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  214. }
  215. content = Base64.encode(content);
  216. // XEP-0337-ish
  217. var message = $msg({to: this.connection.emuc.focusMucJid, type: 'normal'});
  218. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  219. id: 'PeerConnectionStats'});
  220. message.c('message').t(content).up();
  221. if (deflate) {
  222. message.c('tag', {name: "deflated", value: "true"}).up();
  223. }
  224. message.up();
  225. this.connection.send(message);
  226. return true;
  227. };
  228. // Gets the logs from strophe.jingle.
  229. XMPP.prototype.getJingleLog = function () {
  230. return this.connection.jingle ? this.connection.jingle.getLog() : {};
  231. };
  232. // Gets the logs from strophe.
  233. XMPP.prototype.getXmppLog = function () {
  234. return this.connection.logger ? this.connection.logger.log : null;
  235. };
  236. XMPP.prototype.dial = function (to, from, roomName,roomPass) {
  237. this.connection.rayo.dial(to, from, roomName,roomPass);
  238. };
  239. XMPP.prototype.setMute = function (jid, mute) {
  240. this.connection.moderate.setMute(jid, mute);
  241. };
  242. XMPP.prototype.eject = function (jid) {
  243. this.connection.moderate.eject(jid);
  244. };
  245. XMPP.prototype.getSessions = function () {
  246. return this.connection.jingle.sessions;
  247. };
  248. XMPP.prototype.disconnect = function () {
  249. if (this.disconnectInProgress || !this.connection || !this.connection.connected)
  250. {
  251. this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
  252. return;
  253. }
  254. this.disconnectInProgress = true;
  255. this.connection.disconnect();
  256. };
  257. module.exports = XMPP;