forked from strophe/strophejs-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strophe.si-filetransfer.js
159 lines (124 loc) · 3.57 KB
/
strophe.si-filetransfer.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
/*global Strophe $iq $ */
/*
(c) 2013 - Arlo Breault <arlolra@gmail.com>
Freely distributed under the MPL v2.0 license.
File: strophe.si-filetransfer.js
XEP-0096: SI File Transfer
http://xmpp.org/extensions/xep-0096.html
*/
;(function () {
"use strict";
function noop() {}
function inVals(stanza, ns) {
var ok = false;
var $mthds = $('si feature x field[var="stream-method"] value', stanza);
$mthds.each(function (i, m) {
if ($(m).text() === ns) ok = true;
});
return ok;
}
Strophe.addConnectionPlugin('si_filetransfer', {
_c: null,
_cb: null,
init: function (c) {
this._c = c;
Strophe.addNamespace('SI', 'http://jabber.org/protocol/si');
Strophe.addNamespace('SI_FILE_TRANSFER',
Strophe.NS.SI + '/profile/file-transfer');
Strophe.addNamespace('FEATURE_NEG',
'http://jabber.org/protocol/feature-neg');
c.addHandler(this._receive.bind(this), Strophe.NS.SI, 'iq', 'set');
},
_receive: function (m) {
var $m = $(m);
var from = $m.attr('from');
var id = $m.attr('id')
var sid = $('si', $m).attr('id');
var iq = $iq({
type: 'result',
to: from,
id: id
}).c('si', {
xmlns: Strophe.NS.SI,
id: sid
}).c('file', {
xmlns: Strophe.NS.SI_FILE_TRANSFER
}).up().c('feature', {
xmlns: Strophe.NS.FEATURE_NEG
}).c('x', {
xmlns: 'jabber:x:data',
type: 'submit'
}).c('field', {
'var': 'stream-method'
});
// check for In-Band Bytestream plugin
// and IBB accepted
if ( Object.hasOwnProperty.call(this._c, 'ibb') &&
inVals(m, Strophe.NS.IBB)
) iq.c('value').t(Strophe.NS.IBB);
this._send(iq, noop, noop);
var $file = $('file', $m);
var filename = $file.attr('name');
var size = $file.attr('size');
var mime = $('si', $m).attr('mime-type');
// callback message
if (typeof this._cb === 'function') {
this._cb(from, sid, filename, size, mime);
}
return true;
},
_success: function (cb, stanza) {
var err;
// search for ibb
if (!inVals(stanza, Strophe.NS.IBB))
err = new Error('In-Band Bytestream not supported');
cb(err);
},
_fail: function (cb, stanza) {
var err = 'timed out';
if (stanza) err = stanza;
cb(new Error(err));
},
_send: function (iq, success, fail) {
this._c.sendIQ(iq, success, fail, 60 * 1000);
},
send: function (to, sid, filename, size, mime, cb) {
// check for In-Band Bytestream plugin
if (!Object.hasOwnProperty.call(this._c, 'ibb')) {
Strophe.warn('The In-Band Bytestream plugin is required.');
return;
}
var iq = $iq({
type: 'set',
to: to,
id: this._c.getUniqueId('si-filetransfer')
}).c('si', {
xmlns: Strophe.NS.SI,
id: sid,
profile: Strophe.NS.SI_FILE_TRANSFER,
'mime-type': mime
}).c('file', {
xmlns: Strophe.NS.SI_FILE_TRANSFER,
name: filename,
size: size
}).up().c('feature', {
xmlns: Strophe.NS.FEATURE_NEG
}).c('x', {
xmlns: 'jabber:x:data',
type: 'form'
}).c('field', {
'var': 'stream-method',
type: 'list-single'
}).c('option')
.c('value')
.t(Strophe.NS.IBB);
this._send(iq,
this._success.bind(this, cb),
this._fail.bind(this, cb)
);
},
addFileHandler: function (fn) {
this._cb = fn;
}
});
}());