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

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