xmppify 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/app/assets/javascripts/xmpp/index.js +0 -0
  8. data/app/assets/javascripts/xmpp/scripts/checkplayer.js +12 -0
  9. data/app/assets/javascripts/xmpp/scripts/crossdomain.xml +7 -0
  10. data/app/assets/javascripts/xmpp/scripts/flXHR.js +2 -0
  11. data/app/assets/javascripts/xmpp/scripts/flXHR.swf +0 -0
  12. data/app/assets/javascripts/xmpp/scripts/flXHR.vbs +44 -0
  13. data/app/assets/javascripts/xmpp/scripts/flensed.js +12 -0
  14. data/app/assets/javascripts/xmpp/scripts/strophe.flxhr.js +31 -0
  15. data/app/assets/javascripts/xmpp/scripts/strophe.js +3538 -0
  16. data/app/assets/javascripts/xmpp/scripts/swfobject.js +5 -0
  17. data/app/assets/javascripts/xmpp/scripts/updateplayer.swf +0 -0
  18. data/app/assets/javascripts/xmpp/scripts/xmpp.js.coffee +386 -0
  19. data/app/assets/javascripts/xmpp/strophejs-plugins/LICENSE.txt +19 -0
  20. data/app/assets/javascripts/xmpp/strophejs-plugins/README.markdown +95 -0
  21. data/app/assets/javascripts/xmpp/strophejs-plugins/archive/README.markdown +14 -0
  22. data/app/assets/javascripts/xmpp/strophejs-plugins/archive/iso8601_support.js +32 -0
  23. data/app/assets/javascripts/xmpp/strophejs-plugins/archive/strophe.archive.js +88 -0
  24. data/app/assets/javascripts/xmpp/strophejs-plugins/caps/README.markdown +53 -0
  25. data/app/assets/javascripts/xmpp/strophejs-plugins/caps/sha1.js +202 -0
  26. data/app/assets/javascripts/xmpp/strophejs-plugins/caps/strophe.CAPS.coffee +153 -0
  27. data/app/assets/javascripts/xmpp/strophejs-plugins/caps/strophe.CAPS.js +88 -0
  28. data/app/assets/javascripts/xmpp/strophejs-plugins/caps/strophe.caps.jsonly.js +240 -0
  29. data/app/assets/javascripts/xmpp/strophejs-plugins/chatstates/strophe.chatstates.js +80 -0
  30. data/app/assets/javascripts/xmpp/strophejs-plugins/cmds/README.markdown +47 -0
  31. data/app/assets/javascripts/xmpp/strophejs-plugins/cmds/spec/GetUrlComamndSpec.js +66 -0
  32. data/app/assets/javascripts/xmpp/strophejs-plugins/cmds/spec/strophe.cmds.spec.coffee +145 -0
  33. data/app/assets/javascripts/xmpp/strophejs-plugins/cmds/spec/support/jasmine.json +16 -0
  34. data/app/assets/javascripts/xmpp/strophejs-plugins/cmds/src/strophe.cmds.coffee +265 -0
  35. data/app/assets/javascripts/xmpp/strophejs-plugins/cmds/src/strophe.cmds.js +403 -0
  36. data/app/assets/javascripts/xmpp/strophejs-plugins/dataforms/README.markdown +82 -0
  37. data/app/assets/javascripts/xmpp/strophejs-plugins/dataforms/spec/strophe.x.coffee +715 -0
  38. data/app/assets/javascripts/xmpp/strophejs-plugins/dataforms/src/strophe.x.coffee +443 -0
  39. data/app/assets/javascripts/xmpp/strophejs-plugins/dataforms/src/strophe.x.js +854 -0
  40. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/.gitignore +1 -0
  41. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/README.markdown +0 -0
  42. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/convert.js +13 -0
  43. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/jasmine.json +11 -0
  44. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/lib/strophe.disco.js +137 -0
  45. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/public/javascript/strophe.disco.js +137 -0
  46. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/01_querying_information.xml +6 -0
  47. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/02_result_set_for_information_request.xml +22 -0
  48. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/03_target_entity_does_not_exist.xml +10 -0
  49. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/04_service_unavailable.xml +11 -0
  50. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/09_querying_specific_jid_and_node_combination.xml +7 -0
  51. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/10_jid_and_node_result.xml +12 -0
  52. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/11_requesting_all_items.xml +6 -0
  53. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/12_result_set_for_all_items.xml +23 -0
  54. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/13_empty_result_set.xml +7 -0
  55. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/17_service_returns_nodes.xml +16 -0
  56. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/18_requesting_further_nodes.xml +7 -0
  57. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/24_jid_and_node_error.xml +11 -0
  58. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/DiscoNodeSpec.js +50 -0
  59. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/SpecHelper.js +51 -0
  60. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/Stanzas.js +0 -0
  61. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/strophe.disco.spec.helper.js +54 -0
  62. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/strophe.disco.spec.js +0 -0
  63. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/spec/support/jasmine.json +13 -0
  64. data/app/assets/javascripts/xmpp/strophejs-plugins/disco/strophe.disco.js +232 -0
  65. data/app/assets/javascripts/xmpp/strophejs-plugins/epic/public/javascript/jquery.epic.js +61 -0
  66. data/app/assets/javascripts/xmpp/strophejs-plugins/epic/spec/SpecHelper.js +9 -0
  67. data/app/assets/javascripts/xmpp/strophejs-plugins/epic/spec/jquery.epic.js +39 -0
  68. data/app/assets/javascripts/xmpp/strophejs-plugins/epic/spec/support/jasmine.json +22 -0
  69. data/app/assets/javascripts/xmpp/strophejs-plugins/iexdomain/iexdomain.js +36 -0
  70. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/README.markdown +117 -0
  71. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/buster.js +23 -0
  72. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/jid.coffee +64 -0
  73. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/jid.js +92 -0
  74. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/spec/browserSetup.coffee +1 -0
  75. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/spec/jid.spec.coffee +96 -0
  76. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/spec/strophe.joap.spec.coffee +461 -0
  77. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/strophe.joap.coffee +304 -0
  78. data/app/assets/javascripts/xmpp/strophejs-plugins/joap/strophe.joap.js +522 -0
  79. data/app/assets/javascripts/xmpp/strophejs-plugins/mam/LICENSE.txt +19 -0
  80. data/app/assets/javascripts/xmpp/strophejs-plugins/mam/README.markdown +52 -0
  81. data/app/assets/javascripts/xmpp/strophejs-plugins/mam/strophe.mam.js +62 -0
  82. data/app/assets/javascripts/xmpp/strophejs-plugins/muc/LICENSE.txt +20 -0
  83. data/app/assets/javascripts/xmpp/strophejs-plugins/muc/strophe.muc.js +1003 -0
  84. data/app/assets/javascripts/xmpp/strophejs-plugins/ping/README.markdown +28 -0
  85. data/app/assets/javascripts/xmpp/strophejs-plugins/ping/strophe.ping.js +68 -0
  86. data/app/assets/javascripts/xmpp/strophejs-plugins/private/README.markdown +16 -0
  87. data/app/assets/javascripts/xmpp/strophejs-plugins/private/strophe.private.js +192 -0
  88. data/app/assets/javascripts/xmpp/strophejs-plugins/pubsub/strophe.pubsub.js +555 -0
  89. data/app/assets/javascripts/xmpp/strophejs-plugins/receipts/strophe.receipts.js +155 -0
  90. data/app/assets/javascripts/xmpp/strophejs-plugins/register/README.markdown +44 -0
  91. data/app/assets/javascripts/xmpp/strophejs-plugins/register/strophe.register.js +431 -0
  92. data/app/assets/javascripts/xmpp/strophejs-plugins/roster/public/javascript/strophe.roster.js +40 -0
  93. data/app/assets/javascripts/xmpp/strophejs-plugins/roster/spec/strophe.roster.spec.js +65 -0
  94. data/app/assets/javascripts/xmpp/strophejs-plugins/roster/spec/support/jasmine.json +15 -0
  95. data/app/assets/javascripts/xmpp/strophejs-plugins/roster/strophe.roster.js +438 -0
  96. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/.gitignore +6 -0
  97. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/README.md +113 -0
  98. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/buster.js +11 -0
  99. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/lib/strophe.js +4198 -0
  100. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/spec/strophe.rpc.spec.helper.js +38 -0
  101. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/spec/strophe.rpc.spec.js +262 -0
  102. data/app/assets/javascripts/xmpp/strophejs-plugins/rpc/strophe.rpc.js +464 -0
  103. data/app/assets/javascripts/xmpp/strophejs-plugins/rsm/strophe.rsm.js +52 -0
  104. data/app/assets/javascripts/xmpp/strophejs-plugins/serverdate/strophe.serverdate.js +58 -0
  105. data/app/assets/javascripts/xmpp/strophejs-plugins/vcard/examples/index.html +17 -0
  106. data/app/assets/javascripts/xmpp/strophejs-plugins/vcard/examples/strophevcard.js +0 -0
  107. data/app/assets/javascripts/xmpp/strophejs-plugins/vcard/strophe.vcard.coffee +44 -0
  108. data/app/assets/javascripts/xmpp/strophejs-plugins/vcard/strophe.vcard.js +66 -0
  109. data/app/assets/stylesheets/xmpp/index.css +0 -0
  110. data/app/controllers/omniauth_callbacks_controller.rb +15 -0
  111. data/app/controllers/ruby_bosh.rb +221 -0
  112. data/app/models/identity.rb +3 -0
  113. data/app/models/user.rb +40 -0
  114. data/bin/rails +8 -0
  115. data/db/migrate/20140329065635_create_users.rb +10 -0
  116. data/db/migrate/20140329070238_create_identities.rb +11 -0
  117. data/lib/xmppify/version.rb +3 -0
  118. data/lib/xmppify.rb +6 -0
  119. data/xmppify.gemspec +23 -0
  120. metadata +197 -0
