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.6KB

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