|
@@ -5,23 +5,21 @@
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
//
|
|
//
|
|
/*global Backbone, define, window, document */
|
|
/*global Backbone, define, window, document */
|
|
-
|
|
|
|
(function (root, factory) {
|
|
(function (root, factory) {
|
|
- define([
|
|
|
|
- "sizzle",
|
|
|
|
- "jquery",
|
|
|
|
- "lodash",
|
|
|
|
- "polyfill",
|
|
|
|
- "locales",
|
|
|
|
- "utils",
|
|
|
|
- "moment_with_locales",
|
|
|
|
- "strophe",
|
|
|
|
- "pluggable",
|
|
|
|
- "strophe.disco",
|
|
|
|
- "backbone.browserStorage",
|
|
|
|
- "backbone.overview",
|
|
|
|
|
|
+ define(["sizzle",
|
|
|
|
+ "jquery",
|
|
|
|
+ "lodash",
|
|
|
|
+ "polyfill",
|
|
|
|
+ "locales",
|
|
|
|
+ "utils",
|
|
|
|
+ "moment_with_locales",
|
|
|
|
+ "strophe",
|
|
|
|
+ "pluggable",
|
|
|
|
+ "strophe.disco",
|
|
|
|
+ "backbone.browserStorage",
|
|
|
|
+ "backbone.overview",
|
|
], factory);
|
|
], factory);
|
|
-}(this, function (sizzle, $, _, dummy, locales, utils, moment, Strophe, pluggable) {
|
|
|
|
|
|
+}(this, function (sizzle, $, _, polyfill, locales, utils, moment, Strophe, pluggable) {
|
|
/*
|
|
/*
|
|
* Cannot use this due to Safari bug.
|
|
* Cannot use this due to Safari bug.
|
|
* See https://github.com/jcbrand/converse.js/issues/196
|
|
* See https://github.com/jcbrand/converse.js/issues/196
|
|
@@ -31,6 +29,7 @@
|
|
// Strophe globals
|
|
// Strophe globals
|
|
var $build = Strophe.$build;
|
|
var $build = Strophe.$build;
|
|
var $iq = Strophe.$iq;
|
|
var $iq = Strophe.$iq;
|
|
|
|
+ var $msg = Strophe.$msg;
|
|
var $pres = Strophe.$pres;
|
|
var $pres = Strophe.$pres;
|
|
var b64_sha1 = Strophe.SHA1.b64_sha1;
|
|
var b64_sha1 = Strophe.SHA1.b64_sha1;
|
|
Strophe = Strophe.Strophe;
|
|
Strophe = Strophe.Strophe;
|
|
@@ -2034,5 +2033,209 @@
|
|
return init_deferred.promise();
|
|
return init_deferred.promise();
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- return _converse;
|
|
|
|
|
|
+
|
|
|
|
+ // API methods only available to plugins
|
|
|
|
+ _converse.api = {
|
|
|
|
+ '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 (!_.includes(_.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 (_.isString(message)) {
|
|
|
|
+ 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 (_.includes(_.keys(_converse.default_settings), key)) {
|
|
|
|
+ return _converse[key];
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ 'set': function (key, val) {
|
|
|
|
+ var o = {};
|
|
|
|
+ if (_.isObject(key)) {
|
|
|
|
+ _.assignIn(_converse, _.pick(key, _.keys(_converse.default_settings)));
|
|
|
|
+ } else if (_.isString("string")) {
|
|
|
|
+ o[key] = val;
|
|
|
|
+ _.assignIn(_converse, _.pick(o, _.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 (_.isUndefined(jids)) {
|
|
|
|
+ jids = _converse.roster.pluck('jid');
|
|
|
|
+ } else if (_.isString(jids)) {
|
|
|
|
+ return _transform(jids);
|
|
|
|
+ }
|
|
|
|
+ return _.map(jids, _transform);
|
|
|
|
+ },
|
|
|
|
+ 'add': function (jid, name) {
|
|
|
|
+ if (!_.isString(jid) || !_.includes(jid, '@')) {
|
|
|
|
+ throw new TypeError('contacts.add: invalid jid');
|
|
|
|
+ }
|
|
|
|
+ _converse.roster.addAndSubscribe(jid, _.isEmpty(name)? jid: name);
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ 'chats': {
|
|
|
|
+ 'open': function (jids, attrs) {
|
|
|
|
+ var chatbox;
|
|
|
|
+ if (_.isUndefined(jids)) {
|
|
|
|
+ _converse.log("chats.open: You need to provide at least one JID", "error");
|
|
|
|
+ return null;
|
|
|
|
+ } else if (_.isString(jids)) {
|
|
|
|
+ chatbox = _converse.wrappedChatBox(
|
|
|
|
+ _converse.chatboxes.getChatBox(jids, true, attrs).trigger('show')
|
|
|
|
+ );
|
|
|
|
+ return chatbox;
|
|
|
|
+ }
|
|
|
|
+ return _.map(jids, function (jid) {
|
|
|
|
+ chatbox = _converse.wrappedChatBox(
|
|
|
|
+ _converse.chatboxes.getChatBox(jid, true, attrs).trigger('show')
|
|
|
|
+ );
|
|
|
|
+ return chatbox;
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ 'get': function (jids) {
|
|
|
|
+ if (_.isUndefined(jids)) {
|
|
|
|
+ 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 (_.isString(jids)) {
|
|
|
|
+ return _converse.wrappedChatBox(_converse.chatboxes.getChatBox(jids));
|
|
|
|
+ }
|
|
|
|
+ return _.map(jids,
|
|
|
|
+ _.partial(
|
|
|
|
+ _.flow(
|
|
|
|
+ _converse.chatboxes.getChatBox.bind(_converse.chatboxes),
|
|
|
|
+ _converse.wrappedChatBox.bind(_converse)
|
|
|
|
+ ), _, true
|
|
|
|
+ )
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ 'tokens': {
|
|
|
|
+ 'get': function (id) {
|
|
|
|
+ if (!_converse.expose_rid_and_sid || _.isUndefined(_converse.connection)) {
|
|
|
|
+ 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': _converse.once,
|
|
|
|
+ 'on': _converse.on,
|
|
|
|
+ 'not': _converse.off,
|
|
|
|
+ 'stanza': function (name, options, handler) {
|
|
|
|
+ if (_.isFunction(options)) {
|
|
|
|
+ handler = options;
|
|
|
|
+ options = {};
|
|
|
|
+ } else {
|
|
|
|
+ options = options || {};
|
|
|
|
+ }
|
|
|
|
+ _converse.connection.addHandler(
|
|
|
|
+ handler,
|
|
|
|
+ options.ns,
|
|
|
|
+ name,
|
|
|
|
+ options.type,
|
|
|
|
+ options.id,
|
|
|
|
+ options.from,
|
|
|
|
+ options
|
|
|
|
+ );
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ 'waitUntil': function (name) {
|
|
|
|
+ var promise = _converse.promises[name];
|
|
|
|
+ if (_.isUndefined(promise)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return _converse.promises[name].promise();
|
|
|
|
+ },
|
|
|
|
+ 'send': function (stanza) {
|
|
|
|
+ _converse.connection.send(stanza);
|
|
|
|
+ },
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // The public API
|
|
|
|
+ return {
|
|
|
|
+ 'initialize': function (settings, callback) {
|
|
|
|
+ return _converse.initialize(settings, callback);
|
|
|
|
+ },
|
|
|
|
+ 'plugins': {
|
|
|
|
+ 'add': function (name, plugin) {
|
|
|
|
+ plugin.__name__ = name;
|
|
|
|
+ if (!_.isUndefined(_converse.pluggable.plugins[name])) {
|
|
|
|
+ throw new TypeError(
|
|
|
|
+ 'Error: plugin with name "'+name+'" has already been '+
|
|
|
|
+ 'registered!');
|
|
|
|
+ } else {
|
|
|
|
+ _converse.pluggable.plugins[name] = plugin;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ 'env': {
|
|
|
|
+ '$build': $build,
|
|
|
|
+ '$iq': $iq,
|
|
|
|
+ '$msg': $msg,
|
|
|
|
+ '$pres': $pres,
|
|
|
|
+ 'Strophe': Strophe,
|
|
|
|
+ 'b64_sha1': b64_sha1,
|
|
|
|
+ '_': _,
|
|
|
|
+ 'jQuery': $,
|
|
|
|
+ 'moment': moment,
|
|
|
|
+ 'utils': utils
|
|
|
|
+ }
|
|
|
|
+ };
|
|
}));
|
|
}));
|