@@ -0,0 +1,4198 @@
1
+ // This code was written by Tyler Akins and has been placed in the
2
+ // public domain. It would be nice if you left this header intact.
3
+ // Base64 code from Tyler Akins -- http://rumkin.com
4
+
5
+ var Base64 = (function () {
6
+ var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
7
+
8
+ var obj = {
9
+ /**
10
+ * Encodes a string in base64
11
+ * @param {String} input The string to encode in base64.
12
+ */
13
+ encode: function (input) {
14
+ var output = "";
15
+ var chr1, chr2, chr3;
16
+ var enc1, enc2, enc3, enc4;
17
+ var i = 0;
18
+
19
+ do {
20
+ chr1 = input.charCodeAt(i++);
21
+ chr2 = input.charCodeAt(i++);
22
+ chr3 = input.charCodeAt(i++);
23
+
24
+ enc1 = chr1 >> 2;
25
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
26
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
27
+ enc4 = chr3 & 63;
28
+
29
+ if (isNaN(chr2)) {
30
+ enc3 = enc4 = 64;
31
+ } else if (isNaN(chr3)) {
32
+ enc4 = 64;
33
+ }
34
+
35
+ output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
36
+ keyStr.charAt(enc3) + keyStr.charAt(enc4);
37
+ } while (i < input.length);
38
+
39
+ return output;
40
+ },
41
+
42
+ /**
43
+ * Decodes a base64 string.
44
+ * @param {String} input The string to decode.
45
+ */
46
+ decode: function (input) {
47
+ var output = "";
48
+ var chr1, chr2, chr3;
49
+ var enc1, enc2, enc3, enc4;
50
+ var i = 0;
51
+
52
+ // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
53
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
54
+
55
+ do {
56
+ enc1 = keyStr.indexOf(input.charAt(i++));
57
+ enc2 = keyStr.indexOf(input.charAt(i++));
58
+ enc3 = keyStr.indexOf(input.charAt(i++));
59
+ enc4 = keyStr.indexOf(input.charAt(i++));
60
+
61
+ chr1 = (enc1 << 2) | (enc2 >> 4);
62
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
63
+ chr3 = ((enc3 & 3) << 6) | enc4;
64
+
65
+ output = output + String.fromCharCode(chr1);
66
+
67
+ if (enc3 != 64) {
68
+ output = output + String.fromCharCode(chr2);
69
+ }
70
+ if (enc4 != 64) {
71
+ output = output + String.fromCharCode(chr3);
72
+ }
73
+ } while (i < input.length);
74
+
75
+ return output;
76
+ }
77
+ };
78
+
79
+ return obj;
80
+ })();
81
+ /*
82
+ * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
83
+ * in FIPS PUB 180-1
84
+ * Version 2.1a Copyright Paul Johnston 2000 - 2002.
85
+ * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
86
+ * Distributed under the BSD License
87
+ * See http://pajhome.org.uk/crypt/md5 for details.
88
+ */
89
+
90
+ /*
91
+ * Configurable variables. You may need to tweak these to be compatible with
92
+ * the server-side, but the defaults work in most cases.
93
+ */
94
+ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
95
+ var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
96
+ var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
97
+
98
+ /*
99
+ * These are the functions you'll usually want to call
100
+ * They take string arguments and return either hex or base-64 encoded strings
101
+ */
102
+ function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
103
+ function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
104
+ function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
105
+ function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
106
+ function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
107
+ function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
108
+
109
+ /*
110
+ * Perform a simple self-test to see if the VM is working
111
+ */
112
+ function sha1_vm_test()
113
+ {
114
+ return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
115
+ }
116
+
117
+ /*
118
+ * Calculate the SHA-1 of an array of big-endian words, and a bit length
119
+ */
120
+ function core_sha1(x, len)
121
+ {
122
+ /* append padding */
123
+ x[len >> 5] |= 0x80 << (24 - len % 32);
124
+ x[((len + 64 >> 9) << 4) + 15] = len;
125
+
126
+ var w = new Array(80);
127
+ var a = 1732584193;
128
+ var b = -271733879;
129
+ var c = -1732584194;
130
+ var d = 271733878;
131
+ var e = -1009589776;
132
+
133
+ var i, j, t, olda, oldb, oldc, oldd, olde;
134
+ for (i = 0; i < x.length; i += 16)
135
+ {
136
+ olda = a;
137
+ oldb = b;
138
+ oldc = c;
139
+ oldd = d;
140
+ olde = e;
141
+
142
+ for (j = 0; j < 80; j++)
143
+ {
144
+ if (j < 16) { w[j] = x[i + j]; }
145
+ else { w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); }
146
+ t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
147
+ safe_add(safe_add(e, w[j]), sha1_kt(j)));
148
+ e = d;
149
+ d = c;
150
+ c = rol(b, 30);
151
+ b = a;
152
+ a = t;
153
+ }
154
+
155
+ a = safe_add(a, olda);
156
+ b = safe_add(b, oldb);
157
+ c = safe_add(c, oldc);
158
+ d = safe_add(d, oldd);
159
+ e = safe_add(e, olde);
160
+ }
161
+ return [a, b, c, d, e];
162
+ }
163
+
164
+ /*
165
+ * Perform the appropriate triplet combination function for the current
166
+ * iteration
167
+ */
168
+ function sha1_ft(t, b, c, d)
169
+ {
170
+ if (t < 20) { return (b & c) | ((~b) & d); }
171
+ if (t < 40) { return b ^ c ^ d; }
172
+ if (t < 60) { return (b & c) | (b & d) | (c & d); }
173
+ return b ^ c ^ d;
174
+ }
175
+
176
+ /*
177
+ * Determine the appropriate additive constant for the current iteration
178
+ */
179
+ function sha1_kt(t)
180
+ {
181
+ return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
182
+ (t < 60) ? -1894007588 : -899497514;
183
+ }
184
+
185
+ /*
186
+ * Calculate the HMAC-SHA1 of a key and some data
187
+ */
188
+ function core_hmac_sha1(key, data)
189
+ {
190
+ var bkey = str2binb(key);
191
+ if (bkey.length > 16) { bkey = core_sha1(bkey, key.length * chrsz); }
192
+
193
+ var ipad = new Array(16), opad = new Array(16);
194
+ for (var i = 0; i < 16; i++)
195
+ {
196
+ ipad[i] = bkey[i] ^ 0x36363636;
197
+ opad[i] = bkey[i] ^ 0x5C5C5C5C;
198
+ }
199
+
200
+ var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
201
+ return core_sha1(opad.concat(hash), 512 + 160);
202
+ }
203
+
204
+ /*
205
+ * Add integers, wrapping at 2^32. This uses 16-bit operations internally
206
+ * to work around bugs in some JS interpreters.
207
+ */
208
+ function safe_add(x, y)
209
+ {
210
+ var lsw = (x & 0xFFFF) + (y & 0xFFFF);
211
+ var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
212
+ return (msw << 16) | (lsw & 0xFFFF);
213
+ }
214
+
215
+ /*
216
+ * Bitwise rotate a 32-bit number to the left.
217
+ */
218
+ function rol(num, cnt)
219
+ {
220
+ return (num << cnt) | (num >>> (32 - cnt));
221
+ }
222
+
223
+ /*
224
+ * Convert an 8-bit or 16-bit string to an array of big-endian words
225
+ * In 8-bit function, characters >255 have their hi-byte silently ignored.
226
+ */
227
+ function str2binb(str)
228
+ {
229
+ var bin = [];
230
+ var mask = (1 << chrsz) - 1;
231
+ for (var i = 0; i < str.length * chrsz; i += chrsz)
232
+ {
233
+ bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
234
+ }
235
+ return bin;
236
+ }
237
+
238
+ /*
239
+ * Convert an array of big-endian words to a string
240
+ */
241
+ function binb2str(bin)
242
+ {
243
+ var str = "";
244
+ var mask = (1 << chrsz) - 1;
245
+ for (var i = 0; i < bin.length * 32; i += chrsz)
246
+ {
247
+ str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
248
+ }
249
+ return str;
250
+ }
251
+
252
+ /*
253
+ * Convert an array of big-endian words to a hex string.
254
+ */
255
+ function binb2hex(binarray)
256
+ {
257
+ var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
258
+ var str = "";
259
+ for (var i = 0; i < binarray.length * 4; i++)
260
+ {
261
+ str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
262
+ hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
263
+ }
264
+ return str;
265
+ }
266
+
267
+ /*
268
+ * Convert an array of big-endian words to a base-64 string
269
+ */
270
+ function binb2b64(binarray)
271
+ {
272
+ var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
273
+ var str = "";
274
+ var triplet, j;
275
+ for (var i = 0; i < binarray.length * 4; i += 3)
276
+ {
277
+ triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) |
278
+ (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) |
279
+ ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
280
+ for (j = 0; j < 4; j++)
281
+ {
282
+ if (i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
283
+ else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
284
+ }
285
+ }
286
+ return str;
287
+ }
288
+ /*
289
+ * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
290
+ * Digest Algorithm, as defined in RFC 1321.
291
+ * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
292
+ * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
293
+ * Distributed under the BSD License
294
+ * See http://pajhome.org.uk/crypt/md5 for more info.
295
+ */
296
+
297
+ var MD5 = (function () {
298
+ /*
299
+ * Configurable variables. You may need to tweak these to be compatible with
300
+ * the server-side, but the defaults work in most cases.
301
+ */
302
+ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
303
+ var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
304
+ var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
305
+
306
+ /*
307
+ * Add integers, wrapping at 2^32. This uses 16-bit operations internally
308
+ * to work around bugs in some JS interpreters.
309
+ */
310
+ var safe_add = function (x, y) {
311
+ var lsw = (x & 0xFFFF) + (y & 0xFFFF);
312
+ var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
313
+ return (msw << 16) | (lsw & 0xFFFF);
314
+ };
315
+
316
+ /*
317
+ * Bitwise rotate a 32-bit number to the left.
318
+ */
319
+ var bit_rol = function (num, cnt) {
320
+ return (num << cnt) | (num >>> (32 - cnt));
321
+ };
322
+
323
+ /*
324
+ * Convert a string to an array of little-endian words
325
+ * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
326
+ */
327
+ var str2binl = function (str) {
328
+ var bin = [];
329
+ var mask = (1 << chrsz) - 1;
330
+ for(var i = 0; i < str.length * chrsz; i += chrsz)
331
+ {
332
+ bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
333
+ }
334
+ return bin;
335
+ };
336
+
337
+ /*
338
+ * Convert an array of little-endian words to a string
339
+ */
340
+ var binl2str = function (bin) {
341
+ var str = "";
342
+ var mask = (1 << chrsz) - 1;
343
+ for(var i = 0; i < bin.length * 32; i += chrsz)
344
+ {
345
+ str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
346
+ }
347
+ return str;
348
+ };
349
+
350
+ /*
351
+ * Convert an array of little-endian words to a hex string.
352
+ */
353
+ var binl2hex = function (binarray) {
354
+ var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
355
+ var str = "";
356
+ for(var i = 0; i < binarray.length * 4; i++)
357
+ {
358
+ str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
359
+ hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
360
+ }
361
+ return str;
362
+ };
363
+
364
+ /*
365
+ * Convert an array of little-endian words to a base-64 string
366
+ */
367
+ var binl2b64 = function (binarray) {
368
+ var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
369
+ var str = "";
370
+ var triplet, j;
371
+ for(var i = 0; i < binarray.length * 4; i += 3)
372
+ {
373
+ triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) |
374
+ (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) |
375
+ ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
376
+ for(j = 0; j < 4; j++)
377
+ {
378
+ if(i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
379
+ else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
380
+ }
381
+ }
382
+ return str;
383
+ };
384
+
385
+ /*
386
+ * These functions implement the four basic operations the algorithm uses.
387
+ */
388
+ var md5_cmn = function (q, a, b, x, s, t) {
389
+ return safe_add(bit_rol(safe_add(safe_add(a, q),safe_add(x, t)), s),b);
390
+ };
391
+
392
+ var md5_ff = function (a, b, c, d, x, s, t) {
393
+ return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
394
+ };
395
+
396
+ var md5_gg = function (a, b, c, d, x, s, t) {
397
+ return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
398
+ };
399
+
400
+ var md5_hh = function (a, b, c, d, x, s, t) {
401
+ return md5_cmn(b ^ c ^ d, a, b, x, s, t);
402
+ };
403
+
404
+ var md5_ii = function (a, b, c, d, x, s, t) {
405
+ return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
406
+ };
407
+
408
+ /*
409
+ * Calculate the MD5 of an array of little-endian words, and a bit length
410
+ */
411
+ var core_md5 = function (x, len) {
412
+ /* append padding */
413
+ x[len >> 5] |= 0x80 << ((len) % 32);
414
+ x[(((len + 64) >>> 9) << 4) + 14] = len;
415
+
416
+ var a = 1732584193;
417
+ var b = -271733879;
418
+ var c = -1732584194;
419
+ var d = 271733878;
420
+
421
+ var olda, oldb, oldc, oldd;
422
+ for (var i = 0; i < x.length; i += 16)
423
+ {
424
+ olda = a;
425
+ oldb = b;
426
+ oldc = c;
427
+ oldd = d;
428
+
429
+ a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
430
+ d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
431
+ c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
432
+ b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
433
+ a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
434
+ d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
435
+ c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
436
+ b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
437
+ a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
438
+ d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
439
+ c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
440
+ b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
441
+ a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
442
+ d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
443
+ c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
444
+ b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
445
+
446
+ a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
447
+ d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
448
+ c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
449
+ b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
450
+ a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
451
+ d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
452
+ c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
453
+ b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
454
+ a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
455
+ d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
456
+ c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
457
+ b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
458
+ a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
459
+ d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
460
+ c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
461
+ b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
462
+
463
+ a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
464
+ d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
465
+ c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
466
+ b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
467
+ a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
468
+ d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
469
+ c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
470
+ b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
471
+ a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
472
+ d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
473
+ c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
474
+ b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
475
+ a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
476
+ d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
477
+ c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
478
+ b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
479
+
480
+ a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
481
+ d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
482
+ c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
483
+ b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
484
+ a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
485
+ d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
486
+ c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
487
+ b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
488
+ a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
489
+ d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
490
+ c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
491
+ b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
492
+ a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
493
+ d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
494
+ c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
495
+ b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
496
+
497
+ a = safe_add(a, olda);
498
+ b = safe_add(b, oldb);
499
+ c = safe_add(c, oldc);
500
+ d = safe_add(d, oldd);
501
+ }
502
+ return [a, b, c, d];
503
+ };
504
+
505
+
506
+ /*
507
+ * Calculate the HMAC-MD5, of a key and some data
508
+ */
509
+ var core_hmac_md5 = function (key, data) {
510
+ var bkey = str2binl(key);
511
+ if(bkey.length > 16) { bkey = core_md5(bkey, key.length * chrsz); }
512
+
513
+ var ipad = new Array(16), opad = new Array(16);
514
+ for(var i = 0; i < 16; i++)
515
+ {
516
+ ipad[i] = bkey[i] ^ 0x36363636;
517
+ opad[i] = bkey[i] ^ 0x5C5C5C5C;
518
+ }
519
+
520
+ var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
521
+ return core_md5(opad.concat(hash), 512 + 128);
522
+ };
523
+
524
+ var obj = {
525
+ /*
526
+ * These are the functions you'll usually want to call.
527
+ * They take string arguments and return either hex or base-64 encoded
528
+ * strings.
529
+ */
530
+ hexdigest: function (s) {
531
+ return binl2hex(core_md5(str2binl(s), s.length * chrsz));
532
+ },
533
+
534
+ b64digest: function (s) {
535
+ return binl2b64(core_md5(str2binl(s), s.length * chrsz));
536
+ },
537
+
538
+ hash: function (s) {
539
+ return binl2str(core_md5(str2binl(s), s.length * chrsz));
540
+ },
541
+
542
+ hmac_hexdigest: function (key, data) {
543
+ return binl2hex(core_hmac_md5(key, data));
544
+ },
545
+
546
+ hmac_b64digest: function (key, data) {
547
+ return binl2b64(core_hmac_md5(key, data));
548
+ },
549
+
550
+ hmac_hash: function (key, data) {
551
+ return binl2str(core_hmac_md5(key, data));
552
+ },
553
+
554
+ /*
555
+ * Perform a simple self-test to see if the VM is working
556
+ */
557
+ test: function () {
558
+ return MD5.hexdigest("abc") === "900150983cd24fb0d6963f7d28e17f72";
559
+ }
560
+ };
561
+
562
+ return obj;
563
+ })();
564
+ /*
565
+ This program is distributed under the terms of the MIT license.
566
+ Please see the LICENSE file for details.
567
+
568
+ Copyright 2006-2008, OGG, LLC
569
+ */
570
+
571
+ /* jslint configuration: */
572
+ /*global document, window, setTimeout, clearTimeout, console,
573
+ XMLHttpRequest, ActiveXObject,
574
+ Base64, MD5,
575
+ Strophe, $build, $msg, $iq, $pres */
576
+
577
+ /** File: strophe.js
578
+ * A JavaScript library for XMPP BOSH.
579
+ *
580
+ * This is the JavaScript version of the Strophe library. Since JavaScript
581
+ * has no facilities for persistent TCP connections, this library uses
582
+ * Bidirectional-streams Over Synchronous HTTP (BOSH) to emulate
583
+ * a persistent, stateful, two-way connection to an XMPP server. More
584
+ * information on BOSH can be found in XEP 124.
585
+ */
586
+
587
+ /** PrivateFunction: Function.prototype.bind
588
+ * Bind a function to an instance.
589
+ *
590
+ * This Function object extension method creates a bound method similar
591
+ * to those in Python. This means that the 'this' object will point
592
+ * to the instance you want. See
593
+ * <a href='https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind'>MDC's bind() documentation</a> and
594
+ * <a href='http://benjamin.smedbergs.us/blog/2007-01-03/bound-functions-and-function-imports-in-javascript/'>Bound Functions and Function Imports in JavaScript</a>
595
+ * for a complete explanation.
596
+ *
597
+ * This extension already exists in some browsers (namely, Firefox 3), but
598
+ * we provide it to support those that don't.
599
+ *
600
+ * Parameters:
601
+ * (Object) obj - The object that will become 'this' in the bound function.
602
+ * (Object) argN - An option argument that will be prepended to the
603
+ * arguments given for the function call
604
+ *
605
+ * Returns:
606
+ * The bound function.
607
+ */
608
+ if (!Function.prototype.bind) {
609
+ Function.prototype.bind = function (obj /*, arg1, arg2, ... */)
610
+ {
611
+ var func = this;
612
+ var _slice = Array.prototype.slice;
613
+ var _concat = Array.prototype.concat;
614
+ var _args = _slice.call(arguments, 1);
615
+
616
+ return function () {
617
+ return func.apply(obj ? obj : this,
618
+ _concat.call(_args,
619
+ _slice.call(arguments, 0)));
620
+ };
621
+ };
622
+ }
623
+
624
+ /** PrivateFunction: Array.prototype.indexOf
625
+ * Return the index of an object in an array.
626
+ *
627
+ * This function is not supplied by some JavaScript implementations, so
628
+ * we provide it if it is missing. This code is from:
629
+ * http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
630
+ *
631
+ * Parameters:
632
+ * (Object) elt - The object to look for.
633
+ * (Integer) from - The index from which to start looking. (optional).
634
+ *
635
+ * Returns:
636
+ * The index of elt in the array or -1 if not found.
637
+ */
638
+ if (!Array.prototype.indexOf)
639
+ {
640
+ Array.prototype.indexOf = function(elt /*, from*/)
641
+ {
642
+ var len = this.length;
643
+
644
+ var from = Number(arguments[1]) || 0;
645
+ from = (from < 0) ? Math.ceil(from) : Math.floor(from);
646
+ if (from < 0) {
647
+ from += len;
648
+ }
649
+
650
+ for (; from < len; from++) {
651
+ if (from in this && this[from] === elt) {
652
+ return from;
653
+ }
654
+ }
655
+
656
+ return -1;
657
+ };
658
+ }
659
+
660
+ /* All of the Strophe globals are defined in this special function below so
661
+ * that references to the globals become closures. This will ensure that
662
+ * on page reload, these references will still be available to callbacks
663
+ * that are still executing.
664
+ */
665
+
666
+ (function (callback) {
667
+ var Strophe;
668
+
669
+ /** Function: $build
670
+ * Create a Strophe.Builder.
671
+ * This is an alias for 'new Strophe.Builder(name, attrs)'.
672
+ *
673
+ * Parameters:
674
+ * (String) name - The root element name.
675
+ * (Object) attrs - The attributes for the root element in object notation.
676
+ *
677
+ * Returns:
678
+ * A new Strophe.Builder object.
679
+ */
680
+ function $build(name, attrs) { return new Strophe.Builder(name, attrs); }
681
+ /** Function: $msg
682
+ * Create a Strophe.Builder with a <message/> element as the root.
683
+ *
684
+ * Parmaeters:
685
+ * (Object) attrs - The <message/> element attributes in object notation.
686
+ *
687
+ * Returns:
688
+ * A new Strophe.Builder object.
689
+ */
690
+ function $msg(attrs) { return new Strophe.Builder("message", attrs); }
691
+ /** Function: $iq
692
+ * Create a Strophe.Builder with an <iq/> element as the root.
693
+ *
694
+ * Parameters:
695
+ * (Object) attrs - The <iq/> element attributes in object notation.
696
+ *
697
+ * Returns:
698
+ * A new Strophe.Builder object.
699
+ */
700
+ function $iq(attrs) { return new Strophe.Builder("iq", attrs); }
701
+ /** Function: $pres
702
+ * Create a Strophe.Builder with a <presence/> element as the root.
703
+ *
704
+ * Parameters:
705
+ * (Object) attrs - The <presence/> element attributes in object notation.
706
+ *
707
+ * Returns:
708
+ * A new Strophe.Builder object.
709
+ */
710
+ function $pres(attrs) { return new Strophe.Builder("presence", attrs); }
711
+
712
+ /** Class: Strophe
713
+ * An object container for all Strophe library functions.
714
+ *
715
+ * This class is just a container for all the objects and constants
716
+ * used in the library. It is not meant to be instantiated, but to
717
+ * provide a namespace for library objects, constants, and functions.
718
+ */
719
+ Strophe = {
720
+ /** Constant: VERSION
721
+ * The version of the Strophe library. Unreleased builds will have
722
+ * a version of head-HASH where HASH is a partial revision.
723
+ */
724
+ VERSION: "",
725
+
726
+ /** Constants: XMPP Namespace Constants
727
+ * Common namespace constants from the XMPP RFCs and XEPs.
728
+ *
729
+ * NS.HTTPBIND - HTTP BIND namespace from XEP 124.
730
+ * NS.BOSH - BOSH namespace from XEP 206.
731
+ * NS.CLIENT - Main XMPP client namespace.
732
+ * NS.AUTH - Legacy authentication namespace.
733
+ * NS.ROSTER - Roster operations namespace.
734
+ * NS.PROFILE - Profile namespace.
735
+ * NS.DISCO_INFO - Service discovery info namespace from XEP 30.
736
+ * NS.DISCO_ITEMS - Service discovery items namespace from XEP 30.
737
+ * NS.MUC - Multi-User Chat namespace from XEP 45.
738
+ * NS.SASL - XMPP SASL namespace from RFC 3920.
739
+ * NS.STREAM - XMPP Streams namespace from RFC 3920.
740
+ * NS.BIND - XMPP Binding namespace from RFC 3920.
741
+ * NS.SESSION - XMPP Session namespace from RFC 3920.
742
+ * NS.XHTML_IM - XHTML-IM namespace from XEP 71.
743
+ * NS.XHTML - XHTML body namespace from XEP 71.
744
+ */
745
+ NS: {
746
+ HTTPBIND: "http://jabber.org/protocol/httpbind",
747
+ BOSH: "urn:xmpp:xbosh",
748
+ CLIENT: "jabber:client",
749
+ AUTH: "jabber:iq:auth",
750
+ ROSTER: "jabber:iq:roster",
751
+ PROFILE: "jabber:iq:profile",
752
+ DISCO_INFO: "http://jabber.org/protocol/disco#info",
753
+ DISCO_ITEMS: "http://jabber.org/protocol/disco#items",
754
+ MUC: "http://jabber.org/protocol/muc",
755
+ SASL: "urn:ietf:params:xml:ns:xmpp-sasl",
756
+ STREAM: "http://etherx.jabber.org/streams",
757
+ BIND: "urn:ietf:params:xml:ns:xmpp-bind",
758
+ SESSION: "urn:ietf:params:xml:ns:xmpp-session",
759
+ VERSION: "jabber:iq:version",
760
+ STANZAS: "urn:ietf:params:xml:ns:xmpp-stanzas",
761
+ XHTML_IM: "http://jabber.org/protocol/xhtml-im",
762
+ XHTML: "http://www.w3.org/1999/xhtml"
763
+ },
764
+
765
+
766
+ /** Constants: XHTML_IM Namespace
767
+ * contains allowed tags, tag attributes, and css properties.
768
+ * Used in the createHtml function to filter incoming html into the allowed XHTML-IM subset.
769
+ * See http://xmpp.org/extensions/xep-0071.html#profile-summary for the list of recommended
770
+ * allowed tags and their attributes.
771
+ */
772
+ XHTML: {
773
+ tags: ['a','blockquote','br','cite','em','img','li','ol','p','span','strong','ul','body'],
774
+ attributes: {
775
+ 'a': ['href'],
776
+ 'blockquote': ['style'],
777
+ 'br': [],
778
+ 'cite': ['style'],
779
+ 'em': [],
780
+ 'img': ['src', 'alt', 'style', 'height', 'width'],
781
+ 'li': ['style'],
782
+ 'ol': ['style'],
783
+ 'p': ['style'],
784
+ 'span': ['style'],
785
+ 'strong': [],
786
+ 'ul': ['style'],
787
+ 'body': []
788
+ },
789
+ css: ['background-color','color','font-family','font-size','font-style','font-weight','margin-left','margin-right','text-align','text-decoration'],
790
+ validTag: function(tag)
791
+ {
792
+ for(var i = 0; i < Strophe.XHTML.tags.length; i++) {
793
+ if(tag == Strophe.XHTML.tags[i]) {
794
+ return true;
795
+ }
796
+ }
797
+ return false;
798
+ },
799
+ validAttribute: function(tag, attribute)
800
+ {
801
+ if(typeof Strophe.XHTML.attributes[tag] !== 'undefined' && Strophe.XHTML.attributes[tag].length > 0) {
802
+ for(var i = 0; i < Strophe.XHTML.attributes[tag].length; i++) {
803
+ if(attribute == Strophe.XHTML.attributes[tag][i]) {
804
+ return true;
805
+ }
806
+ }
807
+ }
808
+ return false;
809
+ },
810
+ validCSS: function(style)
811
+ {
812
+ for(var i = 0; i < Strophe.XHTML.css.length; i++) {
813
+ if(style == Strophe.XHTML.css[i]) {
814
+ return true;
815
+ }
816
+ }
817
+ return false;
818
+ }
819
+ },
820
+
821
+ /** Function: addNamespace
822
+ * This function is used to extend the current namespaces in
823
+ * Strophe.NS. It takes a key and a value with the key being the
824
+ * name of the new namespace, with its actual value.
825
+ * For example:
826
+ * Strophe.addNamespace('PUBSUB', "http://jabber.org/protocol/pubsub");
827
+ *
828
+ * Parameters:
829
+ * (String) name - The name under which the namespace will be
830
+ * referenced under Strophe.NS
831
+ * (String) value - The actual namespace.
832
+ */
833
+ addNamespace: function (name, value)
834
+ {
835
+ Strophe.NS[name] = value;
836
+ },
837
+
838
+ /** Constants: Connection Status Constants
839
+ * Connection status constants for use by the connection handler
840
+ * callback.
841
+ *
842
+ * Status.ERROR - An error has occurred
843
+ * Status.CONNECTING - The connection is currently being made
844
+ * Status.CONNFAIL - The connection attempt failed
845
+ * Status.AUTHENTICATING - The connection is authenticating
846
+ * Status.AUTHFAIL - The authentication attempt failed
847
+ * Status.CONNECTED - The connection has succeeded
848
+ * Status.DISCONNECTED - The connection has been terminated
849
+ * Status.DISCONNECTING - The connection is currently being terminated
850
+ * Status.ATTACHED - The connection has been attached
851
+ */
852
+ Status: {
853
+ ERROR: 0,
854
+ CONNECTING: 1,
855
+ CONNFAIL: 2,
856
+ AUTHENTICATING: 3,
857
+ AUTHFAIL: 4,
858
+ CONNECTED: 5,
859
+ DISCONNECTED: 6,
860
+ DISCONNECTING: 7,
861
+ ATTACHED: 8
862
+ },
863
+
864
+ /** Constants: Log Level Constants
865
+ * Logging level indicators.
866
+ *
867
+ * LogLevel.DEBUG - Debug output
868
+ * LogLevel.INFO - Informational output
869
+ * LogLevel.WARN - Warnings
870
+ * LogLevel.ERROR - Errors
871
+ * LogLevel.FATAL - Fatal errors
872
+ */
873
+ LogLevel: {
874
+ DEBUG: 0,
875
+ INFO: 1,
876
+ WARN: 2,
877
+ ERROR: 3,
878
+ FATAL: 4
879
+ },
880
+
881
+ /** PrivateConstants: DOM Element Type Constants
882
+ * DOM element types.
883
+ *
884
+ * ElementType.NORMAL - Normal element.
885
+ * ElementType.TEXT - Text data element.
886
+ * ElementType.FRAGMENT - XHTML fragment element.
887
+ */
888
+ ElementType: {
889
+ NORMAL: 1,
890
+ TEXT: 3,
891
+ CDATA: 4,
892
+ FRAGMENT: 11
893
+ },
894
+
895
+ /** PrivateConstants: Timeout Values
896
+ * Timeout values for error states. These values are in seconds.
897
+ * These should not be changed unless you know exactly what you are
898
+ * doing.
899
+ *
900
+ * TIMEOUT - Timeout multiplier. A waiting request will be considered
901
+ * failed after Math.floor(TIMEOUT * wait) seconds have elapsed.
902
+ * This defaults to 1.1, and with default wait, 66 seconds.
903
+ * SECONDARY_TIMEOUT - Secondary timeout multiplier. In cases where
904
+ * Strophe can detect early failure, it will consider the request
905
+ * failed if it doesn't return after
906
+ * Math.floor(SECONDARY_TIMEOUT * wait) seconds have elapsed.
907
+ * This defaults to 0.1, and with default wait, 6 seconds.
908
+ */
909
+ TIMEOUT: 1.1,
910
+ SECONDARY_TIMEOUT: 0.1,
911
+
912
+ /** Function: forEachChild
913
+ * Map a function over some or all child elements of a given element.
914
+ *
915
+ * This is a small convenience function for mapping a function over
916
+ * some or all of the children of an element. If elemName is null, all
917
+ * children will be passed to the function, otherwise only children
918
+ * whose tag names match elemName will be passed.
919
+ *
920
+ * Parameters:
921
+ * (XMLElement) elem - The element to operate on.
922
+ * (String) elemName - The child element tag name filter.
923
+ * (Function) func - The function to apply to each child. This
924
+ * function should take a single argument, a DOM element.
925
+ */
926
+ forEachChild: function (elem, elemName, func)
927
+ {
928
+ var i, childNode;
929
+
930
+ for (i = 0; i < elem.childNodes.length; i++) {
931
+ childNode = elem.childNodes[i];
932
+ if (childNode.nodeType == Strophe.ElementType.NORMAL &&
933
+ (!elemName || this.isTagEqual(childNode, elemName))) {
934
+ func(childNode);
935
+ }
936
+ }
937
+ },
938
+
939
+ /** Function: isTagEqual
940
+ * Compare an element's tag name with a string.
941
+ *
942
+ * This function is case insensitive.
943
+ *
944
+ * Parameters:
945
+ * (XMLElement) el - A DOM element.
946
+ * (String) name - The element name.
947
+ *
948
+ * Returns:
949
+ * true if the element's tag name matches _el_, and false
950
+ * otherwise.
951
+ */
952
+ isTagEqual: function (el, name)
953
+ {
954
+ return el.tagName.toLowerCase() == name.toLowerCase();
955
+ },
956
+
957
+ /** PrivateVariable: _xmlGenerator
958
+ * _Private_ variable that caches a DOM document to
959
+ * generate elements.
960
+ */
961
+ _xmlGenerator: null,
962
+
963
+ /** PrivateFunction: _makeGenerator
964
+ * _Private_ function that creates a dummy XML DOM document to serve as
965
+ * an element and text node generator.
966
+ */
967
+ _makeGenerator: function () {
968
+ var doc;
969
+
970
+ if (document.implementation.createDocument === undefined) {
971
+ doc = this._getIEXmlDom();
972
+ doc.appendChild(doc.createElement('strophe'));
973
+ } else {
974
+ doc = document.implementation
975
+ .createDocument('jabber:client', 'strophe', null);
976
+ }
977
+
978
+ return doc;
979
+ },
980
+
981
+ /** Function: xmlGenerator
982
+ * Get the DOM document to generate elements.
983
+ *
984
+ * Returns:
985
+ * The currently used DOM document.
986
+ */
987
+ xmlGenerator: function () {
988
+ if (!Strophe._xmlGenerator) {
989
+ Strophe._xmlGenerator = Strophe._makeGenerator();
990
+ }
991
+ return Strophe._xmlGenerator;
992
+ },
993
+
994
+ /** PrivateFunction: _getIEXmlDom
995
+ * Gets IE xml doc object
996
+ *
997
+ * Returns:
998
+ * A Microsoft XML DOM Object
999
+ * See Also:
1000
+ * http://msdn.microsoft.com/en-us/library/ms757837%28VS.85%29.aspx
1001
+ */
1002
+ _getIEXmlDom : function() {
1003
+ var doc = null;
1004
+ var docStrings = [
1005
+ "Msxml2.DOMDocument.6.0",
1006
+ "Msxml2.DOMDocument.5.0",
1007
+ "Msxml2.DOMDocument.4.0",
1008
+ "MSXML2.DOMDocument.3.0",
1009
+ "MSXML2.DOMDocument",
1010
+ "MSXML.DOMDocument",
1011
+ "Microsoft.XMLDOM"
1012
+ ];
1013
+
1014
+ for (var d = 0; d < docStrings.length; d++) {
1015
+ if (doc === null) {
1016
+ try {
1017
+ doc = new ActiveXObject(docStrings[d]);
1018
+ } catch (e) {
1019
+ doc = null;
1020
+ }
1021
+ } else {
1022
+ break;
1023
+ }
1024
+ }
1025
+
1026
+ return doc;
1027
+ },
1028
+
1029
+ /** Function: xmlElement
1030
+ * Create an XML DOM element.
1031
+ *
1032
+ * This function creates an XML DOM element correctly across all
1033
+ * implementations. Note that these are not HTML DOM elements, which
1034
+ * aren't appropriate for XMPP stanzas.
1035
+ *
1036
+ * Parameters:
1037
+ * (String) name - The name for the element.
1038
+ * (Array|Object) attrs - An optional array or object containing
1039
+ * key/value pairs to use as element attributes. The object should
1040
+ * be in the format {'key': 'value'} or {key: 'value'}. The array
1041
+ * should have the format [['key1', 'value1'], ['key2', 'value2']].
1042
+ * (String) text - The text child data for the element.
1043
+ *
1044
+ * Returns:
1045
+ * A new XML DOM element.
1046
+ */
1047
+ xmlElement: function (name)
1048
+ {
1049
+ if (!name) { return null; }
1050
+
1051
+ var node = Strophe.xmlGenerator().createElement(name);
1052
+
1053
+ // FIXME: this should throw errors if args are the wrong type or
1054
+ // there are more than two optional args
1055
+ var a, i, k;
1056
+ for (a = 1; a < arguments.length; a++) {
1057
+ if (!arguments[a]) { continue; }
1058
+ if (typeof(arguments[a]) == "string" ||
1059
+ typeof(arguments[a]) == "number") {
1060
+ node.appendChild(Strophe.xmlTextNode(arguments[a]));
1061
+ } else if (typeof(arguments[a]) == "object" &&
1062
+ typeof(arguments[a].sort) == "function") {
1063
+ for (i = 0; i < arguments[a].length; i++) {
1064
+ if (typeof(arguments[a][i]) == "object" &&
1065
+ typeof(arguments[a][i].sort) == "function") {
1066
+ node.setAttribute(arguments[a][i][0],
1067
+ arguments[a][i][1]);
1068
+ }
1069
+ }
1070
+ } else if (typeof(arguments[a]) == "object") {
1071
+ for (k in arguments[a]) {
1072
+ if (arguments[a].hasOwnProperty(k)) {
1073
+ node.setAttribute(k, arguments[a][k]);
1074
+ }
1075
+ }
1076
+ }
1077
+ }
1078
+
1079
+ return node;
1080
+ },
1081
+
1082
+ /* Function: xmlescape
1083
+ * Excapes invalid xml characters.
1084
+ *
1085
+ * Parameters:
1086
+ * (String) text - text to escape.
1087
+ *
1088
+ * Returns:
1089
+ * Escaped text.
1090
+ */
1091
+ xmlescape: function(text)
1092
+ {
1093
+ text = text.replace(/\&/g, "&amp;");
1094
+ text = text.replace(/</g, "&lt;");
1095
+ text = text.replace(/>/g, "&gt;");
1096
+ text = text.replace(/'/g, "&apos;");
1097
+ text = text.replace(/"/g, "&quot;");
1098
+ return text;
1099
+ },
1100
+
1101
+ /** Function: xmlTextNode
1102
+ * Creates an XML DOM text node.
1103
+ *
1104
+ * Provides a cross implementation version of document.createTextNode.
1105
+ *
1106
+ * Parameters:
1107
+ * (String) text - The content of the text node.
1108
+ *
1109
+ * Returns:
1110
+ * A new XML DOM text node.
1111
+ */
1112
+ xmlTextNode: function (text)
1113
+ {
1114
+ return Strophe.xmlGenerator().createTextNode(text);
1115
+ },
1116
+
1117
+ /** Function: xmlHtmlNode
1118
+ * Creates an XML DOM html node.
1119
+ *
1120
+ * Parameters:
1121
+ * (String) html - The content of the html node.
1122
+ *
1123
+ * Returns:
1124
+ * A new XML DOM text node.
1125
+ */
1126
+ xmlHtmlNode: function (html)
1127
+ {
1128
+ //ensure text is escaped
1129
+ if (window.DOMParser) {
1130
+ parser = new DOMParser();
1131
+ node = parser.parseFromString(html, "text/xml");
1132
+ } else {
1133
+ node = new ActiveXObject("Microsoft.XMLDOM");
1134
+ node.async="false";
1135
+ node.loadXML(html);
1136
+ }
1137
+ return node;
1138
+ },
1139
+
1140
+ /** Function: getText
1141
+ * Get the concatenation of all text children of an element.
1142
+ *
1143
+ * Parameters:
1144
+ * (XMLElement) elem - A DOM element.
1145
+ *
1146
+ * Returns:
1147
+ * A String with the concatenated text of all text element children.
1148
+ */
1149
+ getText: function (elem)
1150
+ {
1151
+ if (!elem) { return null; }
1152
+
1153
+ var str = "";
1154
+ if (elem.childNodes.length === 0 && elem.nodeType ==
1155
+ Strophe.ElementType.TEXT) {
1156
+ str += elem.nodeValue;
1157
+ }
1158
+
1159
+ for (var i = 0; i < elem.childNodes.length; i++) {
1160
+ if (elem.childNodes[i].nodeType == Strophe.ElementType.TEXT) {
1161
+ str += elem.childNodes[i].nodeValue;
1162
+ }
1163
+ }
1164
+
1165
+ return Strophe.xmlescape(str);
1166
+ },
1167
+
1168
+ /** Function: copyElement
1169
+ * Copy an XML DOM element.
1170
+ *
1171
+ * This function copies a DOM element and all its descendants and returns
1172
+ * the new copy.
1173
+ *
1174
+ * Parameters:
1175
+ * (XMLElement) elem - A DOM element.
1176
+ *
1177
+ * Returns:
1178
+ * A new, copied DOM element tree.
1179
+ */
1180
+ copyElement: function (elem)
1181
+ {
1182
+ var i, el;
1183
+ if (elem.nodeType == Strophe.ElementType.NORMAL) {
1184
+ el = Strophe.xmlElement(elem.tagName);
1185
+
1186
+ for (i = 0; i < elem.attributes.length; i++) {
1187
+ el.setAttribute(elem.attributes[i].nodeName.toLowerCase(),
1188
+ elem.attributes[i].value);
1189
+ }
1190
+
1191
+ for (i = 0; i < elem.childNodes.length; i++) {
1192
+ el.appendChild(Strophe.copyElement(elem.childNodes[i]));
1193
+ }
1194
+ } else if (elem.nodeType == Strophe.ElementType.TEXT) {
1195
+ el = Strophe.xmlGenerator().createTextNode(elem.nodeValue);
1196
+ }
1197
+
1198
+ return el;
1199
+ },
1200
+
1201
+
1202
+ /** Function: createHtml
1203
+ * Copy an HTML DOM element into an XML DOM.
1204
+ *
1205
+ * This function copies a DOM element and all its descendants and returns
1206
+ * the new copy.
1207
+ *
1208
+ * Parameters:
1209
+ * (HTMLElement) elem - A DOM element.
1210
+ *
1211
+ * Returns:
1212
+ * A new, copied DOM element tree.
1213
+ */
1214
+ createHtml: function (elem)
1215
+ {
1216
+ var i, el, j, tag, attribute, value, css, cssAttrs, attr, cssName, cssValue, children, child;
1217
+ if (elem.nodeType == Strophe.ElementType.NORMAL) {
1218
+ tag = elem.nodeName.toLowerCase();
1219
+ if(Strophe.XHTML.validTag(tag)) {
1220
+ try {
1221
+ el = Strophe.xmlElement(tag);
1222
+ for(i = 0; i < Strophe.XHTML.attributes[tag].length; i++) {
1223
+ attribute = Strophe.XHTML.attributes[tag][i];
1224
+ value = elem.getAttribute(attribute);
1225
+ if(typeof value == 'undefined' || value === null || value === '' || value === false || value === 0) {
1226
+ continue;
1227
+ }
1228
+ if(attribute == 'style' && typeof value == 'object') {
1229
+ if(typeof value.cssText != 'undefined') {
1230
+ value = value.cssText; // we're dealing with IE, need to get CSS out
1231
+ }
1232
+ }
1233
+ // filter out invalid css styles
1234
+ if(attribute == 'style') {
1235
+ css = [];
1236
+ cssAttrs = value.split(';');
1237
+ for(j = 0; j < cssAttrs.length; j++) {
1238
+ attr = cssAttrs[j].split(':');
1239
+ cssName = attr[0].replace(/^\s*/, "").replace(/\s*$/, "").toLowerCase();
1240
+ if(Strophe.XHTML.validCSS(cssName)) {
1241
+ cssValue = attr[1].replace(/^\s*/, "").replace(/\s*$/, "");
1242
+ css.push(cssName + ': ' + cssValue);
1243
+ }
1244
+ }
1245
+ if(css.length > 0) {
1246
+ value = css.join('; ');
1247
+ el.setAttribute(attribute, value);
1248
+ }
1249
+ } else {
1250
+ el.setAttribute(attribute, value);
1251
+ }
1252
+ }
1253
+
1254
+ for (i = 0; i < elem.childNodes.length; i++) {
1255
+ el.appendChild(Strophe.createHtml(elem.childNodes[i]));
1256
+ }
1257
+ } catch(e) { // invalid elements
1258
+ el = Strophe.xmlTextNode('');
1259
+ }
1260
+ } else {
1261
+ el = Strophe.xmlGenerator().createDocumentFragment();
1262
+ for (i = 0; i < elem.childNodes.length; i++) {
1263
+ el.appendChild(Strophe.createHtml(elem.childNodes[i]));
1264
+ }
1265
+ }
1266
+ } else if (elem.nodeType == Strophe.ElementType.FRAGMENT) {
1267
+ el = Strophe.xmlGenerator().createDocumentFragment();
1268
+ for (i = 0; i < elem.childNodes.length; i++) {
1269
+ el.appendChild(Strophe.createHtml(elem.childNodes[i]));
1270
+ }
1271
+ } else if (elem.nodeType == Strophe.ElementType.TEXT) {
1272
+ el = Strophe.xmlTextNode(elem.nodeValue);
1273
+ }
1274
+
1275
+ return el;
1276
+ },
1277
+
1278
+ /** Function: escapeNode
1279
+ * Escape the node part (also called local part) of a JID.
1280
+ *
1281
+ * Parameters:
1282
+ * (String) node - A node (or local part).
1283
+ *
1284
+ * Returns:
1285
+ * An escaped node (or local part).
1286
+ */
1287
+ escapeNode: function (node)
1288
+ {
1289
+ return node.replace(/^\s+|\s+$/g, '')
1290
+ .replace(/\\/g, "\\5c")
1291
+ .replace(/ /g, "\\20")
1292
+ .replace(/\"/g, "\\22")
1293
+ .replace(/\&/g, "\\26")
1294
+ .replace(/\'/g, "\\27")
1295
+ .replace(/\//g, "\\2f")
1296
+ .replace(/:/g, "\\3a")
1297
+ .replace(/</g, "\\3c")
1298
+ .replace(/>/g, "\\3e")
1299
+ .replace(/@/g, "\\40");
1300
+ },
1301
+
1302
+ /** Function: unescapeNode
1303
+ * Unescape a node part (also called local part) of a JID.
1304
+ *
1305
+ * Parameters:
1306
+ * (String) node - A node (or local part).
1307
+ *
1308
+ * Returns:
1309
+ * An unescaped node (or local part).
1310
+ */
1311
+ unescapeNode: function (node)
1312
+ {
1313
+ return node.replace(/\\20/g, " ")
1314
+ .replace(/\\22/g, '"')
1315
+ .replace(/\\26/g, "&")
1316
+ .replace(/\\27/g, "'")
1317
+ .replace(/\\2f/g, "/")
1318
+ .replace(/\\3a/g, ":")
1319
+ .replace(/\\3c/g, "<")
1320
+ .replace(/\\3e/g, ">")
1321
+ .replace(/\\40/g, "@")
1322
+ .replace(/\\5c/g, "\\");
1323
+ },
1324
+
1325
+ /** Function: getNodeFromJid
1326
+ * Get the node portion of a JID String.
1327
+ *
1328
+ * Parameters:
1329
+ * (String) jid - A JID.
1330
+ *
1331
+ * Returns:
1332
+ * A String containing the node.
1333
+ */
1334
+ getNodeFromJid: function (jid)
1335
+ {
1336
+ if (jid.indexOf("@") < 0) { return null; }
1337
+ return jid.split("@")[0];
1338
+ },
1339
+
1340
+ /** Function: getDomainFromJid
1341
+ * Get the domain portion of a JID String.
1342
+ *
1343
+ * Parameters:
1344
+ * (String) jid - A JID.
1345
+ *
1346
+ * Returns:
1347
+ * A String containing the domain.
1348
+ */
1349
+ getDomainFromJid: function (jid)
1350
+ {
1351
+ var bare = Strophe.getBareJidFromJid(jid);
1352
+ if (bare.indexOf("@") < 0) {
1353
+ return bare;
1354
+ } else {
1355
+ var parts = bare.split("@");
1356
+ parts.splice(0, 1);
1357
+ return parts.join('@');
1358
+ }
1359
+ },
1360
+
1361
+ /** Function: getResourceFromJid
1362
+ * Get the resource portion of a JID String.
1363
+ *
1364
+ * Parameters:
1365
+ * (String) jid - A JID.
1366
+ *
1367
+ * Returns:
1368
+ * A String containing the resource.
1369
+ */
1370
+ getResourceFromJid: function (jid)
1371
+ {
1372
+ var s = jid.split("/");
1373
+ if (s.length < 2) { return null; }
1374
+ s.splice(0, 1);
1375
+ return s.join('/');
1376
+ },
1377
+
1378
+ /** Function: getBareJidFromJid
1379
+ * Get the bare JID from a JID String.
1380
+ *
1381
+ * Parameters:
1382
+ * (String) jid - A JID.
1383
+ *
1384
+ * Returns:
1385
+ * A String containing the bare JID.
1386
+ */
1387
+ getBareJidFromJid: function (jid)
1388
+ {
1389
+ return jid ? jid.split("/")[0] : null;
1390
+ },
1391
+
1392
+ /** Function: log
1393
+ * User overrideable logging function.
1394
+ *
1395
+ * This function is called whenever the Strophe library calls any
1396
+ * of the logging functions. The default implementation of this
1397
+ * function does nothing. If client code wishes to handle the logging
1398
+ * messages, it should override this with
1399
+ * > Strophe.log = function (level, msg) {
1400
+ * > (user code here)
1401
+ * > };
1402
+ *
1403
+ * Please note that data sent and received over the wire is logged
1404
+ * via Strophe.Connection.rawInput() and Strophe.Connection.rawOutput().
1405
+ *
1406
+ * The different levels and their meanings are
1407
+ *
1408
+ * DEBUG - Messages useful for debugging purposes.
1409
+ * INFO - Informational messages. This is mostly information like
1410
+ * 'disconnect was called' or 'SASL auth succeeded'.
1411
+ * WARN - Warnings about potential problems. This is mostly used
1412
+ * to report transient connection errors like request timeouts.
1413
+ * ERROR - Some error occurred.
1414
+ * FATAL - A non-recoverable fatal error occurred.
1415
+ *
1416
+ * Parameters:
1417
+ * (Integer) level - The log level of the log message. This will
1418
+ * be one of the values in Strophe.LogLevel.
1419
+ * (String) msg - The log message.
1420
+ */
1421
+ log: function (level, msg)
1422
+ {
1423
+ return;
1424
+ },
1425
+
1426
+ /** Function: debug
1427
+ * Log a message at the Strophe.LogLevel.DEBUG level.
1428
+ *
1429
+ * Parameters:
1430
+ * (String) msg - The log message.
1431
+ */
1432
+ debug: function(msg)
1433
+ {
1434
+ this.log(this.LogLevel.DEBUG, msg);
1435
+ },
1436
+
1437
+ /** Function: info
1438
+ * Log a message at the Strophe.LogLevel.INFO level.
1439
+ *
1440
+ * Parameters:
1441
+ * (String) msg - The log message.
1442
+ */
1443
+ info: function (msg)
1444
+ {
1445
+ this.log(this.LogLevel.INFO, msg);
1446
+ },
1447
+
1448
+ /** Function: warn
1449
+ * Log a message at the Strophe.LogLevel.WARN level.
1450
+ *
1451
+ * Parameters:
1452
+ * (String) msg - The log message.
1453
+ */
1454
+ warn: function (msg)
1455
+ {
1456
+ this.log(this.LogLevel.WARN, msg);
1457
+ },
1458
+
1459
+ /** Function: error
1460
+ * Log a message at the Strophe.LogLevel.ERROR level.
1461
+ *
1462
+ * Parameters:
1463
+ * (String) msg - The log message.
1464
+ */
1465
+ error: function (msg)
1466
+ {
1467
+ this.log(this.LogLevel.ERROR, msg);
1468
+ },
1469
+
1470
+ /** Function: fatal
1471
+ * Log a message at the Strophe.LogLevel.FATAL level.
1472
+ *
1473
+ * Parameters:
1474
+ * (String) msg - The log message.
1475
+ */
1476
+ fatal: function (msg)
1477
+ {
1478
+ this.log(this.LogLevel.FATAL, msg);
1479
+ },
1480
+
1481
+ /** Function: serialize
1482
+ * Render a DOM element and all descendants to a String.
1483
+ *
1484
+ * Parameters:
1485
+ * (XMLElement) elem - A DOM element.
1486
+ *
1487
+ * Returns:
1488
+ * The serialized element tree as a String.
1489
+ */
1490
+ serialize: function (elem)
1491
+ {
1492
+ var result;
1493
+
1494
+ if (!elem) { return null; }
1495
+
1496
+ if (typeof(elem.tree) === "function") {
1497
+ elem = elem.tree();
1498
+ }
1499
+
1500
+ var nodeName = elem.nodeName;
1501
+ var i, child;
1502
+
1503
+ if (elem.getAttribute("_realname")) {
1504
+ nodeName = elem.getAttribute("_realname");
1505
+ }
1506
+
1507
+ result = "<" + nodeName;
1508
+ for (i = 0; i < elem.attributes.length; i++) {
1509
+ if(elem.attributes[i].nodeName != "_realname") {
1510
+ result += " " + elem.attributes[i].nodeName.toLowerCase() +
1511
+ "='" + elem.attributes[i].value
1512
+ .replace(/&/g, "&amp;")
1513
+ .replace(/\'/g, "&apos;")
1514
+ .replace(/>/g, "&gt;")
1515
+ .replace(/</g, "&lt;") + "'";
1516
+ }
1517
+ }
1518
+
1519
+ if (elem.childNodes.length > 0) {
1520
+ result += ">";
1521
+ for (i = 0; i < elem.childNodes.length; i++) {
1522
+ child = elem.childNodes[i];
1523
+ switch( child.nodeType ){
1524
+ case Strophe.ElementType.NORMAL:
1525
+ // normal element, so recurse
1526
+ result += Strophe.serialize(child);
1527
+ break;
1528
+ case Strophe.ElementType.TEXT:
1529
+ // text element to escape values
1530
+ result += Strophe.xmlescape(child.nodeValue);
1531
+ break;
1532
+ case Strophe.ElementType.CDATA:
1533
+ // cdata section so don't escape values
1534
+ result += "<![CDATA["+child.nodeValue+"]]>";
1535
+ }
1536
+ }
1537
+ result += "</" + nodeName + ">";
1538
+ } else {
1539
+ result += "/>";
1540
+ }
1541
+
1542
+ return result;
1543
+ },
1544
+
1545
+ /** PrivateVariable: _requestId
1546
+ * _Private_ variable that keeps track of the request ids for
1547
+ * connections.
1548
+ */
1549
+ _requestId: 0,
1550
+
1551
+ /** PrivateVariable: Strophe.connectionPlugins
1552
+ * _Private_ variable Used to store plugin names that need
1553
+ * initialization on Strophe.Connection construction.
1554
+ */
1555
+ _connectionPlugins: {},
1556
+
1557
+ /** Function: addConnectionPlugin
1558
+ * Extends the Strophe.Connection object with the given plugin.
1559
+ *
1560
+ * Parameters:
1561
+ * (String) name - The name of the extension.
1562
+ * (Object) ptype - The plugin's prototype.
1563
+ */
1564
+ addConnectionPlugin: function (name, ptype)
1565
+ {
1566
+ Strophe._connectionPlugins[name] = ptype;
1567
+ }
1568
+ };
1569
+
1570
+ /** Class: Strophe.Builder
1571
+ * XML DOM builder.
1572
+ *
1573
+ * This object provides an interface similar to JQuery but for building
1574
+ * DOM element easily and rapidly. All the functions except for toString()
1575
+ * and tree() return the object, so calls can be chained. Here's an
1576
+ * example using the $iq() builder helper.
1577
+ * > $iq({to: 'you', from: 'me', type: 'get', id: '1'})
1578
+ * > .c('query', {xmlns: 'strophe:example'})
1579
+ * > .c('example')
1580
+ * > .toString()
1581
+ * The above generates this XML fragment
1582
+ * > <iq to='you' from='me' type='get' id='1'>
1583
+ * > <query xmlns='strophe:example'>
1584
+ * > <example/>
1585
+ * > </query>
1586
+ * > </iq>
1587
+ * The corresponding DOM manipulations to get a similar fragment would be
1588
+ * a lot more tedious and probably involve several helper variables.
1589
+ *
1590
+ * Since adding children makes new operations operate on the child, up()
1591
+ * is provided to traverse up the tree. To add two children, do
1592
+ * > builder.c('child1', ...).up().c('child2', ...)
1593
+ * The next operation on the Builder will be relative to the second child.
1594
+ */
1595
+
1596
+ /** Constructor: Strophe.Builder
1597
+ * Create a Strophe.Builder object.
1598
+ *
1599
+ * The attributes should be passed in object notation. For example
1600
+ * > var b = new Builder('message', {to: 'you', from: 'me'});
1601
+ * or
1602
+ * > var b = new Builder('messsage', {'xml:lang': 'en'});
1603
+ *
1604
+ * Parameters:
1605
+ * (String) name - The name of the root element.
1606
+ * (Object) attrs - The attributes for the root element in object notation.
1607
+ *
1608
+ * Returns:
1609
+ * A new Strophe.Builder.
1610
+ */
1611
+ Strophe.Builder = function (name, attrs)
1612
+ {
1613
+ // Set correct namespace for jabber:client elements
1614
+ if (name == "presence" || name == "message" || name == "iq") {
1615
+ if (attrs && !attrs.xmlns) {
1616
+ attrs.xmlns = Strophe.NS.CLIENT;
1617
+ } else if (!attrs) {
1618
+ attrs = {xmlns: Strophe.NS.CLIENT};
1619
+ }
1620
+ }
1621
+
1622
+ // Holds the tree being built.
1623
+ this.nodeTree = Strophe.xmlElement(name, attrs);
1624
+
1625
+ // Points to the current operation node.
1626
+ this.node = this.nodeTree;
1627
+ };
1628
+
1629
+ Strophe.Builder.prototype = {
1630
+ /** Function: tree
1631
+ * Return the DOM tree.
1632
+ *
1633
+ * This function returns the current DOM tree as an element object. This
1634
+ * is suitable for passing to functions like Strophe.Connection.send().
1635
+ *
1636
+ * Returns:
1637
+ * The DOM tree as a element object.
1638
+ */
1639
+ tree: function ()
1640
+ {
1641
+ return this.nodeTree;
1642
+ },
1643
+
1644
+ /** Function: toString
1645
+ * Serialize the DOM tree to a String.
1646
+ *
1647
+ * This function returns a string serialization of the current DOM
1648
+ * tree. It is often used internally to pass data to a
1649
+ * Strophe.Request object.
1650
+ *
1651
+ * Returns:
1652
+ * The serialized DOM tree in a String.
1653
+ */
1654
+ toString: function ()
1655
+ {
1656
+ return Strophe.serialize(this.nodeTree);
1657
+ },
1658
+
1659
+ /** Function: up
1660
+ * Make the current parent element the new current element.
1661
+ *
1662
+ * This function is often used after c() to traverse back up the tree.
1663
+ * For example, to add two children to the same element
1664
+ * > builder.c('child1', {}).up().c('child2', {});
1665
+ *
1666
+ * Returns:
1667
+ * The Stophe.Builder object.
1668
+ */
1669
+ up: function ()
1670
+ {
1671
+ this.node = this.node.parentNode;
1672
+ return this;
1673
+ },
1674
+
1675
+ /** Function: attrs
1676
+ * Add or modify attributes of the current element.
1677
+ *
1678
+ * The attributes should be passed in object notation. This function
1679
+ * does not move the current element pointer.
1680
+ *
1681
+ * Parameters:
1682
+ * (Object) moreattrs - The attributes to add/modify in object notation.
1683
+ *
1684
+ * Returns:
1685
+ * The Strophe.Builder object.
1686
+ */
1687
+ attrs: function (moreattrs)
1688
+ {
1689
+ for (var k in moreattrs) {
1690
+ if (moreattrs.hasOwnProperty(k)) {
1691
+ this.node.setAttribute(k, moreattrs[k]);
1692
+ }
1693
+ }
1694
+ return this;
1695
+ },
1696
+
1697
+ /** Function: c
1698
+ * Add a child to the current element and make it the new current
1699
+ * element.
1700
+ *
1701
+ * This function moves the current element pointer to the child,
1702
+ * unless text is provided. If you need to add another child, it
1703
+ * is necessary to use up() to go back to the parent in the tree.
1704
+ *
1705
+ * Parameters:
1706
+ * (String) name - The name of the child.
1707
+ * (Object) attrs - The attributes of the child in object notation.
1708
+ * (String) text - The text to add to the child.
1709
+ *
1710
+ * Returns:
1711
+ * The Strophe.Builder object.
1712
+ */
1713
+ c: function (name, attrs, text)
1714
+ {
1715
+ var child = Strophe.xmlElement(name, attrs, text);
1716
+ this.node.appendChild(child);
1717
+ if (!text) {
1718
+ this.node = child;
1719
+ }
1720
+ return this;
1721
+ },
1722
+
1723
+ /** Function: cnode
1724
+ * Add a child to the current element and make it the new current
1725
+ * element.
1726
+ *
1727
+ * This function is the same as c() except that instead of using a
1728
+ * name and an attributes object to create the child it uses an
1729
+ * existing DOM element object.
1730
+ *
1731
+ * Parameters:
1732
+ * (XMLElement) elem - A DOM element.
1733
+ *
1734
+ * Returns:
1735
+ * The Strophe.Builder object.
1736
+ */
1737
+ cnode: function (elem)
1738
+ {
1739
+ var xmlGen = Strophe.xmlGenerator();
1740
+ try {
1741
+ var impNode = (xmlGen.importNode !== undefined);
1742
+ }
1743
+ catch (e) {
1744
+ var impNode = false;
1745
+ }
1746
+ var newElem = impNode ?
1747
+ xmlGen.importNode(elem, true) :
1748
+ Strophe.copyElement(elem);
1749
+ this.node.appendChild(newElem);
1750
+ this.node = newElem;
1751
+ return this;
1752
+ },
1753
+
1754
+ /** Function: t
1755
+ * Add a child text element.
1756
+ *
1757
+ * This *does not* make the child the new current element since there
1758
+ * are no children of text elements.
1759
+ *
1760
+ * Parameters:
1761
+ * (String) text - The text data to append to the current element.
1762
+ *
1763
+ * Returns:
1764
+ * The Strophe.Builder object.
1765
+ */
1766
+ t: function (text)
1767
+ {
1768
+ var child = Strophe.xmlTextNode(text);
1769
+ this.node.appendChild(child);
1770
+ return this;
1771
+ },
1772
+
1773
+ /** Function: h
1774
+ * Replace current element contents with the HTML passed in.
1775
+ *
1776
+ * This *does not* make the child the new current element
1777
+ *
1778
+ * Parameters:
1779
+ * (String) html - The html to insert as contents of current element.
1780
+ *
1781
+ * Returns:
1782
+ * The Strophe.Builder object.
1783
+ */
1784
+ h: function (html)
1785
+ {
1786
+ var fragment = document.createElement('body');
1787
+
1788
+ // force the browser to try and fix any invalid HTML tags
1789
+ fragment.innerHTML = html;
1790
+
1791
+ // copy cleaned html into an xml dom
1792
+ var xhtml = Strophe.createHtml(fragment);
1793
+
1794
+ while(xhtml.childNodes.length > 0) {
1795
+ this.node.appendChild(xhtml.childNodes[0]);
1796
+ }
1797
+ return this;
1798
+ }
1799
+ };
1800
+
1801
+ /** PrivateClass: Strophe.Handler
1802
+ * _Private_ helper class for managing stanza handlers.
1803
+ *
1804
+ * A Strophe.Handler encapsulates a user provided callback function to be
1805
+ * executed when matching stanzas are received by the connection.
1806
+ * Handlers can be either one-off or persistant depending on their
1807
+ * return value. Returning true will cause a Handler to remain active, and
1808
+ * returning false will remove the Handler.
1809
+ *
1810
+ * Users will not use Strophe.Handler objects directly, but instead they
1811
+ * will use Strophe.Connection.addHandler() and
1812
+ * Strophe.Connection.deleteHandler().
1813
+ */
1814
+
1815
+ /** PrivateConstructor: Strophe.Handler
1816
+ * Create and initialize a new Strophe.Handler.
1817
+ *
1818
+ * Parameters:
1819
+ * (Function) handler - A function to be executed when the handler is run.
1820
+ * (String) ns - The namespace to match.
1821
+ * (String) name - The element name to match.
1822
+ * (String) type - The element type to match.
1823
+ * (String) id - The element id attribute to match.
1824
+ * (String) from - The element from attribute to match.
1825
+ * (Object) options - Handler options
1826
+ *
1827
+ * Returns:
1828
+ * A new Strophe.Handler object.
1829
+ */
1830
+ Strophe.Handler = function (handler, ns, name, type, id, from, options)
1831
+ {
1832
+ this.handler = handler;
1833
+ this.ns = ns;
1834
+ this.name = name;
1835
+ this.type = type;
1836
+ this.id = id;
1837
+ this.options = options || {matchbare: false};
1838
+
1839
+ // default matchBare to false if undefined
1840
+ if (!this.options.matchBare) {
1841
+ this.options.matchBare = false;
1842
+ }
1843
+
1844
+ if (this.options.matchBare) {
1845
+ this.from = from ? Strophe.getBareJidFromJid(from) : null;
1846
+ } else {
1847
+ this.from = from;
1848
+ }
1849
+
1850
+ // whether the handler is a user handler or a system handler
1851
+ this.user = true;
1852
+ };
1853
+
1854
+ Strophe.Handler.prototype = {
1855
+ /** PrivateFunction: isMatch
1856
+ * Tests if a stanza matches the Strophe.Handler.
1857
+ *
1858
+ * Parameters:
1859
+ * (XMLElement) elem - The XML element to test.
1860
+ *
1861
+ * Returns:
1862
+ * true if the stanza matches and false otherwise.
1863
+ */
1864
+ isMatch: function (elem)
1865
+ {
1866
+ var nsMatch;
1867
+ var from = null;
1868
+
1869
+ if (this.options.matchBare) {
1870
+ from = Strophe.getBareJidFromJid(elem.getAttribute('from'));
1871
+ } else {
1872
+ from = elem.getAttribute('from');
1873
+ }
1874
+
1875
+ nsMatch = false;
1876
+ if (!this.ns) {
1877
+ nsMatch = true;
1878
+ } else {
1879
+ var that = this;
1880
+ Strophe.forEachChild(elem, null, function (elem) {
1881
+ if (elem.getAttribute("xmlns") == that.ns) {
1882
+ nsMatch = true;
1883
+ }
1884
+ });
1885
+
1886
+ nsMatch = nsMatch || elem.getAttribute("xmlns") == this.ns;
1887
+ }
1888
+
1889
+ if (nsMatch &&
1890
+ (!this.name || Strophe.isTagEqual(elem, this.name)) &&
1891
+ (!this.type || elem.getAttribute("type") == this.type) &&
1892
+ (!this.id || elem.getAttribute("id") == this.id) &&
1893
+ (!this.from || from == this.from)) {
1894
+ return true;
1895
+ }
1896
+
1897
+ return false;
1898
+ },
1899
+
1900
+ /** PrivateFunction: run
1901
+ * Run the callback on a matching stanza.
1902
+ *
1903
+ * Parameters:
1904
+ * (XMLElement) elem - The DOM element that triggered the
1905
+ * Strophe.Handler.
1906
+ *
1907
+ * Returns:
1908
+ * A boolean indicating if the handler should remain active.
1909
+ */
1910
+ run: function (elem)
1911
+ {
1912
+ var result = null;
1913
+ try {
1914
+ result = this.handler(elem);
1915
+ } catch (e) {
1916
+ if (e.sourceURL) {
1917
+ Strophe.fatal("error: " + this.handler +
1918
+ " " + e.sourceURL + ":" +
1919
+ e.line + " - " + e.name + ": " + e.message);
1920
+ } else if (e.fileName) {
1921
+ if (typeof(console) != "undefined") {
1922
+ console.trace();
1923
+ console.error(this.handler, " - error - ", e, e.message);
1924
+ }
1925
+ Strophe.fatal("error: " + this.handler + " " +
1926
+ e.fileName + ":" + e.lineNumber + " - " +
1927
+ e.name + ": " + e.message);
1928
+ } else {
1929
+ Strophe.fatal("error: " + e.message + "\n" + e.stack);
1930
+ }
1931
+
1932
+ throw e;
1933
+ }
1934
+
1935
+ return result;
1936
+ },
1937
+
1938
+ /** PrivateFunction: toString
1939
+ * Get a String representation of the Strophe.Handler object.
1940
+ *
1941
+ * Returns:
1942
+ * A String.
1943
+ */
1944
+ toString: function ()
1945
+ {
1946
+ return "{Handler: " + this.handler + "(" + this.name + "," +
1947
+ this.id + "," + this.ns + ")}";
1948
+ }
1949
+ };
1950
+
1951
+ /** PrivateClass: Strophe.TimedHandler
1952
+ * _Private_ helper class for managing timed handlers.
1953
+ *
1954
+ * A Strophe.TimedHandler encapsulates a user provided callback that
1955
+ * should be called after a certain period of time or at regular
1956
+ * intervals. The return value of the callback determines whether the
1957
+ * Strophe.TimedHandler will continue to fire.
1958
+ *
1959
+ * Users will not use Strophe.TimedHandler objects directly, but instead
1960
+ * they will use Strophe.Connection.addTimedHandler() and
1961
+ * Strophe.Connection.deleteTimedHandler().
1962
+ */
1963
+
1964
+ /** PrivateConstructor: Strophe.TimedHandler
1965
+ * Create and initialize a new Strophe.TimedHandler object.
1966
+ *
1967
+ * Parameters:
1968
+ * (Integer) period - The number of milliseconds to wait before the
1969
+ * handler is called.
1970
+ * (Function) handler - The callback to run when the handler fires. This
1971
+ * function should take no arguments.
1972
+ *
1973
+ * Returns:
1974
+ * A new Strophe.TimedHandler object.
1975
+ */
1976
+ Strophe.TimedHandler = function (period, handler)
1977
+ {
1978
+ this.period = period;
1979
+ this.handler = handler;
1980
+
1981
+ this.lastCalled = new Date().getTime();
1982
+ this.user = true;
1983
+ };
1984
+
1985
+ Strophe.TimedHandler.prototype = {
1986
+ /** PrivateFunction: run
1987
+ * Run the callback for the Strophe.TimedHandler.
1988
+ *
1989
+ * Returns:
1990
+ * true if the Strophe.TimedHandler should be called again, and false
1991
+ * otherwise.
1992
+ */
1993
+ run: function ()
1994
+ {
1995
+ this.lastCalled = new Date().getTime();
1996
+ return this.handler();
1997
+ },
1998
+
1999
+ /** PrivateFunction: reset
2000
+ * Reset the last called time for the Strophe.TimedHandler.
2001
+ */
2002
+ reset: function ()
2003
+ {
2004
+ this.lastCalled = new Date().getTime();
2005
+ },
2006
+
2007
+ /** PrivateFunction: toString
2008
+ * Get a string representation of the Strophe.TimedHandler object.
2009
+ *
2010
+ * Returns:
2011
+ * The string representation.
2012
+ */
2013
+ toString: function ()
2014
+ {
2015
+ return "{TimedHandler: " + this.handler + "(" + this.period +")}";
2016
+ }
2017
+ };
2018
+
2019
+ /** PrivateClass: Strophe.Request
2020
+ * _Private_ helper class that provides a cross implementation abstraction
2021
+ * for a BOSH related XMLHttpRequest.
2022
+ *
2023
+ * The Strophe.Request class is used internally to encapsulate BOSH request
2024
+ * information. It is not meant to be used from user's code.
2025
+ */
2026
+
2027
+ /** PrivateConstructor: Strophe.Request
2028
+ * Create and initialize a new Strophe.Request object.
2029
+ *
2030
+ * Parameters:
2031
+ * (XMLElement) elem - The XML data to be sent in the request.
2032
+ * (Function) func - The function that will be called when the
2033
+ * XMLHttpRequest readyState changes.
2034
+ * (Integer) rid - The BOSH rid attribute associated with this request.
2035
+ * (Integer) sends - The number of times this same request has been
2036
+ * sent.
2037
+ */
2038
+ Strophe.Request = function (elem, func, rid, sends)
2039
+ {
2040
+ this.id = ++Strophe._requestId;
2041
+ this.xmlData = elem;
2042
+ this.data = Strophe.serialize(elem);
2043
+ // save original function in case we need to make a new request
2044
+ // from this one.
2045
+ this.origFunc = func;
2046
+ this.func = func;
2047
+ this.rid = rid;
2048
+ this.date = NaN;
2049
+ this.sends = sends || 0;
2050
+ this.abort = false;
2051
+ this.dead = null;
2052
+ this.age = function () {
2053
+ if (!this.date) { return 0; }
2054
+ var now = new Date();
2055
+ return (now - this.date) / 1000;
2056
+ };
2057
+ this.timeDead = function () {
2058
+ if (!this.dead) { return 0; }
2059
+ var now = new Date();
2060
+ return (now - this.dead) / 1000;
2061
+ };
2062
+ this.xhr = this._newXHR();
2063
+ };
2064
+
2065
+ Strophe.Request.prototype = {
2066
+ /** PrivateFunction: getResponse
2067
+ * Get a response from the underlying XMLHttpRequest.
2068
+ *
2069
+ * This function attempts to get a response from the request and checks
2070
+ * for errors.
2071
+ *
2072
+ * Throws:
2073
+ * "parsererror" - A parser error occured.
2074
+ *
2075
+ * Returns:
2076
+ * The DOM element tree of the response.
2077
+ */
2078
+ getResponse: function ()
2079
+ {
2080
+ var node = null;
2081
+ if (this.xhr.responseXML && this.xhr.responseXML.documentElement) {
2082
+ node = this.xhr.responseXML.documentElement;
2083
+ if (node.tagName == "parsererror") {
2084
+ Strophe.error("invalid response received");
2085
+ Strophe.error("responseText: " + this.xhr.responseText);
2086
+ Strophe.error("responseXML: " +
2087
+ Strophe.serialize(this.xhr.responseXML));
2088
+ throw "parsererror";
2089
+ }
2090
+ } else if (this.xhr.responseText) {
2091
+ Strophe.error("invalid response received");
2092
+ Strophe.error("responseText: " + this.xhr.responseText);
2093
+ Strophe.error("responseXML: " +
2094
+ Strophe.serialize(this.xhr.responseXML));
2095
+ }
2096
+
2097
+ return node;
2098
+ },
2099
+
2100
+ /** PrivateFunction: _newXHR
2101
+ * _Private_ helper function to create XMLHttpRequests.
2102
+ *
2103
+ * This function creates XMLHttpRequests across all implementations.
2104
+ *
2105
+ * Returns:
2106
+ * A new XMLHttpRequest.
2107
+ */
2108
+ _newXHR: function ()
2109
+ {
2110
+ var xhr = null;
2111
+ if (window.XMLHttpRequest) {
2112
+ xhr = new XMLHttpRequest();
2113
+ if (xhr.overrideMimeType) {
2114
+ xhr.overrideMimeType("text/xml");
2115
+ }
2116
+ } else if (window.ActiveXObject) {
2117
+ xhr = new ActiveXObject("Microsoft.XMLHTTP");
2118
+ }
2119
+
2120
+ // use Function.bind() to prepend ourselves as an argument
2121
+ xhr.onreadystatechange = this.func.bind(null, this);
2122
+
2123
+ return xhr;
2124
+ }
2125
+ };
2126
+
2127
+ /** Class: Strophe.Connection
2128
+ * XMPP Connection manager.
2129
+ *
2130
+ * This class is the main part of Strophe. It manages a BOSH connection
2131
+ * to an XMPP server and dispatches events to the user callbacks as
2132
+ * data arrives. It supports SASL PLAIN, SASL DIGEST-MD5, and legacy
2133
+ * authentication.
2134
+ *
2135
+ * After creating a Strophe.Connection object, the user will typically
2136
+ * call connect() with a user supplied callback to handle connection level
2137
+ * events like authentication failure, disconnection, or connection
2138
+ * complete.
2139
+ *
2140
+ * The user will also have several event handlers defined by using
2141
+ * addHandler() and addTimedHandler(). These will allow the user code to
2142
+ * respond to interesting stanzas or do something periodically with the
2143
+ * connection. These handlers will be active once authentication is
2144
+ * finished.
2145
+ *
2146
+ * To send data to the connection, use send().
2147
+ */
2148
+
2149
+ /** Constructor: Strophe.Connection
2150
+ * Create and initialize a Strophe.Connection object.
2151
+ *
2152
+ * Parameters:
2153
+ * (String) service - The BOSH service URL.
2154
+ *
2155
+ * Returns:
2156
+ * A new Strophe.Connection object.
2157
+ */
2158
+ Strophe.Connection = function (service)
2159
+ {
2160
+ /* The path to the httpbind service. */
2161
+ this.service = service;
2162
+ /* The connected JID. */
2163
+ this.jid = "";
2164
+ /* the JIDs domain */
2165
+ this.domain = null;
2166
+ /* request id for body tags */
2167
+ this.rid = Math.floor(Math.random() * 4294967295);
2168
+ /* The current session ID. */
2169
+ this.sid = null;
2170
+ this.streamId = null;
2171
+ /* stream:features */
2172
+ this.features = null;
2173
+
2174
+ // SASL
2175
+ this._sasl_data = [];
2176
+ this.do_session = false;
2177
+ this.do_bind = false;
2178
+
2179
+ // handler lists
2180
+ this.timedHandlers = [];
2181
+ this.handlers = [];
2182
+ this.removeTimeds = [];
2183
+ this.removeHandlers = [];
2184
+ this.addTimeds = [];
2185
+ this.addHandlers = [];
2186
+
2187
+ this._authentication = {};
2188
+ this._idleTimeout = null;
2189
+ this._disconnectTimeout = null;
2190
+
2191
+ this.do_authentication = true;
2192
+ this.authenticated = false;
2193
+ this.disconnecting = false;
2194
+ this.connected = false;
2195
+
2196
+ this.errors = 0;
2197
+
2198
+ this.paused = false;
2199
+
2200
+ // default BOSH values
2201
+ this.hold = 1;
2202
+ this.wait = 60;
2203
+ this.window = 5;
2204
+
2205
+ this._data = [];
2206
+ this._requests = [];
2207
+ this._uniqueId = Math.round(Math.random() * 10000);
2208
+
2209
+ this._sasl_success_handler = null;
2210
+ this._sasl_failure_handler = null;
2211
+ this._sasl_challenge_handler = null;
2212
+
2213
+ // Max retries before disconnecting
2214
+ this.maxRetries = 5;
2215
+
2216
+ // setup onIdle callback every 1/10th of a second
2217
+ this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
2218
+
2219
+ // initialize plugins
2220
+ for (var k in Strophe._connectionPlugins) {
2221
+ if (Strophe._connectionPlugins.hasOwnProperty(k)) {
2222
+ var ptype = Strophe._connectionPlugins[k];
2223
+ // jslint complaints about the below line, but this is fine
2224
+ var F = function () {};
2225
+ F.prototype = ptype;
2226
+ this[k] = new F();
2227
+ this[k].init(this);
2228
+ }
2229
+ }
2230
+ };
2231
+
2232
+ Strophe.Connection.prototype = {
2233
+ /** Function: reset
2234
+ * Reset the connection.
2235
+ *
2236
+ * This function should be called after a connection is disconnected
2237
+ * before that connection is reused.
2238
+ */
2239
+ reset: function ()
2240
+ {
2241
+ this.rid = Math.floor(Math.random() * 4294967295);
2242
+
2243
+ this.sid = null;
2244
+ this.streamId = null;
2245
+
2246
+ // SASL
2247
+ this.do_session = false;
2248
+ this.do_bind = false;
2249
+
2250
+ // handler lists
2251
+ this.timedHandlers = [];
2252
+ this.handlers = [];
2253
+ this.removeTimeds = [];
2254
+ this.removeHandlers = [];
2255
+ this.addTimeds = [];
2256
+ this.addHandlers = [];
2257
+ this._authentication = {};
2258
+
2259
+ this.authenticated = false;
2260
+ this.disconnecting = false;
2261
+ this.connected = false;
2262
+
2263
+ this.errors = 0;
2264
+
2265
+ this._requests = [];
2266
+ this._uniqueId = Math.round(Math.random()*10000);
2267
+ },
2268
+
2269
+ /** Function: pause
2270
+ * Pause the request manager.
2271
+ *
2272
+ * This will prevent Strophe from sending any more requests to the
2273
+ * server. This is very useful for temporarily pausing while a lot
2274
+ * of send() calls are happening quickly. This causes Strophe to
2275
+ * send the data in a single request, saving many request trips.
2276
+ */
2277
+ pause: function ()
2278
+ {
2279
+ this.paused = true;
2280
+ },
2281
+
2282
+ /** Function: resume
2283
+ * Resume the request manager.
2284
+ *
2285
+ * This resumes after pause() has been called.
2286
+ */
2287
+ resume: function ()
2288
+ {
2289
+ this.paused = false;
2290
+ },
2291
+
2292
+ /** Function: getUniqueId
2293
+ * Generate a unique ID for use in <iq/> elements.
2294
+ *
2295
+ * All <iq/> stanzas are required to have unique id attributes. This
2296
+ * function makes creating these easy. Each connection instance has
2297
+ * a counter which starts from zero, and the value of this counter
2298
+ * plus a colon followed by the suffix becomes the unique id. If no
2299
+ * suffix is supplied, the counter is used as the unique id.
2300
+ *
2301
+ * Suffixes are used to make debugging easier when reading the stream
2302
+ * data, and their use is recommended. The counter resets to 0 for
2303
+ * every new connection for the same reason. For connections to the
2304
+ * same server that authenticate the same way, all the ids should be
2305
+ * the same, which makes it easy to see changes. This is useful for
2306
+ * automated testing as well.
2307
+ *
2308
+ * Parameters:
2309
+ * (String) suffix - A optional suffix to append to the id.
2310
+ *
2311
+ * Returns:
2312
+ * A unique string to be used for the id attribute.
2313
+ */
2314
+ getUniqueId: function (suffix)
2315
+ {
2316
+ if (typeof(suffix) == "string" || typeof(suffix) == "number") {
2317
+ return ++this._uniqueId + ":" + suffix;
2318
+ } else {
2319
+ return ++this._uniqueId + "";
2320
+ }
2321
+ },
2322
+
2323
+ /** Function: connect
2324
+ * Starts the connection process.
2325
+ *
2326
+ * As the connection process proceeds, the user supplied callback will
2327
+ * be triggered multiple times with status updates. The callback
2328
+ * should take two arguments - the status code and the error condition.
2329
+ *
2330
+ * The status code will be one of the values in the Strophe.Status
2331
+ * constants. The error condition will be one of the conditions
2332
+ * defined in RFC 3920 or the condition 'strophe-parsererror'.
2333
+ *
2334
+ * Please see XEP 124 for a more detailed explanation of the optional
2335
+ * parameters below.
2336
+ *
2337
+ * Parameters:
2338
+ * (String) jid - The user's JID. This may be a bare JID,
2339
+ * or a full JID. If a node is not supplied, SASL ANONYMOUS
2340
+ * authentication will be attempted.
2341
+ * (String) pass - The user's password.
2342
+ * (Function) callback - The connect callback function.
2343
+ * (Integer) wait - The optional HTTPBIND wait value. This is the
2344
+ * time the server will wait before returning an empty result for
2345
+ * a request. The default setting of 60 seconds is recommended.
2346
+ * Other settings will require tweaks to the Strophe.TIMEOUT value.
2347
+ * (Integer) hold - The optional HTTPBIND hold value. This is the
2348
+ * number of connections the server will hold at one time. This
2349
+ * should almost always be set to 1 (the default).
2350
+ * (String) route
2351
+ */
2352
+ connect: function (jid, pass, callback, wait, hold, route)
2353
+ {
2354
+ this.jid = jid;
2355
+ this.pass = pass;
2356
+ this.connect_callback = callback;
2357
+ this.disconnecting = false;
2358
+ this.connected = false;
2359
+ this.authenticated = false;
2360
+ this.errors = 0;
2361
+
2362
+ this.wait = wait || this.wait;
2363
+ this.hold = hold || this.hold;
2364
+
2365
+ // parse jid for domain and resource
2366
+ this.domain = this.domain || Strophe.getDomainFromJid(this.jid);
2367
+
2368
+ // build the body tag
2369
+ var body = this._buildBody().attrs({
2370
+ to: this.domain,
2371
+ "xml:lang": "en",
2372
+ wait: this.wait,
2373
+ hold: this.hold,
2374
+ content: "text/xml; charset=utf-8",
2375
+ ver: "1.6",
2376
+ "xmpp:version": "1.0",
2377
+ "xmlns:xmpp": Strophe.NS.BOSH
2378
+ });
2379
+
2380
+ if(route){
2381
+ body.attrs({
2382
+ route: route
2383
+ });
2384
+ }
2385
+
2386
+ this._changeConnectStatus(Strophe.Status.CONNECTING, null);
2387
+
2388
+ var _connect_cb = this._connect_callback || this._connect_cb;
2389
+ this._connect_callback = null;
2390
+
2391
+ this._requests.push(
2392
+ new Strophe.Request(body.tree(),
2393
+ this._onRequestStateChange.bind(
2394
+ this, _connect_cb.bind(this)),
2395
+ body.tree().getAttribute("rid")));
2396
+ this._throttledRequestHandler();
2397
+ },
2398
+
2399
+ /** Function: attach
2400
+ * Attach to an already created and authenticated BOSH session.
2401
+ *
2402
+ * This function is provided to allow Strophe to attach to BOSH
2403
+ * sessions which have been created externally, perhaps by a Web
2404
+ * application. This is often used to support auto-login type features
2405
+ * without putting user credentials into the page.
2406
+ *
2407
+ * Parameters:
2408
+ * (String) jid - The full JID that is bound by the session.
2409
+ * (String) sid - The SID of the BOSH session.
2410
+ * (String) rid - The current RID of the BOSH session. This RID
2411
+ * will be used by the next request.
2412
+ * (Function) callback The connect callback function.
2413
+ * (Integer) wait - The optional HTTPBIND wait value. This is the
2414
+ * time the server will wait before returning an empty result for
2415
+ * a request. The default setting of 60 seconds is recommended.
2416
+ * Other settings will require tweaks to the Strophe.TIMEOUT value.
2417
+ * (Integer) hold - The optional HTTPBIND hold value. This is the
2418
+ * number of connections the server will hold at one time. This
2419
+ * should almost always be set to 1 (the default).
2420
+ * (Integer) wind - The optional HTTBIND window value. This is the
2421
+ * allowed range of request ids that are valid. The default is 5.
2422
+ */
2423
+ attach: function (jid, sid, rid, callback, wait, hold, wind)
2424
+ {
2425
+ this.jid = jid;
2426
+ this.sid = sid;
2427
+ this.rid = rid;
2428
+ this.connect_callback = callback;
2429
+
2430
+ this.domain = Strophe.getDomainFromJid(this.jid);
2431
+
2432
+ this.authenticated = true;
2433
+ this.connected = true;
2434
+
2435
+ this.wait = wait || this.wait;
2436
+ this.hold = hold || this.hold;
2437
+ this.window = wind || this.window;
2438
+
2439
+ this._changeConnectStatus(Strophe.Status.ATTACHED, null);
2440
+ },
2441
+
2442
+ /** Function: xmlInput
2443
+ * User overrideable function that receives XML data coming into the
2444
+ * connection.
2445
+ *
2446
+ * The default function does nothing. User code can override this with
2447
+ * > Strophe.Connection.xmlInput = function (elem) {
2448
+ * > (user code)
2449
+ * > };
2450
+ *
2451
+ * Parameters:
2452
+ * (XMLElement) elem - The XML data received by the connection.
2453
+ */
2454
+ xmlInput: function (elem)
2455
+ {
2456
+ return;
2457
+ },
2458
+
2459
+ /** Function: xmlOutput
2460
+ * User overrideable function that receives XML data sent to the
2461
+ * connection.
2462
+ *
2463
+ * The default function does nothing. User code can override this with
2464
+ * > Strophe.Connection.xmlOutput = function (elem) {
2465
+ * > (user code)
2466
+ * > };
2467
+ *
2468
+ * Parameters:
2469
+ * (XMLElement) elem - The XMLdata sent by the connection.
2470
+ */
2471
+ xmlOutput: function (elem)
2472
+ {
2473
+ return;
2474
+ },
2475
+
2476
+ /** Function: rawInput
2477
+ * User overrideable function that receives raw data coming into the
2478
+ * connection.
2479
+ *
2480
+ * The default function does nothing. User code can override this with
2481
+ * > Strophe.Connection.rawInput = function (data) {
2482
+ * > (user code)
2483
+ * > };
2484
+ *
2485
+ * Parameters:
2486
+ * (String) data - The data received by the connection.
2487
+ */
2488
+ rawInput: function (data)
2489
+ {
2490
+ return;
2491
+ },
2492
+
2493
+ /** Function: rawOutput
2494
+ * User overrideable function that receives raw data sent to the
2495
+ * connection.
2496
+ *
2497
+ * The default function does nothing. User code can override this with
2498
+ * > Strophe.Connection.rawOutput = function (data) {
2499
+ * > (user code)
2500
+ * > };
2501
+ *
2502
+ * Parameters:
2503
+ * (String) data - The data sent by the connection.
2504
+ */
2505
+ rawOutput: function (data)
2506
+ {
2507
+ return;
2508
+ },
2509
+
2510
+ /** Function: send
2511
+ * Send a stanza.
2512
+ *
2513
+ * This function is called to push data onto the send queue to
2514
+ * go out over the wire. Whenever a request is sent to the BOSH
2515
+ * server, all pending data is sent and the queue is flushed.
2516
+ *
2517
+ * Parameters:
2518
+ * (XMLElement |
2519
+ * [XMLElement] |
2520
+ * Strophe.Builder) elem - The stanza to send.
2521
+ */
2522
+ send: function (elem)
2523
+ {
2524
+ if (elem === null) { return ; }
2525
+ if (typeof(elem.sort) === "function") {
2526
+ for (var i = 0; i < elem.length; i++) {
2527
+ this._queueData(elem[i]);
2528
+ }
2529
+ } else if (typeof(elem.tree) === "function") {
2530
+ this._queueData(elem.tree());
2531
+ } else {
2532
+ this._queueData(elem);
2533
+ }
2534
+
2535
+ this._throttledRequestHandler();
2536
+ clearTimeout(this._idleTimeout);
2537
+ this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
2538
+ },
2539
+
2540
+ /** Function: flush
2541
+ * Immediately send any pending outgoing data.
2542
+ *
2543
+ * Normally send() queues outgoing data until the next idle period
2544
+ * (100ms), which optimizes network use in the common cases when
2545
+ * several send()s are called in succession. flush() can be used to
2546
+ * immediately send all pending data.
2547
+ */
2548
+ flush: function ()
2549
+ {
2550
+ // cancel the pending idle period and run the idle function
2551
+ // immediately
2552
+ clearTimeout(this._idleTimeout);
2553
+ this._onIdle();
2554
+ },
2555
+
2556
+ /** Function: sendIQ
2557
+ * Helper function to send IQ stanzas.
2558
+ *
2559
+ * Parameters:
2560
+ * (XMLElement) elem - The stanza to send.
2561
+ * (Function) callback - The callback function for a successful request.
2562
+ * (Function) errback - The callback function for a failed or timed
2563
+ * out request. On timeout, the stanza will be null.
2564
+ * (Integer) timeout - The time specified in milliseconds for a
2565
+ * timeout to occur.
2566
+ *
2567
+ * Returns:
2568
+ * The id used to send the IQ.
2569
+ */
2570
+ sendIQ: function(elem, callback, errback, timeout) {
2571
+ var timeoutHandler = null;
2572
+ var that = this;
2573
+
2574
+ if (typeof(elem.tree) === "function") {
2575
+ elem = elem.tree();
2576
+ }
2577
+ var id = elem.getAttribute('id');
2578
+
2579
+ // inject id if not found
2580
+ if (!id) {
2581
+ id = this.getUniqueId("sendIQ");
2582
+ elem.setAttribute("id", id);
2583
+ }
2584
+
2585
+ var handler = this.addHandler(function (stanza) {
2586
+ // remove timeout handler if there is one
2587
+ if (timeoutHandler) {
2588
+ that.deleteTimedHandler(timeoutHandler);
2589
+ }
2590
+
2591
+ var iqtype = stanza.getAttribute('type');
2592
+ if (iqtype == 'result') {
2593
+ if (callback) {
2594
+ callback(stanza);
2595
+ }
2596
+ } else if (iqtype == 'error') {
2597
+ if (errback) {
2598
+ errback(stanza);
2599
+ }
2600
+ } else {
2601
+ throw {
2602
+ name: "StropheError",
2603
+ message: "Got bad IQ type of " + iqtype
2604
+ };
2605
+ }
2606
+ }, null, 'iq', null, id);
2607
+
2608
+ // if timeout specified, setup timeout handler.
2609
+ if (timeout) {
2610
+ timeoutHandler = this.addTimedHandler(timeout, function () {
2611
+ // get rid of normal handler
2612
+ that.deleteHandler(handler);
2613
+
2614
+ // call errback on timeout with null stanza
2615
+ if (errback) {
2616
+ errback(null);
2617
+ }
2618
+ return false;
2619
+ });
2620
+ }
2621
+
2622
+ this.send(elem);
2623
+
2624
+ return id;
2625
+ },
2626
+
2627
+ /** PrivateFunction: _queueData
2628
+ * Queue outgoing data for later sending. Also ensures that the data
2629
+ * is a DOMElement.
2630
+ */
2631
+ _queueData: function (element) {
2632
+ if (element === null ||
2633
+ !element.tagName ||
2634
+ !element.childNodes) {
2635
+ throw {
2636
+ name: "StropheError",
2637
+ message: "Cannot queue non-DOMElement."
2638
+ };
2639
+ }
2640
+
2641
+ this._data.push(element);
2642
+ },
2643
+
2644
+ /** PrivateFunction: _sendRestart
2645
+ * Send an xmpp:restart stanza.
2646
+ */
2647
+ _sendRestart: function ()
2648
+ {
2649
+ this._data.push("restart");
2650
+
2651
+ this._throttledRequestHandler();
2652
+ clearTimeout(this._idleTimeout);
2653
+ this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
2654
+ },
2655
+
2656
+ /** Function: addTimedHandler
2657
+ * Add a timed handler to the connection.
2658
+ *
2659
+ * This function adds a timed handler. The provided handler will
2660
+ * be called every period milliseconds until it returns false,
2661
+ * the connection is terminated, or the handler is removed. Handlers
2662
+ * that wish to continue being invoked should return true.
2663
+ *
2664
+ * Because of method binding it is necessary to save the result of
2665
+ * this function if you wish to remove a handler with
2666
+ * deleteTimedHandler().
2667
+ *
2668
+ * Note that user handlers are not active until authentication is
2669
+ * successful.
2670
+ *
2671
+ * Parameters:
2672
+ * (Integer) period - The period of the handler.
2673
+ * (Function) handler - The callback function.
2674
+ *
2675
+ * Returns:
2676
+ * A reference to the handler that can be used to remove it.
2677
+ */
2678
+ addTimedHandler: function (period, handler)
2679
+ {
2680
+ var thand = new Strophe.TimedHandler(period, handler);
2681
+ this.addTimeds.push(thand);
2682
+ return thand;
2683
+ },
2684
+
2685
+ /** Function: deleteTimedHandler
2686
+ * Delete a timed handler for a connection.
2687
+ *
2688
+ * This function removes a timed handler from the connection. The
2689
+ * handRef parameter is *not* the function passed to addTimedHandler(),
2690
+ * but is the reference returned from addTimedHandler().
2691
+ *
2692
+ * Parameters:
2693
+ * (Strophe.TimedHandler) handRef - The handler reference.
2694
+ */
2695
+ deleteTimedHandler: function (handRef)
2696
+ {
2697
+ // this must be done in the Idle loop so that we don't change
2698
+ // the handlers during iteration
2699
+ this.removeTimeds.push(handRef);
2700
+ },
2701
+
2702
+ /** Function: addHandler
2703
+ * Add a stanza handler for the connection.
2704
+ *
2705
+ * This function adds a stanza handler to the connection. The
2706
+ * handler callback will be called for any stanza that matches
2707
+ * the parameters. Note that if multiple parameters are supplied,
2708
+ * they must all match for the handler to be invoked.
2709
+ *
2710
+ * The handler will receive the stanza that triggered it as its argument.
2711
+ * The handler should return true if it is to be invoked again;
2712
+ * returning false will remove the handler after it returns.
2713
+ *
2714
+ * As a convenience, the ns parameters applies to the top level element
2715
+ * and also any of its immediate children. This is primarily to make
2716
+ * matching /iq/query elements easy.
2717
+ *
2718
+ * The options argument contains handler matching flags that affect how
2719
+ * matches are determined. Currently the only flag is matchBare (a
2720
+ * boolean). When matchBare is true, the from parameter and the from
2721
+ * attribute on the stanza will be matched as bare JIDs instead of
2722
+ * full JIDs. To use this, pass {matchBare: true} as the value of
2723
+ * options. The default value for matchBare is false.
2724
+ *
2725
+ * The return value should be saved if you wish to remove the handler
2726
+ * with deleteHandler().
2727
+ *
2728
+ * Parameters:
2729
+ * (Function) handler - The user callback.
2730
+ * (String) ns - The namespace to match.
2731
+ * (String) name - The stanza name to match.
2732
+ * (String) type - The stanza type attribute to match.
2733
+ * (String) id - The stanza id attribute to match.
2734
+ * (String) from - The stanza from attribute to match.
2735
+ * (String) options - The handler options
2736
+ *
2737
+ * Returns:
2738
+ * A reference to the handler that can be used to remove it.
2739
+ */
2740
+ addHandler: function (handler, ns, name, type, id, from, options)
2741
+ {
2742
+
2743
+ var hand = new Strophe.Handler(handler, ns, name, type, id, from, options);
2744
+ this.addHandlers.push(hand);
2745
+ return hand;
2746
+ },
2747
+
2748
+ /** Function: deleteHandler
2749
+ * Delete a stanza handler for a connection.
2750
+ *
2751
+ * This function removes a stanza handler from the connection. The
2752
+ * handRef parameter is *not* the function passed to addHandler(),
2753
+ * but is the reference returned from addHandler().
2754
+ *
2755
+ * Parameters:
2756
+ * (Strophe.Handler) handRef - The handler reference.
2757
+ */
2758
+ deleteHandler: function (handRef)
2759
+ {
2760
+ // this must be done in the Idle loop so that we don't change
2761
+ // the handlers during iteration
2762
+ this.removeHandlers.push(handRef);
2763
+ },
2764
+
2765
+ /** Function: disconnect
2766
+ * Start the graceful disconnection process.
2767
+ *
2768
+ * This function starts the disconnection process. This process starts
2769
+ * by sending unavailable presence and sending BOSH body of type
2770
+ * terminate. A timeout handler makes sure that disconnection happens
2771
+ * even if the BOSH server does not respond.
2772
+ *
2773
+ * The user supplied connection callback will be notified of the
2774
+ * progress as this process happens.
2775
+ *
2776
+ * Parameters:
2777
+ * (String) reason - The reason the disconnect is occuring.
2778
+ */
2779
+ disconnect: function (reason)
2780
+ {
2781
+ this._changeConnectStatus(Strophe.Status.DISCONNECTING, reason);
2782
+
2783
+ Strophe.info("Disconnect was called because: " + reason);
2784
+ if (this.connected) {
2785
+ // setup timeout handler
2786
+ this._disconnectTimeout = this._addSysTimedHandler(
2787
+ 8000, this._onDisconnectTimeout.bind(this));
2788
+ this._sendTerminate();
2789
+ }
2790
+ },
2791
+
2792
+ /** PrivateFunction: _changeConnectStatus
2793
+ * _Private_ helper function that makes sure plugins and the user's
2794
+ * callback are notified of connection status changes.
2795
+ *
2796
+ * Parameters:
2797
+ * (Integer) status - the new connection status, one of the values
2798
+ * in Strophe.Status
2799
+ * (String) condition - the error condition or null
2800
+ */
2801
+ _changeConnectStatus: function (status, condition)
2802
+ {
2803
+ // notify all plugins listening for status changes
2804
+ for (var k in Strophe._connectionPlugins) {
2805
+ if (Strophe._connectionPlugins.hasOwnProperty(k)) {
2806
+ var plugin = this[k];
2807
+ if (plugin.statusChanged) {
2808
+ try {
2809
+ plugin.statusChanged(status, condition);
2810
+ } catch (err) {
2811
+ Strophe.error("" + k + " plugin caused an exception " +
2812
+ "changing status: " + err);
2813
+ }
2814
+ }
2815
+ }
2816
+ }
2817
+
2818
+ // notify the user's callback
2819
+ if (this.connect_callback) {
2820
+ try {
2821
+ this.connect_callback(status, condition);
2822
+ } catch (e) {
2823
+ Strophe.error("User connection callback caused an " +
2824
+ "exception: " + e);
2825
+ }
2826
+ }
2827
+ },
2828
+
2829
+ /** PrivateFunction: _buildBody
2830
+ * _Private_ helper function to generate the <body/> wrapper for BOSH.
2831
+ *
2832
+ * Returns:
2833
+ * A Strophe.Builder with a <body/> element.
2834
+ */
2835
+ _buildBody: function ()
2836
+ {
2837
+ var bodyWrap = $build('body', {
2838
+ rid: this.rid++,
2839
+ xmlns: Strophe.NS.HTTPBIND
2840
+ });
2841
+
2842
+ if (this.sid !== null) {
2843
+ bodyWrap.attrs({sid: this.sid});
2844
+ }
2845
+
2846
+ return bodyWrap;
2847
+ },
2848
+
2849
+ /** PrivateFunction: _removeRequest
2850
+ * _Private_ function to remove a request from the queue.
2851
+ *
2852
+ * Parameters:
2853
+ * (Strophe.Request) req - The request to remove.
2854
+ */
2855
+ _removeRequest: function (req)
2856
+ {
2857
+ Strophe.debug("removing request");
2858
+
2859
+ var i;
2860
+ for (i = this._requests.length - 1; i >= 0; i--) {
2861
+ if (req == this._requests[i]) {
2862
+ this._requests.splice(i, 1);
2863
+ }
2864
+ }
2865
+
2866
+ // IE6 fails on setting to null, so set to empty function
2867
+ req.xhr.onreadystatechange = function () {};
2868
+
2869
+ this._throttledRequestHandler();
2870
+ },
2871
+
2872
+ /** PrivateFunction: _restartRequest
2873
+ * _Private_ function to restart a request that is presumed dead.
2874
+ *
2875
+ * Parameters:
2876
+ * (Integer) i - The index of the request in the queue.
2877
+ */
2878
+ _restartRequest: function (i)
2879
+ {
2880
+ var req = this._requests[i];
2881
+ if (req.dead === null) {
2882
+ req.dead = new Date();
2883
+ }
2884
+
2885
+ this._processRequest(i);
2886
+ },
2887
+
2888
+ /** PrivateFunction: _processRequest
2889
+ * _Private_ function to process a request in the queue.
2890
+ *
2891
+ * This function takes requests off the queue and sends them and
2892
+ * restarts dead requests.
2893
+ *
2894
+ * Parameters:
2895
+ * (Integer) i - The index of the request in the queue.
2896
+ */
2897
+ _processRequest: function (i)
2898
+ {
2899
+ var req = this._requests[i];
2900
+ var reqStatus = -1;
2901
+
2902
+ try {
2903
+ if (req.xhr.readyState == 4) {
2904
+ reqStatus = req.xhr.status;
2905
+ }
2906
+ } catch (e) {
2907
+ Strophe.error("caught an error in _requests[" + i +
2908
+ "], reqStatus: " + reqStatus);
2909
+ }
2910
+
2911
+ if (typeof(reqStatus) == "undefined") {
2912
+ reqStatus = -1;
2913
+ }
2914
+
2915
+ // make sure we limit the number of retries
2916
+ if (req.sends > this.maxRetries) {
2917
+ this._onDisconnectTimeout();
2918
+ return;
2919
+ }
2920
+
2921
+ var time_elapsed = req.age();
2922
+ var primaryTimeout = (!isNaN(time_elapsed) &&
2923
+ time_elapsed > Math.floor(Strophe.TIMEOUT * this.wait));
2924
+ var secondaryTimeout = (req.dead !== null &&
2925
+ req.timeDead() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait));
2926
+ var requestCompletedWithServerError = (req.xhr.readyState == 4 &&
2927
+ (reqStatus < 1 ||
2928
+ reqStatus >= 500));
2929
+ if (primaryTimeout || secondaryTimeout ||
2930
+ requestCompletedWithServerError) {
2931
+ if (secondaryTimeout) {
2932
+ Strophe.error("Request " +
2933
+ this._requests[i].id +
2934
+ " timed out (secondary), restarting");
2935
+ }
2936
+ req.abort = true;
2937
+ req.xhr.abort();
2938
+ // setting to null fails on IE6, so set to empty function
2939
+ req.xhr.onreadystatechange = function () {};
2940
+ this._requests[i] = new Strophe.Request(req.xmlData,
2941
+ req.origFunc,
2942
+ req.rid,
2943
+ req.sends);
2944
+ req = this._requests[i];
2945
+ }
2946
+
2947
+ if (req.xhr.readyState === 0) {
2948
+ Strophe.debug("request id " + req.id +
2949
+ "." + req.sends + " posting");
2950
+
2951
+ try {
2952
+ req.xhr.open("POST", this.service, true);
2953
+ } catch (e2) {
2954
+ Strophe.error("XHR open failed.");
2955
+ if (!this.connected) {
2956
+ this._changeConnectStatus(Strophe.Status.CONNFAIL,
2957
+ "bad-service");
2958
+ }
2959
+ this.disconnect();
2960
+ return;
2961
+ }
2962
+
2963
+ // Fires the XHR request -- may be invoked immediately
2964
+ // or on a gradually expanding retry window for reconnects
2965
+ var sendFunc = function () {
2966
+ req.date = new Date();
2967
+ req.xhr.send(req.data);
2968
+ };
2969
+
2970
+ // Implement progressive backoff for reconnects --
2971
+ // First retry (send == 1) should also be instantaneous
2972
+ if (req.sends > 1) {
2973
+ // Using a cube of the retry number creates a nicely
2974
+ // expanding retry window
2975
+ var backoff = Math.min(Math.floor(Strophe.TIMEOUT * this.wait),
2976
+ Math.pow(req.sends, 3)) * 1000;
2977
+ setTimeout(sendFunc, backoff);
2978
+ } else {
2979
+ sendFunc();
2980
+ }
2981
+
2982
+ req.sends++;
2983
+
2984
+ if (this.xmlOutput !== Strophe.Connection.prototype.xmlOutput) {
2985
+ this.xmlOutput(req.xmlData);
2986
+ }
2987
+ if (this.rawOutput !== Strophe.Connection.prototype.rawOutput) {
2988
+ this.rawOutput(req.data);
2989
+ }
2990
+ } else {
2991
+ Strophe.debug("_processRequest: " +
2992
+ (i === 0 ? "first" : "second") +
2993
+ " request has readyState of " +
2994
+ req.xhr.readyState);
2995
+ }
2996
+ },
2997
+
2998
+ /** PrivateFunction: _throttledRequestHandler
2999
+ * _Private_ function to throttle requests to the connection window.
3000
+ *
3001
+ * This function makes sure we don't send requests so fast that the
3002
+ * request ids overflow the connection window in the case that one
3003
+ * request died.
3004
+ */
3005
+ _throttledRequestHandler: function ()
3006
+ {
3007
+ if (!this._requests) {
3008
+ Strophe.debug("_throttledRequestHandler called with " +
3009
+ "undefined requests");
3010
+ } else {
3011
+ Strophe.debug("_throttledRequestHandler called with " +
3012
+ this._requests.length + " requests");
3013
+ }
3014
+
3015
+ if (!this._requests || this._requests.length === 0) {
3016
+ return;
3017
+ }
3018
+
3019
+ if (this._requests.length > 0) {
3020
+ this._processRequest(0);
3021
+ }
3022
+
3023
+ if (this._requests.length > 1 &&
3024
+ Math.abs(this._requests[0].rid -
3025
+ this._requests[1].rid) < this.window) {
3026
+ this._processRequest(1);
3027
+ }
3028
+ },
3029
+
3030
+ /** PrivateFunction: _onRequestStateChange
3031
+ * _Private_ handler for Strophe.Request state changes.
3032
+ *
3033
+ * This function is called when the XMLHttpRequest readyState changes.
3034
+ * It contains a lot of error handling logic for the many ways that
3035
+ * requests can fail, and calls the request callback when requests
3036
+ * succeed.
3037
+ *
3038
+ * Parameters:
3039
+ * (Function) func - The handler for the request.
3040
+ * (Strophe.Request) req - The request that is changing readyState.
3041
+ */
3042
+ _onRequestStateChange: function (func, req)
3043
+ {
3044
+ Strophe.debug("request id " + req.id +
3045
+ "." + req.sends + " state changed to " +
3046
+ req.xhr.readyState);
3047
+
3048
+ if (req.abort) {
3049
+ req.abort = false;
3050
+ return;
3051
+ }
3052
+
3053
+ // request complete
3054
+ var reqStatus;
3055
+ if (req.xhr.readyState == 4) {
3056
+ reqStatus = 0;
3057
+ try {
3058
+ reqStatus = req.xhr.status;
3059
+ } catch (e) {
3060
+ // ignore errors from undefined status attribute. works
3061
+ // around a browser bug
3062
+ }
3063
+
3064
+ if (typeof(reqStatus) == "undefined") {
3065
+ reqStatus = 0;
3066
+ }
3067
+
3068
+ if (this.disconnecting) {
3069
+ if (reqStatus >= 400) {
3070
+ this._hitError(reqStatus);
3071
+ return;
3072
+ }
3073
+ }
3074
+
3075
+ var reqIs0 = (this._requests[0] == req);
3076
+ var reqIs1 = (this._requests[1] == req);
3077
+
3078
+ if ((reqStatus > 0 && reqStatus < 500) || req.sends > 5) {
3079
+ // remove from internal queue
3080
+ this._removeRequest(req);
3081
+ Strophe.debug("request id " +
3082
+ req.id +
3083
+ " should now be removed");
3084
+ }
3085
+
3086
+ // request succeeded
3087
+ if (reqStatus == 200) {
3088
+ // if request 1 finished, or request 0 finished and request
3089
+ // 1 is over Strophe.SECONDARY_TIMEOUT seconds old, we need to
3090
+ // restart the other - both will be in the first spot, as the
3091
+ // completed request has been removed from the queue already
3092
+ if (reqIs1 ||
3093
+ (reqIs0 && this._requests.length > 0 &&
3094
+ this._requests[0].age() > Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait))) {
3095
+ this._restartRequest(0);
3096
+ }
3097
+ // call handler
3098
+ Strophe.debug("request id " +
3099
+ req.id + "." +
3100
+ req.sends + " got 200");
3101
+ func(req);
3102
+ this.errors = 0;
3103
+ } else {
3104
+ Strophe.error("request id " +
3105
+ req.id + "." +
3106
+ req.sends + " error " + reqStatus +
3107
+ " happened");
3108
+ if (reqStatus === 0 ||
3109
+ (reqStatus >= 400 && reqStatus < 600) ||
3110
+ reqStatus >= 12000) {
3111
+ this._hitError(reqStatus);
3112
+ if (reqStatus >= 400 && reqStatus < 500) {
3113
+ this._changeConnectStatus(Strophe.Status.DISCONNECTING,
3114
+ null);
3115
+ this._doDisconnect();
3116
+ }
3117
+ }
3118
+ }
3119
+
3120
+ if (!((reqStatus > 0 && reqStatus < 500) ||
3121
+ req.sends > 5)) {
3122
+ this._throttledRequestHandler();
3123
+ }
3124
+ }
3125
+ },
3126
+
3127
+ /** PrivateFunction: _hitError
3128
+ * _Private_ function to handle the error count.
3129
+ *
3130
+ * Requests are resent automatically until their error count reaches
3131
+ * 5. Each time an error is encountered, this function is called to
3132
+ * increment the count and disconnect if the count is too high.
3133
+ *
3134
+ * Parameters:
3135
+ * (Integer) reqStatus - The request status.
3136
+ */
3137
+ _hitError: function (reqStatus)
3138
+ {
3139
+ this.errors++;
3140
+ Strophe.warn("request errored, status: " + reqStatus +
3141
+ ", number of errors: " + this.errors);
3142
+ if (this.errors > 4) {
3143
+ this._onDisconnectTimeout();
3144
+ }
3145
+ },
3146
+
3147
+ /** PrivateFunction: _doDisconnect
3148
+ * _Private_ function to disconnect.
3149
+ *
3150
+ * This is the last piece of the disconnection logic. This resets the
3151
+ * connection and alerts the user's connection callback.
3152
+ */
3153
+ _doDisconnect: function ()
3154
+ {
3155
+ Strophe.info("_doDisconnect was called");
3156
+ this.authenticated = false;
3157
+ this.disconnecting = false;
3158
+ this.sid = null;
3159
+ this.streamId = null;
3160
+ this.rid = Math.floor(Math.random() * 4294967295);
3161
+
3162
+ // tell the parent we disconnected
3163
+ if (this.connected) {
3164
+ this._changeConnectStatus(Strophe.Status.DISCONNECTED, null);
3165
+ this.connected = false;
3166
+ }
3167
+
3168
+ // delete handlers
3169
+ this.handlers = [];
3170
+ this.timedHandlers = [];
3171
+ this.removeTimeds = [];
3172
+ this.removeHandlers = [];
3173
+ this.addTimeds = [];
3174
+ this.addHandlers = [];
3175
+ },
3176
+
3177
+ /** PrivateFunction: _dataRecv
3178
+ * _Private_ handler to processes incoming data from the the connection.
3179
+ *
3180
+ * Except for _connect_cb handling the initial connection request,
3181
+ * this function handles the incoming data for all requests. This
3182
+ * function also fires stanza handlers that match each incoming
3183
+ * stanza.
3184
+ *
3185
+ * Parameters:
3186
+ * (Strophe.Request) req - The request that has data ready.
3187
+ */
3188
+ _dataRecv: function (req)
3189
+ {
3190
+ try {
3191
+ var elem = req.getResponse();
3192
+ } catch (e) {
3193
+ if (e != "parsererror") { throw e; }
3194
+ this.disconnect("strophe-parsererror");
3195
+ }
3196
+ if (elem === null) { return; }
3197
+
3198
+ if (this.xmlInput !== Strophe.Connection.prototype.xmlInput) {
3199
+ this.xmlInput(elem);
3200
+ }
3201
+ if (this.rawInput !== Strophe.Connection.prototype.rawInput) {
3202
+ this.rawInput(Strophe.serialize(elem));
3203
+ }
3204
+
3205
+ // remove handlers scheduled for deletion
3206
+ var i, hand;
3207
+ while (this.removeHandlers.length > 0) {
3208
+ hand = this.removeHandlers.pop();
3209
+ i = this.handlers.indexOf(hand);
3210
+ if (i >= 0) {
3211
+ this.handlers.splice(i, 1);
3212
+ }
3213
+ }
3214
+
3215
+ // add handlers scheduled for addition
3216
+ while (this.addHandlers.length > 0) {
3217
+ this.handlers.push(this.addHandlers.pop());
3218
+ }
3219
+
3220
+ // handle graceful disconnect
3221
+ if (this.disconnecting && this._requests.length === 0) {
3222
+ this.deleteTimedHandler(this._disconnectTimeout);
3223
+ this._disconnectTimeout = null;
3224
+ this._doDisconnect();
3225
+ return;
3226
+ }
3227
+
3228
+ var typ = elem.getAttribute("type");
3229
+ var cond, conflict;
3230
+ if (typ !== null && typ == "terminate") {
3231
+ // Don't process stanzas that come in after disconnect
3232
+ if (this.disconnecting) {
3233
+ return;
3234
+ }
3235
+
3236
+ // an error occurred
3237
+ cond = elem.getAttribute("condition");
3238
+ conflict = elem.getElementsByTagName("conflict");
3239
+ if (cond !== null) {
3240
+ if (cond == "remote-stream-error" && conflict.length > 0) {
3241
+ cond = "conflict";
3242
+ }
3243
+ this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
3244
+ } else {
3245
+ this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
3246
+ }
3247
+ this.disconnect();
3248
+ return;
3249
+ }
3250
+
3251
+ // send each incoming stanza through the handler chain
3252
+ var that = this;
3253
+ Strophe.forEachChild(elem, null, function (child) {
3254
+ var i, newList;
3255
+ // process handlers
3256
+ newList = that.handlers;
3257
+ that.handlers = [];
3258
+ for (i = 0; i < newList.length; i++) {
3259
+ var hand = newList[i];
3260
+ // encapsulate 'handler.run' not to lose the whole handler list if
3261
+ // one of the handlers throws an exception
3262
+ try {
3263
+ if (hand.isMatch(child) &&
3264
+ (that.authenticated || !hand.user)) {
3265
+ if (hand.run(child)) {
3266
+ that.handlers.push(hand);
3267
+ }
3268
+ } else {
3269
+ that.handlers.push(hand);
3270
+ }
3271
+ } catch(e) {
3272
+ //if the handler throws an exception, we consider it as false
3273
+ }
3274
+ }
3275
+ });
3276
+ },
3277
+
3278
+ /** PrivateFunction: _sendTerminate
3279
+ * _Private_ function to send initial disconnect sequence.
3280
+ *
3281
+ * This is the first step in a graceful disconnect. It sends
3282
+ * the BOSH server a terminate body and includes an unavailable
3283
+ * presence if authentication has completed.
3284
+ */
3285
+ _sendTerminate: function ()
3286
+ {
3287
+ Strophe.info("_sendTerminate was called");
3288
+ var body = this._buildBody().attrs({type: "terminate"});
3289
+
3290
+ if (this.authenticated) {
3291
+ body.c('presence', {
3292
+ xmlns: Strophe.NS.CLIENT,
3293
+ type: 'unavailable'
3294
+ });
3295
+ }
3296
+
3297
+ this.disconnecting = true;
3298
+
3299
+ var req = new Strophe.Request(body.tree(),
3300
+ this._onRequestStateChange.bind(
3301
+ this, this._dataRecv.bind(this)),
3302
+ body.tree().getAttribute("rid"));
3303
+
3304
+ this._requests.push(req);
3305
+ this._throttledRequestHandler();
3306
+ },
3307
+
3308
+ /** PrivateFunction: _connect_cb
3309
+ * _Private_ handler for initial connection request.
3310
+ *
3311
+ * This handler is used to process the initial connection request
3312
+ * response from the BOSH server. It is used to set up authentication
3313
+ * handlers and start the authentication process.
3314
+ *
3315
+ * SASL authentication will be attempted if available, otherwise
3316
+ * the code will fall back to legacy authentication.
3317
+ *
3318
+ * Parameters:
3319
+ * (Strophe.Request) req - The current request.
3320
+ * (Function) _callback - low level (xmpp) connect callback function.
3321
+ * Useful for plugins with their own xmpp connect callback (when their)
3322
+ * want to do something special).
3323
+ */
3324
+ _connect_cb: function (req, _callback)
3325
+ {
3326
+ Strophe.info("_connect_cb was called");
3327
+
3328
+ this.connected = true;
3329
+ var bodyWrap = req.getResponse();
3330
+ if (!bodyWrap) { return; }
3331
+
3332
+ if (this.xmlInput !== Strophe.Connection.prototype.xmlInput) {
3333
+ this.xmlInput(bodyWrap);
3334
+ }
3335
+ if (this.rawInput !== Strophe.Connection.prototype.rawInput) {
3336
+ this.rawInput(Strophe.serialize(bodyWrap));
3337
+ }
3338
+
3339
+ var typ = bodyWrap.getAttribute("type");
3340
+ var cond, conflict;
3341
+ if (typ !== null && typ == "terminate") {
3342
+ // an error occurred
3343
+ cond = bodyWrap.getAttribute("condition");
3344
+ conflict = bodyWrap.getElementsByTagName("conflict");
3345
+ if (cond !== null) {
3346
+ if (cond == "remote-stream-error" && conflict.length > 0) {
3347
+ cond = "conflict";
3348
+ }
3349
+ this._changeConnectStatus(Strophe.Status.CONNFAIL, cond);
3350
+ } else {
3351
+ this._changeConnectStatus(Strophe.Status.CONNFAIL, "unknown");
3352
+ }
3353
+ return;
3354
+ }
3355
+
3356
+ // check to make sure we don't overwrite these if _connect_cb is
3357
+ // called multiple times in the case of missing stream:features
3358
+ if (!this.sid) {
3359
+ this.sid = bodyWrap.getAttribute("sid");
3360
+ }
3361
+ if (!this.stream_id) {
3362
+ this.stream_id = bodyWrap.getAttribute("authid");
3363
+ }
3364
+ var wind = bodyWrap.getAttribute('requests');
3365
+ if (wind) { this.window = parseInt(wind, 10); }
3366
+ var hold = bodyWrap.getAttribute('hold');
3367
+ if (hold) { this.hold = parseInt(hold, 10); }
3368
+ var wait = bodyWrap.getAttribute('wait');
3369
+ if (wait) { this.wait = parseInt(wait, 10); }
3370
+
3371
+ this._authentication.sasl_scram_sha1 = false;
3372
+ this._authentication.sasl_plain = false;
3373
+ this._authentication.sasl_digest_md5 = false;
3374
+ this._authentication.sasl_anonymous = false;
3375
+ this._authentication.legacy_auth = false;
3376
+
3377
+
3378
+ // Check for the stream:features tag
3379
+ var hasFeatures = bodyWrap.getElementsByTagName("stream:features").length > 0;
3380
+ if (!hasFeatures) {
3381
+ hasFeatures = bodyWrap.getElementsByTagName("features").length > 0;
3382
+ }
3383
+ var mechanisms = bodyWrap.getElementsByTagName("mechanism");
3384
+ var i, mech, auth_str, hashed_auth_str,
3385
+ found_authentication = false;
3386
+ if (hasFeatures && mechanisms.length > 0) {
3387
+ var missmatchedmechs = 0;
3388
+ for (i = 0; i < mechanisms.length; i++) {
3389
+ mech = Strophe.getText(mechanisms[i]);
3390
+ if (mech == 'SCRAM-SHA-1') {
3391
+ this._authentication.sasl_scram_sha1 = true;
3392
+ } else if (mech == 'DIGEST-MD5') {
3393
+ this._authentication.sasl_digest_md5 = true;
3394
+ } else if (mech == 'PLAIN') {
3395
+ this._authentication.sasl_plain = true;
3396
+ } else if (mech == 'ANONYMOUS') {
3397
+ this._authentication.sasl_anonymous = true;
3398
+ } else missmatchedmechs++;
3399
+ }
3400
+
3401
+ this._authentication.legacy_auth =
3402
+ bodyWrap.getElementsByTagName("auth").length > 0;
3403
+
3404
+ found_authentication =
3405
+ this._authentication.legacy_auth ||
3406
+ missmatchedmechs < mechanisms.length;
3407
+ }
3408
+ if (!found_authentication) {
3409
+ _callback = _callback || this._connect_cb;
3410
+ // we didn't get stream:features yet, so we need wait for it
3411
+ // by sending a blank poll request
3412
+ var body = this._buildBody();
3413
+ this._requests.push(
3414
+ new Strophe.Request(body.tree(),
3415
+ this._onRequestStateChange.bind(
3416
+ this, _callback.bind(this)),
3417
+ body.tree().getAttribute("rid")));
3418
+ this._throttledRequestHandler();
3419
+ return;
3420
+ }
3421
+ if (this.do_authentication !== false)
3422
+ this.authenticate();
3423
+ },
3424
+
3425
+ /** Function: authenticate
3426
+ * Set up authentication
3427
+ *
3428
+ * Contiunues the initial connection request by setting up authentication
3429
+ * handlers and start the authentication process.
3430
+ *
3431
+ * SASL authentication will be attempted if available, otherwise
3432
+ * the code will fall back to legacy authentication.
3433
+ *
3434
+ */
3435
+ authenticate: function ()
3436
+ {
3437
+ if (Strophe.getNodeFromJid(this.jid) === null &&
3438
+ this._authentication.sasl_anonymous) {
3439
+ this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
3440
+ this._sasl_success_handler = this._addSysHandler(
3441
+ this._sasl_success_cb.bind(this), null,
3442
+ "success", null, null);
3443
+ this._sasl_failure_handler = this._addSysHandler(
3444
+ this._sasl_failure_cb.bind(this), null,
3445
+ "failure", null, null);
3446
+
3447
+ this.send($build("auth", {
3448
+ xmlns: Strophe.NS.SASL,
3449
+ mechanism: "ANONYMOUS"
3450
+ }).tree());
3451
+ } else if (Strophe.getNodeFromJid(this.jid) === null) {
3452
+ // we don't have a node, which is required for non-anonymous
3453
+ // client connections
3454
+ this._changeConnectStatus(Strophe.Status.CONNFAIL,
3455
+ 'x-strophe-bad-non-anon-jid');
3456
+ this.disconnect();
3457
+ } else if (this._authentication.sasl_scram_sha1) {
3458
+ var cnonce = MD5.hexdigest(Math.random() * 1234567890);
3459
+
3460
+ var auth_str = "n=" + Strophe.getNodeFromJid(this.jid);
3461
+ auth_str += ",r=";
3462
+ auth_str += cnonce;
3463
+
3464
+ this._sasl_data["cnonce"] = cnonce;
3465
+ this._sasl_data["client-first-message-bare"] = auth_str;
3466
+
3467
+ auth_str = "n,," + auth_str;
3468
+
3469
+ this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
3470
+ this._sasl_challenge_handler = this._addSysHandler(
3471
+ this._sasl_scram_challenge_cb.bind(this), null,
3472
+ "challenge", null, null);
3473
+ this._sasl_failure_handler = this._addSysHandler(
3474
+ this._sasl_failure_cb.bind(this), null,
3475
+ "failure", null, null);
3476
+
3477
+ this.send($build("auth", {
3478
+ xmlns: Strophe.NS.SASL,
3479
+ mechanism: "SCRAM-SHA-1"
3480
+ }).t(Base64.encode(auth_str)).tree());
3481
+ } else if (this._authentication.sasl_digest_md5) {
3482
+ this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
3483
+ this._sasl_challenge_handler = this._addSysHandler(
3484
+ this._sasl_digest_challenge1_cb.bind(this), null,
3485
+ "challenge", null, null);
3486
+ this._sasl_failure_handler = this._addSysHandler(
3487
+ this._sasl_failure_cb.bind(this), null,
3488
+ "failure", null, null);
3489
+
3490
+ this.send($build("auth", {
3491
+ xmlns: Strophe.NS.SASL,
3492
+ mechanism: "DIGEST-MD5"
3493
+ }).tree());
3494
+ } else if (this._authentication.sasl_plain) {
3495
+ // Build the plain auth string (barejid null
3496
+ // username null password) and base 64 encoded.
3497
+ auth_str = Strophe.getBareJidFromJid(this.jid);
3498
+ auth_str = auth_str + "\u0000";
3499
+ auth_str = auth_str + Strophe.getNodeFromJid(this.jid);
3500
+ auth_str = auth_str + "\u0000";
3501
+ auth_str = auth_str + this.pass;
3502
+
3503
+ this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
3504
+ this._sasl_success_handler = this._addSysHandler(
3505
+ this._sasl_success_cb.bind(this), null,
3506
+ "success", null, null);
3507
+ this._sasl_failure_handler = this._addSysHandler(
3508
+ this._sasl_failure_cb.bind(this), null,
3509
+ "failure", null, null);
3510
+
3511
+ hashed_auth_str = Base64.encode(auth_str);
3512
+ this.send($build("auth", {
3513
+ xmlns: Strophe.NS.SASL,
3514
+ mechanism: "PLAIN"
3515
+ }).t(hashed_auth_str).tree());
3516
+ } else {
3517
+ this._changeConnectStatus(Strophe.Status.AUTHENTICATING, null);
3518
+ this._addSysHandler(this._auth1_cb.bind(this), null, null,
3519
+ null, "_auth_1");
3520
+
3521
+ this.send($iq({
3522
+ type: "get",
3523
+ to: this.domain,
3524
+ id: "_auth_1"
3525
+ }).c("query", {
3526
+ xmlns: Strophe.NS.AUTH
3527
+ }).c("username", {}).t(Strophe.getNodeFromJid(this.jid)).tree());
3528
+ }
3529
+ },
3530
+
3531
+ /** PrivateFunction: _sasl_digest_challenge1_cb
3532
+ * _Private_ handler for DIGEST-MD5 SASL authentication.
3533
+ *
3534
+ * Parameters:
3535
+ * (XMLElement) elem - The challenge stanza.
3536
+ *
3537
+ * Returns:
3538
+ * false to remove the handler.
3539
+ */
3540
+ _sasl_digest_challenge1_cb: function (elem)
3541
+ {
3542
+ var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;
3543
+
3544
+ var challenge = Base64.decode(Strophe.getText(elem));
3545
+ var cnonce = MD5.hexdigest("" + (Math.random() * 1234567890));
3546
+ var realm = "";
3547
+ var host = null;
3548
+ var nonce = "";
3549
+ var qop = "";
3550
+ var matches;
3551
+
3552
+ // remove unneeded handlers
3553
+ this.deleteHandler(this._sasl_failure_handler);
3554
+
3555
+ while (challenge.match(attribMatch)) {
3556
+ matches = challenge.match(attribMatch);
3557
+ challenge = challenge.replace(matches[0], "");
3558
+ matches[2] = matches[2].replace(/^"(.+)"$/, "$1");
3559
+ switch (matches[1]) {
3560
+ case "realm":
3561
+ realm = matches[2];
3562
+ break;
3563
+ case "nonce":
3564
+ nonce = matches[2];
3565
+ break;
3566
+ case "qop":
3567
+ qop = matches[2];
3568
+ break;
3569
+ case "host":
3570
+ host = matches[2];
3571
+ break;
3572
+ }
3573
+ }
3574
+
3575
+ var digest_uri = "xmpp/" + this.domain;
3576
+ if (host !== null) {
3577
+ digest_uri = digest_uri + "/" + host;
3578
+ }
3579
+
3580
+ var A1 = MD5.hash(Strophe.getNodeFromJid(this.jid) +
3581
+ ":" + realm + ":" + this.pass) +
3582
+ ":" + nonce + ":" + cnonce;
3583
+ var A2 = 'AUTHENTICATE:' + digest_uri;
3584
+
3585
+ var responseText = "";
3586
+ responseText += 'username=' +
3587
+ this._quote(Strophe.getNodeFromJid(this.jid)) + ',';
3588
+ responseText += 'realm=' + this._quote(realm) + ',';
3589
+ responseText += 'nonce=' + this._quote(nonce) + ',';
3590
+ responseText += 'cnonce=' + this._quote(cnonce) + ',';
3591
+ responseText += 'nc="00000001",';
3592
+ responseText += 'qop="auth",';
3593
+ responseText += 'digest-uri=' + this._quote(digest_uri) + ',';
3594
+ responseText += 'response=' + this._quote(
3595
+ MD5.hexdigest(MD5.hexdigest(A1) + ":" +
3596
+ nonce + ":00000001:" +
3597
+ cnonce + ":auth:" +
3598
+ MD5.hexdigest(A2))) + ',';
3599
+ responseText += 'charset="utf-8"';
3600
+
3601
+ this._sasl_challenge_handler = this._addSysHandler(
3602
+ this._sasl_digest_challenge2_cb.bind(this), null,
3603
+ "challenge", null, null);
3604
+ this._sasl_success_handler = this._addSysHandler(
3605
+ this._sasl_success_cb.bind(this), null,
3606
+ "success", null, null);
3607
+ this._sasl_failure_handler = this._addSysHandler(
3608
+ this._sasl_failure_cb.bind(this), null,
3609
+ "failure", null, null);
3610
+
3611
+ this.send($build('response', {
3612
+ xmlns: Strophe.NS.SASL
3613
+ }).t(Base64.encode(responseText)).tree());
3614
+
3615
+ return false;
3616
+ },
3617
+
3618
+ /** PrivateFunction: _quote
3619
+ * _Private_ utility function to backslash escape and quote strings.
3620
+ *
3621
+ * Parameters:
3622
+ * (String) str - The string to be quoted.
3623
+ *
3624
+ * Returns:
3625
+ * quoted string
3626
+ */
3627
+ _quote: function (str)
3628
+ {
3629
+ return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
3630
+ //" end string workaround for emacs
3631
+ },
3632
+
3633
+
3634
+ /** PrivateFunction: _sasl_digest_challenge2_cb
3635
+ * _Private_ handler for second step of DIGEST-MD5 SASL authentication.
3636
+ *
3637
+ * Parameters:
3638
+ * (XMLElement) elem - The challenge stanza.
3639
+ *
3640
+ * Returns:
3641
+ * false to remove the handler.
3642
+ */
3643
+ _sasl_digest_challenge2_cb: function (elem)
3644
+ {
3645
+ // remove unneeded handlers
3646
+ this.deleteHandler(this._sasl_success_handler);
3647
+ this.deleteHandler(this._sasl_failure_handler);
3648
+
3649
+ this._sasl_success_handler = this._addSysHandler(
3650
+ this._sasl_success_cb.bind(this), null,
3651
+ "success", null, null);
3652
+ this._sasl_failure_handler = this._addSysHandler(
3653
+ this._sasl_failure_cb.bind(this), null,
3654
+ "failure", null, null);
3655
+ this.send($build('response', {xmlns: Strophe.NS.SASL}).tree());
3656
+ return false;
3657
+ },
3658
+
3659
+ /** PrivateFunction: _sasl_scram_challenge_cb
3660
+ * _Private_ handler for SCRAM-SHA-1 SASL authentication.
3661
+ *
3662
+ * Parameters:
3663
+ * (XMLElement) elem - The challenge stanza.
3664
+ *
3665
+ * Returns:
3666
+ * false to remove the handler.
3667
+ */
3668
+ _sasl_scram_challenge_cb: function (elem)
3669
+ {
3670
+ var nonce, salt, iter, Hi, U, U_old;
3671
+ var clientKey, serverKey, clientSignature;
3672
+ var responseText = "c=biws,";
3673
+ var challenge = Base64.decode(Strophe.getText(elem));
3674
+ var authMessage = this._sasl_data["client-first-message-bare"] + "," +
3675
+ challenge + ",";
3676
+ var cnonce = this._sasl_data["cnonce"]
3677
+ var attribMatch = /([a-z]+)=([^,]+)(,|$)/;
3678
+
3679
+ // remove unneeded handlers
3680
+ this.deleteHandler(this._sasl_failure_handler);
3681
+
3682
+ while (challenge.match(attribMatch)) {
3683
+ matches = challenge.match(attribMatch);
3684
+ challenge = challenge.replace(matches[0], "");
3685
+ switch (matches[1]) {
3686
+ case "r":
3687
+ nonce = matches[2];
3688
+ break;
3689
+ case "s":
3690
+ salt = matches[2];
3691
+ break;
3692
+ case "i":
3693
+ iter = matches[2];
3694
+ break;
3695
+ }
3696
+ }
3697
+
3698
+ if (!(nonce.substr(0, cnonce.length) === cnonce)) {
3699
+ this._sasl_data = [];
3700
+ return this._sasl_failure_cb(null);
3701
+ }
3702
+
3703
+ responseText += "r=" + nonce;
3704
+ authMessage += responseText;
3705
+
3706
+ salt = Base64.decode(salt);
3707
+ salt += "\0\0\0\1";
3708
+
3709
+ Hi = U_old = core_hmac_sha1(this.pass, salt);
3710
+ for (i = 1; i < iter; i++) {
3711
+ U = core_hmac_sha1(this.pass, binb2str(U_old));
3712
+ for (k = 0; k < 5; k++) {
3713
+ Hi[k] ^= U[k];
3714
+ }
3715
+ U_old = U;
3716
+ }
3717
+ Hi = binb2str(Hi);
3718
+
3719
+ clientKey = core_hmac_sha1(Hi, "Client Key");
3720
+ serverKey = str_hmac_sha1(Hi, "Server Key");
3721
+ clientSignature = core_hmac_sha1(str_sha1(binb2str(clientKey)), authMessage);
3722
+ this._sasl_data["server-signature"] = b64_hmac_sha1(serverKey, authMessage);
3723
+
3724
+ for (k = 0; k < 5; k++) {
3725
+ clientKey[k] ^= clientSignature[k];
3726
+ }
3727
+
3728
+ responseText += ",p=" + Base64.encode(binb2str(clientKey));
3729
+
3730
+ this._sasl_success_handler = this._addSysHandler(
3731
+ this._sasl_success_cb.bind(this), null,
3732
+ "success", null, null);
3733
+ this._sasl_failure_handler = this._addSysHandler(
3734
+ this._sasl_failure_cb.bind(this), null,
3735
+ "failure", null, null);
3736
+
3737
+ this.send($build('response', {
3738
+ xmlns: Strophe.NS.SASL
3739
+ }).t(Base64.encode(responseText)).tree());
3740
+
3741
+ return false;
3742
+ },
3743
+
3744
+ /** PrivateFunction: _auth1_cb
3745
+ * _Private_ handler for legacy authentication.
3746
+ *
3747
+ * This handler is called in response to the initial <iq type='get'/>
3748
+ * for legacy authentication. It builds an authentication <iq/> and
3749
+ * sends it, creating a handler (calling back to _auth2_cb()) to
3750
+ * handle the result
3751
+ *
3752
+ * Parameters:
3753
+ * (XMLElement) elem - The stanza that triggered the callback.
3754
+ *
3755
+ * Returns:
3756
+ * false to remove the handler.
3757
+ */
3758
+ _auth1_cb: function (elem)
3759
+ {
3760
+ // build plaintext auth iq
3761
+ var iq = $iq({type: "set", id: "_auth_2"})
3762
+ .c('query', {xmlns: Strophe.NS.AUTH})
3763
+ .c('username', {}).t(Strophe.getNodeFromJid(this.jid))
3764
+ .up()
3765
+ .c('password').t(this.pass);
3766
+
3767
+ if (!Strophe.getResourceFromJid(this.jid)) {
3768
+ // since the user has not supplied a resource, we pick
3769
+ // a default one here. unlike other auth methods, the server
3770
+ // cannot do this for us.
3771
+ this.jid = Strophe.getBareJidFromJid(this.jid) + '/strophe';
3772
+ }
3773
+ iq.up().c('resource', {}).t(Strophe.getResourceFromJid(this.jid));
3774
+
3775
+ this._addSysHandler(this._auth2_cb.bind(this), null,
3776
+ null, null, "_auth_2");
3777
+
3778
+ this.send(iq.tree());
3779
+
3780
+ return false;
3781
+ },
3782
+
3783
+ /** PrivateFunction: _sasl_success_cb
3784
+ * _Private_ handler for succesful SASL authentication.
3785
+ *
3786
+ * Parameters:
3787
+ * (XMLElement) elem - The matching stanza.
3788
+ *
3789
+ * Returns:
3790
+ * false to remove the handler.
3791
+ */
3792
+ _sasl_success_cb: function (elem)
3793
+ {
3794
+ if (this._sasl_data["server-signature"]) {
3795
+ var serverSignature;
3796
+ var success = Base64.decode(Strophe.getText(elem));
3797
+ var attribMatch = /([a-z]+)=([^,]+)(,|$)/;
3798
+ matches = success.match(attribMatch);
3799
+ if (matches[1] == "v") {
3800
+ serverSignature = matches[2];
3801
+ }
3802
+ if (serverSignature != this._sasl_data["server-signature"]) {
3803
+ // remove old handlers
3804
+ this.deleteHandler(this._sasl_failure_handler);
3805
+ this._sasl_failure_handler = null;
3806
+ if (this._sasl_challenge_handler) {
3807
+ this.deleteHandler(this._sasl_challenge_handler);
3808
+ this._sasl_challenge_handler = null;
3809
+ }
3810
+
3811
+ this._sasl_data = [];
3812
+ return this._sasl_failure_cb(null);
3813
+ }
3814
+ }
3815
+
3816
+ Strophe.info("SASL authentication succeeded.");
3817
+
3818
+ // remove old handlers
3819
+ this.deleteHandler(this._sasl_failure_handler);
3820
+ this._sasl_failure_handler = null;
3821
+ if (this._sasl_challenge_handler) {
3822
+ this.deleteHandler(this._sasl_challenge_handler);
3823
+ this._sasl_challenge_handler = null;
3824
+ }
3825
+
3826
+ this._addSysHandler(this._sasl_auth1_cb.bind(this), null,
3827
+ "stream:features", null, null);
3828
+
3829
+ // we must send an xmpp:restart now
3830
+ this._sendRestart();
3831
+
3832
+ return false;
3833
+ },
3834
+
3835
+ /** PrivateFunction: _sasl_auth1_cb
3836
+ * _Private_ handler to start stream binding.
3837
+ *
3838
+ * Parameters:
3839
+ * (XMLElement) elem - The matching stanza.
3840
+ *
3841
+ * Returns:
3842
+ * false to remove the handler.
3843
+ */
3844
+ _sasl_auth1_cb: function (elem)
3845
+ {
3846
+ // save stream:features for future usage
3847
+ this.features = elem;
3848
+
3849
+ var i, child;
3850
+
3851
+ for (i = 0; i < elem.childNodes.length; i++) {
3852
+ child = elem.childNodes[i];
3853
+ if (child.nodeName == 'bind') {
3854
+ this.do_bind = true;
3855
+ }
3856
+
3857
+ if (child.nodeName == 'session') {
3858
+ this.do_session = true;
3859
+ }
3860
+ }
3861
+
3862
+ if (!this.do_bind) {
3863
+ this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
3864
+ return false;
3865
+ } else {
3866
+ this._addSysHandler(this._sasl_bind_cb.bind(this), null, null,
3867
+ null, "_bind_auth_2");
3868
+
3869
+ var resource = Strophe.getResourceFromJid(this.jid);
3870
+ if (resource) {
3871
+ this.send($iq({type: "set", id: "_bind_auth_2"})
3872
+ .c('bind', {xmlns: Strophe.NS.BIND})
3873
+ .c('resource', {}).t(resource).tree());
3874
+ } else {
3875
+ this.send($iq({type: "set", id: "_bind_auth_2"})
3876
+ .c('bind', {xmlns: Strophe.NS.BIND})
3877
+ .tree());
3878
+ }
3879
+ }
3880
+
3881
+ return false;
3882
+ },
3883
+
3884
+ /** PrivateFunction: _sasl_bind_cb
3885
+ * _Private_ handler for binding result and session start.
3886
+ *
3887
+ * Parameters:
3888
+ * (XMLElement) elem - The matching stanza.
3889
+ *
3890
+ * Returns:
3891
+ * false to remove the handler.
3892
+ */
3893
+ _sasl_bind_cb: function (elem)
3894
+ {
3895
+ if (elem.getAttribute("type") == "error") {
3896
+ Strophe.info("SASL binding failed.");
3897
+ var conflict = elem.getElementsByTagName("conflict"), condition;
3898
+ if (conflict.length > 0) {
3899
+ condition = 'conflict';
3900
+ }
3901
+ this._changeConnectStatus(Strophe.Status.AUTHFAIL, condition);
3902
+ return false;
3903
+ }
3904
+
3905
+ // TODO - need to grab errors
3906
+ var bind = elem.getElementsByTagName("bind");
3907
+ var jidNode;
3908
+ if (bind.length > 0) {
3909
+ // Grab jid
3910
+ jidNode = bind[0].getElementsByTagName("jid");
3911
+ if (jidNode.length > 0) {
3912
+ this.jid = Strophe.getText(jidNode[0]);
3913
+
3914
+ if (this.do_session) {
3915
+ this._addSysHandler(this._sasl_session_cb.bind(this),
3916
+ null, null, null, "_session_auth_2");
3917
+
3918
+ this.send($iq({type: "set", id: "_session_auth_2"})
3919
+ .c('session', {xmlns: Strophe.NS.SESSION})
3920
+ .tree());
3921
+ } else {
3922
+ this.authenticated = true;
3923
+ this._changeConnectStatus(Strophe.Status.CONNECTED, null);
3924
+ }
3925
+ }
3926
+ } else {
3927
+ Strophe.info("SASL binding failed.");
3928
+ this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
3929
+ return false;
3930
+ }
3931
+ },
3932
+
3933
+ /** PrivateFunction: _sasl_session_cb
3934
+ * _Private_ handler to finish successful SASL connection.
3935
+ *
3936
+ * This sets Connection.authenticated to true on success, which
3937
+ * starts the processing of user handlers.
3938
+ *
3939
+ * Parameters:
3940
+ * (XMLElement) elem - The matching stanza.
3941
+ *
3942
+ * Returns:
3943
+ * false to remove the handler.
3944
+ */
3945
+ _sasl_session_cb: function (elem)
3946
+ {
3947
+ if (elem.getAttribute("type") == "result") {
3948
+ this.authenticated = true;
3949
+ this._changeConnectStatus(Strophe.Status.CONNECTED, null);
3950
+ } else if (elem.getAttribute("type") == "error") {
3951
+ Strophe.info("Session creation failed.");
3952
+ this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
3953
+ return false;
3954
+ }
3955
+
3956
+ return false;
3957
+ },
3958
+
3959
+ /** PrivateFunction: _sasl_failure_cb
3960
+ * _Private_ handler for SASL authentication failure.
3961
+ *
3962
+ * Parameters:
3963
+ * (XMLElement) elem - The matching stanza.
3964
+ *
3965
+ * Returns:
3966
+ * false to remove the handler.
3967
+ */
3968
+ _sasl_failure_cb: function (elem)
3969
+ {
3970
+ // delete unneeded handlers
3971
+ if (this._sasl_success_handler) {
3972
+ this.deleteHandler(this._sasl_success_handler);
3973
+ this._sasl_success_handler = null;
3974
+ }
3975
+ if (this._sasl_challenge_handler) {
3976
+ this.deleteHandler(this._sasl_challenge_handler);
3977
+ this._sasl_challenge_handler = null;
3978
+ }
3979
+
3980
+ this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
3981
+ return false;
3982
+ },
3983
+
3984
+ /** PrivateFunction: _auth2_cb
3985
+ * _Private_ handler to finish legacy authentication.
3986
+ *
3987
+ * This handler is called when the result from the jabber:iq:auth
3988
+ * <iq/> stanza is returned.
3989
+ *
3990
+ * Parameters:
3991
+ * (XMLElement) elem - The stanza that triggered the callback.
3992
+ *
3993
+ * Returns:
3994
+ * false to remove the handler.
3995
+ */
3996
+ _auth2_cb: function (elem)
3997
+ {
3998
+ if (elem.getAttribute("type") == "result") {
3999
+ this.authenticated = true;
4000
+ this._changeConnectStatus(Strophe.Status.CONNECTED, null);
4001
+ } else if (elem.getAttribute("type") == "error") {
4002
+ this._changeConnectStatus(Strophe.Status.AUTHFAIL, null);
4003
+ this.disconnect();
4004
+ }
4005
+
4006
+ return false;
4007
+ },
4008
+
4009
+ /** PrivateFunction: _addSysTimedHandler
4010
+ * _Private_ function to add a system level timed handler.
4011
+ *
4012
+ * This function is used to add a Strophe.TimedHandler for the
4013
+ * library code. System timed handlers are allowed to run before
4014
+ * authentication is complete.
4015
+ *
4016
+ * Parameters:
4017
+ * (Integer) period - The period of the handler.
4018
+ * (Function) handler - The callback function.
4019
+ */
4020
+ _addSysTimedHandler: function (period, handler)
4021
+ {
4022
+ var thand = new Strophe.TimedHandler(period, handler);
4023
+ thand.user = false;
4024
+ this.addTimeds.push(thand);
4025
+ return thand;
4026
+ },
4027
+
4028
+ /** PrivateFunction: _addSysHandler
4029
+ * _Private_ function to add a system level stanza handler.
4030
+ *
4031
+ * This function is used to add a Strophe.Handler for the
4032
+ * library code. System stanza handlers are allowed to run before
4033
+ * authentication is complete.
4034
+ *
4035
+ * Parameters:
4036
+ * (Function) handler - The callback function.
4037
+ * (String) ns - The namespace to match.
4038
+ * (String) name - The stanza name to match.
4039
+ * (String) type - The stanza type attribute to match.
4040
+ * (String) id - The stanza id attribute to match.
4041
+ */
4042
+ _addSysHandler: function (handler, ns, name, type, id)
4043
+ {
4044
+ var hand = new Strophe.Handler(handler, ns, name, type, id);
4045
+ hand.user = false;
4046
+ this.addHandlers.push(hand);
4047
+ return hand;
4048
+ },
4049
+
4050
+ /** PrivateFunction: _onDisconnectTimeout
4051
+ * _Private_ timeout handler for handling non-graceful disconnection.
4052
+ *
4053
+ * If the graceful disconnect process does not complete within the
4054
+ * time allotted, this handler finishes the disconnect anyway.
4055
+ *
4056
+ * Returns:
4057
+ * false to remove the handler.
4058
+ */
4059
+ _onDisconnectTimeout: function ()
4060
+ {
4061
+ Strophe.info("_onDisconnectTimeout was called");
4062
+
4063
+ // cancel all remaining requests and clear the queue
4064
+ var req;
4065
+ while (this._requests.length > 0) {
4066
+ req = this._requests.pop();
4067
+ req.abort = true;
4068
+ req.xhr.abort();
4069
+ // jslint complains, but this is fine. setting to empty func
4070
+ // is necessary for IE6
4071
+ req.xhr.onreadystatechange = function () {};
4072
+ }
4073
+
4074
+ // actually disconnect
4075
+ this._doDisconnect();
4076
+
4077
+ return false;
4078
+ },
4079
+
4080
+ /** PrivateFunction: _onIdle
4081
+ * _Private_ handler to process events during idle cycle.
4082
+ *
4083
+ * This handler is called every 100ms to fire timed handlers that
4084
+ * are ready and keep poll requests going.
4085
+ */
4086
+ _onIdle: function ()
4087
+ {
4088
+ var i, thand, since, newList;
4089
+
4090
+ // add timed handlers scheduled for addition
4091
+ // NOTE: we add before remove in the case a timed handler is
4092
+ // added and then deleted before the next _onIdle() call.
4093
+ while (this.addTimeds.length > 0) {
4094
+ this.timedHandlers.push(this.addTimeds.pop());
4095
+ }
4096
+
4097
+ // remove timed handlers that have been scheduled for deletion
4098
+ while (this.removeTimeds.length > 0) {
4099
+ thand = this.removeTimeds.pop();
4100
+ i = this.timedHandlers.indexOf(thand);
4101
+ if (i >= 0) {
4102
+ this.timedHandlers.splice(i, 1);
4103
+ }
4104
+ }
4105
+
4106
+ // call ready timed handlers
4107
+ var now = new Date().getTime();
4108
+ newList = [];
4109
+ for (i = 0; i < this.timedHandlers.length; i++) {
4110
+ thand = this.timedHandlers[i];
4111
+ if (this.authenticated || !thand.user) {
4112
+ since = thand.lastCalled + thand.period;
4113
+ if (since - now <= 0) {
4114
+ if (thand.run()) {
4115
+ newList.push(thand);
4116
+ }
4117
+ } else {
4118
+ newList.push(thand);
4119
+ }
4120
+ }
4121
+ }
4122
+ this.timedHandlers = newList;
4123
+
4124
+ var body, time_elapsed;
4125
+
4126
+ // if no requests are in progress, poll
4127
+ if (this.authenticated && this._requests.length === 0 &&
4128
+ this._data.length === 0 && !this.disconnecting) {
4129
+ Strophe.info("no requests during idle cycle, sending " +
4130
+ "blank request");
4131
+ this._data.push(null);
4132
+ }
4133
+
4134
+ if (this._requests.length < 2 && this._data.length > 0 &&
4135
+ !this.paused) {
4136
+ body = this._buildBody();
4137
+ for (i = 0; i < this._data.length; i++) {
4138
+ if (this._data[i] !== null) {
4139
+ if (this._data[i] === "restart") {
4140
+ body.attrs({
4141
+ to: this.domain,
4142
+ "xml:lang": "en",
4143
+ "xmpp:restart": "true",
4144
+ "xmlns:xmpp": Strophe.NS.BOSH
4145
+ });
4146
+ } else {
4147
+ body.cnode(this._data[i]).up();
4148
+ }
4149
+ }
4150
+ }
4151
+ delete this._data;
4152
+ this._data = [];
4153
+ this._requests.push(
4154
+ new Strophe.Request(body.tree(),
4155
+ this._onRequestStateChange.bind(
4156
+ this, this._dataRecv.bind(this)),
4157
+ body.tree().getAttribute("rid")));
4158
+ this._processRequest(this._requests.length - 1);
4159
+ }
4160
+
4161
+ if (this._requests.length > 0) {
4162
+ time_elapsed = this._requests[0].age();
4163
+ if (this._requests[0].dead !== null) {
4164
+ if (this._requests[0].timeDead() >
4165
+ Math.floor(Strophe.SECONDARY_TIMEOUT * this.wait)) {
4166
+ this._throttledRequestHandler();
4167
+ }
4168
+ }
4169
+
4170
+ if (time_elapsed > Math.floor(Strophe.TIMEOUT * this.wait)) {
4171
+ Strophe.warn("Request " +
4172
+ this._requests[0].id +
4173
+ " timed out, over " + Math.floor(Strophe.TIMEOUT * this.wait) +
4174
+ " seconds since last activity");
4175
+ this._throttledRequestHandler();
4176
+ }
4177
+ }
4178
+
4179
+ clearTimeout(this._idleTimeout);
4180
+
4181
+ // reactivate the timer only if connected
4182
+ if (this.connected) {
4183
+ this._idleTimeout = setTimeout(this._onIdle.bind(this), 100);
4184
+ }
4185
+ }
4186
+ };
4187
+
4188
+ if (callback) {
4189
+ callback(Strophe, $build, $msg, $iq, $pres);
4190
+ }
4191
+
4192
+ })(function () {
4193
+ window.Strophe = arguments[0];
4194
+ window.$build = arguments[1];
4195
+ window.$msg = arguments[2];
4196
+ window.$iq = arguments[3];
4197
+ window.$pres = arguments[4];
4198
+ });