converse-api.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Converse.js (A browser based XMPP chat client)
  2. // http://conversejs.org
  3. //
  4. // Copyright (c) 2012-2016, Jan-Carel Brand <jc@opkode.com>
  5. // Licensed under the Mozilla Public License (MPLv2)
  6. //
  7. /*global define */
  8. /* This is a Converse.js plugin which add support for multi-user chat rooms, as
  9. * specified in XEP-0045 Multi-user chat.
  10. */
  11. (function (root, factory) {
  12. define("converse-api", [
  13. "jquery",
  14. "underscore",
  15. "moment_with_locales",
  16. "strophe",
  17. "utils",
  18. "converse-core"
  19. ],
  20. factory);
  21. }(this, function ($, _, moment, strophe, utils, converse) {
  22. var Strophe = strophe.Strophe;
  23. return {
  24. 'initialize': function (settings, callback) {
  25. converse.initialize(settings, callback);
  26. },
  27. 'connection': {
  28. 'connected': function () {
  29. return converse.connection && converse.connection.connected || false;
  30. },
  31. 'disconnect': function () {
  32. converse.connection.disconnect();
  33. },
  34. },
  35. 'user': {
  36. 'jid': function () {
  37. return converse.connection.jid;
  38. },
  39. 'login': function (credentials) {
  40. converse.initConnection();
  41. converse.logIn(credentials);
  42. },
  43. 'logout': function () {
  44. converse.logOut();
  45. },
  46. 'status': {
  47. 'get': function () {
  48. return converse.xmppstatus.get('status');
  49. },
  50. 'set': function (value, message) {
  51. var data = {'status': value};
  52. if (!_.contains(_.keys(converse.STATUS_WEIGHTS), value)) {
  53. throw new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1');
  54. }
  55. if (typeof message === "string") {
  56. data.status_message = message;
  57. }
  58. converse.xmppstatus.sendPresence(value);
  59. converse.xmppstatus.save(data);
  60. },
  61. 'message': {
  62. 'get': function () {
  63. return converse.xmppstatus.get('status_message');
  64. },
  65. 'set': function (stat) {
  66. converse.xmppstatus.save({'status_message': stat});
  67. }
  68. }
  69. },
  70. },
  71. 'settings': {
  72. 'get': function (key) {
  73. if (_.contains(Object.keys(converse.default_settings), key)) {
  74. return converse[key];
  75. }
  76. },
  77. 'set': function (key, val) {
  78. var o = {};
  79. if (typeof key === "object") {
  80. _.extend(converse, _.pick(key, Object.keys(converse.default_settings)));
  81. } else if (typeof key === "string") {
  82. o[key] = val;
  83. _.extend(converse, _.pick(o, Object.keys(converse.default_settings)));
  84. }
  85. }
  86. },
  87. 'contacts': {
  88. 'get': function (jids) {
  89. var _transform = function (jid) {
  90. var contact = converse.roster.get(Strophe.getBareJidFromJid(jid));
  91. if (contact) {
  92. return contact.attributes;
  93. }
  94. return null;
  95. };
  96. if (typeof jids === "undefined") {
  97. jids = converse.roster.pluck('jid');
  98. } else if (typeof jids === "string") {
  99. return _transform(jids);
  100. }
  101. return _.map(jids, _transform);
  102. },
  103. 'add': function (jid, name) {
  104. if (typeof jid !== "string" || jid.indexOf('@') < 0) {
  105. throw new TypeError('contacts.add: invalid jid');
  106. }
  107. converse.roster.addAndSubscribe(jid, _.isEmpty(name)? jid: name);
  108. }
  109. },
  110. 'chats': {
  111. 'open': function (jids) {
  112. var chatbox;
  113. if (typeof jids === "undefined") {
  114. converse.log("chats.open: You need to provide at least one JID", "error");
  115. return null;
  116. } else if (typeof jids === "string") {
  117. chatbox = converse.wrappedChatBox(converse.chatboxes.getChatBox(jids, true));
  118. chatbox.open();
  119. return chatbox;
  120. }
  121. return _.map(jids, function (jid) {
  122. chatbox = converse.wrappedChatBox(converse.chatboxes.getChatBox(jid, true));
  123. chatbox.open();
  124. return chatbox;
  125. });
  126. },
  127. 'get': function (jids) {
  128. if (typeof jids === "undefined") {
  129. converse.log("chats.get: You need to provide at least one JID", "error");
  130. return null;
  131. } else if (typeof jids === "string") {
  132. return converse.wrappedChatBox(converse.chatboxes.getChatBox(jids, true));
  133. }
  134. return _.map(jids, _.partial(_.compose(converse.wrappedChatBox, converse.chatboxes.getChatBox.bind(converse.chatboxes)), _, true));
  135. }
  136. },
  137. 'tokens': {
  138. 'get': function (id) {
  139. if (!converse.expose_rid_and_sid || typeof converse.connection === "undefined") {
  140. return null;
  141. }
  142. if (id.toLowerCase() === 'rid') {
  143. return converse.connection.rid || converse.connection._proto.rid;
  144. } else if (id.toLowerCase() === 'sid') {
  145. return converse.connection.sid || converse.connection._proto.sid;
  146. }
  147. }
  148. },
  149. 'listen': {
  150. 'once': function (evt, handler) {
  151. converse.once(evt, handler);
  152. },
  153. 'on': function (evt, handler) {
  154. converse.on(evt, handler);
  155. },
  156. 'not': function (evt, handler) {
  157. converse.off(evt, handler);
  158. },
  159. 'stanza': function (name, options, handler) {
  160. if (typeof options === 'function') {
  161. handler = options;
  162. options = {};
  163. } else {
  164. options = options || {};
  165. }
  166. converse.connection.addHandler(
  167. handler,
  168. options.ns,
  169. name,
  170. options.type,
  171. options.id,
  172. options.from,
  173. options
  174. );
  175. },
  176. },
  177. 'send': function (stanza) {
  178. converse.connection.send(stanza);
  179. },
  180. 'plugins': {
  181. 'add': function (name, plugin) {
  182. converse.plugins[name] = plugin;
  183. },
  184. 'remove': function (name) {
  185. delete converse.plugins[name];
  186. },
  187. 'override': function (name, value) {
  188. /* Helper method for overriding methods and attributes directly on the
  189. * converse object. For Backbone objects, use instead the 'extend'
  190. * method.
  191. *
  192. * If a method is overridden, then the original method will still be
  193. * available via the _super attribute.
  194. *
  195. * name: The attribute being overridden.
  196. * value: The value of the attribute being overridden.
  197. */
  198. converse._overrideAttribute(name, value);
  199. },
  200. 'extend': function (obj, attributes) {
  201. /* Helper method for overriding or extending Converse's Backbone Views or Models
  202. *
  203. * When a method is overriden, the original will still be available
  204. * on the _super attribute of the object being overridden.
  205. *
  206. * obj: The Backbone View or Model
  207. * attributes: A hash of attributes, such as you would pass to Backbone.Model.extend or Backbone.View.extend
  208. */
  209. converse._extendObject(obj, attributes);
  210. }
  211. },
  212. 'env': {
  213. '$build': strophe.$build,
  214. '$iq': strophe.$iq,
  215. '$msg': strophe.$msg,
  216. '$pres': strophe.$pres,
  217. 'Strophe': strophe.Strophe,
  218. 'b64_sha1': strophe.SHA1.b64_sha1,
  219. '_': _,
  220. 'jQuery': $,
  221. 'moment': moment,
  222. 'utils': utils
  223. }
  224. };
  225. }));