forked from ggozad/strophe.plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrophe.messaging.js
More file actions
80 lines (67 loc) · 2.98 KB
/
strophe.messaging.js
File metadata and controls
80 lines (67 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// XMPP plugins for Strophe v0.2
// (c) 2012 Yiorgis Gozadinos.
// strophe.plugins is distributed under the MIT license.
// http://github.com/ggozad/strophe.plugins
// Plugin to deal with basic instant messaging
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'underscore', 'backbone', 'strophe'], function ($, _, Backbone, Strophe) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return factory($, _, Backbone, Strophe);
});
} else {
// Browser globals
factory(root.$, root._, root.Backbone, root.Strophe);
}
}(this,function ($, _, Backbone, Strophe) {
Strophe.addConnectionPlugin('Messaging', {
_connection: null,
init: function (conn) {
this._connection = conn;
Strophe.addNamespace('XHTML_IM', 'http://jabber.org/protocol/xhtml-im');
Strophe.addNamespace('XHTML', 'http://www.w3.org/1999/xhtml');
_.extend(this, Backbone.Events);
},
// Register message notifications when connected
statusChanged: function (status, condition) {
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.ATTACHED) {
this._connection.addHandler(this._onReceiveChatMessage.bind(this), null, 'message', 'chat');
}
},
// Upon message receipt trigger an `xmpp:message` event.
_onReceiveChatMessage: function (message) {
var body, html_body;
body = $(message).children('body').text();
if (body === '') {
return true; // Typing notifications are not handled.
}
html_body = $('html[xmlns="' + Strophe.NS.XHTML_IM + '"] > body', message);
if (html_body.length > 0) {
html_body = $('<div>').append(html_body.contents()).html();
} else {
html_body = null;
}
this.trigger('xmpp:message', {jid: message.getAttribute('from'),
type: message.getAttribute('type'),
body: body,
html_body: html_body});
return true;
},
// **send** sends a message. `body` is the plaintext contents whereas `html_body` is the html version.
send: function (to, body, html_body) {
var msg = $msg({to: to, type: 'chat'});
if (body) {
msg.c('body', {}, body);
}
if (html_body) {
msg.c('html', {xmlns: Strophe.NS.XHTML_IM})
.c('body', {xmlns: Strophe.NS.XHTML})
.h(html_body);
}
this._connection.send(msg.tree());
}
});
}));