converse-core.js 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. // Converse.js (A browser based XMPP chat client)
  2. // http://conversejs.org
  3. //
  4. // Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
  5. // Licensed under the Mozilla Public License (MPLv2)
  6. //
  7. /*global Backbone, define, window, document, JSON */
  8. (function (root, factory) {
  9. define(["sizzle",
  10. "es6-promise",
  11. "lodash.noconflict",
  12. "polyfill",
  13. "utils",
  14. "moment_with_locales",
  15. "strophe",
  16. "pluggable",
  17. "backbone.noconflict",
  18. "backbone.browserStorage",
  19. "backbone.overview",
  20. ], factory);
  21. }(this, function (
  22. sizzle, Promise, _, polyfill,
  23. utils, moment, Strophe, pluggable, Backbone) {
  24. /* Cannot use this due to Safari bug.
  25. * See https://github.com/jcbrand/converse.js/issues/196
  26. */
  27. // "use strict";
  28. // Strophe globals
  29. const { $build, $iq, $msg, $pres } = Strophe;
  30. const b64_sha1 = Strophe.SHA1.b64_sha1;
  31. Strophe = Strophe.Strophe;
  32. // Use Mustache style syntax for variable interpolation
  33. /* Configuration of Lodash templates (this config is distinct to the
  34. * config of requirejs-tpl in main.js). This one is for normal inline templates.
  35. */
  36. _.templateSettings = {
  37. 'escape': /\{\{\{([\s\S]+?)\}\}\}/g,
  38. 'evaluate': /\{\[([\s\S]+?)\]\}/g,
  39. 'interpolate': /\{\{([\s\S]+?)\}\}/g
  40. };
  41. const _converse = {
  42. 'templates': {},
  43. 'promises': {}
  44. }
  45. _.extend(_converse, Backbone.Events);
  46. _converse.core_plugins = [
  47. 'converse-bookmarks',
  48. 'converse-chatboxes',
  49. 'converse-chatview',
  50. 'converse-controlbox',
  51. 'converse-core',
  52. 'converse-disco',
  53. 'converse-dragresize',
  54. 'converse-headline',
  55. 'converse-mam',
  56. 'converse-minimize',
  57. 'converse-muc',
  58. 'converse-notification',
  59. 'converse-otr',
  60. 'converse-ping',
  61. 'converse-register',
  62. 'converse-roomslist',
  63. 'converse-rosterview',
  64. 'converse-vcard'
  65. ];
  66. // Make converse pluggable
  67. pluggable.enable(_converse, '_converse', 'pluggable');
  68. // Module-level constants
  69. _converse.STATUS_WEIGHTS = {
  70. 'offline': 6,
  71. 'unavailable': 5,
  72. 'xa': 4,
  73. 'away': 3,
  74. 'dnd': 2,
  75. 'chat': 1, // We currently don't differentiate between "chat" and "online"
  76. 'online': 1
  77. };
  78. _converse.PRETTY_CHAT_STATUS = {
  79. 'offline': 'Offline',
  80. 'unavailable': 'Unavailable',
  81. 'xa': 'Extended Away',
  82. 'away': 'Away',
  83. 'dnd': 'Do not disturb',
  84. 'chat': 'Chattty',
  85. 'online': 'Online'
  86. };
  87. _converse.ANONYMOUS = "anonymous";
  88. _converse.CLOSED = 'closed';
  89. _converse.EXTERNAL = "external";
  90. _converse.LOGIN = "login";
  91. _converse.LOGOUT = "logout";
  92. _converse.OPENED = 'opened';
  93. _converse.PREBIND = "prebind";
  94. _converse.PRETTY_CONNECTION_STATUS = {
  95. 0: 'ERROR',
  96. 1: 'CONNECTING',
  97. 2: 'CONNFAIL',
  98. 3: 'AUTHENTICATING',
  99. 4: 'AUTHFAIL',
  100. 5: 'CONNECTED',
  101. 6: 'DISCONNECTED',
  102. 7: 'DISCONNECTING',
  103. 8: 'ATTACHED',
  104. 9: 'REDIRECT'
  105. };
  106. _converse.DEFAULT_IMAGE_TYPE = 'image/png';
  107. _converse.DEFAULT_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABt+uBvAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gwHCy455JBsggAABkJJREFUeNrtnM1PE1sUwHvvTD8otWLHST/Gimi1CEgr6M6FEWuIBo2pujDVsNDEP8GN/4MbN7oxrlipG2OCgZgYlxAbkRYw1KqkIDRCSkM7nXvvW8x7vjyNeQ9m7p1p3z1LQk/v/Dhz7vkEXL161cHl9wI5Ag6IA+KAOCAOiAPigDggLhwQB2S+iNZ+PcYY/SWEEP2HAAAIoSAIoihCCP+ngDDGtVotGAz29/cfOXJEUZSOjg6n06lp2sbGRqlUWlhYyGazS0tLbrdbEASrzgksyeYJId3d3el0uqenRxRFAAAA4KdfIIRgjD9+/Pj8+fOpqSndslofEIQwHA6Pjo4mEon//qmFhYXHjx8vLi4ihBgDEnp7e9l8E0Jo165dQ0NDd+/eDYVC2/qsJElDQ0OEkKWlpa2tLZamxAhQo9EIBoOjo6MXL17csZLe3l5FUT59+lQul5l5JRaAVFWNRqN37tw5ceKEQVWRSOTw4cOFQuHbt2+iKLYCIISQLMu3b99OJpOmKAwEAgcPHszn8+vr6wzsiG6UQQhxuVyXLl0aGBgwUW0sFstkMl6v90fo1KyAMMYDAwPnzp0zXfPg4GAqlWo0Gk0MiBAiy/L58+edTqf5Aa4onj59OhaLYYybFRCEMBaL0fNxBw4cSCQStN0QRUBut3t4eJjq6U+dOiVJElVPRBFQIBDo6+ujCqirqyscDlONGykC2lYyYSR6pBoQQapHZwAoHo/TuARYAOrs7GQASFEUqn6aIiBJkhgA6ujooFpUo6iaTa7koFwnaoWadLNe81tbWwzoaJrWrICWl5cZAFpbW6OabVAEtLi4yABQsVjUNK0pAWWzWQaAcrlcswKanZ1VVZUqHYRQEwOq1Wpv3ryhCmh6erpcLjdrNl+v1ycnJ+l5UELI27dvv3//3qxxEADgy5cvExMT9Mznw4cPtFtAdAPFarU6Pj5eKpVM17yxsfHy5cvV1VXazXu62gVBKBQKT58+rdVqJqrFGL948eLdu3dU8/g/H4FBUaJYLAqC0NPTY9brMD4+PjY25mDSracOCABACJmZmXE6nUePHjWu8NWrV48ePSKEsGlAs7Agfd5nenq6Wq0mk0kjDzY2NvbkyRMIIbP2PLvhBUEQ8vl8NpuNx+M+n29bzhVjvLKycv/+/YmJCcazQuwA6YzW1tYmJyf1SY+2trZ/rRk1Go1SqfT69esHDx4UCgVmNaa/zZ/9ABUhRFXVYDB48uTJeDweiUQkSfL7/T9MA2NcqVTK5fLy8vL8/PzU1FSxWHS5XJaM4wGr9sUwxqqqer3eUCgkSZJuUBBCfTRvc3OzXC6vrKxUKhWn02nhCJ5lM4oQQo/HgxD6+vXr58+fHf8sDOp+HQDg8XgclorFU676dKLlo6yWRdItIBwQB8QBcUCtfosRQjRNQwhhjPUC4w46WXryBSHU1zgEQWBz99EFhDGu1+t+v//48ePxeFxRlD179ng8nh0Efgiher2+vr6ur3HMzMysrq7uTJVdACGEurq6Ll++nEgkPB7Pj9jPoDHqOxyqqubz+WfPnuVyuV9XPeyeagAAAoHArVu3BgcHab8CuVzu4cOHpVKJUnfA5GweY+xyuc6cOXPv3r1IJMLAR8iyPDw8XK/Xi8Wiqqqmm5KZgBBC7e3tN27cuHbtGuPVpf7+/lAoNDs7W61WzfVKpgHSSzw3b95MpVKW3MfRaDQSiczNzVUqFRMZmQOIEOL1eq9fv3727FlL1t50URRFluX5+flqtWpWEGAOIFEUU6nUlStXLKSjy759+xwOx9zcnKZpphzGHMzhcDiTydgk9r1w4YIp7RPTAAmCkMlk2FeLf/tIEKbTab/fbwtAhJBoNGrutpNx6e7uPnTokC1eMU3T0um0DZPMkZER6wERQnw+n/FFSxpy7Nix3bt3WwwIIcRgIWnHkkwmjecfRgGx7DtuV/r6+iwGhDHev3+/bQF1dnYaH6E2CkiWZdsC2rt3r8WAHA5HW1ubbQGZcjajgOwTH/4qNko1Wlg4IA6IA+KAOKBWBUQIsfNojyliKIoRRfH9+/dut9umf3wzpoUNNQ4BAJubmwz+ic+OxefzWWlBhJD29nbug7iT5sIBcUAcEAfEAXFAHBAHxOVn+QMrmWpuPZx12gAAAABJRU5ErkJggg==";
  108. _converse.log = function (message, level) {
  109. if (message instanceof Error) {
  110. message = message.stack;
  111. }
  112. const logger = _.assign({
  113. 'debug': _.get(console, 'log') ? console.log.bind(console) : _.noop,
  114. 'error': _.get(console, 'log') ? console.log.bind(console) : _.noop,
  115. 'info': _.get(console, 'log') ? console.log.bind(console) : _.noop,
  116. 'warn': _.get(console, 'log') ? console.log.bind(console) : _.noop
  117. }, console);
  118. if (level === Strophe.LogLevel.ERROR) {
  119. if (_converse.debug) {
  120. logger.trace(`ERROR: ${message}`);
  121. } else {
  122. logger.error(`ERROR: ${message}`);
  123. }
  124. } else if (level === Strophe.LogLevel.WARN) {
  125. logger.warn(`WARNING: ${message}`);
  126. } else if (level === Strophe.LogLevel.FATAL) {
  127. if (_converse.debug) {
  128. logger.trace(`FATAL: ${message}`);
  129. } else {
  130. logger.error(`FATAL: ${message}`);
  131. }
  132. } else if (_converse.debug) {
  133. if (level === Strophe.LogLevel.DEBUG) {
  134. logger.debug(`DEBUG: ${message}`);
  135. } else {
  136. logger.info(`INFO: ${message}`);
  137. }
  138. }
  139. };
  140. const PROMISES = [
  141. 'initialized',
  142. 'cachedRoster',
  143. 'connectionInitialized',
  144. 'pluginsInitialized',
  145. 'roster',
  146. 'rosterContactsFetched',
  147. 'rosterGroupsFetched',
  148. 'rosterInitialized',
  149. 'statusInitialized'
  150. ];
  151. function addPromise (promise) {
  152. /* Private function, used to add a new promise to the ones already
  153. * available via the `waitUntil` api method.
  154. */
  155. _converse.promises[promise] = utils.getWrappedPromise();
  156. }
  157. _converse.emit = function (name) {
  158. /* Event emitter and promise resolver */
  159. _converse.trigger.apply(this, arguments);
  160. const promise = _converse.promises[name];
  161. if (!_.isUndefined(promise)) {
  162. promise.resolve();
  163. }
  164. };
  165. _converse.initialize = function (settings, callback) {
  166. "use strict";
  167. settings = !_.isUndefined(settings) ? settings : {};
  168. const init_promise = utils.getWrappedPromise();
  169. _.each(PROMISES, addPromise);
  170. if (!_.isUndefined(_converse.connection)) {
  171. // Looks like _converse.initialized was called again without logging
  172. // out or disconnecting in the previous session.
  173. // This happens in tests. We therefore first clean up.
  174. Backbone.history.stop();
  175. delete _converse.controlboxtoggle;
  176. _converse.connection.reset();
  177. _converse.off();
  178. _converse.stopListening();
  179. _converse._tearDown();
  180. }
  181. let unloadevent;
  182. if ('onpagehide' in window) {
  183. // Pagehide gets thrown in more cases than unload. Specifically it
  184. // gets thrown when the page is cached and not just
  185. // closed/destroyed. It's the only viable event on mobile Safari.
  186. // https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
  187. unloadevent = 'pagehide';
  188. } else if ('onbeforeunload' in window) {
  189. unloadevent = 'beforeunload';
  190. } else if ('onunload' in window) {
  191. unloadevent = 'unload';
  192. }
  193. // Logging
  194. Strophe.log = function (level, msg) { _converse.log(level+' '+msg, level); };
  195. Strophe.error = function (msg) { _converse.log(msg, Strophe.LogLevel.ERROR); };
  196. // Add Strophe Namespaces
  197. Strophe.addNamespace('CARBONS', 'urn:xmpp:carbons:2');
  198. Strophe.addNamespace('CHATSTATES', 'http://jabber.org/protocol/chatstates');
  199. Strophe.addNamespace('CSI', 'urn:xmpp:csi:0');
  200. Strophe.addNamespace('DELAY', 'urn:xmpp:delay');
  201. Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
  202. Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
  203. Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
  204. Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
  205. Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');
  206. Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
  207. Strophe.addNamespace('XFORM', 'jabber:x:data');
  208. // Instance level constants
  209. this.TIMEOUTS = { // Set as module attr so that we can override in tests.
  210. 'PAUSED': 10000,
  211. 'INACTIVE': 90000
  212. };
  213. // Internationalization
  214. this.locale = utils.getLocale(settings.i18n, utils.isConverseLocale);
  215. if (!moment.locale) {
  216. //moment.lang is deprecated after 2.8.1, use moment.locale instead
  217. moment.locale = moment.lang;
  218. }
  219. moment.locale(utils.getLocale(settings.i18n, utils.isMomentLocale));
  220. const __ = _converse.__ = utils.__.bind(_converse);
  221. _converse.___ = utils.___;
  222. // XEP-0085 Chat states
  223. // http://xmpp.org/extensions/xep-0085.html
  224. this.INACTIVE = 'inactive';
  225. this.ACTIVE = 'active';
  226. this.COMPOSING = 'composing';
  227. this.PAUSED = 'paused';
  228. this.GONE = 'gone';
  229. // Default configuration values
  230. // ----------------------------
  231. this.default_settings = {
  232. allow_contact_requests: true,
  233. allow_non_roster_messaging: false,
  234. animate: true,
  235. authentication: 'login', // Available values are "login", "prebind", "anonymous" and "external".
  236. auto_away: 0, // Seconds after which user status is set to 'away'
  237. auto_login: false, // Currently only used in connection with anonymous login
  238. auto_reconnect: false,
  239. auto_subscribe: false,
  240. auto_xa: 0, // Seconds after which user status is set to 'xa'
  241. blacklisted_plugins: [],
  242. bosh_service_url: undefined,
  243. connection_options: {},
  244. credentials_url: null, // URL from where login credentials can be fetched
  245. csi_waiting_time: 0, // Support for XEP-0352. Seconds before client is considered idle and CSI is sent out.
  246. debug: false,
  247. default_state: 'online',
  248. expose_rid_and_sid: false,
  249. filter_by_resource: false,
  250. forward_messages: false,
  251. hide_offline_users: false,
  252. include_offline_state: false,
  253. jid: undefined,
  254. keepalive: true,
  255. message_carbons: true,
  256. message_storage: 'session',
  257. password: undefined,
  258. prebind_url: null,
  259. priority: 0,
  260. registration_domain: '',
  261. rid: undefined,
  262. roster_groups: true,
  263. show_only_online_users: false,
  264. show_send_button: false,
  265. sid: undefined,
  266. storage: 'session',
  267. strict_plugin_dependencies: false,
  268. synchronize_availability: true,
  269. websocket_url: undefined,
  270. whitelisted_plugins: [],
  271. xhr_custom_status: false,
  272. xhr_custom_status_url: '',
  273. };
  274. _.assignIn(this, this.default_settings);
  275. // Allow only whitelisted configuration attributes to be overwritten
  276. _.assignIn(this, _.pick(settings, _.keys(this.default_settings)));
  277. if (this.authentication === _converse.ANONYMOUS) {
  278. if (this.auto_login && !this.jid) {
  279. throw new Error("Config Error: you need to provide the server's " +
  280. "domain via the 'jid' option when using anonymous " +
  281. "authentication with auto_login.");
  282. }
  283. }
  284. // Module-level variables
  285. // ----------------------
  286. this.callback = callback || _.noop;
  287. /* When reloading the page:
  288. * For new sessions, we need to send out a presence stanza to notify
  289. * the server/network that we're online.
  290. * When re-attaching to an existing session (e.g. via the keepalive
  291. * option), we don't need to again send out a presence stanza, because
  292. * it's as if "we never left" (see onConnectStatusChanged).
  293. * https://github.com/jcbrand/converse.js/issues/521
  294. */
  295. this.send_initial_presence = true;
  296. this.msg_counter = 0;
  297. this.user_settings = settings; // Save the user settings so that they can be used by plugins
  298. // Module-level functions
  299. // ----------------------
  300. this.generateResource = () => `/converse.js-${Math.floor(Math.random()*139749825).toString()}`;
  301. this.sendCSI = function (stat) {
  302. /* Send out a Chat Status Notification (XEP-0352)
  303. *
  304. * Parameters:
  305. * (String) stat: The user's chat status
  306. */
  307. /* Send out a Chat Status Notification (XEP-0352) */
  308. // XXX if (converse.features[Strophe.NS.CSI] || true) {
  309. _converse.connection.send($build(stat, {xmlns: Strophe.NS.CSI}));
  310. _converse.inactive = (stat === _converse.INACTIVE) ? true : false;
  311. };
  312. this.onUserActivity = function () {
  313. /* Resets counters and flags relating to CSI and auto_away/auto_xa */
  314. if (_converse.idle_seconds > 0) {
  315. _converse.idle_seconds = 0;
  316. }
  317. if (!_converse.connection.authenticated) {
  318. // We can't send out any stanzas when there's no authenticated connection.
  319. // converse can happen when the connection reconnects.
  320. return;
  321. }
  322. if (_converse.inactive) {
  323. _converse.sendCSI(_converse.ACTIVE);
  324. }
  325. if (_converse.auto_changed_status === true) {
  326. _converse.auto_changed_status = false;
  327. // XXX: we should really remember the original state here, and
  328. // then set it back to that...
  329. _converse.xmppstatus.setStatus(_converse.default_state);
  330. }
  331. };
  332. this.onEverySecond = function () {
  333. /* An interval handler running every second.
  334. * Used for CSI and the auto_away and auto_xa features.
  335. */
  336. if (!_converse.connection.authenticated) {
  337. // We can't send out any stanzas when there's no authenticated connection.
  338. // This can happen when the connection reconnects.
  339. return;
  340. }
  341. const stat = _converse.xmppstatus.getStatus();
  342. _converse.idle_seconds++;
  343. if (_converse.csi_waiting_time > 0 &&
  344. _converse.idle_seconds > _converse.csi_waiting_time &&
  345. !_converse.inactive) {
  346. _converse.sendCSI(_converse.INACTIVE);
  347. }
  348. if (_converse.auto_away > 0 &&
  349. _converse.idle_seconds > _converse.auto_away &&
  350. stat !== 'away' && stat !== 'xa' && stat !== 'dnd') {
  351. _converse.auto_changed_status = true;
  352. _converse.xmppstatus.setStatus('away');
  353. } else if (_converse.auto_xa > 0 &&
  354. _converse.idle_seconds > _converse.auto_xa &&
  355. stat !== 'xa' && stat !== 'dnd') {
  356. _converse.auto_changed_status = true;
  357. _converse.xmppstatus.setStatus('xa');
  358. }
  359. };
  360. this.registerIntervalHandler = function () {
  361. /* Set an interval of one second and register a handler for it.
  362. * Required for the auto_away, auto_xa and csi_waiting_time features.
  363. */
  364. if (_converse.auto_away < 1 && _converse.auto_xa < 1 && _converse.csi_waiting_time < 1) {
  365. // Waiting time of less then one second means features aren't used.
  366. return;
  367. }
  368. _converse.idle_seconds = 0;
  369. _converse.auto_changed_status = false; // Was the user's status changed by _converse.js?
  370. window.addEventListener('click', _converse.onUserActivity);
  371. window.addEventListener('focus', _converse.onUserActivity);
  372. window.addEventListener('keypress', _converse.onUserActivity);
  373. window.addEventListener('mousemove', _converse.onUserActivity);
  374. window.addEventListener(unloadevent, _converse.onUserActivity);
  375. _converse.everySecondTrigger = window.setInterval(_converse.onEverySecond, 1000);
  376. };
  377. this.giveFeedback = function (subject, klass, message) {
  378. _converse.connfeedback.set({
  379. 'subject': subject,
  380. 'klass': klass,
  381. 'message': message
  382. });
  383. };
  384. this.rejectPresenceSubscription = function (jid, message) {
  385. /* Reject or cancel another user's subscription to our presence updates.
  386. *
  387. * Parameters:
  388. * (String) jid - The Jabber ID of the user whose subscription
  389. * is being canceled.
  390. * (String) message - An optional message to the user
  391. */
  392. const pres = $pres({to: jid, type: "unsubscribed"});
  393. if (message && message !== "") { pres.c("status").t(message); }
  394. _converse.connection.send(pres);
  395. };
  396. this.reconnect = _.debounce(function () {
  397. _converse.log('RECONNECTING');
  398. _converse.log('The connection has dropped, attempting to reconnect.');
  399. _converse.giveFeedback(
  400. __("Reconnecting"),
  401. 'warn',
  402. __('The connection has dropped, attempting to reconnect.')
  403. );
  404. _converse.connection.reconnecting = true;
  405. _converse._tearDown();
  406. _converse.logIn(null, true);
  407. }, 3000, {'leading': true});
  408. this.disconnect = function () {
  409. _converse.log('DISCONNECTED');
  410. delete _converse.connection.reconnecting;
  411. _converse.connection.reset();
  412. _converse._tearDown();
  413. _converse.emit('disconnected');
  414. };
  415. this.onDisconnected = function () {
  416. /* Gets called once strophe's status reaches Strophe.Status.DISCONNECTED.
  417. * Will either start a teardown process for converse.js or attempt
  418. * to reconnect.
  419. */
  420. const reason = _converse.disconnection_reason;
  421. if (_converse.disconnection_cause === Strophe.Status.AUTHFAIL) {
  422. if (_converse.credentials_url && _converse.auto_reconnect) {
  423. /* In this case, we reconnect, because we might be receiving
  424. * expirable tokens from the credentials_url.
  425. */
  426. _converse.emit('will-reconnect');
  427. return _converse.reconnect();
  428. } else {
  429. return _converse.disconnect();
  430. }
  431. } else if (_converse.disconnection_cause === _converse.LOGOUT ||
  432. (!_.isUndefined(reason) && reason === _.get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) ||
  433. reason === "host-unknown" ||
  434. reason === "remote-connection-failed" ||
  435. !_converse.auto_reconnect) {
  436. return _converse.disconnect();
  437. }
  438. _converse.emit('will-reconnect');
  439. _converse.reconnect();
  440. };
  441. this.setDisconnectionCause = function (cause, reason, override) {
  442. /* Used to keep track of why we got disconnected, so that we can
  443. * decide on what the next appropriate action is (in onDisconnected)
  444. */
  445. if (_.isUndefined(cause)) {
  446. delete _converse.disconnection_cause;
  447. delete _converse.disconnection_reason;
  448. } else if (_.isUndefined(_converse.disconnection_cause) || override) {
  449. _converse.disconnection_cause = cause;
  450. _converse.disconnection_reason = reason;
  451. }
  452. };
  453. this.onConnectStatusChanged = function (status, message) {
  454. /* Callback method called by Strophe as the Strophe.Connection goes
  455. * through various states while establishing or tearing down a
  456. * connection.
  457. */
  458. _converse.log(`Status changed to: ${_converse.PRETTY_CONNECTION_STATUS[status]}`);
  459. if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
  460. _converse.giveFeedback();
  461. // By default we always want to send out an initial presence stanza.
  462. _converse.send_initial_presence = true;
  463. _converse.setDisconnectionCause();
  464. if (_converse.connection.reconnecting) {
  465. _converse.log(status === Strophe.Status.CONNECTED ? 'Reconnected' : 'Reattached');
  466. _converse.onConnected(true);
  467. } else {
  468. _converse.log(status === Strophe.Status.CONNECTED ? 'Connected' : 'Attached');
  469. if (_converse.connection.restored) {
  470. // No need to send an initial presence stanza when
  471. // we're restoring an existing session.
  472. _converse.send_initial_presence = false;
  473. }
  474. _converse.onConnected();
  475. }
  476. } else if (status === Strophe.Status.DISCONNECTED) {
  477. _converse.setDisconnectionCause(status, message);
  478. _converse.onDisconnected();
  479. } else if (status === Strophe.Status.ERROR) {
  480. _converse.giveFeedback(
  481. __('Connection error'),
  482. 'error',
  483. __('An error occurred while connecting to the chat server.')
  484. );
  485. } else if (status === Strophe.Status.CONNECTING) {
  486. _converse.giveFeedback(__('Connecting…'));
  487. } else if (status === Strophe.Status.AUTHENTICATING) {
  488. _converse.giveFeedback(__('Authenticating…'));
  489. } else if (status === Strophe.Status.AUTHFAIL) {
  490. _converse.giveFeedback(__('Authentication failed: '+message), 'error');
  491. _converse.setDisconnectionCause(status, message, true);
  492. _converse.onDisconnected();
  493. } else if (status === Strophe.Status.CONNFAIL) {
  494. let feedback = message;
  495. if (message === "host-unknown" || message == "remote-connection-failed") {
  496. feedback = __("Sorry, we could not connect to the XMPP host with domain: ") +
  497. `\"${Strophe.getDomainFromJid(_converse.connection.jid)}\"`;
  498. } else if (!_.isUndefined(message) && message === _.get(Strophe, 'ErrorCondition.NO_AUTH_MECH')) {
  499. feedback = __("The XMPP server did not offer a supported authentication mechanism");
  500. }
  501. _converse.giveFeedback(
  502. __('Connection failed'),
  503. 'error',
  504. feedback
  505. );
  506. _converse.setDisconnectionCause(status, message);
  507. } else if (status === Strophe.Status.DISCONNECTING) {
  508. _converse.setDisconnectionCause(status, message);
  509. }
  510. };
  511. this.incrementMsgCounter = function () {
  512. this.msg_counter += 1;
  513. const unreadMsgCount = this.msg_counter;
  514. if (document.title.search(/^Messages \(\d+\) /) === -1) {
  515. document.title = `Messages (${unreadMsgCount}) ${document.title}`;
  516. } else {
  517. document.title = document.title.replace(
  518. /^Messages \(\d+\) /, `Messages (${unreadMsgCount}) `
  519. );
  520. }
  521. };
  522. this.clearMsgCounter = function () {
  523. this.msg_counter = 0;
  524. if (document.title.search(/^Messages \(\d+\) /) !== -1) {
  525. document.title = document.title.replace(/^Messages \(\d+\) /, "");
  526. }
  527. };
  528. this.initStatus = () =>
  529. new Promise((resolve, reject) => {
  530. const promise = new utils.getWrappedPromise();
  531. this.xmppstatus = new this.XMPPStatus();
  532. const id = b64_sha1(`converse.xmppstatus-${_converse.bare_jid}`);
  533. this.xmppstatus.id = id; // Appears to be necessary for backbone.browserStorage
  534. this.xmppstatus.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
  535. this.xmppstatus.fetch({
  536. success: resolve,
  537. error: resolve
  538. });
  539. _converse.emit('statusInitialized');
  540. });
  541. this.initSession = function () {
  542. _converse.session = new Backbone.Model();
  543. const id = b64_sha1('converse.bosh-session');
  544. _converse.session.id = id; // Appears to be necessary for backbone.browserStorage
  545. _converse.session.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
  546. _converse.session.fetch();
  547. };
  548. this.clearSession = function () {
  549. if (!_.isUndefined(this.roster)) {
  550. this.roster.browserStorage._clear();
  551. }
  552. if (!_.isUndefined(this.session) && this.session.browserStorage) {
  553. this.session.browserStorage._clear();
  554. }
  555. };
  556. this.logOut = function () {
  557. _converse.clearSession();
  558. _converse.setDisconnectionCause(_converse.LOGOUT, undefined, true);
  559. if (!_.isUndefined(_converse.connection)) {
  560. _converse.connection.disconnect();
  561. } else {
  562. _converse._tearDown();
  563. }
  564. _converse.emit('logout');
  565. };
  566. this.saveWindowState = function (ev, hidden) {
  567. // XXX: eventually we should be able to just use
  568. // document.visibilityState (when we drop support for older
  569. // browsers).
  570. let state;
  571. const event_map = {
  572. 'focus': "visible",
  573. 'focusin': "visible",
  574. 'pageshow': "visible",
  575. 'blur': "hidden",
  576. 'focusout': "hidden",
  577. 'pagehide': "hidden"
  578. };
  579. ev = ev || document.createEvent('Events');
  580. if (ev.type in event_map) {
  581. state = event_map[ev.type];
  582. } else {
  583. state = document[hidden] ? "hidden" : "visible";
  584. }
  585. if (state === 'visible') {
  586. _converse.clearMsgCounter();
  587. }
  588. _converse.windowState = state;
  589. _converse.emit('windowStateChanged', {state});
  590. };
  591. this.registerGlobalEventHandlers = function () {
  592. // Taken from:
  593. // http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active
  594. let hidden = "hidden";
  595. // Standards:
  596. if (hidden in document) {
  597. document.addEventListener("visibilitychange", _.partial(_converse.saveWindowState, _, hidden));
  598. } else if ((hidden = "mozHidden") in document) {
  599. document.addEventListener("mozvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
  600. } else if ((hidden = "webkitHidden") in document) {
  601. document.addEventListener("webkitvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
  602. } else if ((hidden = "msHidden") in document) {
  603. document.addEventListener("msvisibilitychange", _.partial(_converse.saveWindowState, _, hidden));
  604. } else if ("onfocusin" in document) {
  605. // IE 9 and lower:
  606. document.onfocusin = document.onfocusout = _.partial(_converse.saveWindowState, _, hidden);
  607. } else {
  608. // All others:
  609. window.onpageshow = window.onpagehide = window.onfocus = window.onblur = _.partial(_converse.saveWindowState, _, hidden);
  610. }
  611. // set the initial state (but only if browser supports the Page Visibility API)
  612. if( document[hidden] !== undefined ) {
  613. _.partial(_converse.saveWindowState, _, hidden)({type: document[hidden] ? "blur" : "focus"});
  614. }
  615. };
  616. this.enableCarbons = function () {
  617. /* Ask the XMPP server to enable Message Carbons
  618. * See XEP-0280 https://xmpp.org/extensions/xep-0280.html#enabling
  619. */
  620. if (!this.message_carbons || this.session.get('carbons_enabled')) {
  621. return;
  622. }
  623. const carbons_iq = new Strophe.Builder('iq', {
  624. from: this.connection.jid,
  625. id: 'enablecarbons',
  626. type: 'set'
  627. })
  628. .c('enable', {xmlns: Strophe.NS.CARBONS});
  629. this.connection.addHandler((iq) => {
  630. if (iq.querySelectorAll('error').length > 0) {
  631. _converse.log(
  632. 'An error occured while trying to enable message carbons.',
  633. Strophe.LogLevel.ERROR);
  634. } else {
  635. this.session.save({carbons_enabled: true});
  636. _converse.log('Message carbons have been enabled.');
  637. }
  638. }, null, "iq", null, "enablecarbons");
  639. this.connection.send(carbons_iq);
  640. };
  641. this.initRoster = function () {
  642. /* Initialize the Bakcbone collections that represent the contats
  643. * roster and the roster groups.
  644. */
  645. _converse.roster = new _converse.RosterContacts();
  646. _converse.roster.browserStorage = new Backbone.BrowserStorage.session(
  647. b64_sha1(`converse.contacts-${_converse.bare_jid}`));
  648. _converse.rostergroups = new _converse.RosterGroups();
  649. _converse.rostergroups.browserStorage = new Backbone.BrowserStorage.session(
  650. b64_sha1(`converse.roster.groups${_converse.bare_jid}`));
  651. _converse.emit('rosterInitialized');
  652. };
  653. this.populateRoster = function () {
  654. /* Fetch all the roster groups, and then the roster contacts.
  655. * Emit an event after fetching is done in each case.
  656. */
  657. _converse.rostergroups.fetchRosterGroups().then(function () {
  658. _converse.emit('rosterGroupsFetched');
  659. _converse.roster.fetchRosterContacts().then(function () {
  660. _converse.emit('rosterContactsFetched');
  661. _converse.sendInitialPresence();
  662. });
  663. });
  664. };
  665. this.unregisterPresenceHandler = function () {
  666. if (!_.isUndefined(_converse.presence_ref)) {
  667. _converse.connection.deleteHandler(_converse.presence_ref);
  668. delete _converse.presence_ref;
  669. }
  670. };
  671. this.registerPresenceHandler = function () {
  672. _converse.unregisterPresenceHandler();
  673. _converse.presence_ref = _converse.connection.addHandler(
  674. function (presence) {
  675. _converse.roster.presenceHandler(presence);
  676. return true;
  677. }, null, 'presence', null);
  678. };
  679. this.sendInitialPresence = function () {
  680. if (_converse.send_initial_presence) {
  681. _converse.xmppstatus.sendPresence();
  682. }
  683. };
  684. this.onStatusInitialized = function (reconnecting) {
  685. /* Continue with session establishment (e.g. fetching chat boxes,
  686. * populating the roster etc.) necessary once the connection has
  687. * been established.
  688. */
  689. if (reconnecting) {
  690. // No need to recreate the roster, otherwise we lose our
  691. // cached data. However we still emit an event, to give
  692. // event handlers a chance to register views for the
  693. // roster and its groups, before we start populating.
  694. _converse.emit('rosterReadyAfterReconnection');
  695. } else {
  696. _converse.registerIntervalHandler();
  697. _converse.initRoster();
  698. }
  699. _converse.roster.onConnected();
  700. _converse.populateRoster();
  701. _converse.registerPresenceHandler();
  702. if (reconnecting) {
  703. _converse.xmppstatus.sendPresence();
  704. } else {
  705. init_promise.resolve();
  706. _converse.emit('initialized');
  707. }
  708. };
  709. this.setUserJid = function () {
  710. _converse.jid = _converse.connection.jid;
  711. _converse.bare_jid = Strophe.getBareJidFromJid(_converse.connection.jid);
  712. _converse.resource = Strophe.getResourceFromJid(_converse.connection.jid);
  713. _converse.domain = Strophe.getDomainFromJid(_converse.connection.jid);
  714. };
  715. this.onConnected = function (reconnecting) {
  716. /* Called as soon as a new connection has been established, either
  717. * by logging in or by attaching to an existing BOSH session.
  718. */
  719. // Solves problem of returned PubSub BOSH response not received
  720. // by browser.
  721. _converse.connection.flush();
  722. _converse.setUserJid();
  723. _converse.initSession();
  724. _converse.enableCarbons();
  725. // If there's no xmppstatus obj, then we were never connected to
  726. // begin with, so we set reconnecting to false.
  727. reconnecting = _.isUndefined(_converse.xmppstatus) ? false : reconnecting;
  728. if (reconnecting) {
  729. _converse.onStatusInitialized(true);
  730. _converse.emit('reconnected');
  731. } else {
  732. _converse.initStatus()
  733. .then(
  734. _.partial(_converse.onStatusInitialized, false),
  735. _.partial(_converse.onStatusInitialized, false))
  736. .catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  737. _converse.emit('connected');
  738. }
  739. };
  740. this.RosterContact = Backbone.Model.extend({
  741. defaults: {
  742. 'bookmarked': false,
  743. 'chat_state': undefined,
  744. 'chat_status': 'offline',
  745. 'groups': [],
  746. 'image': _converse.DEFAULT_IMAGE,
  747. 'image_type': _converse.DEFAULT_IMAGE_TYPE,
  748. 'num_unread': 0,
  749. 'status': '',
  750. },
  751. initialize (attributes) {
  752. const { jid } = attributes;
  753. const bare_jid = Strophe.getBareJidFromJid(jid).toLowerCase();
  754. const resource = Strophe.getResourceFromJid(jid);
  755. attributes.jid = bare_jid;
  756. this.set(_.assignIn({
  757. 'id': bare_jid,
  758. 'jid': bare_jid,
  759. 'fullname': bare_jid,
  760. 'user_id': Strophe.getNodeFromJid(jid),
  761. 'resources': resource ? {resource :0} : {},
  762. }, attributes));
  763. this.on('destroy', () => { this.removeFromRoster(); });
  764. this.on('change:chat_status', function (item) {
  765. _converse.emit('contactStatusChanged', item.attributes);
  766. });
  767. },
  768. subscribe (message) {
  769. /* Send a presence subscription request to this roster contact
  770. *
  771. * Parameters:
  772. * (String) message - An optional message to explain the
  773. * reason for the subscription request.
  774. */
  775. this.save('ask', "subscribe"); // ask === 'subscribe' Means we have ask to subscribe to them.
  776. const pres = $pres({to: this.get('jid'), type: "subscribe"});
  777. if (message && message !== "") {
  778. pres.c("status").t(message).up();
  779. }
  780. const nick = _converse.xmppstatus.get('fullname');
  781. if (nick && nick !== "") {
  782. pres.c('nick', {'xmlns': Strophe.NS.NICK}).t(nick).up();
  783. }
  784. _converse.connection.send(pres);
  785. return this;
  786. },
  787. ackSubscribe () {
  788. /* Upon receiving the presence stanza of type "subscribed",
  789. * the user SHOULD acknowledge receipt of that subscription
  790. * state notification by sending a presence stanza of type
  791. * "subscribe" to the contact
  792. */
  793. _converse.connection.send($pres({
  794. 'type': 'subscribe',
  795. 'to': this.get('jid')
  796. }));
  797. },
  798. ackUnsubscribe () {
  799. /* Upon receiving the presence stanza of type "unsubscribed",
  800. * the user SHOULD acknowledge receipt of that subscription state
  801. * notification by sending a presence stanza of type "unsubscribe"
  802. * this step lets the user's server know that it MUST no longer
  803. * send notification of the subscription state change to the user.
  804. * Parameters:
  805. * (String) jid - The Jabber ID of the user who is unsubscribing
  806. */
  807. _converse.connection.send($pres({'type': 'unsubscribe', 'to': this.get('jid')}));
  808. this.destroy(); // Will cause removeFromRoster to be called.
  809. },
  810. unauthorize (message) {
  811. /* Unauthorize this contact's presence subscription
  812. * Parameters:
  813. * (String) message - Optional message to send to the person being unauthorized
  814. */
  815. _converse.rejectPresenceSubscription(this.get('jid'), message);
  816. return this;
  817. },
  818. authorize (message) {
  819. /* Authorize presence subscription
  820. * Parameters:
  821. * (String) message - Optional message to send to the person being authorized
  822. */
  823. const pres = $pres({to: this.get('jid'), type: "subscribed"});
  824. if (message && message !== "") {
  825. pres.c("status").t(message);
  826. }
  827. _converse.connection.send(pres);
  828. return this;
  829. },
  830. addResource (presence) {
  831. /* Adds a new resource and it's associated attributes as taken
  832. * from the passed in presence stanza.
  833. *
  834. * Also updates the contact's chat_status if the presence has
  835. * higher priority (and is newer).
  836. */
  837. const jid = presence.getAttribute('from'),
  838. chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
  839. resource = Strophe.getResourceFromJid(jid),
  840. delay = presence.querySelector(
  841. `delay[xmlns="${Strophe.NS.DELAY}"]`
  842. ),
  843. timestamp = _.isNull(delay) ? moment().format() : moment(delay.getAttribute('stamp')).format();
  844. let priority = _.propertyOf(presence.querySelector('priority'))('textContent') || 0;
  845. priority = _.isNaN(parseInt(priority, 10)) ? 0 : parseInt(priority, 10);
  846. const resources = _.isObject(this.get('resources')) ? this.get('resources') : {};
  847. resources[resource] = {
  848. 'priority': priority,
  849. 'status': chat_status,
  850. 'timestamp': timestamp
  851. };
  852. const changed = {'resources': resources};
  853. const hpr = this.getHighestPriorityResource();
  854. if (priority == hpr.priority && timestamp == hpr.timestamp) {
  855. // Only set the chat status if this is the newest resource
  856. // with the highest priority
  857. changed.chat_status = chat_status;
  858. }
  859. this.save(changed);
  860. return resources;
  861. },
  862. removeResource (resource) {
  863. /* Remove the passed in resource from the contact's resources map.
  864. *
  865. * Also recomputes the chat_status given that there's one less
  866. * resource.
  867. */
  868. let resources = this.get('resources');
  869. if (!_.isObject(resources)) {
  870. resources = {};
  871. } else {
  872. delete resources[resource];
  873. }
  874. this.save({
  875. 'resources': resources,
  876. 'chat_status': _.propertyOf(
  877. this.getHighestPriorityResource())('status') || 'offline'
  878. });
  879. },
  880. getHighestPriorityResource () {
  881. /* Return the resource with the highest priority.
  882. *
  883. * If multiple resources have the same priority, take the
  884. * newest one.
  885. */
  886. const resources = this.get('resources');
  887. if (_.isObject(resources) && _.size(resources)) {
  888. const val = _.flow(
  889. _.values,
  890. _.partial(_.sortBy, _, ['priority', 'timestamp']),
  891. _.reverse
  892. )(resources)[0];
  893. if (!_.isUndefined(val)) {
  894. return val;
  895. }
  896. }
  897. },
  898. removeFromRoster (callback) {
  899. /* Instruct the XMPP server to remove this contact from our roster
  900. * Parameters:
  901. * (Function) callback
  902. */
  903. const iq = $iq({type: 'set'})
  904. .c('query', {xmlns: Strophe.NS.ROSTER})
  905. .c('item', {jid: this.get('jid'), subscription: "remove"});
  906. _converse.connection.sendIQ(iq, callback, callback);
  907. return this;
  908. }
  909. });
  910. this.RosterContacts = Backbone.Collection.extend({
  911. model: _converse.RosterContact,
  912. comparator (contact1, contact2) {
  913. const status1 = contact1.get('chat_status') || 'offline';
  914. const status2 = contact2.get('chat_status') || 'offline';
  915. if (_converse.STATUS_WEIGHTS[status1] === _converse.STATUS_WEIGHTS[status2]) {
  916. const name1 = contact1.get('fullname').toLowerCase();
  917. const name2 = contact2.get('fullname').toLowerCase();
  918. return name1 < name2 ? -1 : (name1 > name2? 1 : 0);
  919. } else {
  920. return _converse.STATUS_WEIGHTS[status1] < _converse.STATUS_WEIGHTS[status2] ? -1 : 1;
  921. }
  922. },
  923. onConnected () {
  924. /* Called as soon as the connection has been established
  925. * (either after initial login, or after reconnection).
  926. *
  927. * Use the opportunity to register stanza handlers.
  928. */
  929. this.registerRosterHandler();
  930. this.registerRosterXHandler();
  931. },
  932. registerRosterHandler () {
  933. /* Register a handler for roster IQ "set" stanzas, which update
  934. * roster contacts.
  935. */
  936. _converse.connection.addHandler(
  937. _converse.roster.onRosterPush.bind(_converse.roster),
  938. Strophe.NS.ROSTER, 'iq', "set"
  939. );
  940. },
  941. registerRosterXHandler () {
  942. /* Register a handler for RosterX message stanzas, which are
  943. * used to suggest roster contacts to a user.
  944. */
  945. let t = 0;
  946. _converse.connection.addHandler(
  947. function (msg) {
  948. window.setTimeout(
  949. function () {
  950. _converse.connection.flush();
  951. _converse.roster.subscribeToSuggestedItems.bind(_converse.roster)(msg);
  952. }, t);
  953. t += msg.querySelectorAll('item').length*250;
  954. return true;
  955. },
  956. Strophe.NS.ROSTERX, 'message', null
  957. );
  958. },
  959. fetchRosterContacts () {
  960. /* Fetches the roster contacts, first by trying the
  961. * sessionStorage cache, and if that's empty, then by querying
  962. * the XMPP server.
  963. *
  964. * Returns a promise which resolves once the contacts have been
  965. * fetched.
  966. */
  967. return new Promise((resolve, reject) => {
  968. this.fetch({
  969. add: true,
  970. success (collection) {
  971. if (collection.length === 0) {
  972. /* We don't have any roster contacts stored in sessionStorage,
  973. * so lets fetch the roster from the XMPP server. We pass in
  974. * 'sendPresence' as callback method, because after initially
  975. * fetching the roster we are ready to receive presence
  976. * updates from our contacts.
  977. */
  978. _converse.send_initial_presence = true;
  979. _converse.roster.fetchFromServer(resolve);
  980. } else {
  981. _converse.emit('cachedRoster', collection);
  982. resolve();
  983. }
  984. }
  985. });
  986. });
  987. },
  988. subscribeToSuggestedItems (msg) {
  989. _.each(msg.querySelectorAll('item'), function (item) {
  990. if (item.getAttribute('action') === 'add') {
  991. _converse.roster.addAndSubscribe(
  992. item.getAttribute('jid'),
  993. null,
  994. _converse.xmppstatus.get('fullname')
  995. );
  996. }
  997. });
  998. return true;
  999. },
  1000. isSelf (jid) {
  1001. return utils.isSameBareJID(jid, _converse.connection.jid);
  1002. },
  1003. addAndSubscribe (jid, name, groups, message, attributes) {
  1004. /* Add a roster contact and then once we have confirmation from
  1005. * the XMPP server we subscribe to that contact's presence updates.
  1006. * Parameters:
  1007. * (String) jid - The Jabber ID of the user being added and subscribed to.
  1008. * (String) name - The name of that user
  1009. * (Array of Strings) groups - Any roster groups the user might belong to
  1010. * (String) message - An optional message to explain the
  1011. * reason for the subscription request.
  1012. * (Object) attributes - Any additional attributes to be stored on the user's model.
  1013. */
  1014. const handler = (contact) => {
  1015. if (contact instanceof _converse.RosterContact) {
  1016. contact.subscribe(message);
  1017. }
  1018. }
  1019. this.addContact(jid, name, groups, attributes).then(handler, handler);
  1020. },
  1021. sendContactAddIQ (jid, name, groups, callback, errback) {
  1022. /* Send an IQ stanza to the XMPP server to add a new roster contact.
  1023. *
  1024. * Parameters:
  1025. * (String) jid - The Jabber ID of the user being added
  1026. * (String) name - The name of that user
  1027. * (Array of Strings) groups - Any roster groups the user might belong to
  1028. * (Function) callback - A function to call once the IQ is returned
  1029. * (Function) errback - A function to call if an error occured
  1030. */
  1031. name = _.isEmpty(name)? jid: name;
  1032. const iq = $iq({type: 'set'})
  1033. .c('query', {xmlns: Strophe.NS.ROSTER})
  1034. .c('item', { jid, name });
  1035. _.each(groups, function (group) { iq.c('group').t(group).up(); });
  1036. _converse.connection.sendIQ(iq, callback, errback);
  1037. },
  1038. addContact (jid, name, groups, attributes) {
  1039. /* Adds a RosterContact instance to _converse.roster and
  1040. * registers the contact on the XMPP server.
  1041. * Returns a promise which is resolved once the XMPP server has
  1042. * responded.
  1043. *
  1044. * Parameters:
  1045. * (String) jid - The Jabber ID of the user being added and subscribed to.
  1046. * (String) name - The name of that user
  1047. * (Array of Strings) groups - Any roster groups the user might belong to
  1048. * (Object) attributes - Any additional attributes to be stored on the user's model.
  1049. */
  1050. return new Promise((resolve, reject) => {
  1051. groups = groups || [];
  1052. name = _.isEmpty(name)? jid: name;
  1053. this.sendContactAddIQ(jid, name, groups,
  1054. () => {
  1055. const contact = this.create(_.assignIn({
  1056. ask: undefined,
  1057. fullname: name,
  1058. groups,
  1059. jid,
  1060. requesting: false,
  1061. subscription: 'none'
  1062. }, attributes), {sort: false});
  1063. resolve(contact);
  1064. },
  1065. function (err) {
  1066. alert(__('Sorry, there was an error while trying to add %1$s as a contact.', name));
  1067. _converse.log(err, Strophe.LogLevel.ERROR);
  1068. resolve(err);
  1069. }
  1070. );
  1071. });
  1072. },
  1073. subscribeBack (bare_jid) {
  1074. const contact = this.get(bare_jid);
  1075. if (contact instanceof _converse.RosterContact) {
  1076. contact.authorize().subscribe();
  1077. } else {
  1078. // Can happen when a subscription is retried or roster was deleted
  1079. const handler = (contact) => {
  1080. if (contact instanceof _converse.RosterContact) {
  1081. contact.authorize().subscribe();
  1082. }
  1083. }
  1084. this.addContact(bare_jid, '', [], { 'subscription': 'from' }).then(handler, handler);
  1085. }
  1086. },
  1087. getNumOnlineContacts () {
  1088. let ignored = ['offline', 'unavailable'];
  1089. if (_converse.show_only_online_users) {
  1090. ignored = _.union(ignored, ['dnd', 'xa', 'away']);
  1091. }
  1092. return _.sum(this.models.filter((model) => !_.includes(ignored, model.get('chat_status'))));
  1093. },
  1094. onRosterPush (iq) {
  1095. /* Handle roster updates from the XMPP server.
  1096. * See: https://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push
  1097. *
  1098. * Parameters:
  1099. * (XMLElement) IQ - The IQ stanza received from the XMPP server.
  1100. */
  1101. const id = iq.getAttribute('id');
  1102. const from = iq.getAttribute('from');
  1103. if (from && from !== "" && Strophe.getBareJidFromJid(from) !== _converse.bare_jid) {
  1104. // Receiving client MUST ignore stanza unless it has no from or from = user's bare JID.
  1105. // XXX: Some naughty servers apparently send from a full
  1106. // JID so we need to explicitly compare bare jids here.
  1107. // https://github.com/jcbrand/converse.js/issues/493
  1108. _converse.connection.send(
  1109. $iq({type: 'error', id, from: _converse.connection.jid})
  1110. .c('error', {'type': 'cancel'})
  1111. .c('service-unavailable', {'xmlns': Strophe.NS.ROSTER })
  1112. );
  1113. return true;
  1114. }
  1115. _converse.connection.send($iq({type: 'result', id, from: _converse.connection.jid}));
  1116. const items = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"] item`, iq);
  1117. _.each(items, this.updateContact.bind(this));
  1118. _converse.emit('rosterPush', iq);
  1119. return true;
  1120. },
  1121. fetchFromServer (callback) {
  1122. /* Get the roster from the XMPP server */
  1123. const iq = $iq({type: 'get', 'id': _converse.connection.getUniqueId('roster')})
  1124. .c('query', {xmlns: Strophe.NS.ROSTER});
  1125. return _converse.connection.sendIQ(iq, (iq) => {
  1126. this.onReceivedFromServer(iq);
  1127. callback.apply(this, arguments);
  1128. });
  1129. },
  1130. onReceivedFromServer (iq) {
  1131. /* An IQ stanza containing the roster has been received from
  1132. * the XMPP server.
  1133. */
  1134. const items = sizzle(`query[xmlns="${Strophe.NS.ROSTER}"] item`, iq);
  1135. _.each(items, this.updateContact.bind(this));
  1136. _converse.emit('roster', iq);
  1137. },
  1138. updateContact (item) {
  1139. /* Update or create RosterContact models based on items
  1140. * received in the IQ from the server.
  1141. */
  1142. const jid = item.getAttribute('jid');
  1143. if (this.isSelf(jid)) { return; }
  1144. const contact = this.get(jid),
  1145. subscription = item.getAttribute("subscription"),
  1146. ask = item.getAttribute("ask"),
  1147. groups = _.map(item.getElementsByTagName('group'), Strophe.getText);
  1148. if (!contact) {
  1149. if ((subscription === "none" && ask === null) || (subscription === "remove")) {
  1150. return; // We're lazy when adding contacts.
  1151. }
  1152. this.create({
  1153. ask,
  1154. fullname: item.getAttribute("name") || jid,
  1155. groups,
  1156. jid,
  1157. subscription
  1158. }, {sort: false});
  1159. } else {
  1160. if (subscription === "remove") {
  1161. return contact.destroy(); // will trigger removeFromRoster
  1162. }
  1163. // We only find out about requesting contacts via the
  1164. // presence handler, so if we receive a contact
  1165. // here, we know they aren't requesting anymore.
  1166. // see docs/DEVELOPER.rst
  1167. contact.save({
  1168. subscription,
  1169. ask,
  1170. requesting: null,
  1171. groups
  1172. });
  1173. }
  1174. },
  1175. createRequestingContact (presence) {
  1176. /* Creates a Requesting Contact.
  1177. *
  1178. * Note: this method gets completely overridden by converse-vcard.js
  1179. */
  1180. const bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from')),
  1181. nick_el = presence.querySelector(`nick[xmlns="${Strophe.NS.NICK}"]`);
  1182. const user_data = {
  1183. jid: bare_jid,
  1184. subscription: 'none',
  1185. ask: null,
  1186. requesting: true,
  1187. fullname: nick_el && nick_el.textContent || bare_jid,
  1188. };
  1189. this.create(user_data);
  1190. _converse.emit('contactRequest', user_data);
  1191. },
  1192. handleIncomingSubscription (presence) {
  1193. const jid = presence.getAttribute('from'),
  1194. bare_jid = Strophe.getBareJidFromJid(jid),
  1195. contact = this.get(bare_jid);
  1196. if (!_converse.allow_contact_requests) {
  1197. _converse.rejectPresenceSubscription(
  1198. jid,
  1199. __("This client does not allow presence subscriptions")
  1200. );
  1201. }
  1202. if (_converse.auto_subscribe) {
  1203. if ((!contact) || (contact.get('subscription') !== 'to')) {
  1204. this.subscribeBack(bare_jid);
  1205. } else {
  1206. contact.authorize();
  1207. }
  1208. } else {
  1209. if (contact) {
  1210. if (contact.get('subscription') !== 'none') {
  1211. contact.authorize();
  1212. } else if (contact.get('ask') === "subscribe") {
  1213. contact.authorize();
  1214. }
  1215. } else {
  1216. this.createRequestingContact(presence);
  1217. }
  1218. }
  1219. },
  1220. presenceHandler (presence) {
  1221. const presence_type = presence.getAttribute('type');
  1222. if (presence_type === 'error') { return true; }
  1223. const jid = presence.getAttribute('from'),
  1224. bare_jid = Strophe.getBareJidFromJid(jid),
  1225. resource = Strophe.getResourceFromJid(jid),
  1226. chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online',
  1227. status_message = _.propertyOf(presence.querySelector('status'))('textContent'),
  1228. contact = this.get(bare_jid);
  1229. if (this.isSelf(bare_jid)) {
  1230. if ((_converse.connection.jid !== jid) &&
  1231. (presence_type !== 'unavailable') &&
  1232. (_converse.synchronize_availability === true ||
  1233. _converse.synchronize_availability === resource)) {
  1234. // Another resource has changed its status and
  1235. // synchronize_availability option set to update,
  1236. // we'll update ours as well.
  1237. _converse.xmppstatus.save({'status': chat_status});
  1238. if (status_message) {
  1239. _converse.xmppstatus.save({'status_message': status_message});
  1240. }
  1241. }
  1242. return;
  1243. } else if (sizzle(`query[xmlns="${Strophe.NS.MUC}"]`, presence).length) {
  1244. return; // Ignore MUC
  1245. }
  1246. if (contact && (status_message !== contact.get('status'))) {
  1247. contact.save({'status': status_message});
  1248. }
  1249. if (presence_type === 'subscribed' && contact) {
  1250. contact.ackSubscribe();
  1251. } else if (presence_type === 'unsubscribed' && contact) {
  1252. contact.ackUnsubscribe();
  1253. } else if (presence_type === 'unsubscribe') {
  1254. return;
  1255. } else if (presence_type === 'subscribe') {
  1256. this.handleIncomingSubscription(presence);
  1257. } else if (presence_type === 'unavailable' && contact) {
  1258. contact.removeResource(resource);
  1259. } else if (contact) {
  1260. // presence_type is undefined
  1261. contact.addResource(presence);
  1262. }
  1263. }
  1264. });
  1265. this.RosterGroup = Backbone.Model.extend({
  1266. initialize (attributes) {
  1267. this.set(_.assignIn({
  1268. description: __('Click to hide these contacts'),
  1269. state: _converse.OPENED
  1270. }, attributes));
  1271. // Collection of contacts belonging to this group.
  1272. this.contacts = new _converse.RosterContacts();
  1273. }
  1274. });
  1275. this.RosterGroups = Backbone.Collection.extend({
  1276. model: _converse.RosterGroup,
  1277. fetchRosterGroups () {
  1278. /* Fetches all the roster groups from sessionStorage.
  1279. *
  1280. * Returns a promise which resolves once the groups have been
  1281. * returned.
  1282. */
  1283. return new Promise((resolve, reject) => {
  1284. this.fetch({
  1285. silent: true, // We need to first have all groups before
  1286. // we can start positioning them, so we set
  1287. // 'silent' to true.
  1288. success: resolve
  1289. });
  1290. });
  1291. }
  1292. });
  1293. this.Message = Backbone.Model.extend({
  1294. defaults(){
  1295. return {
  1296. msgid: _converse.connection.getUniqueId()
  1297. };
  1298. }
  1299. });
  1300. this.Messages = Backbone.Collection.extend({
  1301. model: _converse.Message,
  1302. comparator: 'time'
  1303. });
  1304. this.ChatBox = Backbone.Model.extend({
  1305. defaults: {
  1306. 'type': 'chatbox',
  1307. 'bookmarked': false,
  1308. 'chat_state': undefined,
  1309. 'num_unread': 0,
  1310. 'url': ''
  1311. },
  1312. initialize () {
  1313. this.messages = new _converse.Messages();
  1314. this.messages.browserStorage = new Backbone.BrowserStorage[_converse.message_storage](
  1315. b64_sha1(`converse.messages${this.get('jid')}${_converse.bare_jid}`));
  1316. this.save({
  1317. // The chat_state will be set to ACTIVE once the chat box is opened
  1318. // and we listen for change:chat_state, so shouldn't set it to ACTIVE here.
  1319. 'box_id' : b64_sha1(this.get('jid')),
  1320. 'time_opened': this.get('time_opened') || moment().valueOf(),
  1321. 'user_id' : Strophe.getNodeFromJid(this.get('jid'))
  1322. });
  1323. },
  1324. getMessageBody (message) {
  1325. const type = message.getAttribute('type');
  1326. return (type === 'error') ?
  1327. _.propertyOf(message.querySelector('error text'))('textContent') :
  1328. _.propertyOf(message.querySelector('body'))('textContent');
  1329. },
  1330. getMessageAttributes (message, delay, original_stanza) {
  1331. delay = delay || message.querySelector('delay');
  1332. const type = message.getAttribute('type'),
  1333. body = this.getMessageBody(message);
  1334. const delayed = !_.isNull(delay),
  1335. is_groupchat = type === 'groupchat',
  1336. chat_state = message.getElementsByTagName(_converse.COMPOSING).length && _converse.COMPOSING ||
  1337. message.getElementsByTagName(_converse.PAUSED).length && _converse.PAUSED ||
  1338. message.getElementsByTagName(_converse.INACTIVE).length && _converse.INACTIVE ||
  1339. message.getElementsByTagName(_converse.ACTIVE).length && _converse.ACTIVE ||
  1340. message.getElementsByTagName(_converse.GONE).length && _converse.GONE;
  1341. let from;
  1342. if (is_groupchat) {
  1343. from = Strophe.unescapeNode(Strophe.getResourceFromJid(message.getAttribute('from')));
  1344. } else {
  1345. from = Strophe.getBareJidFromJid(message.getAttribute('from'));
  1346. }
  1347. const time = delayed ? delay.getAttribute('stamp') : moment().format();
  1348. let sender, fullname;
  1349. if ((is_groupchat && from === this.get('nick')) || (!is_groupchat && from === _converse.bare_jid)) {
  1350. sender = 'me';
  1351. fullname = _converse.xmppstatus.get('fullname') || from;
  1352. } else {
  1353. sender = 'them';
  1354. fullname = this.get('fullname') || from;
  1355. }
  1356. return {
  1357. 'type': type,
  1358. 'chat_state': chat_state,
  1359. 'delayed': delayed,
  1360. 'fullname': fullname,
  1361. 'message': body || undefined,
  1362. 'msgid': message.getAttribute('id'),
  1363. 'sender': sender,
  1364. 'time': time
  1365. };
  1366. },
  1367. createMessage (message, delay, original_stanza) {
  1368. return this.messages.create(this.getMessageAttributes.apply(this, arguments));
  1369. },
  1370. newMessageWillBeHidden () {
  1371. /* Returns a boolean to indicate whether a newly received
  1372. * message will be visible to the user or not.
  1373. */
  1374. return this.get('hidden') ||
  1375. this.get('minimized') ||
  1376. this.isScrolledUp() ||
  1377. _converse.windowState === 'hidden';
  1378. },
  1379. incrementUnreadMsgCounter (stanza) {
  1380. /* Given a newly received message, update the unread counter if
  1381. * necessary.
  1382. */
  1383. if (_.isNull(stanza.querySelector('body'))) {
  1384. return; // The message has no text
  1385. }
  1386. if (utils.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
  1387. this.save({'num_unread': this.get('num_unread') + 1});
  1388. _converse.incrementMsgCounter();
  1389. }
  1390. },
  1391. clearUnreadMsgCounter() {
  1392. this.save({'num_unread': 0});
  1393. },
  1394. isScrolledUp () {
  1395. return this.get('scrolled', true);
  1396. }
  1397. });
  1398. this.ConnectionFeedback = Backbone.Model.extend({
  1399. initialize () {
  1400. this.on('change', this.emitConnectionFeedbackChange);
  1401. },
  1402. emitConnectionFeedbackChange () {
  1403. _converse.emit('connfeedback', {
  1404. 'klass': _converse.connfeedback.get('klass'),
  1405. 'message': _converse.connfeedback.get('message'),
  1406. 'subject': _converse.connfeedback.get('subject')
  1407. });
  1408. }
  1409. });
  1410. this.connfeedback = new this.ConnectionFeedback();
  1411. this.XMPPStatus = Backbone.Model.extend({
  1412. initialize () {
  1413. this.set({
  1414. 'status' : this.getStatus()
  1415. });
  1416. this.on('change', (item) => {
  1417. if (_.has(item.changed, 'status')) {
  1418. _converse.emit('statusChanged', this.get('status'));
  1419. }
  1420. if (_.has(item.changed, 'status_message')) {
  1421. _converse.emit('statusMessageChanged', this.get('status_message'));
  1422. }
  1423. });
  1424. },
  1425. constructPresence (type, status_message) {
  1426. let presence;
  1427. type = _.isString(type) ? type : (this.get('status') || _converse.default_state);
  1428. status_message = _.isString(status_message) ? status_message : undefined;
  1429. // Most of these presence types are actually not explicitly sent,
  1430. // but I add all of them here for reference and future proofing.
  1431. if ((type === 'unavailable') ||
  1432. (type === 'probe') ||
  1433. (type === 'error') ||
  1434. (type === 'unsubscribe') ||
  1435. (type === 'unsubscribed') ||
  1436. (type === 'subscribe') ||
  1437. (type === 'subscribed')) {
  1438. presence = $pres({'type': type});
  1439. } else if (type === 'offline') {
  1440. presence = $pres({'type': 'unavailable'});
  1441. } else if (type === 'online') {
  1442. presence = $pres();
  1443. } else {
  1444. presence = $pres().c('show').t(type).up();
  1445. }
  1446. if (status_message) {
  1447. presence.c('status').t(status_message).up();
  1448. }
  1449. presence.c('priority').t(
  1450. _.isNaN(Number(_converse.priority)) ? 0 : _converse.priority
  1451. );
  1452. return presence;
  1453. },
  1454. sendPresence (type, status_message) {
  1455. _converse.connection.send(this.constructPresence(type, status_message));
  1456. },
  1457. setStatus (value) {
  1458. this.sendPresence(value);
  1459. this.save({'status': value});
  1460. },
  1461. getStatus () {
  1462. return this.get('status') || _converse.default_state;
  1463. },
  1464. setStatusMessage (status_message) {
  1465. this.sendPresence(this.getStatus(), status_message);
  1466. this.save({'status_message': status_message});
  1467. if (this.xhr_custom_status) {
  1468. const xhr = new XMLHttpRequest();
  1469. xhr.open('POST', this.xhr_custom_status_url, true);
  1470. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  1471. xhr.send({'msg': status_message});
  1472. }
  1473. const prev_status = this.get('status_message');
  1474. if (prev_status === status_message) {
  1475. this.trigger("update-status-ui", this);
  1476. }
  1477. }
  1478. });
  1479. this.setUpXMLLogging = function () {
  1480. Strophe.log = function (level, msg) {
  1481. _converse.log(msg, level);
  1482. };
  1483. if (this.debug) {
  1484. this.connection.xmlInput = function (body) {
  1485. _converse.log(body.outerHTML, Strophe.LogLevel.DEBUG);
  1486. };
  1487. this.connection.xmlOutput = function (body) {
  1488. _converse.log(body.outerHTML, Strophe.LogLevel.DEBUG);
  1489. };
  1490. }
  1491. };
  1492. this.fetchLoginCredentials = () =>
  1493. new Promise((resolve, reject) => {
  1494. const xhr = new XMLHttpRequest();
  1495. xhr.open('GET', _converse.credentials_url, true);
  1496. xhr.setRequestHeader('Accept', "application/json, text/javascript");
  1497. xhr.onload = function() {
  1498. if (xhr.status >= 200 && xhr.status < 400) {
  1499. const data = JSON.parse(xhr.responseText);
  1500. resolve({
  1501. 'jid': data.jid,
  1502. 'password': data.password
  1503. });
  1504. } else {
  1505. xhr.onerror();
  1506. }
  1507. };
  1508. xhr.onerror = function () {
  1509. delete _converse.connection;
  1510. _converse.emit('noResumeableSession', this);
  1511. reject(xhr.responseText);
  1512. };
  1513. xhr.send();
  1514. });
  1515. this.startNewBOSHSession = function () {
  1516. const xhr = new XMLHttpRequest();
  1517. xhr.open('GET', _converse.prebind_url, true);
  1518. xhr.setRequestHeader('Accept', "application/json, text/javascript");
  1519. xhr.onload = function() {
  1520. if (xhr.status >= 200 && xhr.status < 400) {
  1521. const data = JSON.parse(xhr.responseText);
  1522. _converse.connection.attach(
  1523. data.jid, data.sid, data.rid,
  1524. _converse.onConnectStatusChanged);
  1525. } else {
  1526. xhr.onerror();
  1527. }
  1528. };
  1529. xhr.onerror = function () {
  1530. delete _converse.connection;
  1531. _converse.emit('noResumeableSession', this);
  1532. };
  1533. xhr.send();
  1534. };
  1535. this.restoreBOSHSession = function (jid_is_required) {
  1536. /* Tries to restore a cached BOSH session. */
  1537. if (!this.jid) {
  1538. const msg = "restoreBOSHSession: tried to restore a \"keepalive\" session "+
  1539. "but we don't have the JID for the user!";
  1540. if (jid_is_required) {
  1541. throw new Error(msg);
  1542. } else {
  1543. _converse.log(msg);
  1544. }
  1545. }
  1546. try {
  1547. this.connection.restore(this.jid, this.onConnectStatusChanged);
  1548. return true;
  1549. } catch (e) {
  1550. _converse.log(
  1551. "Could not restore session for jid: "+
  1552. this.jid+" Error message: "+e.message);
  1553. this.clearSession(); // If there's a roster, we want to clear it (see #555)
  1554. return false;
  1555. }
  1556. };
  1557. this.attemptPreboundSession = function (reconnecting) {
  1558. /* Handle session resumption or initialization when prebind is
  1559. * being used.
  1560. */
  1561. if (!reconnecting) {
  1562. if (this.keepalive && this.restoreBOSHSession(true)) {
  1563. return;
  1564. }
  1565. // No keepalive, or session resumption has failed.
  1566. if (this.jid && this.sid && this.rid) {
  1567. return this.connection.attach(
  1568. this.jid, this.sid, this.rid,
  1569. this.onConnectStatusChanged
  1570. );
  1571. }
  1572. }
  1573. if (this.prebind_url) {
  1574. return this.startNewBOSHSession();
  1575. } else {
  1576. throw new Error(
  1577. "attemptPreboundSession: If you use prebind and not keepalive, "+
  1578. "then you MUST supply JID, RID and SID values or a prebind_url.");
  1579. }
  1580. };
  1581. this.attemptNonPreboundSession = function (credentials, reconnecting) {
  1582. /* Handle session resumption or initialization when prebind is not being used.
  1583. *
  1584. * Two potential options exist and are handled in this method:
  1585. * 1. keepalive
  1586. * 2. auto_login
  1587. */
  1588. if (!reconnecting && this.keepalive && this.restoreBOSHSession()) {
  1589. return;
  1590. }
  1591. if (credentials) {
  1592. // When credentials are passed in, they override prebinding
  1593. // or credentials fetching via HTTP
  1594. this.autoLogin(credentials);
  1595. } else if (this.auto_login) {
  1596. if (this.credentials_url) {
  1597. this.fetchLoginCredentials().then(
  1598. this.autoLogin.bind(this),
  1599. this.autoLogin.bind(this)
  1600. );
  1601. } else if (!this.jid) {
  1602. throw new Error(
  1603. "attemptNonPreboundSession: If you use auto_login, "+
  1604. "you also need to give either a jid value (and if "+
  1605. "applicable a password) or you need to pass in a URL "+
  1606. "from where the username and password can be fetched "+
  1607. "(via credentials_url)."
  1608. );
  1609. } else {
  1610. this.autoLogin(); // Probably ANONYMOUS login
  1611. }
  1612. } else if (reconnecting) {
  1613. this.autoLogin();
  1614. }
  1615. };
  1616. this.autoLogin = function (credentials) {
  1617. if (credentials) {
  1618. // If passed in, the credentials come from credentials_url,
  1619. // so we set them on the converse object.
  1620. this.jid = credentials.jid;
  1621. }
  1622. if (this.authentication === _converse.ANONYMOUS) {
  1623. if (!this.jid) {
  1624. throw new Error("Config Error: when using anonymous login " +
  1625. "you need to provide the server's domain via the 'jid' option. " +
  1626. "Either when calling converse.initialize, or when calling " +
  1627. "_converse.api.user.login.");
  1628. }
  1629. if (!this.connection.reconnecting) {
  1630. this.connection.reset();
  1631. }
  1632. this.connection.connect(this.jid.toLowerCase(), null, this.onConnectStatusChanged);
  1633. } else if (this.authentication === _converse.LOGIN) {
  1634. const password = _.isNil(credentials) ? (_converse.connection.pass || this.password) : credentials.password;
  1635. if (!password) {
  1636. if (this.auto_login) {
  1637. throw new Error("initConnection: If you use auto_login and "+
  1638. "authentication='login' then you also need to provide a password.");
  1639. }
  1640. _converse.setDisconnectionCause(Strophe.Status.AUTHFAIL, undefined, true);
  1641. _converse.disconnect();
  1642. return;
  1643. }
  1644. const resource = Strophe.getResourceFromJid(this.jid);
  1645. if (!resource) {
  1646. this.jid = this.jid.toLowerCase() + _converse.generateResource();
  1647. } else {
  1648. this.jid = Strophe.getBareJidFromJid(this.jid).toLowerCase()+'/'+resource;
  1649. }
  1650. if (!this.connection.reconnecting) {
  1651. this.connection.reset();
  1652. }
  1653. this.connection.connect(this.jid, password, this.onConnectStatusChanged);
  1654. }
  1655. };
  1656. this.logIn = function (credentials, reconnecting) {
  1657. // We now try to resume or automatically set up a new session.
  1658. // Otherwise the user will be shown a login form.
  1659. if (this.authentication === _converse.PREBIND) {
  1660. this.attemptPreboundSession(reconnecting);
  1661. } else {
  1662. this.attemptNonPreboundSession(credentials, reconnecting);
  1663. }
  1664. };
  1665. this.initConnection = function () {
  1666. if (!this.connection) {
  1667. if (!this.bosh_service_url && ! this.websocket_url) {
  1668. throw new Error("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
  1669. }
  1670. if (('WebSocket' in window || 'MozWebSocket' in window) && this.websocket_url) {
  1671. this.connection = new Strophe.Connection(this.websocket_url, this.connection_options);
  1672. } else if (this.bosh_service_url) {
  1673. this.connection = new Strophe.Connection(
  1674. this.bosh_service_url,
  1675. _.assignIn(this.connection_options, {'keepalive': this.keepalive})
  1676. );
  1677. } else {
  1678. throw new Error("initConnection: this browser does not support websockets and bosh_service_url wasn't specified.");
  1679. }
  1680. }
  1681. _converse.emit('connectionInitialized');
  1682. };
  1683. this._tearDown = function () {
  1684. /* Remove those views which are only allowed with a valid
  1685. * connection.
  1686. */
  1687. _converse.emit('beforeTearDown');
  1688. _converse.unregisterPresenceHandler();
  1689. if (_converse.roster) {
  1690. _converse.roster.off().reset(); // Removes roster contacts
  1691. }
  1692. if (!_.isUndefined(_converse.session)) {
  1693. _converse.session.destroy();
  1694. }
  1695. window.removeEventListener('click', _converse.onUserActivity);
  1696. window.removeEventListener('focus', _converse.onUserActivity);
  1697. window.removeEventListener('keypress', _converse.onUserActivity);
  1698. window.removeEventListener('mousemove', _converse.onUserActivity);
  1699. window.removeEventListener(unloadevent, _converse.onUserActivity);
  1700. window.clearInterval(_converse.everySecondTrigger);
  1701. _converse.emit('afterTearDown');
  1702. return _converse;
  1703. };
  1704. this.initPlugins = function () {
  1705. // If initialize gets called a second time (e.g. during tests), then we
  1706. // need to re-apply all plugins (for a new converse instance), and we
  1707. // therefore need to clear this array that prevents plugins from being
  1708. // initialized twice.
  1709. // If initialize is called for the first time, then this array is empty
  1710. // in any case.
  1711. _converse.pluggable.initialized_plugins = [];
  1712. const whitelist = _converse.core_plugins.concat(
  1713. _converse.whitelisted_plugins);
  1714. _converse.pluggable.initializePlugins({
  1715. 'updateSettings' () {
  1716. _converse.log(
  1717. "(DEPRECATION) "+
  1718. "The `updateSettings` method has been deprecated. "+
  1719. "Please use `_converse.api.settings.update` instead.",
  1720. Strophe.LogLevel.WARN
  1721. )
  1722. _converse.api.settings.update.apply(_converse, arguments);
  1723. },
  1724. '_converse': _converse
  1725. }, whitelist, _converse.blacklisted_plugins);
  1726. _converse.emit('pluginsInitialized');
  1727. };
  1728. // Initialization
  1729. // --------------
  1730. // This is the end of the initialize method.
  1731. if (settings.connection) {
  1732. this.connection = settings.connection;
  1733. }
  1734. _converse.initPlugins();
  1735. _converse.initConnection();
  1736. _converse.setUpXMLLogging();
  1737. _converse.logIn();
  1738. _converse.registerGlobalEventHandlers();
  1739. Backbone.history.start();
  1740. if (!_.isUndefined(_converse.connection) &&
  1741. _converse.connection.service === 'jasmine tests') {
  1742. return _converse;
  1743. } else {
  1744. return init_promise.promise;
  1745. }
  1746. };
  1747. // API methods only available to plugins
  1748. _converse.api = {
  1749. 'connection': {
  1750. 'connected' () {
  1751. return _converse.connection && _converse.connection.connected || false;
  1752. },
  1753. 'disconnect' () {
  1754. _converse.connection.disconnect();
  1755. },
  1756. },
  1757. 'emit' () {
  1758. _converse.emit.apply(_converse, arguments);
  1759. },
  1760. 'user': {
  1761. 'jid' () {
  1762. return _converse.connection.jid;
  1763. },
  1764. 'login' (credentials) {
  1765. _converse.initConnection();
  1766. _converse.logIn(credentials);
  1767. },
  1768. 'logout' () {
  1769. _converse.logOut();
  1770. },
  1771. 'status': {
  1772. 'get' () {
  1773. return _converse.xmppstatus.get('status');
  1774. },
  1775. 'set' (value, message) {
  1776. const data = {'status': value};
  1777. if (!_.includes(_.keys(_converse.STATUS_WEIGHTS), value)) {
  1778. throw new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1');
  1779. }
  1780. if (_.isString(message)) {
  1781. data.status_message = message;
  1782. }
  1783. _converse.xmppstatus.sendPresence(value);
  1784. _converse.xmppstatus.save(data);
  1785. },
  1786. 'message': {
  1787. 'get' () {
  1788. return _converse.xmppstatus.get('status_message');
  1789. },
  1790. 'set' (stat) {
  1791. _converse.xmppstatus.save({'status_message': stat});
  1792. }
  1793. }
  1794. },
  1795. },
  1796. 'settings': {
  1797. 'update' (settings) {
  1798. utils.merge(_converse.default_settings, settings);
  1799. utils.merge(_converse, settings);
  1800. utils.applyUserSettings(_converse, settings, _converse.user_settings);
  1801. },
  1802. 'get' (key) {
  1803. if (_.includes(_.keys(_converse.default_settings), key)) {
  1804. return _converse[key];
  1805. }
  1806. },
  1807. 'set' (key, val) {
  1808. const o = {};
  1809. if (_.isObject(key)) {
  1810. _.assignIn(_converse, _.pick(key, _.keys(_converse.default_settings)));
  1811. } else if (_.isString("string")) {
  1812. o[key] = val;
  1813. _.assignIn(_converse, _.pick(o, _.keys(_converse.default_settings)));
  1814. }
  1815. }
  1816. },
  1817. 'promises': {
  1818. 'add' (promises) {
  1819. promises = _.isArray(promises) ? promises : [promises]
  1820. _.each(promises, addPromise);
  1821. }
  1822. },
  1823. 'contacts': {
  1824. 'get' (jids) {
  1825. const _transform = function (jid) {
  1826. const contact = _converse.roster.get(Strophe.getBareJidFromJid(jid));
  1827. if (contact) {
  1828. return contact.attributes;
  1829. }
  1830. return null;
  1831. };
  1832. if (_.isUndefined(jids)) {
  1833. jids = _converse.roster.pluck('jid');
  1834. } else if (_.isString(jids)) {
  1835. return _transform(jids);
  1836. }
  1837. return _.map(jids, _transform);
  1838. },
  1839. 'add' (jid, name) {
  1840. if (!_.isString(jid) || !_.includes(jid, '@')) {
  1841. throw new TypeError('contacts.add: invalid jid');
  1842. }
  1843. _converse.roster.addAndSubscribe(jid, _.isEmpty(name)? jid: name);
  1844. }
  1845. },
  1846. 'tokens': {
  1847. 'get' (id) {
  1848. if (!_converse.expose_rid_and_sid || _.isUndefined(_converse.connection)) {
  1849. return null;
  1850. }
  1851. if (id.toLowerCase() === 'rid') {
  1852. return _converse.connection.rid || _converse.connection._proto.rid;
  1853. } else if (id.toLowerCase() === 'sid') {
  1854. return _converse.connection.sid || _converse.connection._proto.sid;
  1855. }
  1856. }
  1857. },
  1858. 'listen': {
  1859. 'once': _converse.once.bind(_converse),
  1860. 'on': _converse.on.bind(_converse),
  1861. 'not': _converse.off.bind(_converse),
  1862. 'stanza' (name, options, handler) {
  1863. if (_.isFunction(options)) {
  1864. handler = options;
  1865. options = {};
  1866. } else {
  1867. options = options || {};
  1868. }
  1869. _converse.connection.addHandler(
  1870. handler,
  1871. options.ns,
  1872. name,
  1873. options.type,
  1874. options.id,
  1875. options.from,
  1876. options
  1877. );
  1878. },
  1879. },
  1880. 'waitUntil' (name) {
  1881. const promise = _converse.promises[name];
  1882. if (_.isUndefined(promise)) {
  1883. return null;
  1884. }
  1885. return promise.promise;
  1886. },
  1887. 'send' (stanza) {
  1888. _converse.connection.send(stanza);
  1889. },
  1890. };
  1891. // The public API
  1892. return {
  1893. 'initialize' (settings, callback) {
  1894. return _converse.initialize(settings, callback);
  1895. },
  1896. 'plugins': {
  1897. 'add' (name, plugin) {
  1898. plugin.__name__ = name;
  1899. if (!_.isUndefined(_converse.pluggable.plugins[name])) {
  1900. throw new TypeError(
  1901. `Error: plugin with name "${name}" has already been `+
  1902. 'registered!');
  1903. } else {
  1904. _converse.pluggable.plugins[name] = plugin;
  1905. }
  1906. }
  1907. },
  1908. 'env': {
  1909. '$build': $build,
  1910. '$iq': $iq,
  1911. '$msg': $msg,
  1912. '$pres': $pres,
  1913. 'Backbone': Backbone,
  1914. 'Promise': Promise,
  1915. 'Strophe': Strophe,
  1916. '_': _,
  1917. 'b64_sha1': b64_sha1,
  1918. 'moment': moment,
  1919. 'sizzle': sizzle,
  1920. 'utils': utils
  1921. }
  1922. };
  1923. }));