xmppify 0.0.12

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