-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstrophe.ibb.js
166 lines (129 loc) · 3.21 KB
/
strophe.ibb.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*global Strophe $iq $ */
/*
(c) 2013 - Arlo Breault <[email protected]>
Freely distributed under the MPL v2.0 license.
File: strophe.ibb.js
XEP-0047: In-Band Bytestreams
http://xmpp.org/extensions/xep-0047.html
*/
;(function () {
"use strict";
function noop() {}
Strophe.addConnectionPlugin('ibb', {
_c: null,
_cb: null,
init: function (c) {
this._c = c;
Strophe.addNamespace('IBB', 'http://jabber.org/protocol/ibb');
c.addHandler(this._receive.bind(this), Strophe.NS.IBB, 'iq', 'set');
},
_createErr: function (to, id, type, name) {
var iq = $iq({
type: 'error',
to: to,
id: id
}).c('error', {
type: type
}).c(name, {
xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'
})
return iq;
},
_receive: function (m) {
var $m = $(m);
var from = $m.attr('from');
var id = $m.attr('id')
// support ibb?
// proceed?
// prefer smaller chunks?
var iq = $iq({
type: 'result',
to: from,
id: id
});
this._send(iq, noop, noop);
var child = $m.children().get(0);
var type = child.tagName.toLowerCase();
var sid = $(child).attr('sid');
var data, seq;
if (type === 'data') {
data = $(child).text();
seq = $(child).attr('seq');
}
// callback message
if (typeof this._cb === 'function') {
this._cb(type, from, sid, data, seq);
}
return true; // keep handler active
},
_success: function (cb) {
cb(null);
},
_fail: function (cb, stanza) {
var err = 'timed out';
if (stanza) {
err = $('error', stanza)
.children()
.get(0)
.tagName
.toLowerCase();
}
cb(new Error(err));
},
_send: function (iq, success, fail) {
this._c.sendIQ(iq, success, fail, 60 * 1000);
},
open: function (to, sid, bs, cb) {
if (parseInt(bs ? bs : 0, 10) > 65535)
return cb(new Error('Block-size too large.'))
// construct iq
var iq = $iq({
type: 'set',
to: to,
id: this._c.getUniqueId('ibb')
}).c('open', {
xmlns: Strophe.NS.IBB,
stanza: 'iq',
sid: sid,
'block-size': bs || '4096'
});
this._send(iq,
this._success.bind(this, cb),
this._fail.bind(this, cb)
);
},
data: function (to, sid, seq, data, cb) {
var iq = $iq({
type: 'set',
to: to,
id: this._c.getUniqueId('ibb')
}).c('data', {
xmlns: Strophe.NS.IBB,
seq: seq.toString(),
sid: sid
}).t(data);
this._send(iq,
this._success.bind(this, cb),
this._fail.bind(this, cb)
);
},
close: function (to, sid, cb) {
// construct iq
var iq = $iq({
type: 'set',
to: to,
id: this._c.getUniqueId('ibb')
}).c('close', {
xmlns: Strophe.NS.IBB,
sid: sid
});
this._send(iq,
this._success.bind(this, cb),
this._fail.bind(this, cb)
);
},
addIBBHandler: function (fn) {
this._cb = fn;
}
});
}());