123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- // Converse.js (A browser based XMPP chat client)
- // http://conversejs.org
- //
- // Copyright (c) 2012-2016, Jan-Carel Brand <jc@opkode.com>
- // Licensed under the Mozilla Public License (MPLv2)
- //
- /*global define */
- (function (root, factory) {
- define("converse-api", [
- "jquery",
- "underscore",
- "moment_with_locales",
- "strophe",
- "utils",
- "converse-core"
- ],
- factory);
- }(this, function ($, _, moment, strophe, utils, converse) {
- var Strophe = strophe.Strophe;
- return {
- 'initialize': function (settings, callback) {
- return converse.initialize(settings, callback);
- },
- 'log': converse.log,
- 'connection': {
- 'connected': function () {
- return converse.connection && converse.connection.connected || false;
- },
- 'disconnect': function () {
- converse.connection.disconnect();
- },
- },
- 'user': {
- 'jid': function () {
- return converse.connection.jid;
- },
- 'login': function (credentials) {
- converse.initConnection();
- converse.logIn(credentials);
- },
- 'logout': function () {
- converse.logOut();
- },
- 'status': {
- 'get': function () {
- return converse.xmppstatus.get('status');
- },
- 'set': function (value, message) {
- var data = {'status': value};
- if (!_.contains(_.keys(converse.STATUS_WEIGHTS), value)) {
- throw new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1');
- }
- if (typeof message === "string") {
- data.status_message = message;
- }
- converse.xmppstatus.sendPresence(value);
- converse.xmppstatus.save(data);
- },
- 'message': {
- 'get': function () {
- return converse.xmppstatus.get('status_message');
- },
- 'set': function (stat) {
- converse.xmppstatus.save({'status_message': stat});
- }
- }
- },
- },
- 'settings': {
- 'get': function (key) {
- if (_.contains(Object.keys(converse.default_settings), key)) {
- return converse[key];
- }
- },
- 'set': function (key, val) {
- var o = {};
- if (typeof key === "object") {
- _.extend(converse, _.pick(key, Object.keys(converse.default_settings)));
- } else if (typeof key === "string") {
- o[key] = val;
- _.extend(converse, _.pick(o, Object.keys(converse.default_settings)));
- }
- }
- },
- 'contacts': {
- 'get': function (jids) {
- var _transform = function (jid) {
- var contact = converse.roster.get(Strophe.getBareJidFromJid(jid));
- if (contact) {
- return contact.attributes;
- }
- return null;
- };
- if (typeof jids === "undefined") {
- jids = converse.roster.pluck('jid');
- } else if (typeof jids === "string") {
- return _transform(jids);
- }
- return _.map(jids, _transform);
- },
- 'add': function (jid, name) {
- if (typeof jid !== "string" || jid.indexOf('@') < 0) {
- throw new TypeError('contacts.add: invalid jid');
- }
- converse.roster.addAndSubscribe(jid, _.isEmpty(name)? jid: name);
- }
- },
- 'chats': {
- 'open': function (jids) {
- var chatbox;
- if (typeof jids === "undefined") {
- converse.log("chats.open: You need to provide at least one JID", "error");
- return null;
- } else if (typeof jids === "string") {
- chatbox = converse.wrappedChatBox(
- converse.chatboxes.getChatBox(jids, true).trigger('show')
- );
- return chatbox;
- }
- return _.map(jids, function (jid) {
- chatbox = converse.wrappedChatBox(
- converse.chatboxes.getChatBox(jid, true).trigger('show')
- );
- return chatbox;
- });
- },
- 'get': function (jids) {
- if (typeof jids === "undefined") {
- var result = [];
- converse.chatboxes.each(function (chatbox) {
- // FIXME: Leaky abstraction from MUC. We need to add a
- // base type for chat boxes, and check for that.
- if (chatbox.get('type') !== 'chatroom') {
- result.push(converse.wrappedChatBox(chatbox));
- }
- });
- return result;
- } else if (typeof jids === "string") {
- return converse.wrappedChatBox(converse.chatboxes.getChatBox(jids));
- }
- return _.map(jids,
- _.partial(
- _.compose(
- converse.wrappedChatBox.bind(converse), converse.chatboxes.getChatBox.bind(converse.chatboxes)
- ), _, true
- )
- );
- }
- },
- 'tokens': {
- 'get': function (id) {
- if (!converse.expose_rid_and_sid || typeof converse.connection === "undefined") {
- return null;
- }
- if (id.toLowerCase() === 'rid') {
- return converse.connection.rid || converse.connection._proto.rid;
- } else if (id.toLowerCase() === 'sid') {
- return converse.connection.sid || converse.connection._proto.sid;
- }
- }
- },
- 'listen': {
- 'once': function (evt, handler, context) {
- converse.once(evt, handler, context);
- },
- 'on': function (evt, handler, context) {
- converse.on(evt, handler, context);
- },
- 'not': function (evt, handler) {
- converse.off(evt, handler);
- },
- 'stanza': function (name, options, handler) {
- if (typeof options === 'function') {
- handler = options;
- options = {};
- } else {
- options = options || {};
- }
- converse.connection.addHandler(
- handler,
- options.ns,
- name,
- options.type,
- options.id,
- options.from,
- options
- );
- },
- },
- 'send': function (stanza) {
- converse.connection.send(stanza);
- },
- 'plugins': {
- 'add': function (name, plugin) {
- plugin.__name__ = name;
- converse.pluggable.plugins[name] = plugin;
- },
- 'remove': function (name) {
- delete converse.plugins[name];
- },
- 'override': function (name, value) {
- /* Helper method for overriding methods and attributes directly on the
- * converse object. For Backbone objects, use instead the 'extend'
- * method.
- *
- * If a method is overridden, then the original method will still be
- * available via the __super__ attribute.
- *
- * name: The attribute being overridden.
- * value: The value of the attribute being overridden.
- */
- converse._overrideAttribute(name, value);
- },
- 'extend': function (obj, attributes) {
- /* Helper method for overriding or extending Converse's Backbone Views or Models
- *
- * When a method is overriden, the original will still be available
- * on the __super__ attribute of the object being overridden.
- *
- * obj: The Backbone View or Model
- * attributes: A hash of attributes, such as you would pass to Backbone.Model.extend or Backbone.View.extend
- */
- converse._extendObject(obj, attributes);
- }
- },
- 'env': {
- '$build': strophe.$build,
- '$iq': strophe.$iq,
- '$msg': strophe.$msg,
- '$pres': strophe.$pres,
- 'Strophe': strophe.Strophe,
- 'b64_sha1': strophe.SHA1.b64_sha1,
- '_': _,
- 'jQuery': $,
- 'moment': moment,
- 'utils': utils
- }
- };
- }));
|