2016-02-20 16:06:12 +01:00
|
|
|
// 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) {
|
2016-06-09 23:22:34 +02:00
|
|
|
return converse.initialize(settings, callback);
|
2016-02-20 16:06:12 +01:00
|
|
|
},
|
2016-05-31 09:20:36 +02:00
|
|
|
'log': converse.log,
|
2016-03-29 15:41:10 +02:00
|
|
|
'connection': {
|
2016-03-29 17:41:06 +02:00
|
|
|
'connected': function () {
|
|
|
|
return converse.connection && converse.connection.connected || false;
|
|
|
|
},
|
2016-03-29 15:41:10 +02:00
|
|
|
'disconnect': function () {
|
|
|
|
converse.connection.disconnect();
|
|
|
|
},
|
2016-02-20 16:06:12 +01:00
|
|
|
},
|
|
|
|
'user': {
|
2016-05-10 10:06:54 +02:00
|
|
|
'jid': function () {
|
|
|
|
return converse.connection.jid;
|
|
|
|
},
|
2016-04-13 13:52:28 +02:00
|
|
|
'login': function (credentials) {
|
2016-04-13 17:11:04 +02:00
|
|
|
converse.initConnection();
|
2016-04-13 13:52:28 +02:00
|
|
|
converse.logIn(credentials);
|
|
|
|
},
|
2016-02-20 16:06:12 +01:00
|
|
|
'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;
|
|
|
|
}
|
2016-03-17 06:38:52 +01:00
|
|
|
converse.xmppstatus.sendPresence(value);
|
2016-02-20 16:06:12 +01:00
|
|
|
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") {
|
2016-11-22 17:42:58 +01:00
|
|
|
chatbox = converse.wrappedChatBox(
|
|
|
|
converse.chatboxes.getChatBox(jids, true).trigger('show')
|
|
|
|
);
|
2016-02-20 16:06:12 +01:00
|
|
|
return chatbox;
|
|
|
|
}
|
|
|
|
return _.map(jids, function (jid) {
|
2016-11-22 17:42:58 +01:00
|
|
|
chatbox = converse.wrappedChatBox(
|
|
|
|
converse.chatboxes.getChatBox(jid, true).trigger('show')
|
|
|
|
);
|
2016-02-20 16:06:12 +01:00
|
|
|
return chatbox;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'get': function (jids) {
|
|
|
|
if (typeof jids === "undefined") {
|
2016-06-24 10:54:39 +02:00
|
|
|
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;
|
2016-02-20 16:06:12 +01:00
|
|
|
} else if (typeof jids === "string") {
|
2016-08-12 22:38:39 +02:00
|
|
|
return converse.wrappedChatBox(converse.chatboxes.getChatBox(jids));
|
2016-02-20 16:06:12 +01:00
|
|
|
}
|
2016-06-09 11:01:54 +02:00
|
|
|
return _.map(jids,
|
|
|
|
_.partial(
|
|
|
|
_.compose(
|
|
|
|
converse.wrappedChatBox.bind(converse), converse.chatboxes.getChatBox.bind(converse.chatboxes)
|
|
|
|
), _, true
|
|
|
|
)
|
|
|
|
);
|
2016-02-20 16:06:12 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'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': {
|
2016-09-21 13:12:42 +02:00
|
|
|
'once': function (evt, handler, context) {
|
|
|
|
converse.once(evt, handler, context);
|
2016-02-20 16:06:12 +01:00
|
|
|
},
|
2016-09-21 13:12:42 +02:00
|
|
|
'on': function (evt, handler, context) {
|
|
|
|
converse.on(evt, handler, context);
|
2016-02-20 16:06:12 +01:00
|
|
|
},
|
|
|
|
'not': function (evt, handler) {
|
|
|
|
converse.off(evt, handler);
|
|
|
|
},
|
2016-03-16 19:11:18 +01:00
|
|
|
'stanza': function (name, options, handler) {
|
2016-03-16 19:31:55 +01:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
handler = options;
|
|
|
|
options = {};
|
|
|
|
} else {
|
|
|
|
options = options || {};
|
|
|
|
}
|
2016-03-16 19:11:18 +01:00
|
|
|
converse.connection.addHandler(
|
|
|
|
handler,
|
|
|
|
options.ns,
|
2016-03-29 11:48:49 +02:00
|
|
|
name,
|
2016-03-16 19:11:18 +01:00
|
|
|
options.type,
|
|
|
|
options.id,
|
|
|
|
options.from,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
},
|
2016-02-20 16:06:12 +01:00
|
|
|
},
|
|
|
|
'send': function (stanza) {
|
|
|
|
converse.connection.send(stanza);
|
|
|
|
},
|
|
|
|
'plugins': {
|
|
|
|
'add': function (name, plugin) {
|
2016-06-09 11:01:54 +02:00
|
|
|
plugin.__name__ = name;
|
|
|
|
converse.pluggable.plugins[name] = plugin;
|
2016-02-20 16:06:12 +01:00
|
|
|
},
|
|
|
|
'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
|
2016-08-31 12:06:17 +02:00
|
|
|
* available via the __super__ attribute.
|
2016-02-20 16:06:12 +01:00
|
|
|
*
|
|
|
|
* 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
|
2016-08-31 12:06:17 +02:00
|
|
|
* on the __super__ attribute of the object being overridden.
|
2016-02-20 16:06:12 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}));
|