[2397] | 1 | /** |
---|
| 2 | * This program is distributed under the terms of the MIT license. |
---|
| 3 | * Please see the LICENSE file for details. |
---|
| 4 | * |
---|
| 5 | * Copyright 2008 Michael Garvin |
---|
| 6 | */ |
---|
| 7 | |
---|
[2437] | 8 | var TROPHYIM_BOSH_SERVICE = "/proxy/ejabberd"; //Change to suit |
---|
[2397] | 9 | |
---|
| 10 | var TROPHYIM_LOG_LINES = 200; |
---|
| 11 | |
---|
| 12 | var TROPHYIM_LOGLEVEL = 0; //0=debug, 1=info, 2=warn, 3=error, 4=fatal |
---|
| 13 | |
---|
| 14 | var TROPHYIM_VERSION = "0.3"; |
---|
| 15 | |
---|
| 16 | var TROPHYIM_RESOURCE = "/JABBERITWEB"; |
---|
| 17 | |
---|
| 18 | //Uncomment to make session reattachment work |
---|
| 19 | //var TROPHYIM_JSON_STORE = "json_store.php"; |
---|
| 20 | |
---|
| 21 | /** Object: DOMObjects |
---|
| 22 | * This class contains builders for all the DOM objects needed by TrophyIM |
---|
| 23 | */ |
---|
| 24 | DOMObjects = { |
---|
| 25 | /** Function: xmlParse |
---|
| 26 | * Cross-browser alternative to using innerHTML |
---|
| 27 | * Parses given string, returns valid DOM HTML object |
---|
| 28 | * |
---|
| 29 | * Parameters: |
---|
| 30 | * (String) xml - the xml string to parse |
---|
| 31 | */ |
---|
| 32 | xmlParse : function(xmlString) { |
---|
| 33 | var xmlObj = this.xmlRender(xmlString); |
---|
| 34 | if(xmlObj) { |
---|
| 35 | try { //Firefox, Gecko, etc |
---|
| 36 | if (this.processor == undefined) { |
---|
| 37 | this.processor = new XSLTProcessor(); |
---|
| 38 | this.processor.importStylesheet(this.xmlRender( |
---|
| 39 | '<xsl:stylesheet version="1.0"\ |
---|
| 40 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\ |
---|
| 41 | <xsl:output method="html" indent="yes"/><xsl:template\ |
---|
| 42 | match="@*|node()"><xsl:copy><xsl:copy-of\ |
---|
| 43 | select="@*|node()"/></xsl:copy></xsl:template>\ |
---|
| 44 | </xsl:stylesheet>')); |
---|
| 45 | } |
---|
| 46 | var htmlObj = |
---|
| 47 | this.processor.transformToDocument(xmlObj).documentElement; |
---|
| 48 | //Safari has a quirk where it wraps dom elements in <html><body> |
---|
| 49 | if (htmlObj.tagName.toLowerCase() == 'html') { |
---|
| 50 | htmlObj = htmlObj.firstChild.firstChild; |
---|
| 51 | } |
---|
| 52 | return document.importNode(htmlObj, true); |
---|
| 53 | } catch(e) { |
---|
| 54 | try { //IE is so very very special |
---|
| 55 | var htmlObj = document.importNode(xmlObj.documentElement, true); |
---|
| 56 | if (htmlObj.tagName.toLowerCase() == "div") { |
---|
| 57 | var div_wrapper = document.createElement('div'); |
---|
| 58 | div_wrapper.appendChild(htmlObj); |
---|
| 59 | if(div_wrapper.innerHTML) { |
---|
| 60 | div_wrapper.innerHTML = div_wrapper.innerHTML; |
---|
| 61 | } |
---|
| 62 | htmlObj = div_wrapper.firstChild; |
---|
| 63 | } |
---|
| 64 | return htmlObj; |
---|
| 65 | } catch(e) { |
---|
| 66 | alert( |
---|
| 67 | "TrophyIM Error: Cannot add html to page" + e.message); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | }, |
---|
| 72 | /** Function: xmlRender |
---|
| 73 | * Uses browser-specific methods to turn given string into xml object |
---|
| 74 | * |
---|
| 75 | * Parameters: |
---|
| 76 | * (String) xml - the xml string to parse |
---|
| 77 | */ |
---|
| 78 | xmlRender : function(xmlString) { |
---|
| 79 | try {//IE |
---|
| 80 | var renderObj = new ActiveXObject("Microsoft.XMLDOM"); |
---|
| 81 | renderObj.async="false"; |
---|
| 82 | if(xmlString) { |
---|
| 83 | renderObj.loadXML(xmlString); |
---|
| 84 | } |
---|
| 85 | } catch (e) { |
---|
| 86 | try { //Firefox, Gecko, etc |
---|
| 87 | if (this.parser == undefined) { |
---|
| 88 | this.parser = new DOMParser(); |
---|
| 89 | } |
---|
| 90 | var renderObj = this.parser.parseFromString(xmlString, |
---|
| 91 | "application/xml"); |
---|
| 92 | } catch(e) { |
---|
| 93 | alert("TrophyIM Error: Cannot create new html for page"); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | return renderObj; |
---|
| 98 | }, |
---|
| 99 | /** Function: getHTML |
---|
| 100 | * Returns named HTML snippet as DOM object |
---|
| 101 | * |
---|
| 102 | * Parameters: |
---|
| 103 | * (String) name - name of HTML snippet to retrieve (see HTMLSnippets |
---|
| 104 | * object) |
---|
| 105 | */ |
---|
| 106 | getHTML : function(page) |
---|
| 107 | { |
---|
| 108 | return this.xmlParse(HTMLSnippets[page]); |
---|
| 109 | }, |
---|
| 110 | |
---|
| 111 | /** Function: getScript |
---|
| 112 | * Returns script object with src to given script |
---|
| 113 | * |
---|
| 114 | * Parameters: |
---|
| 115 | * (String) script - name of script to put in src attribute of script |
---|
| 116 | * element |
---|
| 117 | */ |
---|
| 118 | getScript : function(script) |
---|
| 119 | { |
---|
| 120 | var newscript = document.createElement('script'); |
---|
| 121 | newscript.setAttribute('src', script); |
---|
| 122 | newscript.setAttribute('type', 'text/javascript'); |
---|
| 123 | return newscript; |
---|
| 124 | } |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | /** Object: TrophyIM |
---|
| 128 | * |
---|
| 129 | * This is the actual TrophyIM application. It searches for the |
---|
| 130 | * 'trophyimclient' element and inserts itself into that. |
---|
| 131 | */ |
---|
| 132 | TrophyIM = { |
---|
| 133 | |
---|
| 134 | statusConn : { connected : false }, |
---|
| 135 | |
---|
| 136 | /** Constants: |
---|
| 137 | * |
---|
| 138 | * (Boolean) stale_roster - roster is stale and needs to be rewritten. |
---|
| 139 | */ |
---|
| 140 | constants : {stale_roster: false}, |
---|
| 141 | |
---|
| 142 | /** Object: chatHistory |
---|
| 143 | * |
---|
| 144 | * Stores chat history (last 10 message) and current presence of active |
---|
| 145 | * chat tabs. Indexed by jid. |
---|
| 146 | */ |
---|
| 147 | chatHistory : {}, |
---|
| 148 | |
---|
| 149 | /** Object: activeChats |
---|
| 150 | * |
---|
| 151 | * This object stores the currently active chats. |
---|
| 152 | */ |
---|
| 153 | activeChats : {current: null, divs: {}}, |
---|
| 154 | |
---|
| 155 | /** Function: setCookie |
---|
| 156 | * |
---|
| 157 | * Sets cookie name/value pair. Date and path are auto-selected. |
---|
| 158 | * |
---|
| 159 | * Parameters: |
---|
| 160 | * (String) name - the name of the cookie variable |
---|
| 161 | * (String) value - the value of the cookie variable |
---|
| 162 | */ |
---|
| 163 | |
---|
| 164 | setCookie : function(name, value) |
---|
| 165 | { |
---|
| 166 | var expire = new Date(); |
---|
| 167 | expire.setDate(expire.getDate() + 365); |
---|
| 168 | document.cookie = name + "=" + value + "; expires=" + expire.toGMTString(); |
---|
| 169 | }, |
---|
| 170 | |
---|
| 171 | /** Function: delCookie |
---|
| 172 | * |
---|
| 173 | * Deletes cookie |
---|
| 174 | * |
---|
| 175 | * Parameters: |
---|
| 176 | * (String) name) - the name of the cookie to delete |
---|
| 177 | */ |
---|
| 178 | |
---|
| 179 | delCookie : function(name) |
---|
| 180 | { |
---|
| 181 | var expire = new Date(); |
---|
| 182 | expire.setDate(expire.getDate() - 365); |
---|
| 183 | document.cookie = name + "= ; expires=" + expire.toGMTString(); |
---|
| 184 | delete TrophyIM.cookies[name]; |
---|
| 185 | }, |
---|
| 186 | |
---|
| 187 | /** Function: getCookies |
---|
| 188 | * |
---|
| 189 | * Retrieves all trophyim cookies into an indexed object. Inteneded to be |
---|
| 190 | * called once, at which time the app refers to the returned object instead |
---|
| 191 | * of re-parsing the cookie string every time. |
---|
| 192 | * |
---|
| 193 | * Each cookie is also re-applied so as to refresh the expiry date. |
---|
| 194 | */ |
---|
| 195 | |
---|
| 196 | getCookies : function() |
---|
| 197 | { |
---|
| 198 | var cObj = {}; |
---|
| 199 | var cookies = document.cookie.split(';'); |
---|
[2471] | 200 | for (var c in cookies) |
---|
| 201 | { |
---|
| 202 | while (cookies[c].charAt(0)==' ') |
---|
| 203 | { |
---|
[2397] | 204 | cookies[c] = cookies[c].substring(1,cookies[c].length); |
---|
| 205 | } |
---|
[2471] | 206 | if (cookies[c].substr(0, 8) == "trophyim") |
---|
| 207 | { |
---|
[2397] | 208 | var nvpair = cookies[c].split("=", 2); |
---|
| 209 | cObj[nvpair[0]] = nvpair[1]; |
---|
| 210 | TrophyIM.setCookie(nvpair[0], nvpair[1]); |
---|
| 211 | } |
---|
| 212 | } |
---|
[2471] | 213 | |
---|
[2397] | 214 | return cObj; |
---|
| 215 | }, |
---|
| 216 | |
---|
| 217 | /** Function: load |
---|
| 218 | * |
---|
| 219 | * This function searches for the trophyimclient div and loads the client |
---|
| 220 | * into it. |
---|
| 221 | */ |
---|
| 222 | |
---|
| 223 | load : function() |
---|
| 224 | { |
---|
| 225 | if (!TrophyIM.statusConn.connected) |
---|
| 226 | { |
---|
| 227 | TrophyIM.cookies = TrophyIM.getCookies(); |
---|
| 228 | |
---|
| 229 | //Load other .js scripts needed |
---|
| 230 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/strophe.js')); |
---|
| 231 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/md5.js')); |
---|
| 232 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/sha1.js')); |
---|
| 233 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/b64.js')); |
---|
| 234 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'js/json2.js')); //Keep this script last |
---|
| 235 | //Wait a second to give scripts time to load |
---|
| 236 | setTimeout("TrophyIM.showLogin()", 500); |
---|
| 237 | } |
---|
| 238 | else |
---|
| 239 | { |
---|
| 240 | loadIM.rosterDiv(); |
---|
| 241 | } |
---|
| 242 | }, |
---|
| 243 | |
---|
| 244 | /** Function: storeData |
---|
| 245 | * |
---|
| 246 | * Store all our data in the JSONStore, if it is active |
---|
| 247 | */ |
---|
[2471] | 248 | |
---|
[2397] | 249 | storeData : function() |
---|
| 250 | { |
---|
| 251 | if ( TrophyIM.connection && TrophyIM.connection.connected ) |
---|
| 252 | { |
---|
| 253 | TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" + |
---|
| 254 | TrophyIM.connection.sid + "|" + TrophyIM.connection.rid); |
---|
| 255 | TrophyIM.rosterObj.save(); |
---|
| 256 | } |
---|
| 257 | }, |
---|
[2471] | 258 | |
---|
[2397] | 259 | /** Function: showlogin |
---|
| 260 | * |
---|
| 261 | * This function clears out the IM box and either redisplays the login |
---|
| 262 | * page, or re-attaches to Strophe, preserving the logging div if it |
---|
| 263 | * exists, or creating a new one of we are re-attaching. |
---|
| 264 | */ |
---|
[2471] | 265 | |
---|
[2397] | 266 | showLogin : function() |
---|
| 267 | { |
---|
| 268 | //JSON is the last script to load, so we wait on it |
---|
| 269 | //Added Strophe check too because of bug where it's sometimes missing |
---|
| 270 | if (typeof(JSON) != undefined && typeof(Strophe) != undefined) |
---|
| 271 | { |
---|
| 272 | TrophyIM.JSONStore = new TrophyIMJSONStore(); |
---|
| 273 | |
---|
| 274 | if ( TrophyIM.JSONStore.store_working && TrophyIM.cookies['trophyim_bosh_xid'] ) |
---|
| 275 | { |
---|
| 276 | var xids = TrophyIM.cookies['trophyim_bosh_xid'].split("|"); |
---|
| 277 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
| 278 | TrophyIM.constants.stale_roster = true; |
---|
| 279 | |
---|
| 280 | TrophyIM.connection = new Strophe.Connection(TROPHYIM_BOSH_SERVICE); |
---|
| 281 | TrophyIM.connection.rawInput = TrophyIM.rawInput; |
---|
| 282 | TrophyIM.connection.rawOutput = TrophyIM.rawOutput; |
---|
| 283 | Strophe.log = TrophyIM.log; |
---|
| 284 | Strophe.info('Attempting Strophe attach.'); |
---|
| 285 | TrophyIM.connection.attach(xids[0], xids[1], xids[2], TrophyIM.onConnect); |
---|
| 286 | TrophyIM.onConnect(Strophe.Status.CONNECTED); |
---|
| 287 | } |
---|
| 288 | else |
---|
| 289 | { |
---|
| 290 | // List Contact |
---|
| 291 | loadIM.rosterDiv(); |
---|
| 292 | |
---|
| 293 | if ( loadIM.getUserCurrent() != null ) |
---|
| 294 | { |
---|
| 295 | TrophyIM.login( loadIM.getUserCurrent().jid, loadIM.getUserCurrent().password ); |
---|
| 296 | } |
---|
| 297 | else |
---|
| 298 | { |
---|
| 299 | loadIM.loginPage(); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | else |
---|
| 304 | { |
---|
| 305 | setTimeout("TrophyIM.showLogin()", 500); |
---|
| 306 | } |
---|
| 307 | }, |
---|
| 308 | |
---|
| 309 | /** Function: log |
---|
| 310 | * |
---|
| 311 | * This function logs the given message in the trophyimlog div |
---|
| 312 | * |
---|
| 313 | * Parameter: (String) msg - the message to log |
---|
| 314 | */ |
---|
| 315 | |
---|
| 316 | log : function(level, msg) |
---|
| 317 | { |
---|
| 318 | if (TrophyIM.logging_div && level >= TROPHYIM_LOGLEVEL) { |
---|
| 319 | while(TrophyIM.logging_div.childNodes.length > TROPHYIM_LOG_LINES) { |
---|
| 320 | TrophyIM.logging_div.removeChild( |
---|
| 321 | TrophyIM.logging_div.firstChild); |
---|
| 322 | } |
---|
| 323 | var msg_div = document.createElement('div'); |
---|
| 324 | msg_div.className = 'trophyimlogitem'; |
---|
| 325 | msg_div.appendChild(document.createTextNode(msg)); |
---|
| 326 | TrophyIM.logging_div.appendChild(msg_div); |
---|
| 327 | TrophyIM.logging_div.scrollTop = TrophyIM.logging_div.scrollHeight; |
---|
| 328 | } |
---|
| 329 | }, |
---|
| 330 | |
---|
| 331 | /** Function: rawInput |
---|
| 332 | * |
---|
| 333 | * This logs the packets actually recieved by strophe at the debug level |
---|
| 334 | */ |
---|
| 335 | rawInput : function (data) |
---|
| 336 | { |
---|
| 337 | Strophe.debug("RECV: " + data); |
---|
| 338 | }, |
---|
| 339 | |
---|
| 340 | /** Function: rawInput |
---|
| 341 | * |
---|
| 342 | * This logs the packets actually recieved by strophe at the debug level |
---|
| 343 | */ |
---|
| 344 | rawOutput : function (data) |
---|
| 345 | { |
---|
| 346 | Strophe.debug("SEND: " + data); |
---|
| 347 | }, |
---|
| 348 | |
---|
| 349 | /** Function: login |
---|
| 350 | * |
---|
| 351 | * This function logs into server using information given on login page. |
---|
| 352 | * Since the login page is where the logging checkbox is, it makes or |
---|
| 353 | * removes the logging div and cookie accordingly. |
---|
| 354 | * |
---|
| 355 | */ |
---|
| 356 | login : function() |
---|
| 357 | { |
---|
| 358 | if (TrophyIM.JSONStore.store_working) |
---|
| 359 | { |
---|
| 360 | //In case they never logged out |
---|
| 361 | TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', 'chat_history']); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | TrophyIM.connection = new Strophe.Connection(TROPHYIM_BOSH_SERVICE); |
---|
| 365 | TrophyIM.connection.rawInput = TrophyIM.rawInput; |
---|
| 366 | TrophyIM.connection.rawOutput = TrophyIM.rawOutput; |
---|
| 367 | Strophe.log = TrophyIM.log; |
---|
| 368 | |
---|
| 369 | if ( arguments.length > 0 ) |
---|
| 370 | { |
---|
| 371 | var barejid = arguments[0]; |
---|
| 372 | var password = arguments[1]; |
---|
| 373 | |
---|
| 374 | TrophyIM.connection.connect(barejid + TROPHYIM_RESOURCE, password, TrophyIM.onConnect); |
---|
| 375 | } |
---|
| 376 | else |
---|
| 377 | { |
---|
| 378 | var barejid = document.getElementById('trophyimjid').value |
---|
| 379 | var fulljid = barejid + TROPHYIM_RESOURCE; |
---|
| 380 | var password = document.getElementById('trophyimpass').value; |
---|
| 381 | var button = document.getElementById('trophyimconnect'); |
---|
| 382 | |
---|
| 383 | loadIM.setUserCurrent( barejid, password); |
---|
| 384 | |
---|
| 385 | if ( button.value == 'connect' ) |
---|
| 386 | { |
---|
| 387 | button.value = 'disconnect'; |
---|
| 388 | TrophyIM.connection.connect(fulljid, password, TrophyIM.onConnect); |
---|
| 389 | } |
---|
| 390 | else |
---|
| 391 | { |
---|
| 392 | button.value = 'connect'; |
---|
| 393 | TrophyIM.connection.disconnect(); |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | TrophyIM.setCookie('trophyimjid', barejid); |
---|
| 398 | |
---|
| 399 | }, |
---|
| 400 | |
---|
| 401 | /** Function: logout |
---|
| 402 | * |
---|
| 403 | * Logs into fresh session through Strophe, purging any old data. |
---|
| 404 | */ |
---|
| 405 | logout : function() |
---|
| 406 | { |
---|
| 407 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
| 408 | delete TrophyIM['cookies']['trophyim_bosh_xid']; |
---|
| 409 | |
---|
| 410 | if (TrophyIM.JSONStore.store_working) |
---|
| 411 | { |
---|
| 412 | TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', 'chat_history']); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | for (var chat in TrophyIM.activeChats['divs']) |
---|
| 416 | { |
---|
| 417 | delete TrophyIM.activeChats['divs'][chat]; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | TrophyIM.activeChats = {current: null, divs: {}}, |
---|
| 421 | TrophyIM.connection.disconnect(); |
---|
| 422 | TrophyIM.showLogin(); |
---|
| 423 | |
---|
| 424 | }, |
---|
| 425 | |
---|
| 426 | /** Function onConnect |
---|
| 427 | * |
---|
| 428 | * Callback given to Strophe upon connection to BOSH proxy. |
---|
| 429 | */ |
---|
| 430 | onConnect : function(status) |
---|
| 431 | { |
---|
| 432 | if (status == Strophe.Status.CONNECTING) |
---|
| 433 | { |
---|
| 434 | Strophe.info('Strophe is connecting.'); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | if (status == Strophe.Status.CONNFAIL) |
---|
| 438 | { |
---|
| 439 | Strophe.info('Strophe failed to connect.'); |
---|
| 440 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
| 441 | TrophyIM.showLogin(); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | if (status == Strophe.Status.DISCONNECTING) |
---|
| 445 | { |
---|
| 446 | Strophe.info('Strophe is disconnecting.'); |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | if (status == Strophe.Status.DISCONNECTED) |
---|
| 450 | { |
---|
| 451 | Strophe.info('Strophe is disconnected.'); |
---|
| 452 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
| 453 | TrophyIM.showLogin(); |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | if (status == Strophe.Status.CONNECTED) |
---|
| 457 | { |
---|
| 458 | loadIM.setStatusJabber('Disponível','available'); |
---|
| 459 | TrophyIM.statusConn.connected = true; |
---|
| 460 | TrophyIM.showClient(); |
---|
| 461 | Strophe.info('Strophe is connected.'); |
---|
| 462 | } |
---|
| 463 | }, |
---|
| 464 | |
---|
| 465 | /** Function: showClient |
---|
| 466 | * |
---|
| 467 | * This clears out the main div and puts in the main client. It also |
---|
| 468 | * registers all the handlers for Strophe to call in the client. |
---|
| 469 | */ |
---|
| 470 | showClient : function() |
---|
| 471 | { |
---|
| 472 | TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" + |
---|
| 473 | TrophyIM.connection.sid + "|" + TrophyIM.connection.rid); |
---|
| 474 | |
---|
| 475 | TrophyIM.rosterObj = new TrophyIMRoster(); |
---|
| 476 | TrophyIM.connection.addHandler(TrophyIM.onVersion, Strophe.NS.VERSION, 'iq', null, null, null); |
---|
| 477 | TrophyIM.connection.addHandler(TrophyIM.onRoster, Strophe.NS.ROSTER, 'iq', null, null, null); |
---|
| 478 | TrophyIM.connection.addHandler(TrophyIM.onPresence, null, 'presence', null, null, null); |
---|
| 479 | TrophyIM.connection.addHandler(TrophyIM.onMessage, null, 'message', null, null, null); |
---|
| 480 | |
---|
| 481 | //Get roster then announce presence. |
---|
| 482 | TrophyIM.connection.send($iq({type: 'get', xmlns: Strophe.NS.CLIENT}).c('query', {xmlns: Strophe.NS.ROSTER}).tree()); |
---|
| 483 | TrophyIM.connection.send($pres().tree()); |
---|
| 484 | setTimeout("TrophyIM.renderRoster()", 1000); |
---|
| 485 | }, |
---|
| 486 | |
---|
| 487 | /** Function: clearClient |
---|
| 488 | * |
---|
| 489 | * Clears out client div, preserving and returning existing logging_div if |
---|
| 490 | * one exists |
---|
| 491 | */ |
---|
[2471] | 492 | |
---|
| 493 | clearClient : function() |
---|
| 494 | { |
---|
| 495 | if(TrophyIM.logging_div) |
---|
| 496 | { |
---|
| 497 | var logging_div = TrophyIM.client_div.removeChild(document.getElementById('trophyimlog')); |
---|
| 498 | } |
---|
| 499 | else |
---|
| 500 | { |
---|
[2397] | 501 | var logging_div = null; |
---|
| 502 | } |
---|
[2471] | 503 | |
---|
| 504 | while(TrophyIM.client_div.childNodes.length > 0) |
---|
| 505 | { |
---|
[2397] | 506 | TrophyIM.client_div.removeChild(TrophyIM.client_div.firstChild); |
---|
| 507 | } |
---|
[2471] | 508 | |
---|
[2397] | 509 | return logging_div; |
---|
| 510 | }, |
---|
[2471] | 511 | |
---|
[2397] | 512 | /** Function: onVersion |
---|
| 513 | * |
---|
| 514 | * jabber:iq:version query handler |
---|
| 515 | */ |
---|
[2471] | 516 | |
---|
| 517 | onVersion : function(msg) |
---|
| 518 | { |
---|
[2397] | 519 | Strophe.debug("Version handler"); |
---|
[2471] | 520 | if (msg.getAttribute('type') == 'get') |
---|
| 521 | { |
---|
[2397] | 522 | var from = msg.getAttribute('from'); |
---|
| 523 | var to = msg.getAttribute('to'); |
---|
| 524 | var id = msg.getAttribute('id'); |
---|
| 525 | var reply = $iq({type: 'result', to: from, from: to, id: id}).c('query', |
---|
| 526 | {name: "TrophyIM", version: TROPHYIM_VERSION, os: |
---|
| 527 | "Javascript-capable browser"}); |
---|
| 528 | TrophyIM.connection.send(reply.tree()); |
---|
| 529 | } |
---|
| 530 | return true; |
---|
| 531 | }, |
---|
[2471] | 532 | |
---|
[2397] | 533 | /** Function: onRoster |
---|
| 534 | * |
---|
| 535 | * Roster iq handler |
---|
| 536 | */ |
---|
[2471] | 537 | |
---|
[2397] | 538 | onRoster : function(msg) |
---|
| 539 | { |
---|
[2471] | 540 | var roster_items = msg.firstChild.getElementsByTagName('item'); |
---|
[2397] | 541 | |
---|
| 542 | for (var i = 0; i < roster_items.length; i++) |
---|
| 543 | { |
---|
[2471] | 544 | with(roster_items[i]) |
---|
| 545 | { |
---|
| 546 | var groups = getElementsByTagName('group'); |
---|
| 547 | var group_array = []; |
---|
| 548 | |
---|
| 549 | for( var g = 0 ; g < groups.length; g++ ) |
---|
| 550 | { |
---|
| 551 | if( groups[g].hasChildNodes() ) |
---|
| 552 | group_array[group_array.length] = groups[g].firstChild.nodeValue; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('subscription'), getAttribute('name'), group_array); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | /* |
---|
[2397] | 559 | var groups = roster_items[i].getElementsByTagName('group'); |
---|
| 560 | |
---|
[2471] | 561 | var group_array = new Array(); |
---|
| 562 | |
---|
[2397] | 563 | for (var g = 0; g < groups.length; g++) |
---|
| 564 | { |
---|
[2471] | 565 | if( groups[g].firstChild != null ) |
---|
| 566 | group_array[group_array.length] = groups[g].firstChild.nodeValue; |
---|
[2397] | 567 | } |
---|
[2471] | 568 | |
---|
[2397] | 569 | with ( roster_items[i] ) |
---|
| 570 | { |
---|
[2471] | 571 | alert(getAttribute('jid') + "\n" + getAttribute('subscription') + "\n" + getAttribute('name')); |
---|
[2397] | 572 | TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('subscription'), getAttribute('name'), group_array); |
---|
| 573 | } |
---|
[2471] | 574 | */ |
---|
[2397] | 575 | } |
---|
[2471] | 576 | |
---|
[2397] | 577 | if ( msg.getAttribute('type') == 'set' ) |
---|
| 578 | { |
---|
[2471] | 579 | var _iq = $iq({type: 'reply', id: msg.getAttribute('id'), to: msg.getAttribute('from')}); |
---|
| 580 | TrophyIM.connection.send( _iq.tree()); |
---|
[2397] | 581 | } |
---|
| 582 | |
---|
| 583 | return true; |
---|
| 584 | }, |
---|
| 585 | /** Function: onPresence |
---|
| 586 | * |
---|
| 587 | * Presence handler |
---|
| 588 | */ |
---|
| 589 | onPresence : function(msg) |
---|
| 590 | { |
---|
| 591 | Strophe.debug("Presence handler"); |
---|
[2471] | 592 | var type = msg.getAttribute('type') ? msg.getAttribute('type') : 'available'; |
---|
| 593 | var show = msg.getElementsByTagName('show').length ? Strophe.getText(msg.getElementsByTagName('show')[0]) : type; |
---|
| 594 | var status = msg.getElementsByTagName('status').length ? Strophe.getText(msg.getElementsByTagName('status')[0]) : ''; |
---|
| 595 | var priority = msg.getElementsByTagName('priority').length ? parseInt(Strophe.getText(msg.getElementsByTagName('priority')[0])) : 0; |
---|
| 596 | |
---|
| 597 | TrophyIM.rosterObj.setPresence(msg.getAttribute('from'), priority, show, status); |
---|
[2397] | 598 | |
---|
| 599 | return true; |
---|
| 600 | }, |
---|
| 601 | /** Function: onMessage |
---|
| 602 | * |
---|
| 603 | * Message handler |
---|
| 604 | */ |
---|
| 605 | onMessage : function(msg) |
---|
| 606 | { |
---|
| 607 | |
---|
| 608 | var from = msg.getAttribute('from'); |
---|
| 609 | var type = msg.getAttribute('type'); |
---|
| 610 | var elems = msg.getElementsByTagName('body'); |
---|
| 611 | |
---|
| 612 | if ( (type == 'chat' || type == 'normal') && elems.length > 0 ) |
---|
| 613 | { |
---|
| 614 | var barejid = Strophe.getBareJidFromJid(from); |
---|
| 615 | var jid_lower = barejid.toLowerCase(); |
---|
[2471] | 616 | var contact = ""; |
---|
[2491] | 617 | contact = barejid.toLowerCase(); |
---|
| 618 | contact = contact.substring(0, contact.indexOf('@')); |
---|
| 619 | |
---|
[2471] | 620 | if( TrophyIM.rosterObj.roster[barejid] ) |
---|
[2397] | 621 | { |
---|
[2471] | 622 | if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) |
---|
| 623 | { |
---|
| 624 | contact = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; |
---|
| 625 | } |
---|
[2397] | 626 | } |
---|
[2491] | 627 | |
---|
[2397] | 628 | var message = |
---|
| 629 | { |
---|
| 630 | contact : "<font style='font-weight:bold; color:black;'>" + contact + "</font>", |
---|
| 631 | msg : Strophe.getText(elems[0]) |
---|
| 632 | }; |
---|
| 633 | |
---|
| 634 | TrophyIM.makeChat(from); //Make sure we have a chat window |
---|
| 635 | TrophyIM.addMessage(message, jid_lower); |
---|
| 636 | } |
---|
[2471] | 637 | |
---|
[2397] | 638 | return true; |
---|
| 639 | }, |
---|
[2471] | 640 | |
---|
[2397] | 641 | /** Function: makeChat |
---|
| 642 | * |
---|
| 643 | * Make sure chat window to given fulljid exists, switching chat context to |
---|
| 644 | * given resource |
---|
| 645 | */ |
---|
[2471] | 646 | |
---|
[2397] | 647 | makeChat : function(fulljid) |
---|
| 648 | { |
---|
[2471] | 649 | var barejid = Strophe.getBareJidFromJid(fulljid); |
---|
| 650 | var titleWindow = ""; |
---|
[2397] | 651 | |
---|
| 652 | var paramsChatBox = |
---|
| 653 | { |
---|
[2471] | 654 | 'idChatBox' : barejid + "__chatBox", |
---|
| 655 | 'jidTo' : barejid, |
---|
[2397] | 656 | 'path_jabberit' : path_jabberit |
---|
| 657 | }; |
---|
| 658 | |
---|
[2471] | 659 | if( TrophyIM.rosterObj.roster[barejid] ) |
---|
| 660 | { |
---|
| 661 | if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) |
---|
| 662 | { |
---|
| 663 | titleWindow = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | else |
---|
| 667 | { |
---|
| 668 | titleWindow = barejid.toLowerCase(); |
---|
[2491] | 669 | titleWindow = titleWindow.substring(0, titleWindow.indexOf('@')); |
---|
[2471] | 670 | } |
---|
| 671 | |
---|
[2397] | 672 | var winChatBox = |
---|
| 673 | { |
---|
[2471] | 674 | id_window : "window_chat_area_" + barejid, |
---|
[2397] | 675 | width : 387, |
---|
| 676 | height : 365, |
---|
| 677 | top : 100, |
---|
| 678 | left : 400, |
---|
| 679 | draggable : true, |
---|
| 680 | visible : "display", |
---|
| 681 | resizable : true, |
---|
| 682 | zindex : loadIM.getZIndex(), |
---|
[2471] | 683 | title : titleWindow, |
---|
[2397] | 684 | closeAction : "hidden", |
---|
| 685 | content : loadIM.parse("chat_box","chatBox.xsl", paramsChatBox) |
---|
| 686 | } |
---|
| 687 | |
---|
[2471] | 688 | _winBuild(winChatBox); |
---|
[2397] | 689 | |
---|
[2471] | 690 | loadIM.configEvents( document.getElementById( barejid + '__sendBox'), |
---|
[2397] | 691 | 'onkeyup', function(e) |
---|
| 692 | { |
---|
| 693 | if( e.keyCode == 13 ) |
---|
| 694 | { |
---|
[2471] | 695 | TrophyIM.sendMessage( barejid ); |
---|
| 696 | document.getElementById( barejid + '__sendBox').value = ''; |
---|
[2397] | 697 | return false; |
---|
| 698 | } |
---|
| 699 | } |
---|
| 700 | ); |
---|
| 701 | }, |
---|
| 702 | |
---|
| 703 | /** Function addContacts |
---|
| 704 | * |
---|
| 705 | * Parameters: |
---|
[2437] | 706 | * (string) jidFrom |
---|
| 707 | * (string) jidTo |
---|
[2397] | 708 | * (string) name |
---|
| 709 | * (string) group |
---|
| 710 | */ |
---|
| 711 | |
---|
[2437] | 712 | addContact : function( jidFrom, jidTo, name, group ) |
---|
[2397] | 713 | { |
---|
[2437] | 714 | // Set Presence |
---|
| 715 | var newPresence = $pres({from: jidFrom, to: jidTo, type: 'subscribe'}).tree(); |
---|
| 716 | |
---|
[2397] | 717 | TrophyIM.connection.send(newPresence); |
---|
[2437] | 718 | |
---|
| 719 | // Add Contact |
---|
[2491] | 720 | var _id = TrophyIM.connection.getUniqueId('add'); |
---|
[2471] | 721 | var newContact = $iq({type: 'set', id: _id }); |
---|
[2397] | 722 | newContact = newContact.c('query').attrs({xmlns : 'jabber:iq:roster'}); |
---|
[2471] | 723 | newContact = newContact.c('item').attrs({jid: jidTo, name:name }); |
---|
[2397] | 724 | newContact = newContact.c('group').t(group).tree(); |
---|
| 725 | |
---|
| 726 | TrophyIM.connection.send(newContact); |
---|
| 727 | }, |
---|
| 728 | |
---|
| 729 | /** Function: addMessage |
---|
| 730 | * |
---|
| 731 | * Parameters: |
---|
| 732 | * (string) msg - the message to add |
---|
| 733 | * (string) jid - the jid of chat box to add the message to. |
---|
| 734 | */ |
---|
| 735 | |
---|
| 736 | addMessage : function(msg, jid) |
---|
| 737 | { |
---|
| 738 | var chatBox = document.getElementById(jid + "__chatBox"); |
---|
| 739 | var messageDiv = document.createElement("div"); |
---|
| 740 | |
---|
| 741 | messageDiv.style.margin = "3px 0px 3px 3px"; |
---|
| 742 | messageDiv.innerHTML = msg.contact + " : " + msg.msg ; |
---|
| 743 | |
---|
| 744 | chatBox.appendChild(messageDiv); |
---|
| 745 | chatBox.scrollTop = chatBox.scrollHeight; |
---|
| 746 | }, |
---|
| 747 | |
---|
[2437] | 748 | /** Function: removeContact |
---|
| 749 | * |
---|
| 750 | * Parameters: |
---|
| 751 | * (string) jidTo |
---|
| 752 | */ |
---|
| 753 | |
---|
[2491] | 754 | removeContact : function(jidTo, indexTo) |
---|
[2437] | 755 | { |
---|
[2491] | 756 | var divItenContact = null; |
---|
| 757 | var spanShow = null; |
---|
[2437] | 758 | |
---|
[2491] | 759 | if( ( divItenContact = document.getElementById('itenContact_' + jidTo + '_' + indexTo ))) |
---|
[2437] | 760 | { |
---|
[2491] | 761 | spanShow = document.getElementById('span_show_itenContact_' + jidTo + '_' + indexTo ) |
---|
| 762 | |
---|
| 763 | spanShow.parentNode.removeChild(spanShow); |
---|
| 764 | |
---|
[2471] | 765 | loadIM.removeGroup( divItenContact.parentNode ); |
---|
| 766 | |
---|
[2437] | 767 | divItenContact.parentNode.removeChild(divItenContact); |
---|
| 768 | |
---|
| 769 | // Remove Contact |
---|
[2491] | 770 | var _id = TrophyIM.connection.getUniqueId('del'); |
---|
[2471] | 771 | var delContact = $iq({type: 'set', id: _id}) |
---|
[2437] | 772 | delContact = delContact.c('query').attrs({xmlns : 'jabber:iq:roster'}); |
---|
[2471] | 773 | delContact = delContact.c('item').attrs({jid: jidTo, subscription:'remove'}).tree(); |
---|
[2437] | 774 | |
---|
| 775 | TrophyIM.connection.send(delContact); |
---|
| 776 | } |
---|
| 777 | }, |
---|
| 778 | |
---|
[2397] | 779 | /** Function: renderRoster |
---|
| 780 | * |
---|
| 781 | * Renders roster, looking only for jids flagged by setPresence as having |
---|
| 782 | * changed. |
---|
| 783 | */ |
---|
| 784 | |
---|
| 785 | renderRoster : function() |
---|
| 786 | { |
---|
[2471] | 787 | var roster_div = document.getElementById('JabberIMRoster'); |
---|
| 788 | |
---|
| 789 | if( roster_div ) |
---|
[2397] | 790 | { |
---|
[2471] | 791 | var users = new Array(); |
---|
[2397] | 792 | |
---|
[2471] | 793 | for( var user in TrophyIM.rosterObj.roster ) |
---|
[2397] | 794 | { |
---|
[2471] | 795 | users[users.length] = TrophyIM.rosterObj.roster[user].contact.jid; |
---|
| 796 | } |
---|
| 797 | |
---|
| 798 | users.sort(); |
---|
| 799 | |
---|
| 800 | var groups = new Array(); |
---|
| 801 | var flagGeral = false; |
---|
| 802 | |
---|
| 803 | for (var group in TrophyIM.rosterObj.groups) |
---|
| 804 | { |
---|
| 805 | if( group ) |
---|
| 806 | groups[groups.length] = group; |
---|
[2397] | 807 | |
---|
[2471] | 808 | if( group == "Geral" ) |
---|
| 809 | flagGeral = true; |
---|
| 810 | } |
---|
| 811 | |
---|
[2491] | 812 | if( !flagGeral && users.length > 0 ) |
---|
[2471] | 813 | groups[groups.length] = "Geral"; |
---|
[2397] | 814 | |
---|
[2471] | 815 | groups.sort(); |
---|
| 816 | |
---|
| 817 | for ( var i = 0; i < groups.length; i++ ) |
---|
| 818 | { |
---|
| 819 | TrophyIM.renderGroups( groups[i] , roster_div ); |
---|
[2397] | 820 | } |
---|
[2471] | 821 | |
---|
| 822 | TrophyIM.renderItensGroup( users, roster_div ); |
---|
[2397] | 823 | } |
---|
| 824 | |
---|
| 825 | setTimeout("TrophyIM.renderRoster()", 1000 ); |
---|
| 826 | }, |
---|
| 827 | |
---|
| 828 | /** Function: renderGroups |
---|
| 829 | * |
---|
| 830 | * |
---|
| 831 | */ |
---|
| 832 | |
---|
| 833 | renderGroups: function( nameGroup, element ) |
---|
| 834 | { |
---|
| 835 | var _addGroup = function() |
---|
| 836 | { |
---|
[2471] | 837 | var _nameGroup = nameGroup; |
---|
| 838 | var _element = element; |
---|
| 839 | |
---|
[2397] | 840 | var paramsGroup = |
---|
| 841 | { |
---|
[2471] | 842 | 'nameGroup' : _nameGroup, |
---|
[2397] | 843 | 'path_jabberit' : path_jabberit |
---|
| 844 | } |
---|
| 845 | |
---|
[2471] | 846 | _element.innerHTML += loadIM.parse("group","groups.xsl", paramsGroup); |
---|
[2397] | 847 | } |
---|
| 848 | |
---|
| 849 | if( !element.hasChildNodes() ) |
---|
| 850 | { |
---|
[2471] | 851 | _addGroup(); |
---|
[2397] | 852 | } |
---|
| 853 | else |
---|
| 854 | { |
---|
[2471] | 855 | var _NodeChild = element.firstChild; |
---|
| 856 | var flagAdd = false; |
---|
[2397] | 857 | |
---|
[2471] | 858 | while( _NodeChild ) |
---|
[2397] | 859 | { |
---|
[2471] | 860 | if( _NodeChild.childNodes[0].nodeName.toLowerCase() === "span" ) |
---|
[2397] | 861 | { |
---|
[2471] | 862 | if( _NodeChild.childNodes[0].childNodes[0].nodeValue === nameGroup ) |
---|
| 863 | { |
---|
| 864 | flagAdd = true; |
---|
| 865 | } |
---|
[2397] | 866 | } |
---|
| 867 | |
---|
[2471] | 868 | _NodeChild = _NodeChild.nextSibling; |
---|
[2397] | 869 | } |
---|
[2471] | 870 | |
---|
| 871 | if( !flagAdd ) |
---|
| 872 | { |
---|
| 873 | _addGroup(); |
---|
| 874 | } |
---|
[2397] | 875 | } |
---|
| 876 | }, |
---|
| 877 | |
---|
| 878 | /** Function: renderItensGroup |
---|
| 879 | * |
---|
| 880 | * |
---|
| 881 | */ |
---|
| 882 | |
---|
| 883 | renderItensGroup : function( users, element ) |
---|
| 884 | { |
---|
[2471] | 885 | var addItem = function() |
---|
[2397] | 886 | { |
---|
| 887 | if( arguments.length > 0 ) |
---|
| 888 | { |
---|
| 889 | var objContact = arguments[0]; |
---|
[2471] | 890 | var group = arguments[1]; |
---|
| 891 | var element = arguments[2]; |
---|
| 892 | var index = arguments[3]; |
---|
[2491] | 893 | |
---|
[2471] | 894 | var itensJid = document.getElementById( 'itenContact_' + objContact.contact.jid + '_' + index ); |
---|
| 895 | |
---|
[2397] | 896 | if( itensJid == null ) |
---|
| 897 | { |
---|
| 898 | // Name |
---|
| 899 | var nameContact = ""; |
---|
| 900 | |
---|
| 901 | if ( objContact.contact.name ) |
---|
| 902 | nameContact = objContact.contact.name; |
---|
| 903 | else |
---|
| 904 | { |
---|
| 905 | nameContact = objContact.contact.jid; |
---|
| 906 | nameContact = nameContact.substring(0, nameContact.indexOf('@')); |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | // Presence e Status |
---|
| 910 | var presence = "unavailable"; |
---|
| 911 | var status = ""; |
---|
| 912 | var statusDisplay = "none"; |
---|
| 913 | |
---|
| 914 | if (objContact.presence) |
---|
| 915 | { |
---|
| 916 | for (var resource in objContact.presence) |
---|
| 917 | { |
---|
[2471] | 918 | if( objContact.presence[resource].show != 'invisible' ) |
---|
| 919 | presence = objContact.presence[resource].show; |
---|
| 920 | |
---|
[2397] | 921 | if( objContact.presence[resource].status ) |
---|
| 922 | { |
---|
| 923 | status = " ( " + objContact.presence[resource].status + " ) "; |
---|
| 924 | statusDisplay = "block"; |
---|
| 925 | } |
---|
| 926 | } |
---|
| 927 | } |
---|
| 928 | |
---|
| 929 | var paramsContact = |
---|
| 930 | { |
---|
| 931 | 'nameContact' : nameContact, |
---|
| 932 | 'jid' : objContact.contact.jid, |
---|
[2471] | 933 | 'id' : 'itenContact_' + objContact.contact.jid + '_' + index , |
---|
| 934 | 'index' : index, |
---|
| 935 | 'path_jabberit' : path_jabberit, |
---|
[2397] | 936 | 'presence' : presence, |
---|
| 937 | 'status' : status, |
---|
[2491] | 938 | 'statusDisplay' : statusDisplay, |
---|
| 939 | 'subscription' : objContact.contact.subscription |
---|
[2397] | 940 | } |
---|
| 941 | |
---|
[2471] | 942 | if( group != "") |
---|
[2397] | 943 | { |
---|
[2471] | 944 | var _NodeChild = element.firstChild; |
---|
| 945 | |
---|
| 946 | while( _NodeChild ) |
---|
[2397] | 947 | { |
---|
[2471] | 948 | if( _NodeChild.childNodes[0].nodeName.toLowerCase() === "span" ) |
---|
| 949 | { |
---|
| 950 | if( _NodeChild.childNodes[0].childNodes[0].nodeValue === group ) |
---|
| 951 | { |
---|
| 952 | _NodeChild.innerHTML += loadIM.parse("itens_group", "itensGroup.xsl", paramsContact); |
---|
| 953 | } |
---|
| 954 | } |
---|
[2397] | 955 | |
---|
[2471] | 956 | _NodeChild = _NodeChild.nextSibling; |
---|
[2397] | 957 | } |
---|
[2471] | 958 | } |
---|
[2397] | 959 | } |
---|
| 960 | else |
---|
| 961 | { |
---|
| 962 | // Presence e Status |
---|
| 963 | var presence = "unavailable"; |
---|
| 964 | var status = ""; |
---|
| 965 | var statusDisplay = "none"; |
---|
| 966 | |
---|
| 967 | if (objContact.presence) |
---|
| 968 | { |
---|
[2471] | 969 | for ( var resource in objContact.presence ) |
---|
[2397] | 970 | { |
---|
[2491] | 971 | if( objContact.presence[resource].show != 'invisible' ) |
---|
| 972 | presence = objContact.presence[resource].show; |
---|
| 973 | |
---|
[2397] | 974 | if( objContact.presence[resource].status ) |
---|
| 975 | { |
---|
[2471] | 976 | status = " ( " + objContact.presence[resource].status + " ) "; |
---|
[2397] | 977 | statusDisplay = "block"; |
---|
| 978 | } |
---|
[2471] | 979 | } |
---|
| 980 | } |
---|
| 981 | |
---|
| 982 | var is_open = itensJid.parentNode.childNodes[0].style.backgroundImage; |
---|
| 983 | is_open = is_open.indexOf("arrow_down.gif"); |
---|
| 984 | |
---|
| 985 | with ( document.getElementById('span_show_' + 'itenContact_' + objContact.contact.jid + '_' + index ) ) |
---|
| 986 | { |
---|
| 987 | if( is_open > 0 ) |
---|
| 988 | { |
---|
| 989 | style.display = statusDisplay; |
---|
| 990 | innerHTML = status; |
---|
[2397] | 991 | } |
---|
| 992 | } |
---|
[2471] | 993 | |
---|
| 994 | itensJid.style.background = "url('"+path_jabberit+"templates/default/images/" + presence + ".gif')no-repeat center left"; |
---|
[2397] | 995 | } |
---|
| 996 | } |
---|
| 997 | } |
---|
| 998 | |
---|
| 999 | for( var i = 0 ; i < users.length; i++ ) |
---|
| 1000 | { |
---|
| 1001 | if( TrophyIM.rosterObj.roster[users[i]].contact.groups ) |
---|
[2471] | 1002 | { |
---|
| 1003 | var groups = TrophyIM.rosterObj.roster[users[i]].contact.groups; |
---|
| 1004 | |
---|
| 1005 | if( groups.length > 0 ) |
---|
| 1006 | { |
---|
| 1007 | for( var j = 0; j < groups.length; j++ ) |
---|
| 1008 | addItem( TrophyIM.rosterObj.roster[users[i]], groups[j], element, j ); |
---|
| 1009 | } |
---|
[2397] | 1010 | else |
---|
[2471] | 1011 | { |
---|
| 1012 | addItem( TrophyIM.rosterObj.roster[users[i]], "Geral", element, 0 ); |
---|
| 1013 | } |
---|
| 1014 | } |
---|
[2397] | 1015 | else |
---|
[2471] | 1016 | { |
---|
| 1017 | addItem( TrophyIM.rosterObj.roster[users[i]], "Geral", element, 0 ); |
---|
| 1018 | } |
---|
[2397] | 1019 | } |
---|
| 1020 | }, |
---|
| 1021 | |
---|
| 1022 | /** Function: rosterClick |
---|
| 1023 | * |
---|
| 1024 | * Handles actions when a roster item is clicked |
---|
| 1025 | */ |
---|
| 1026 | |
---|
| 1027 | rosterClick : function(fulljid) |
---|
| 1028 | { |
---|
| 1029 | TrophyIM.makeChat(fulljid); |
---|
| 1030 | }, |
---|
| 1031 | |
---|
| 1032 | |
---|
| 1033 | /** Function SetAutorization |
---|
| 1034 | * |
---|
| 1035 | */ |
---|
| 1036 | |
---|
[2491] | 1037 | setAutorization : function( jidTo, jidFrom, _typeSubscription ) |
---|
[2397] | 1038 | { |
---|
[2491] | 1039 | var _id = TrophyIM.connection.getUniqueId(); |
---|
| 1040 | |
---|
| 1041 | TrophyIM.connection.send($pres( ).attrs( {to: jidTo, from: jidFrom, type: _typeSubscription, id: _id}).tree()); |
---|
[2397] | 1042 | }, |
---|
| 1043 | |
---|
| 1044 | /** Function: setPresence |
---|
| 1045 | * |
---|
| 1046 | */ |
---|
| 1047 | |
---|
| 1048 | setPresence : function( show ) |
---|
| 1049 | { |
---|
| 1050 | TrophyIM.connection.send($pres( ).c('show').t(show).tree()); |
---|
| 1051 | }, |
---|
| 1052 | |
---|
| 1053 | /** Function: sendMessage |
---|
| 1054 | * |
---|
| 1055 | * Send message from chat input to user |
---|
| 1056 | */ |
---|
| 1057 | |
---|
| 1058 | sendMessage : function() |
---|
| 1059 | { |
---|
| 1060 | |
---|
| 1061 | if( arguments.length > 0 ) |
---|
| 1062 | { |
---|
| 1063 | var jidTo = arguments[0]; |
---|
| 1064 | var message_input = document.getElementById(jidTo + "__sendBox").value; |
---|
| 1065 | |
---|
| 1066 | if( ( message_input = message_input.replace(/^\s+|\s+$|^\n|\n$/g,"") ) != "" ) |
---|
| 1067 | { |
---|
| 1068 | // Send Message |
---|
| 1069 | TrophyIM.connection.send($msg({to: jidTo, from: TrophyIM.connection.jid, type: 'chat'}).c('body').t(message_input).tree()); |
---|
| 1070 | |
---|
| 1071 | var message = |
---|
| 1072 | { |
---|
| 1073 | contact : "<font style='font-weight:bold; color:red;'>" + "Eu" + "</font>", |
---|
| 1074 | msg : message_input |
---|
| 1075 | } |
---|
| 1076 | |
---|
| 1077 | // Add Message in chatBox; |
---|
| 1078 | TrophyIM.addMessage( message, jidTo); |
---|
| 1079 | document.getElementById(jidTo + "__sendBox").value = ""; |
---|
| 1080 | document.getElementById(jidTo + "__sendBox").focus(); |
---|
| 1081 | } |
---|
| 1082 | } |
---|
| 1083 | } |
---|
| 1084 | }; |
---|
| 1085 | |
---|
| 1086 | /** Class: TrophyIMRoster |
---|
| 1087 | * |
---|
| 1088 | * |
---|
| 1089 | * This object stores the roster and presence info for the TrophyIMClient |
---|
| 1090 | * |
---|
| 1091 | * roster[jid_lower]['contact'] |
---|
| 1092 | * roster[jid_lower]['presence'][resource] |
---|
| 1093 | */ |
---|
| 1094 | function TrophyIMRoster() |
---|
| 1095 | { |
---|
| 1096 | /** Constants: internal arrays |
---|
| 1097 | * (Object) roster - the actual roster/presence information |
---|
| 1098 | * (Object) groups - list of current groups in the roster |
---|
| 1099 | * (Array) changes - array of jids with presence changes |
---|
| 1100 | */ |
---|
| 1101 | if (TrophyIM.JSONStore.store_working) |
---|
| 1102 | { |
---|
| 1103 | var data = TrophyIM.JSONStore.getData(['roster', 'groups']); |
---|
| 1104 | this.roster = (data['roster'] != null) ? data['roster'] : {}; |
---|
| 1105 | this.groups = (data['groups'] != null) ? data['groups'] : {}; |
---|
| 1106 | } |
---|
| 1107 | else |
---|
| 1108 | { |
---|
| 1109 | this.roster = {}; |
---|
| 1110 | this.groups = {}; |
---|
| 1111 | } |
---|
| 1112 | this.changes = new Array(); |
---|
| 1113 | |
---|
| 1114 | if (TrophyIM.constants.stale_roster) |
---|
| 1115 | { |
---|
| 1116 | for (var jid in this.roster) |
---|
| 1117 | { |
---|
| 1118 | this.changes[this.changes.length] = jid; |
---|
| 1119 | } |
---|
| 1120 | } |
---|
[2471] | 1121 | |
---|
| 1122 | /** Function: addChange |
---|
| 1123 | * |
---|
| 1124 | * Adds given jid to this.changes, keeping this.changes sorted and |
---|
| 1125 | * preventing duplicates. |
---|
| 1126 | * |
---|
| 1127 | * Parameters |
---|
| 1128 | * (String) jid : jid to add to this.changes |
---|
| 1129 | */ |
---|
| 1130 | |
---|
| 1131 | this.addChange = function(jid) |
---|
| 1132 | { |
---|
| 1133 | for (var c = 0; c < this.changes.length; c++) |
---|
| 1134 | { |
---|
| 1135 | if (this.changes[c] == jid) |
---|
| 1136 | { |
---|
| 1137 | return; |
---|
| 1138 | } |
---|
| 1139 | } |
---|
| 1140 | |
---|
| 1141 | this.changes[this.changes.length] = jid; |
---|
| 1142 | |
---|
| 1143 | this.changes.sort(); |
---|
| 1144 | } |
---|
[2397] | 1145 | |
---|
| 1146 | /** Function: addContact |
---|
| 1147 | * |
---|
| 1148 | * Adds given contact to roster |
---|
| 1149 | * |
---|
| 1150 | * Parameters: |
---|
| 1151 | * (String) jid - bare jid |
---|
| 1152 | * (String) subscription - subscription attribute for contact |
---|
| 1153 | * (String) name - name attribute for contact |
---|
[2471] | 1154 | * (Array) groups - array of groups contact is member of |
---|
[2397] | 1155 | */ |
---|
| 1156 | |
---|
[2471] | 1157 | this.addContact = function(jid, subscription, name, groups ) |
---|
[2397] | 1158 | { |
---|
[2471] | 1159 | if( jid != loadIM.getUserCurrent().jid ) |
---|
| 1160 | { |
---|
| 1161 | if( subscription !== "remove" ) |
---|
| 1162 | { |
---|
| 1163 | var contact = { jid:jid, subscription:subscription, name:name, groups:groups } |
---|
| 1164 | var jid_lower = jid.toLowerCase(); |
---|
| 1165 | |
---|
| 1166 | if ( this.roster[jid_lower] ) |
---|
| 1167 | { |
---|
| 1168 | this.roster[jid_lower]['contact'] = contact; |
---|
| 1169 | } |
---|
| 1170 | else |
---|
| 1171 | { |
---|
| 1172 | this.roster[jid_lower] = {contact:contact}; |
---|
| 1173 | } |
---|
| 1174 | |
---|
| 1175 | groups = groups ? groups : ['']; |
---|
| 1176 | |
---|
| 1177 | for ( var g = 0; g < groups.length; g++ ) |
---|
| 1178 | { |
---|
| 1179 | if ( !this.groups[groups[g]] ) |
---|
| 1180 | { |
---|
| 1181 | this.groups[groups[g]] = {}; |
---|
| 1182 | } |
---|
| 1183 | |
---|
| 1184 | this.groups[groups[g]][jid_lower] = jid_lower; |
---|
| 1185 | } |
---|
| 1186 | } |
---|
| 1187 | else |
---|
| 1188 | { |
---|
| 1189 | this.removeContact(jid); |
---|
| 1190 | } |
---|
| 1191 | } |
---|
[2397] | 1192 | } |
---|
[2471] | 1193 | |
---|
[2397] | 1194 | /** Function: getContact |
---|
| 1195 | * |
---|
| 1196 | * Returns contact entry for given jid |
---|
| 1197 | * |
---|
| 1198 | * Parameter: (String) jid - jid to return |
---|
| 1199 | */ |
---|
[2471] | 1200 | |
---|
[2397] | 1201 | this.getContact = function(jid) |
---|
| 1202 | { |
---|
| 1203 | if (this.roster[jid.toLowerCase()]) |
---|
| 1204 | { |
---|
| 1205 | return this.roster[jid.toLowerCase()]['contact']; |
---|
| 1206 | } |
---|
| 1207 | } |
---|
[2471] | 1208 | |
---|
| 1209 | /** Function: getPresence |
---|
| 1210 | * |
---|
| 1211 | * Returns best presence for given jid as Array(resource, priority, show, |
---|
| 1212 | * status) |
---|
| 1213 | * |
---|
| 1214 | * Parameter: (String) fulljid - jid to return best presence for |
---|
| 1215 | */ |
---|
| 1216 | |
---|
| 1217 | this.getPresence = function(fulljid) |
---|
| 1218 | { |
---|
| 1219 | var jid = Strophe.getBareJidFromJid(fulljid); |
---|
| 1220 | var current = null; |
---|
| 1221 | |
---|
| 1222 | if (this.roster[jid.toLowerCase()] && this.roster[jid.toLowerCase()]['presence']) |
---|
| 1223 | { |
---|
| 1224 | for (var resource in this.roster[jid.toLowerCase()]['presence']) |
---|
| 1225 | { |
---|
| 1226 | var presence = this.roster[jid.toLowerCase()]['presence'][resource]; |
---|
| 1227 | if (current == null) |
---|
| 1228 | { |
---|
| 1229 | current = presence |
---|
| 1230 | } |
---|
| 1231 | else |
---|
| 1232 | { |
---|
| 1233 | if(presence['priority'] > current['priority'] && ((presence['show'] == "chat" |
---|
| 1234 | || presence['show'] == "available") || (current['show'] != "chat" || |
---|
| 1235 | current['show'] != "available"))) |
---|
| 1236 | { |
---|
| 1237 | current = presence |
---|
| 1238 | } |
---|
| 1239 | } |
---|
| 1240 | } |
---|
| 1241 | } |
---|
| 1242 | return current; |
---|
| 1243 | } |
---|
| 1244 | |
---|
| 1245 | /** Function: groupHasChanges |
---|
| 1246 | * |
---|
| 1247 | * Returns true if current group has members in this.changes |
---|
| 1248 | * |
---|
| 1249 | * Parameters: |
---|
| 1250 | * (String) group - name of group to check |
---|
| 1251 | */ |
---|
| 1252 | |
---|
| 1253 | this.groupHasChanges = function(group) |
---|
| 1254 | { |
---|
| 1255 | for (var c = 0; c < this.changes.length; c++) |
---|
| 1256 | { |
---|
| 1257 | if (this.groups[group][this.changes[c]]) |
---|
| 1258 | { |
---|
| 1259 | return true; |
---|
| 1260 | } |
---|
| 1261 | } |
---|
| 1262 | return false; |
---|
| 1263 | } |
---|
| 1264 | |
---|
| 1265 | /** Function removeContact |
---|
| 1266 | * |
---|
| 1267 | * Parameters |
---|
| 1268 | * (String) jid |
---|
| 1269 | */ |
---|
| 1270 | |
---|
| 1271 | this.removeContact = function(jid) |
---|
| 1272 | { |
---|
| 1273 | var groups = this.roster[ jid ].contact.groups; |
---|
| 1274 | |
---|
[2491] | 1275 | if( groups ) |
---|
[2471] | 1276 | { |
---|
[2491] | 1277 | |
---|
| 1278 | for ( var i = 0; i < groups.length; i++ ) |
---|
| 1279 | { |
---|
| 1280 | delete this.groups[ groups[ i ] ][ jid ]; |
---|
| 1281 | } |
---|
[2471] | 1282 | |
---|
[2491] | 1283 | for ( var i = 0; i < groups.length; i++ ) |
---|
| 1284 | { |
---|
| 1285 | var contacts = 0; |
---|
| 1286 | for ( var contact in this.groups[ groups[ i ] ] ) |
---|
| 1287 | contacts++; |
---|
| 1288 | |
---|
| 1289 | if ( ! contacts ) |
---|
| 1290 | delete this.groups[ groups[ i ] ]; |
---|
| 1291 | } |
---|
[2471] | 1292 | } |
---|
| 1293 | |
---|
| 1294 | // Delete Object roster |
---|
| 1295 | if( this.roster[jid] ) |
---|
| 1296 | delete this.roster[jid]; |
---|
| 1297 | } |
---|
| 1298 | |
---|
[2397] | 1299 | /** Function: setPresence |
---|
| 1300 | * |
---|
| 1301 | * Sets presence |
---|
| 1302 | * |
---|
| 1303 | * Parameters: |
---|
| 1304 | * (String) fulljid: full jid with presence |
---|
| 1305 | * (Integer) priority: priority attribute from presence |
---|
| 1306 | * (String) show: show attribute from presence |
---|
| 1307 | * (String) status: status attribute from presence |
---|
| 1308 | */ |
---|
| 1309 | |
---|
| 1310 | this.setPresence = function(fulljid, priority, show, status) |
---|
| 1311 | { |
---|
| 1312 | var barejid = Strophe.getBareJidFromJid(fulljid); |
---|
| 1313 | var resource = Strophe.getResourceFromJid(fulljid); |
---|
| 1314 | var jid_lower = barejid.toLowerCase(); |
---|
| 1315 | |
---|
[2471] | 1316 | if( barejid != loadIM.getUserCurrent().jid ) |
---|
| 1317 | { |
---|
| 1318 | if( show != 'unavailable') |
---|
[2397] | 1319 | { |
---|
[2471] | 1320 | if (!this.roster[jid_lower]) |
---|
| 1321 | { |
---|
| 1322 | this.addContact(barejid, 'not-in-roster'); |
---|
| 1323 | } |
---|
| 1324 | var presence = |
---|
| 1325 | { |
---|
| 1326 | resource:resource, priority:priority, show:show, status:status |
---|
| 1327 | } |
---|
| 1328 | |
---|
| 1329 | if (!this.roster[jid_lower]['presence']) |
---|
| 1330 | { |
---|
| 1331 | this.roster[jid_lower]['presence'] = {} |
---|
| 1332 | } |
---|
| 1333 | this.roster[jid_lower]['presence'][resource] = presence |
---|
| 1334 | } |
---|
| 1335 | else if (this.roster[jid_lower] && this.roster[jid_lower]['presence'] && this.roster[jid_lower]['presence'][resource]) |
---|
[2397] | 1336 | { |
---|
[2471] | 1337 | delete this.roster[jid_lower]['presence'][resource]; |
---|
| 1338 | } |
---|
| 1339 | |
---|
| 1340 | this.addChange(jid_lower); |
---|
| 1341 | |
---|
| 1342 | if (TrophyIM.activeChats['divs'][jid_lower]) |
---|
[2397] | 1343 | { |
---|
[2471] | 1344 | TrophyIM.setTabPresence(jid_lower, TrophyIM.activeChats['divs'][jid_lower]['tab']); |
---|
| 1345 | } |
---|
[2397] | 1346 | } |
---|
[2471] | 1347 | } |
---|
| 1348 | |
---|
| 1349 | /** Fuction: save |
---|
| 1350 | * |
---|
| 1351 | * Saves roster data to JSON store |
---|
| 1352 | */ |
---|
| 1353 | |
---|
| 1354 | this.save = function() |
---|
| 1355 | { |
---|
| 1356 | if (TrophyIM.JSONStore.store_working) |
---|
[2397] | 1357 | { |
---|
[2471] | 1358 | TrophyIM.JSONStore.setData({roster:this.roster, |
---|
| 1359 | groups:this.groups, active_chat:TrophyIM.activeChats['current'], |
---|
| 1360 | chat_history:TrophyIM.chatHistory}); |
---|
| 1361 | } |
---|
| 1362 | } |
---|
| 1363 | |
---|
[2397] | 1364 | } |
---|
| 1365 | /** Class: TrophyIMJSONStore |
---|
| 1366 | * |
---|
| 1367 | * |
---|
| 1368 | * This object is the mechanism by which TrophyIM stores and retrieves its |
---|
| 1369 | * variables from the url provided by TROPHYIM_JSON_STORE |
---|
| 1370 | * |
---|
| 1371 | */ |
---|
| 1372 | function TrophyIMJSONStore() { |
---|
| 1373 | this.store_working = false; |
---|
| 1374 | /** Function _newXHR |
---|
| 1375 | * |
---|
| 1376 | * Set up new cross-browser xmlhttprequest object |
---|
| 1377 | * |
---|
| 1378 | * Parameters: |
---|
| 1379 | * (function) handler = what to set onreadystatechange to |
---|
| 1380 | */ |
---|
| 1381 | this._newXHR = function (handler) { |
---|
| 1382 | var xhr = null; |
---|
| 1383 | if (window.XMLHttpRequest) { |
---|
| 1384 | xhr = new XMLHttpRequest(); |
---|
| 1385 | if (xhr.overrideMimeType) { |
---|
| 1386 | xhr.overrideMimeType("text/xml"); |
---|
| 1387 | } |
---|
| 1388 | } else if (window.ActiveXObject) { |
---|
| 1389 | xhr = new ActiveXObject("Microsoft.XMLHTTP"); |
---|
| 1390 | } |
---|
| 1391 | return xhr; |
---|
| 1392 | } |
---|
| 1393 | /** Function getData |
---|
| 1394 | * Gets data from JSONStore |
---|
| 1395 | * |
---|
| 1396 | * Parameters: |
---|
| 1397 | * (Array) vars = Variables to get from JSON store |
---|
| 1398 | * |
---|
| 1399 | * Returns: |
---|
| 1400 | * Object with variables indexed by names given in parameter 'vars' |
---|
| 1401 | */ |
---|
| 1402 | this.getData = function(vars) { |
---|
| 1403 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
| 1404 | Strophe.debug("Retrieving JSONStore data"); |
---|
| 1405 | var xhr = this._newXHR(); |
---|
| 1406 | var getdata = "get=" + vars.join(","); |
---|
| 1407 | try { |
---|
| 1408 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
| 1409 | } catch (e) { |
---|
| 1410 | Strophe.error("JSONStore open failed."); |
---|
| 1411 | return false; |
---|
| 1412 | } |
---|
| 1413 | xhr.setRequestHeader('Content-type', |
---|
| 1414 | 'application/x-www-form-urlencoded'); |
---|
| 1415 | xhr.setRequestHeader('Content-length', getdata.length); |
---|
| 1416 | xhr.send(getdata); |
---|
| 1417 | if (xhr.readyState == 4 && xhr.status == 200) { |
---|
| 1418 | try { |
---|
| 1419 | var dataObj = JSON.parse(xhr.responseText); |
---|
| 1420 | return this.emptyFix(dataObj); |
---|
| 1421 | } catch(e) { |
---|
| 1422 | Strophe.error("Could not parse JSONStore response" + |
---|
| 1423 | xhr.responseText); |
---|
| 1424 | return false; |
---|
| 1425 | } |
---|
| 1426 | } else { |
---|
| 1427 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
| 1428 | return false; |
---|
| 1429 | } |
---|
| 1430 | } |
---|
| 1431 | } |
---|
| 1432 | /** Function emptyFix |
---|
| 1433 | * Fix for bugs in external JSON implementations such as |
---|
| 1434 | * http://bugs.php.net/bug.php?id=41504. |
---|
| 1435 | * A.K.A. Don't use PHP, people. |
---|
| 1436 | */ |
---|
| 1437 | this.emptyFix = function(obj) { |
---|
| 1438 | if (typeof(obj) == "object") { |
---|
| 1439 | for (var i in obj) { |
---|
| 1440 | if (i == '_empty_') { |
---|
| 1441 | obj[""] = this.emptyFix(obj['_empty_']); |
---|
| 1442 | delete obj['_empty_']; |
---|
| 1443 | } else { |
---|
| 1444 | obj[i] = this.emptyFix(obj[i]); |
---|
| 1445 | } |
---|
| 1446 | } |
---|
| 1447 | } |
---|
| 1448 | return obj |
---|
| 1449 | } |
---|
| 1450 | /** Function delData |
---|
| 1451 | * Deletes data from JSONStore |
---|
| 1452 | * |
---|
| 1453 | * Parameters: |
---|
| 1454 | * (Array) vars = Variables to delete from JSON store |
---|
| 1455 | * |
---|
| 1456 | * Returns: |
---|
| 1457 | * Status of delete attempt. |
---|
| 1458 | */ |
---|
| 1459 | this.delData = function(vars) { |
---|
| 1460 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
| 1461 | Strophe.debug("Retrieving JSONStore data"); |
---|
| 1462 | var xhr = this._newXHR(); |
---|
| 1463 | var deldata = "del=" + vars.join(","); |
---|
| 1464 | try { |
---|
| 1465 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
| 1466 | } catch (e) { |
---|
| 1467 | Strophe.error("JSONStore open failed."); |
---|
| 1468 | return false; |
---|
| 1469 | } |
---|
| 1470 | xhr.setRequestHeader('Content-type', |
---|
| 1471 | 'application/x-www-form-urlencoded'); |
---|
| 1472 | xhr.setRequestHeader('Content-length', deldata.length); |
---|
| 1473 | xhr.send(deldata); |
---|
| 1474 | if (xhr.readyState == 4 && xhr.status == 200) { |
---|
| 1475 | try { |
---|
| 1476 | var dataObj = JSON.parse(xhr.responseText); |
---|
| 1477 | return dataObj; |
---|
| 1478 | } catch(e) { |
---|
| 1479 | Strophe.error("Could not parse JSONStore response"); |
---|
| 1480 | return false; |
---|
| 1481 | } |
---|
| 1482 | } else { |
---|
| 1483 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
| 1484 | return false; |
---|
| 1485 | } |
---|
| 1486 | } |
---|
| 1487 | } |
---|
| 1488 | /** Function setData |
---|
| 1489 | * Stores data in JSONStore, overwriting values if they exist |
---|
| 1490 | * |
---|
| 1491 | * Parameters: |
---|
| 1492 | * (Object) vars : Object containing named vars to store ({name: value, |
---|
| 1493 | * othername: othervalue}) |
---|
| 1494 | * |
---|
| 1495 | * Returns: |
---|
| 1496 | * Status of storage attempt |
---|
| 1497 | */ |
---|
| 1498 | this.setData = function(vars) { |
---|
| 1499 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
| 1500 | Strophe.debug("Storing JSONStore data"); |
---|
| 1501 | var senddata = "set=" + JSON.stringify(vars); |
---|
| 1502 | var xhr = this._newXHR(); |
---|
| 1503 | try { |
---|
| 1504 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
| 1505 | } catch (e) { |
---|
| 1506 | Strophe.error("JSONStore open failed."); |
---|
| 1507 | return false; |
---|
| 1508 | } |
---|
| 1509 | xhr.setRequestHeader('Content-type', |
---|
| 1510 | 'application/x-www-form-urlencoded'); |
---|
| 1511 | xhr.setRequestHeader('Content-length', senddata.length); |
---|
| 1512 | xhr.send(senddata); |
---|
| 1513 | if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText == |
---|
| 1514 | "OK") { |
---|
| 1515 | return true; |
---|
| 1516 | } else { |
---|
| 1517 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
| 1518 | return false; |
---|
| 1519 | } |
---|
| 1520 | } |
---|
| 1521 | } |
---|
| 1522 | var testData = true; |
---|
| 1523 | if (this.setData({testData:testData})) { |
---|
| 1524 | var testResult = this.getData(['testData']); |
---|
| 1525 | if (testResult && testResult['testData'] == true) { |
---|
| 1526 | this.store_working = true; |
---|
| 1527 | } |
---|
| 1528 | } |
---|
| 1529 | } |
---|
| 1530 | /** Constants: Node types |
---|
| 1531 | * |
---|
| 1532 | * Implementations of constants that IE doesn't have, but we need. |
---|
| 1533 | */ |
---|
| 1534 | if (document.ELEMENT_NODE == null) { |
---|
| 1535 | document.ELEMENT_NODE = 1; |
---|
| 1536 | document.ATTRIBUTE_NODE = 2; |
---|
| 1537 | document.TEXT_NODE = 3; |
---|
| 1538 | document.CDATA_SECTION_NODE = 4; |
---|
| 1539 | document.ENTITY_REFERENCE_NODE = 5; |
---|
| 1540 | document.ENTITY_NODE = 6; |
---|
| 1541 | document.PROCESSING_INSTRUCTION_NODE = 7; |
---|
| 1542 | document.COMMENT_NODE = 8; |
---|
| 1543 | document.DOCUMENT_NODE = 9; |
---|
| 1544 | document.DOCUMENT_TYPE_NODE = 10; |
---|
| 1545 | document.DOCUMENT_FRAGMENT_NODE = 11; |
---|
| 1546 | document.NOTATION_NODE = 12; |
---|
| 1547 | } |
---|
| 1548 | |
---|
| 1549 | /** Function: importNode |
---|
| 1550 | * |
---|
| 1551 | * document.importNode implementation for IE, which doesn't have importNode |
---|
| 1552 | * |
---|
| 1553 | * Parameters: |
---|
| 1554 | * (Object) node - dom object |
---|
| 1555 | * (Boolean) allChildren - import node's children too |
---|
| 1556 | */ |
---|
| 1557 | if (!document.importNode) { |
---|
| 1558 | document.importNode = function(node, allChildren) { |
---|
| 1559 | switch (node.nodeType) { |
---|
| 1560 | case document.ELEMENT_NODE: |
---|
| 1561 | var newNode = document.createElement(node.nodeName); |
---|
| 1562 | if (node.attributes && node.attributes.length > 0) { |
---|
| 1563 | for(var i = 0; i < node.attributes.length; i++) { |
---|
| 1564 | newNode.setAttribute(node.attributes[i].nodeName, |
---|
| 1565 | node.getAttribute(node.attributes[i].nodeName)); |
---|
| 1566 | } |
---|
| 1567 | } |
---|
| 1568 | if (allChildren && node.childNodes && |
---|
| 1569 | node.childNodes.length > 0) { |
---|
| 1570 | for (var i = 0; i < node.childNodes.length; i++) { |
---|
| 1571 | newNode.appendChild(document.importNode( |
---|
| 1572 | node.childNodes[i], allChildren)); |
---|
| 1573 | } |
---|
| 1574 | } |
---|
| 1575 | return newNode; |
---|
| 1576 | break; |
---|
| 1577 | case document.TEXT_NODE: |
---|
| 1578 | case document.CDATA_SECTION_NODE: |
---|
| 1579 | case document.COMMENT_NODE: |
---|
| 1580 | return document.createTextNode(node.nodeValue); |
---|
| 1581 | break; |
---|
| 1582 | } |
---|
| 1583 | }; |
---|
| 1584 | } |
---|
| 1585 | |
---|
| 1586 | /** |
---|
| 1587 | * |
---|
| 1588 | * Bootstrap self into window.onload and window.onunload |
---|
| 1589 | */ |
---|
| 1590 | |
---|
| 1591 | /* |
---|
| 1592 | var oldonload = window.onload; |
---|
| 1593 | window.onload = function() |
---|
| 1594 | { |
---|
| 1595 | if(oldonload) |
---|
| 1596 | { |
---|
| 1597 | oldonload(); |
---|
| 1598 | } |
---|
| 1599 | |
---|
| 1600 | TrophyIM.load(); |
---|
| 1601 | }; |
---|
| 1602 | */ |
---|
| 1603 | |
---|
| 1604 | var oldonunload = window.onunload; |
---|
| 1605 | window.onunload = function() |
---|
| 1606 | { |
---|
| 1607 | if(oldonunload) |
---|
| 1608 | { |
---|
| 1609 | oldonunload(); |
---|
| 1610 | } |
---|
| 1611 | |
---|
| 1612 | TrophyIM.storeData(); |
---|
| 1613 | } |
---|