[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) { |
---|
[2670] | 66 | alert("TrophyIM Error: Cannot add html to page " + e.message); |
---|
[2397] | 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | }, |
---|
| 71 | /** Function: xmlRender |
---|
| 72 | * Uses browser-specific methods to turn given string into xml object |
---|
| 73 | * |
---|
| 74 | * Parameters: |
---|
| 75 | * (String) xml - the xml string to parse |
---|
| 76 | */ |
---|
| 77 | xmlRender : function(xmlString) { |
---|
| 78 | try {//IE |
---|
| 79 | var renderObj = new ActiveXObject("Microsoft.XMLDOM"); |
---|
| 80 | renderObj.async="false"; |
---|
| 81 | if(xmlString) { |
---|
| 82 | renderObj.loadXML(xmlString); |
---|
| 83 | } |
---|
| 84 | } catch (e) { |
---|
| 85 | try { //Firefox, Gecko, etc |
---|
| 86 | if (this.parser == undefined) { |
---|
| 87 | this.parser = new DOMParser(); |
---|
| 88 | } |
---|
| 89 | var renderObj = this.parser.parseFromString(xmlString, |
---|
| 90 | "application/xml"); |
---|
| 91 | } catch(e) { |
---|
| 92 | alert("TrophyIM Error: Cannot create new html for page"); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return renderObj; |
---|
| 97 | }, |
---|
| 98 | /** Function: getHTML |
---|
| 99 | * Returns named HTML snippet as DOM object |
---|
| 100 | * |
---|
| 101 | * Parameters: |
---|
| 102 | * (String) name - name of HTML snippet to retrieve (see HTMLSnippets |
---|
| 103 | * object) |
---|
| 104 | */ |
---|
| 105 | getHTML : function(page) |
---|
| 106 | { |
---|
| 107 | return this.xmlParse(HTMLSnippets[page]); |
---|
| 108 | }, |
---|
| 109 | |
---|
| 110 | /** Function: getScript |
---|
| 111 | * Returns script object with src to given script |
---|
| 112 | * |
---|
| 113 | * Parameters: |
---|
| 114 | * (String) script - name of script to put in src attribute of script |
---|
| 115 | * element |
---|
| 116 | */ |
---|
| 117 | getScript : function(script) |
---|
| 118 | { |
---|
| 119 | var newscript = document.createElement('script'); |
---|
| 120 | newscript.setAttribute('src', script); |
---|
| 121 | newscript.setAttribute('type', 'text/javascript'); |
---|
| 122 | return newscript; |
---|
| 123 | } |
---|
| 124 | }; |
---|
| 125 | |
---|
| 126 | /** Object: TrophyIM |
---|
| 127 | * |
---|
| 128 | * This is the actual TrophyIM application. It searches for the |
---|
| 129 | * 'trophyimclient' element and inserts itself into that. |
---|
| 130 | */ |
---|
| 131 | TrophyIM = { |
---|
| 132 | |
---|
[2641] | 133 | /** AutoConnection |
---|
| 134 | * |
---|
| 135 | */ |
---|
| 136 | |
---|
| 137 | autoConnection : { connect : true }, |
---|
[2647] | 138 | |
---|
| 139 | /** Object: chatHistory |
---|
| 140 | * |
---|
| 141 | * Stores chat history (last 10 message) and current presence of active |
---|
| 142 | * chat tabs. Indexed by jid. |
---|
| 143 | */ |
---|
| 144 | |
---|
| 145 | chatHistory : {}, |
---|
| 146 | |
---|
| 147 | /** Constants: |
---|
| 148 | * |
---|
| 149 | * (Boolean) stale_roster - roster is stale and needs to be rewritten. |
---|
| 150 | */ |
---|
| 151 | |
---|
| 152 | constants : {stale_roster: false}, |
---|
| 153 | |
---|
[2641] | 154 | /** PosWindow |
---|
| 155 | * |
---|
| 156 | */ |
---|
[2599] | 157 | posWindow : { left : 400, top : 100 }, |
---|
| 158 | |
---|
[2641] | 159 | /** StatusConnection |
---|
| 160 | * |
---|
| 161 | */ |
---|
| 162 | |
---|
[2397] | 163 | statusConn : { connected : false }, |
---|
| 164 | |
---|
[2647] | 165 | /** TimeOut Render Roster |
---|
| 166 | * |
---|
| 167 | * |
---|
| 168 | */ |
---|
| 169 | |
---|
| 170 | _timeOut : { renderRoster : null }, |
---|
| 171 | |
---|
[2397] | 172 | /** Function: setCookie |
---|
| 173 | * |
---|
| 174 | * Sets cookie name/value pair. Date and path are auto-selected. |
---|
| 175 | * |
---|
| 176 | * Parameters: |
---|
| 177 | * (String) name - the name of the cookie variable |
---|
| 178 | * (String) value - the value of the cookie variable |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | setCookie : function(name, value) |
---|
| 182 | { |
---|
| 183 | var expire = new Date(); |
---|
| 184 | expire.setDate(expire.getDate() + 365); |
---|
| 185 | document.cookie = name + "=" + value + "; expires=" + expire.toGMTString(); |
---|
| 186 | }, |
---|
| 187 | |
---|
| 188 | /** Function: delCookie |
---|
| 189 | * |
---|
| 190 | * Deletes cookie |
---|
| 191 | * |
---|
| 192 | * Parameters: |
---|
| 193 | * (String) name) - the name of the cookie to delete |
---|
| 194 | */ |
---|
| 195 | |
---|
| 196 | delCookie : function(name) |
---|
| 197 | { |
---|
| 198 | var expire = new Date(); |
---|
| 199 | expire.setDate(expire.getDate() - 365); |
---|
| 200 | document.cookie = name + "= ; expires=" + expire.toGMTString(); |
---|
| 201 | delete TrophyIM.cookies[name]; |
---|
| 202 | }, |
---|
| 203 | |
---|
| 204 | /** Function: getCookies |
---|
| 205 | * |
---|
| 206 | * Retrieves all trophyim cookies into an indexed object. Inteneded to be |
---|
| 207 | * called once, at which time the app refers to the returned object instead |
---|
| 208 | * of re-parsing the cookie string every time. |
---|
| 209 | * |
---|
| 210 | * Each cookie is also re-applied so as to refresh the expiry date. |
---|
| 211 | */ |
---|
| 212 | |
---|
| 213 | getCookies : function() |
---|
| 214 | { |
---|
| 215 | var cObj = {}; |
---|
| 216 | var cookies = document.cookie.split(';'); |
---|
[2647] | 217 | |
---|
| 218 | for(var i = 0 ; i < cookies.length; i++ ) |
---|
[2471] | 219 | { |
---|
[2647] | 220 | while ( cookies[i].charAt(0) == ' ') |
---|
[2471] | 221 | { |
---|
[2647] | 222 | cookies[i] = cookies[i].substring(1,cookies[i].length); |
---|
[2397] | 223 | } |
---|
[2647] | 224 | |
---|
| 225 | if (cookies[i].substr(0, 8) == "trophyim") |
---|
[2471] | 226 | { |
---|
[2647] | 227 | var nvpair = cookies[i].split("=", 2); |
---|
[2397] | 228 | cObj[nvpair[0]] = nvpair[1]; |
---|
| 229 | TrophyIM.setCookie(nvpair[0], nvpair[1]); |
---|
| 230 | } |
---|
| 231 | } |
---|
[2471] | 232 | |
---|
[2397] | 233 | return cObj; |
---|
| 234 | }, |
---|
| 235 | |
---|
| 236 | /** Function: load |
---|
| 237 | * |
---|
| 238 | * This function searches for the trophyimclient div and loads the client |
---|
| 239 | * into it. |
---|
| 240 | */ |
---|
| 241 | |
---|
| 242 | load : function() |
---|
| 243 | { |
---|
[2737] | 244 | if( loadscript.getUserCurrent() == null ) |
---|
| 245 | { |
---|
| 246 | loadscript.setUserCurrent(); |
---|
| 247 | } |
---|
| 248 | |
---|
[2791] | 249 | if ( !TrophyIM.statusConn.connected ) |
---|
[2397] | 250 | { |
---|
| 251 | TrophyIM.cookies = TrophyIM.getCookies(); |
---|
[2841] | 252 | |
---|
[2397] | 253 | //Wait a second to give scripts time to load |
---|
[2737] | 254 | setTimeout( "TrophyIM.showLogin()", 550 ); |
---|
[2397] | 255 | } |
---|
| 256 | else |
---|
| 257 | { |
---|
[2670] | 258 | loadscript.rosterDiv(); |
---|
[2397] | 259 | } |
---|
| 260 | }, |
---|
[2834] | 261 | |
---|
| 262 | /** Function: storeData |
---|
[2397] | 263 | * |
---|
| 264 | * Store all our data in the JSONStore, if it is active |
---|
| 265 | */ |
---|
[2471] | 266 | |
---|
[2397] | 267 | storeData : function() |
---|
| 268 | { |
---|
| 269 | if ( TrophyIM.connection && TrophyIM.connection.connected ) |
---|
| 270 | { |
---|
| 271 | TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" + |
---|
| 272 | TrophyIM.connection.sid + "|" + TrophyIM.connection.rid); |
---|
| 273 | TrophyIM.rosterObj.save(); |
---|
| 274 | } |
---|
| 275 | }, |
---|
[2471] | 276 | |
---|
[2397] | 277 | /** Function: showlogin |
---|
| 278 | * |
---|
| 279 | * This function clears out the IM box and either redisplays the login |
---|
| 280 | * page, or re-attaches to Strophe, preserving the logging div if it |
---|
| 281 | * exists, or creating a new one of we are re-attaching. |
---|
| 282 | */ |
---|
[2471] | 283 | |
---|
[2397] | 284 | showLogin : function() |
---|
| 285 | { |
---|
[2737] | 286 | /** |
---|
| 287 | * |
---|
| 288 | * JSON is the last script to load, so we wait on it |
---|
| 289 | * Added Strophe check too because of bug where it's sometimes missing |
---|
| 290 | * |
---|
| 291 | */ |
---|
| 292 | |
---|
[2803] | 293 | if ( typeof(JSON) != undefined && typeof(Strophe) != undefined ) |
---|
[2397] | 294 | { |
---|
| 295 | TrophyIM.JSONStore = new TrophyIMJSONStore(); |
---|
| 296 | |
---|
[2803] | 297 | if ( TrophyIM.JSONStore.store_working && TrophyIM.cookies['trophyim_bosh_xid'] ) |
---|
[2397] | 298 | { |
---|
| 299 | var xids = TrophyIM.cookies['trophyim_bosh_xid'].split("|"); |
---|
| 300 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
| 301 | TrophyIM.constants.stale_roster = true; |
---|
| 302 | |
---|
| 303 | TrophyIM.connection = new Strophe.Connection(TROPHYIM_BOSH_SERVICE); |
---|
| 304 | TrophyIM.connection.rawInput = TrophyIM.rawInput; |
---|
| 305 | TrophyIM.connection.rawOutput = TrophyIM.rawOutput; |
---|
| 306 | Strophe.log = TrophyIM.log; |
---|
| 307 | Strophe.info('Attempting Strophe attach.'); |
---|
| 308 | TrophyIM.connection.attach(xids[0], xids[1], xids[2], TrophyIM.onConnect); |
---|
| 309 | TrophyIM.onConnect(Strophe.Status.CONNECTED); |
---|
| 310 | } |
---|
| 311 | else |
---|
| 312 | { |
---|
[2607] | 313 | // List Contact |
---|
[2670] | 314 | loadscript.rosterDiv(); |
---|
[2397] | 315 | |
---|
[2791] | 316 | // Get User Current; |
---|
| 317 | var _getUserCurrent = loadscript.getUserCurrent(); |
---|
| 318 | |
---|
| 319 | setTimeout(function() |
---|
[2397] | 320 | { |
---|
[2791] | 321 | if ( _getUserCurrent != null ) |
---|
| 322 | TrophyIM.login( Base64.decode( _getUserCurrent.jid ), Base64.decode( _getUserCurrent.password )); |
---|
| 323 | else |
---|
| 324 | loadscript.loginPage(); |
---|
| 325 | }, 200); |
---|
[2397] | 326 | } |
---|
| 327 | } |
---|
| 328 | else |
---|
| 329 | { |
---|
| 330 | setTimeout("TrophyIM.showLogin()", 500); |
---|
| 331 | } |
---|
| 332 | }, |
---|
| 333 | |
---|
| 334 | /** Function: log |
---|
| 335 | * |
---|
| 336 | * This function logs the given message in the trophyimlog div |
---|
| 337 | * |
---|
| 338 | * Parameter: (String) msg - the message to log |
---|
| 339 | */ |
---|
| 340 | |
---|
| 341 | log : function(level, msg) |
---|
| 342 | { |
---|
| 343 | if (TrophyIM.logging_div && level >= TROPHYIM_LOGLEVEL) { |
---|
| 344 | while(TrophyIM.logging_div.childNodes.length > TROPHYIM_LOG_LINES) { |
---|
| 345 | TrophyIM.logging_div.removeChild( |
---|
| 346 | TrophyIM.logging_div.firstChild); |
---|
| 347 | } |
---|
| 348 | var msg_div = document.createElement('div'); |
---|
| 349 | msg_div.className = 'trophyimlogitem'; |
---|
| 350 | msg_div.appendChild(document.createTextNode(msg)); |
---|
| 351 | TrophyIM.logging_div.appendChild(msg_div); |
---|
| 352 | TrophyIM.logging_div.scrollTop = TrophyIM.logging_div.scrollHeight; |
---|
| 353 | } |
---|
| 354 | }, |
---|
| 355 | |
---|
| 356 | /** Function: rawInput |
---|
| 357 | * |
---|
| 358 | * This logs the packets actually recieved by strophe at the debug level |
---|
| 359 | */ |
---|
| 360 | rawInput : function (data) |
---|
| 361 | { |
---|
| 362 | Strophe.debug("RECV: " + data); |
---|
| 363 | }, |
---|
| 364 | |
---|
| 365 | /** Function: rawInput |
---|
| 366 | * |
---|
| 367 | * This logs the packets actually recieved by strophe at the debug level |
---|
| 368 | */ |
---|
| 369 | rawOutput : function (data) |
---|
| 370 | { |
---|
| 371 | Strophe.debug("SEND: " + data); |
---|
| 372 | }, |
---|
| 373 | |
---|
| 374 | /** Function: login |
---|
| 375 | * |
---|
| 376 | * This function logs into server using information given on login page. |
---|
| 377 | * Since the login page is where the logging checkbox is, it makes or |
---|
| 378 | * removes the logging div and cookie accordingly. |
---|
| 379 | * |
---|
| 380 | */ |
---|
| 381 | login : function() |
---|
| 382 | { |
---|
[2791] | 383 | if ( TrophyIM.JSONStore.store_working ) |
---|
[2397] | 384 | { |
---|
| 385 | //In case they never logged out |
---|
| 386 | TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', 'chat_history']); |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | TrophyIM.connection = new Strophe.Connection(TROPHYIM_BOSH_SERVICE); |
---|
| 390 | TrophyIM.connection.rawInput = TrophyIM.rawInput; |
---|
| 391 | TrophyIM.connection.rawOutput = TrophyIM.rawOutput; |
---|
| 392 | Strophe.log = TrophyIM.log; |
---|
| 393 | |
---|
| 394 | if ( arguments.length > 0 ) |
---|
| 395 | { |
---|
| 396 | var barejid = arguments[0]; |
---|
| 397 | var password = arguments[1]; |
---|
| 398 | |
---|
[2584] | 399 | TrophyIM.connection.connect( barejid + TROPHYIM_RESOURCE, password, TrophyIM.onConnect ); |
---|
[2397] | 400 | } |
---|
| 401 | else |
---|
| 402 | { |
---|
[2791] | 403 | |
---|
[2397] | 404 | var barejid = document.getElementById('trophyimjid').value |
---|
| 405 | var fulljid = barejid + TROPHYIM_RESOURCE; |
---|
| 406 | var password = document.getElementById('trophyimpass').value; |
---|
| 407 | var button = document.getElementById('trophyimconnect'); |
---|
| 408 | |
---|
[2670] | 409 | loadscript.setUserCurrent( barejid, password); |
---|
[2397] | 410 | |
---|
| 411 | if ( button.value == 'connect' ) |
---|
| 412 | { |
---|
| 413 | button.value = 'disconnect'; |
---|
[2791] | 414 | //TrophyIM.connection.connect( fulljid , password, TrophyIM.onConnect ); |
---|
| 415 | |
---|
| 416 | TrophyIM.login( barejid, password ); |
---|
| 417 | _winBuild('window_login_page', 'remove'); |
---|
[2397] | 418 | } |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | TrophyIM.setCookie('trophyimjid', barejid); |
---|
| 422 | }, |
---|
| 423 | |
---|
| 424 | /** Function: logout |
---|
| 425 | * |
---|
| 426 | * Logs into fresh session through Strophe, purging any old data. |
---|
| 427 | */ |
---|
| 428 | logout : function() |
---|
| 429 | { |
---|
[2641] | 430 | TrophyIM.autoConnection.connect = false; |
---|
| 431 | |
---|
| 432 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
[2584] | 433 | |
---|
[2397] | 434 | delete TrophyIM['cookies']['trophyim_bosh_xid']; |
---|
| 435 | |
---|
| 436 | TrophyIM.connection.disconnect(); |
---|
| 437 | }, |
---|
| 438 | |
---|
| 439 | /** Function onConnect |
---|
| 440 | * |
---|
| 441 | * Callback given to Strophe upon connection to BOSH proxy. |
---|
| 442 | */ |
---|
| 443 | onConnect : function(status) |
---|
| 444 | { |
---|
[2647] | 445 | var loading_gif = document.getElementById("JabberIMRosterLoadingGif"); |
---|
[2629] | 446 | |
---|
| 447 | if( status == Strophe.Status.CONNECTING ) |
---|
[2397] | 448 | { |
---|
[2647] | 449 | loading_gif.style.display = "block"; |
---|
| 450 | Strophe.info('Strophe is connecting.'); |
---|
[2397] | 451 | } |
---|
| 452 | |
---|
[2584] | 453 | if( status == Strophe.Status.CONNFAIL ) |
---|
[2397] | 454 | { |
---|
[2629] | 455 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
[2620] | 456 | TrophyIM.statusConn.connected = false; |
---|
[2647] | 457 | loading_gif.style.display = "block"; |
---|
[2397] | 458 | } |
---|
| 459 | |
---|
[2584] | 460 | if( status == Strophe.Status.DISCONNECTING ) |
---|
[2397] | 461 | { |
---|
[2620] | 462 | TrophyIM.statusConn.connected = false; |
---|
[2397] | 463 | } |
---|
| 464 | |
---|
[2584] | 465 | if( status == Strophe.Status.DISCONNECTED ) |
---|
[2397] | 466 | { |
---|
[2641] | 467 | if( TrophyIM.autoConnection.connect ) |
---|
[2620] | 468 | { |
---|
[2647] | 469 | loading_gif.style.display = "block"; |
---|
| 470 | |
---|
[2641] | 471 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
[2620] | 472 | |
---|
[2641] | 473 | TrophyIM.statusConn.connected = false; |
---|
| 474 | |
---|
| 475 | setTimeout(function() |
---|
| 476 | { |
---|
| 477 | TrophyIM.showLogin(); |
---|
| 478 | |
---|
| 479 | },10000); |
---|
| 480 | |
---|
[2670] | 481 | loadscript.clrAllContacts(); |
---|
[2641] | 482 | |
---|
[2863] | 483 | loadscript.setStatusJabber(i18n.STATUS_ANAVAILABLE,"unavailable"); |
---|
[2641] | 484 | |
---|
| 485 | delete TrophyIM.rosterObj.roster; |
---|
| 486 | delete TrophyIM.rosterObj.groups; |
---|
| 487 | } |
---|
[2397] | 488 | } |
---|
| 489 | |
---|
[2584] | 490 | if( status == Strophe.Status.CONNECTED ) |
---|
[2397] | 491 | { |
---|
[2863] | 492 | loadscript.setStatusJabber(i18n.STATUS_AVAILABLE,'available'); |
---|
[2397] | 493 | TrophyIM.statusConn.connected = true; |
---|
| 494 | TrophyIM.showClient(); |
---|
| 495 | } |
---|
| 496 | }, |
---|
| 497 | |
---|
| 498 | /** Function: showClient |
---|
| 499 | * |
---|
| 500 | * This clears out the main div and puts in the main client. It also |
---|
| 501 | * registers all the handlers for Strophe to call in the client. |
---|
| 502 | */ |
---|
| 503 | showClient : function() |
---|
| 504 | { |
---|
| 505 | TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" + |
---|
| 506 | TrophyIM.connection.sid + "|" + TrophyIM.connection.rid); |
---|
| 507 | |
---|
| 508 | TrophyIM.rosterObj = new TrophyIMRoster(); |
---|
| 509 | TrophyIM.connection.addHandler(TrophyIM.onVersion, Strophe.NS.VERSION, 'iq', null, null, null); |
---|
| 510 | TrophyIM.connection.addHandler(TrophyIM.onRoster, Strophe.NS.ROSTER, 'iq', null, null, null); |
---|
| 511 | TrophyIM.connection.addHandler(TrophyIM.onPresence, null, 'presence', null, null, null); |
---|
| 512 | TrophyIM.connection.addHandler(TrophyIM.onMessage, null, 'message', null, null, null); |
---|
| 513 | |
---|
| 514 | //Get roster then announce presence. |
---|
| 515 | TrophyIM.connection.send($iq({type: 'get', xmlns: Strophe.NS.CLIENT}).c('query', {xmlns: Strophe.NS.ROSTER}).tree()); |
---|
| 516 | TrophyIM.connection.send($pres().tree()); |
---|
[2670] | 517 | setTimeout( TrophyIM.renderRoster, 1000); |
---|
[2397] | 518 | }, |
---|
| 519 | |
---|
| 520 | /** Function: clearClient |
---|
| 521 | * |
---|
| 522 | * Clears out client div, preserving and returning existing logging_div if |
---|
| 523 | * one exists |
---|
| 524 | */ |
---|
[2471] | 525 | |
---|
| 526 | clearClient : function() |
---|
| 527 | { |
---|
| 528 | if(TrophyIM.logging_div) |
---|
| 529 | { |
---|
| 530 | var logging_div = TrophyIM.client_div.removeChild(document.getElementById('trophyimlog')); |
---|
| 531 | } |
---|
| 532 | else |
---|
| 533 | { |
---|
[2397] | 534 | var logging_div = null; |
---|
| 535 | } |
---|
[2471] | 536 | |
---|
| 537 | while(TrophyIM.client_div.childNodes.length > 0) |
---|
| 538 | { |
---|
[2397] | 539 | TrophyIM.client_div.removeChild(TrophyIM.client_div.firstChild); |
---|
| 540 | } |
---|
[2471] | 541 | |
---|
[2397] | 542 | return logging_div; |
---|
| 543 | }, |
---|
[2471] | 544 | |
---|
[2397] | 545 | /** Function: onVersion |
---|
| 546 | * |
---|
| 547 | * jabber:iq:version query handler |
---|
| 548 | */ |
---|
[2471] | 549 | |
---|
| 550 | onVersion : function(msg) |
---|
| 551 | { |
---|
[2397] | 552 | Strophe.debug("Version handler"); |
---|
[2471] | 553 | if (msg.getAttribute('type') == 'get') |
---|
| 554 | { |
---|
[2397] | 555 | var from = msg.getAttribute('from'); |
---|
| 556 | var to = msg.getAttribute('to'); |
---|
| 557 | var id = msg.getAttribute('id'); |
---|
| 558 | var reply = $iq({type: 'result', to: from, from: to, id: id}).c('query', |
---|
| 559 | {name: "TrophyIM", version: TROPHYIM_VERSION, os: |
---|
| 560 | "Javascript-capable browser"}); |
---|
| 561 | TrophyIM.connection.send(reply.tree()); |
---|
| 562 | } |
---|
| 563 | return true; |
---|
| 564 | }, |
---|
[2471] | 565 | |
---|
[2397] | 566 | /** Function: onRoster |
---|
| 567 | * |
---|
| 568 | * Roster iq handler |
---|
| 569 | */ |
---|
[2471] | 570 | |
---|
[2397] | 571 | onRoster : function(msg) |
---|
| 572 | { |
---|
[2471] | 573 | var roster_items = msg.firstChild.getElementsByTagName('item'); |
---|
[2397] | 574 | |
---|
| 575 | for (var i = 0; i < roster_items.length; i++) |
---|
| 576 | { |
---|
[2471] | 577 | with(roster_items[i]) |
---|
| 578 | { |
---|
| 579 | var groups = getElementsByTagName('group'); |
---|
| 580 | var group_array = []; |
---|
| 581 | |
---|
| 582 | for( var g = 0 ; g < groups.length; g++ ) |
---|
| 583 | { |
---|
| 584 | if( groups[g].hasChildNodes() ) |
---|
| 585 | group_array[group_array.length] = groups[g].firstChild.nodeValue; |
---|
| 586 | } |
---|
| 587 | |
---|
[2577] | 588 | if( getAttribute('ask') ) |
---|
| 589 | TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('ask'), getAttribute('name'), group_array); |
---|
| 590 | else |
---|
| 591 | TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('subscription'), getAttribute('name'), group_array); |
---|
[2471] | 592 | } |
---|
[2397] | 593 | } |
---|
[2471] | 594 | |
---|
[2397] | 595 | if ( msg.getAttribute('type') == 'set' ) |
---|
| 596 | { |
---|
[2471] | 597 | var _iq = $iq({type: 'reply', id: msg.getAttribute('id'), to: msg.getAttribute('from')}); |
---|
| 598 | TrophyIM.connection.send( _iq.tree()); |
---|
[2397] | 599 | } |
---|
| 600 | |
---|
| 601 | return true; |
---|
| 602 | }, |
---|
| 603 | /** Function: onPresence |
---|
| 604 | * |
---|
| 605 | * Presence handler |
---|
| 606 | */ |
---|
| 607 | onPresence : function(msg) |
---|
| 608 | { |
---|
| 609 | Strophe.debug("Presence handler"); |
---|
[2471] | 610 | var type = msg.getAttribute('type') ? msg.getAttribute('type') : 'available'; |
---|
| 611 | var show = msg.getElementsByTagName('show').length ? Strophe.getText(msg.getElementsByTagName('show')[0]) : type; |
---|
| 612 | var status = msg.getElementsByTagName('status').length ? Strophe.getText(msg.getElementsByTagName('status')[0]) : ''; |
---|
| 613 | var priority = msg.getElementsByTagName('priority').length ? parseInt(Strophe.getText(msg.getElementsByTagName('priority')[0])) : 0; |
---|
| 614 | |
---|
| 615 | TrophyIM.rosterObj.setPresence(msg.getAttribute('from'), priority, show, status); |
---|
[2397] | 616 | |
---|
| 617 | return true; |
---|
| 618 | }, |
---|
[2725] | 619 | |
---|
[2397] | 620 | /** Function: onMessage |
---|
| 621 | * |
---|
| 622 | * Message handler |
---|
| 623 | */ |
---|
| 624 | onMessage : function(msg) |
---|
| 625 | { |
---|
[2713] | 626 | var checkTime = function(i) |
---|
| 627 | { |
---|
| 628 | if ( i < 10 ) i= "0" + i; |
---|
| 629 | |
---|
| 630 | return i; |
---|
| 631 | }; |
---|
| 632 | |
---|
[2716] | 633 | var messageDate = function( _date ) |
---|
| 634 | { |
---|
| 635 | var _dt = _date.substr( 0, _date.indexOf( 'T' ) ).split( '-' ); |
---|
| 636 | var _hr = _date.substr( _date.indexOf( 'T' ) + 1, _date.length - _date.indexOf( 'T' ) - 2 ).split( ':' ); |
---|
| 637 | |
---|
| 638 | ( _date = new Date ).setTime( Date.UTC( _dt[0], _dt[1] - 1, _dt[2], _hr[0], _hr[1], _hr[2] ) ); |
---|
| 639 | |
---|
| 640 | return ( _date.toLocaleDateString( ).replace( /-/g, '/' ) + ' ' + _date.toLocaleTimeString( ) ); |
---|
| 641 | }; |
---|
| 642 | |
---|
[2713] | 643 | var data = new Date(); |
---|
| 644 | var dtNow = checkTime(data.getHours()) + ":" + checkTime(data.getMinutes()) + ":" + checkTime(data.getSeconds()); |
---|
| 645 | |
---|
| 646 | var from = msg.getAttribute('from'); |
---|
| 647 | var type = msg.getAttribute('type'); |
---|
| 648 | var elems = msg.getElementsByTagName('body'); |
---|
| 649 | var delay = ( msg.getElementsByTagName('delay') ) ? msg.getElementsByTagName('delay') : null; |
---|
[2787] | 650 | var stamp = ( delay[0] != null ) ? "<font style='color:red;'>" + messageDate(delay[0].getAttribute('stamp')) + "</font>" : dtNow; |
---|
[2397] | 651 | |
---|
[2724] | 652 | var barejid = Strophe.getBareJidFromJid(from); |
---|
| 653 | var jid_lower = barejid.toLowerCase(); |
---|
| 654 | var contact = ""; |
---|
[2725] | 655 | var state = ""; |
---|
[2724] | 656 | |
---|
[2825] | 657 | var chatBox = document.getElementById(jid_lower + "__chatState"); |
---|
| 658 | var chatStateOnOff = null; |
---|
| 659 | var active = msg.getElementsByTagName('active'); |
---|
[2725] | 660 | |
---|
| 661 | contact = barejid.toLowerCase(); |
---|
| 662 | contact = contact.substring(0, contact.indexOf('@')); |
---|
[2491] | 663 | |
---|
[2724] | 664 | if( TrophyIM.rosterObj.roster[barejid] ) |
---|
| 665 | { |
---|
| 666 | if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) |
---|
| 667 | { |
---|
| 668 | contact = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; |
---|
| 669 | } |
---|
| 670 | } |
---|
[2491] | 671 | |
---|
[2821] | 672 | // Message with body are "content message", this means state active |
---|
[2725] | 673 | if ( elems.length > 0 ) |
---|
[2724] | 674 | { |
---|
[2728] | 675 | state = ""; |
---|
[2825] | 676 | // Set notify chat state capability on when sender notify it themself |
---|
| 677 | chatStateOnOff = document.getElementById(jid_lower + "__chatStateOnOff"); |
---|
| 678 | if (active.length > 0 & chatStateOnOff != null ) |
---|
| 679 | { |
---|
| 680 | chatStateOnOff.value = 'on'; |
---|
| 681 | } |
---|
[2397] | 682 | |
---|
[2725] | 683 | if (type == 'chat' || type == 'normal') |
---|
| 684 | { |
---|
[2724] | 685 | var _message = document.createElement("div"); |
---|
| 686 | _message.innerHTML = Strophe.getText(elems[0]); |
---|
| 687 | |
---|
| 688 | var scripts = _message.getElementsByTagName('script'); |
---|
| 689 | |
---|
| 690 | for (var i = 0; i < scripts.length; i++) |
---|
| 691 | _message.removeChild(scripts[i--]); |
---|
| 692 | |
---|
| 693 | _message.innerHTML = _message.innerHTML.replace(/^\s+|\s+$|^\n|\n$/g, ""); |
---|
[2799] | 694 | |
---|
| 695 | // Get Smiles |
---|
| 696 | _message.innerHTML = loadscript.getSmiles( _message.innerHTML ); |
---|
| 697 | |
---|
[2725] | 698 | if ( _message.hasChildNodes() ) |
---|
[2724] | 699 | { |
---|
| 700 | var message = |
---|
| 701 | { |
---|
| 702 | contact : "[" + stamp + "] <font style='font-weight:bold; color:black;'>" + contact + "</font>", |
---|
| 703 | msg : "</br>" + _message.innerHTML |
---|
| 704 | }; |
---|
| 705 | |
---|
[2787] | 706 | TrophyIM.addMessage( TrophyIM.makeChat( from ), jid_lower, message ); |
---|
[2724] | 707 | } |
---|
| 708 | } |
---|
| 709 | } |
---|
[2799] | 710 | // Message without body are "content message", this mean state is not active |
---|
[2724] | 711 | else |
---|
| 712 | { |
---|
[2725] | 713 | if( chatBox != null ) |
---|
[2828] | 714 | state = TrophyIM.getChatState(msg); |
---|
[2724] | 715 | } |
---|
[2836] | 716 | |
---|
[2821] | 717 | // Clean chat status message some time later |
---|
| 718 | var clearChatState = function(){ |
---|
| 719 | chatBox.innerHTML=''; |
---|
| 720 | } |
---|
[2725] | 721 | |
---|
[2836] | 722 | if (chatBox != null) |
---|
| 723 | { |
---|
| 724 | var clearChatStateTimer; |
---|
| 725 | |
---|
| 726 | chatBox.innerHTML = "<font style='font-weight:bold; color:grey; float:right;'>" + state + "</font>"; |
---|
| 727 | |
---|
| 728 | var _composing = msg.getElementsByTagName('composing'); |
---|
| 729 | |
---|
| 730 | if ( _composing.length == 0 ) |
---|
| 731 | |
---|
| 732 | clearChatStateTimer = setTimeout(clearChatState, 2000); |
---|
| 733 | else |
---|
| 734 | clearTimeout(clearChatStateTimer); |
---|
[2821] | 735 | } |
---|
[2836] | 736 | |
---|
[2724] | 737 | return true; |
---|
| 738 | }, |
---|
[2670] | 739 | |
---|
[2724] | 740 | /** Function: getChatState |
---|
| 741 | * |
---|
| 742 | * Parameters: |
---|
| 743 | * (string) msg - the message to get chat state |
---|
| 744 | * (string) jid - the jid of chat box to update the chat state to. |
---|
| 745 | */ |
---|
| 746 | getChatState : function(msg) |
---|
| 747 | { |
---|
| 748 | var state = msg.getElementsByTagName('inactive'); |
---|
| 749 | |
---|
[2725] | 750 | if ( state.length > 0 ) |
---|
| 751 | { |
---|
[2863] | 752 | return i18n.INACTIVE; |
---|
[2725] | 753 | } |
---|
| 754 | else |
---|
| 755 | { |
---|
[2724] | 756 | state = msg.getElementsByTagName('gone'); |
---|
[2725] | 757 | if ( state.length > 0 ) |
---|
| 758 | { |
---|
[2863] | 759 | return i18n.GONE; |
---|
[2725] | 760 | } |
---|
| 761 | else |
---|
| 762 | { |
---|
[2724] | 763 | state = msg.getElementsByTagName('composing'); |
---|
[2725] | 764 | if ( state.length > 0 ) |
---|
| 765 | { |
---|
[2863] | 766 | return i18n.COMPOSING; |
---|
[2725] | 767 | } |
---|
| 768 | else |
---|
| 769 | { |
---|
[2724] | 770 | state = msg.getElementsByTagName('paused'); |
---|
[2725] | 771 | if ( state.length > 0 ) |
---|
| 772 | { |
---|
[2863] | 773 | return i18n.PAUSED; |
---|
[2724] | 774 | } |
---|
| 775 | } |
---|
| 776 | } |
---|
| 777 | } |
---|
[2725] | 778 | |
---|
[2724] | 779 | return ''; |
---|
| 780 | }, |
---|
| 781 | |
---|
[2471] | 782 | |
---|
[2397] | 783 | /** Function: makeChat |
---|
| 784 | * |
---|
| 785 | * Make sure chat window to given fulljid exists, switching chat context to |
---|
[2825] | 786 | * given resource. |
---|
[2397] | 787 | */ |
---|
[2471] | 788 | |
---|
[2397] | 789 | makeChat : function(fulljid) |
---|
| 790 | { |
---|
[2471] | 791 | var barejid = Strophe.getBareJidFromJid(fulljid); |
---|
| 792 | var titleWindow = ""; |
---|
[2825] | 793 | |
---|
[2397] | 794 | var paramsChatBox = |
---|
| 795 | { |
---|
[2787] | 796 | 'enabledPopUp' : ( ( loadscript.getIsIE() ) ? "none" : "block" ), |
---|
[2471] | 797 | 'idChatBox' : barejid + "__chatBox", |
---|
| 798 | 'jidTo' : barejid, |
---|
[2397] | 799 | 'path_jabberit' : path_jabberit |
---|
| 800 | }; |
---|
[2511] | 801 | |
---|
| 802 | titleWindow = barejid.toLowerCase(); |
---|
| 803 | titleWindow = titleWindow.substring(0, titleWindow.indexOf('@')); |
---|
| 804 | |
---|
[2471] | 805 | if( TrophyIM.rosterObj.roster[barejid] ) |
---|
| 806 | { |
---|
| 807 | if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) |
---|
| 808 | { |
---|
| 809 | titleWindow = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; |
---|
| 810 | } |
---|
| 811 | } |
---|
| 812 | |
---|
[2631] | 813 | // Position Top |
---|
| 814 | TrophyIM.posWindow.top = TrophyIM.posWindow.top + 10; |
---|
| 815 | if( TrophyIM.posWindow.top > 200 ) |
---|
| 816 | TrophyIM.posWindow.top = 100; |
---|
| 817 | |
---|
| 818 | // Position Left |
---|
| 819 | TrophyIM.posWindow.left = TrophyIM.posWindow.left + 5; |
---|
| 820 | if( TrophyIM.posWindow.left > 455 ) |
---|
| 821 | TrophyIM.posWindow.left = 400; |
---|
| 822 | |
---|
[2787] | 823 | var _content = document.createElement( 'div' ); |
---|
| 824 | _content.innerHTML = loadscript.parse( "chat_box", "chatBox.xsl", paramsChatBox); |
---|
| 825 | _content = _content.firstChild; |
---|
| 826 | |
---|
| 827 | var _messages = _content.firstChild.firstChild; |
---|
| 828 | var _textarea = _content.getElementsByTagName( 'textarea' ).item( 0 ); |
---|
| 829 | var _send = _content.getElementsByTagName( 'input' ).item( 0 ); |
---|
[2825] | 830 | var _chatStateOnOff = _content.getElementsByTagName( 'input' ).item( 1 ); |
---|
[2787] | 831 | |
---|
| 832 | function _send_message( ) |
---|
| 833 | { |
---|
| 834 | if ( ! TrophyIM.sendMessage( barejid, _textarea.value ) ) |
---|
| 835 | return false; |
---|
| 836 | |
---|
| 837 | // Add Message in chatBox; |
---|
| 838 | TrophyIM.addMessage( _messages, barejid, { |
---|
[2863] | 839 | contact : "<font style='font-weight:bold; color:red;'>" + i18n.ME + "</font>", |
---|
[2787] | 840 | msg : "<br/>" + _textarea.value |
---|
| 841 | } ); |
---|
| 842 | |
---|
| 843 | _textarea.value = ''; |
---|
| 844 | _textarea.focus( ); |
---|
| 845 | } |
---|
[2825] | 846 | |
---|
| 847 | var composingTimer_ = 0; |
---|
| 848 | var isComposing_ = 0; |
---|
| 849 | var timeCounter; |
---|
[2787] | 850 | |
---|
[2825] | 851 | function setComposing() |
---|
| 852 | { |
---|
| 853 | var checkComposing = function() |
---|
| 854 | { |
---|
| 855 | if (!isComposing_) { |
---|
| 856 | // User stopped composing |
---|
| 857 | composingTimer_ = 0; |
---|
| 858 | clearInterval(timeCounter); |
---|
| 859 | TrophyIM.sendContentMessage(barejid, 'paused'); |
---|
| 860 | } else { |
---|
| 861 | TrophyIM.sendContentMessage(barejid, 'composing'); |
---|
| 862 | } |
---|
| 863 | isComposing_ = 0; // Reset composing |
---|
| 864 | } |
---|
| 865 | |
---|
| 866 | if (!composingTimer_) { |
---|
| 867 | /* User (re)starts composing */ |
---|
| 868 | composingTimer_ = 1; |
---|
| 869 | timeCounter = setInterval(checkComposing,4000); |
---|
| 870 | } |
---|
| 871 | isComposing_ = 1; |
---|
| 872 | } |
---|
| 873 | |
---|
[2787] | 874 | loadscript.configEvents( _send, 'onclick', _send_message ); |
---|
| 875 | loadscript.configEvents( _textarea, 'onkeyup', function( e ) |
---|
| 876 | { |
---|
[2825] | 877 | if ( e.keyCode == 13 ){ |
---|
[2787] | 878 | _send_message( ); |
---|
[2825] | 879 | // User stopped composing |
---|
| 880 | composingTimer_ = 0; |
---|
| 881 | clearInterval(timeCounter); |
---|
| 882 | }else{ |
---|
| 883 | if (_chatStateOnOff.value == 'on') |
---|
| 884 | setComposing(); |
---|
| 885 | } |
---|
| 886 | } ); |
---|
| 887 | |
---|
[2397] | 888 | var winChatBox = |
---|
| 889 | { |
---|
[2471] | 890 | id_window : "window_chat_area_" + barejid, |
---|
[2787] | 891 | barejid : barejid, |
---|
[2397] | 892 | width : 387, |
---|
[2724] | 893 | height : 375, |
---|
[2631] | 894 | top : TrophyIM.posWindow.top, |
---|
| 895 | left : TrophyIM.posWindow.left, |
---|
[2397] | 896 | draggable : true, |
---|
| 897 | visible : "display", |
---|
| 898 | resizable : true, |
---|
[2670] | 899 | zindex : loadscript.getZIndex(), |
---|
[2471] | 900 | title : titleWindow, |
---|
[2397] | 901 | closeAction : "hidden", |
---|
[2787] | 902 | content : _content |
---|
[2397] | 903 | } |
---|
| 904 | |
---|
[2787] | 905 | _win = _winBuild(winChatBox); |
---|
[2511] | 906 | |
---|
[2599] | 907 | // Notification New Message |
---|
[2787] | 908 | loadscript.notification(barejid); |
---|
[2599] | 909 | |
---|
[2511] | 910 | // Photo User; |
---|
[2670] | 911 | loadscript.getPhotoUser(barejid); |
---|
[2787] | 912 | |
---|
| 913 | _textarea.focus( ); |
---|
| 914 | |
---|
| 915 | return ( _messages = _win.content( ).firstChild ); |
---|
[2397] | 916 | }, |
---|
| 917 | |
---|
| 918 | /** Function addContacts |
---|
| 919 | * |
---|
| 920 | * Parameters: |
---|
[2437] | 921 | * (string) jidFrom |
---|
| 922 | * (string) jidTo |
---|
[2397] | 923 | * (string) name |
---|
| 924 | * (string) group |
---|
| 925 | */ |
---|
| 926 | |
---|
[2584] | 927 | addContact : function( jidTo, name, group ) |
---|
[2397] | 928 | { |
---|
[2577] | 929 | // Add Contact |
---|
[2491] | 930 | var _id = TrophyIM.connection.getUniqueId('add'); |
---|
[2471] | 931 | var newContact = $iq({type: 'set', id: _id }); |
---|
[2397] | 932 | newContact = newContact.c('query').attrs({xmlns : 'jabber:iq:roster'}); |
---|
[2471] | 933 | newContact = newContact.c('item').attrs({jid: jidTo, name:name }); |
---|
[2397] | 934 | newContact = newContact.c('group').t(group).tree(); |
---|
| 935 | |
---|
| 936 | TrophyIM.connection.send(newContact); |
---|
| 937 | }, |
---|
| 938 | |
---|
| 939 | /** Function: addMessage |
---|
| 940 | * |
---|
| 941 | * Parameters: |
---|
| 942 | * (string) msg - the message to add |
---|
| 943 | * (string) jid - the jid of chat box to add the message to. |
---|
| 944 | */ |
---|
| 945 | |
---|
[2787] | 946 | addMessage : function(chatBox, jid, msg) |
---|
[2397] | 947 | { |
---|
[2799] | 948 | // Get Smiles |
---|
| 949 | msg.msg = loadscript.getSmiles( msg.msg ); |
---|
| 950 | |
---|
[2397] | 951 | var messageDiv = document.createElement("div"); |
---|
[2716] | 952 | messageDiv.style.margin = "3px 0px 1em 3px"; |
---|
[2397] | 953 | messageDiv.innerHTML = msg.contact + " : " + msg.msg ; |
---|
| 954 | |
---|
| 955 | chatBox.appendChild(messageDiv); |
---|
| 956 | chatBox.scrollTop = chatBox.scrollHeight; |
---|
| 957 | }, |
---|
| 958 | |
---|
[2584] | 959 | /** Function : renameContact |
---|
| 960 | * |
---|
| 961 | * |
---|
| 962 | */ |
---|
| 963 | |
---|
| 964 | renameContact : function( jid, index ) |
---|
| 965 | { |
---|
| 966 | // Name |
---|
| 967 | var name = TrophyIM.rosterObj.roster[jid].contact.name; |
---|
| 968 | |
---|
[2863] | 969 | if(( name = prompt(i18n.ASK_NEW_NAME_QUESTION + name + "!", name ))) |
---|
[2584] | 970 | if(( name = name.replace(/^\s+|\s+$|^\n|\n$/g,"")) == "" ) |
---|
| 971 | name = ""; |
---|
| 972 | |
---|
| 973 | if( name == null || name == "") |
---|
| 974 | name = ""; |
---|
| 975 | |
---|
| 976 | var jidTo = jid |
---|
| 977 | var name = ( name ) ? name : TrophyIM.rosterObj.roster[jid].contact.name; |
---|
| 978 | var group = TrophyIM.rosterObj.roster[jid].contact.groups[0]; |
---|
| 979 | |
---|
| 980 | TrophyIM.addContact( jidTo, name, group ); |
---|
| 981 | |
---|
| 982 | document.getElementById('itenContact_' + jid + '_' + index).innerHTML = name; |
---|
| 983 | }, |
---|
| 984 | |
---|
| 985 | /** Function : renameGroup |
---|
| 986 | * |
---|
| 987 | * |
---|
| 988 | */ |
---|
| 989 | |
---|
| 990 | renameGroup : function( jid, index) |
---|
| 991 | { |
---|
| 992 | var group = TrophyIM.rosterObj.roster[jid].contact.groups[0]; |
---|
| 993 | var presence = TrophyIM.rosterObj.roster[jid].presence; |
---|
| 994 | |
---|
| 995 | // Group |
---|
[2863] | 996 | if(( group = prompt( i18n.ASK_NEW_GROUP_QUESTION, group ))) |
---|
[2584] | 997 | if(( group = group.replace(/^\s+|\s+$|^\n|\n$/g,"")) == "" ) |
---|
| 998 | group = ""; |
---|
| 999 | |
---|
| 1000 | if( group == null || group == "") |
---|
| 1001 | group = ""; |
---|
| 1002 | |
---|
| 1003 | var jidTo = TrophyIM.rosterObj.roster[jid].contact.jid; |
---|
| 1004 | var name = TrophyIM.rosterObj.roster[jid].contact.name; |
---|
| 1005 | var group = ( group ) ? group : TrophyIM.rosterObj.roster[jid].contact.groups[0]; |
---|
| 1006 | |
---|
| 1007 | TrophyIM.rosterObj.removeContact( jid ); |
---|
| 1008 | |
---|
| 1009 | TrophyIM.addContact( jidTo, name, group ); |
---|
| 1010 | |
---|
| 1011 | document.getElementById("JabberIMRoster").innerHTML = ""; |
---|
| 1012 | |
---|
| 1013 | TrophyIM.renderRoster(); |
---|
| 1014 | |
---|
| 1015 | setTimeout(function() |
---|
| 1016 | { |
---|
| 1017 | for( var i in presence ) |
---|
[2670] | 1018 | { |
---|
| 1019 | if ( presence[ i ].constructor == Function ) |
---|
| 1020 | continue; |
---|
| 1021 | |
---|
[2584] | 1022 | TrophyIM.rosterObj.setPresence( jid, presence[i].priority, presence[i].show, presence[i].status); |
---|
[2670] | 1023 | } |
---|
[2584] | 1024 | },500); |
---|
| 1025 | }, |
---|
| 1026 | |
---|
[2437] | 1027 | /** Function: removeContact |
---|
| 1028 | * |
---|
| 1029 | * Parameters: |
---|
| 1030 | * (string) jidTo |
---|
| 1031 | */ |
---|
| 1032 | |
---|
[2491] | 1033 | removeContact : function(jidTo, indexTo) |
---|
[2437] | 1034 | { |
---|
[2491] | 1035 | var divItenContact = null; |
---|
| 1036 | var spanShow = null; |
---|
[2437] | 1037 | |
---|
[2491] | 1038 | if( ( divItenContact = document.getElementById('itenContact_' + jidTo + '_' + indexTo ))) |
---|
[2437] | 1039 | { |
---|
[2491] | 1040 | spanShow = document.getElementById('span_show_itenContact_' + jidTo + '_' + indexTo ) |
---|
| 1041 | |
---|
| 1042 | spanShow.parentNode.removeChild(spanShow); |
---|
| 1043 | |
---|
[2670] | 1044 | loadscript.removeGroup( divItenContact.parentNode ); |
---|
[2471] | 1045 | |
---|
[2437] | 1046 | divItenContact.parentNode.removeChild(divItenContact); |
---|
[2577] | 1047 | |
---|
[2437] | 1048 | // Remove Contact |
---|
[2577] | 1049 | var _id = TrophyIM.connection.getUniqueId(); |
---|
[2471] | 1050 | var delContact = $iq({type: 'set', id: _id}) |
---|
[2437] | 1051 | delContact = delContact.c('query').attrs({xmlns : 'jabber:iq:roster'}); |
---|
[2471] | 1052 | delContact = delContact.c('item').attrs({jid: jidTo, subscription:'remove'}).tree(); |
---|
[2577] | 1053 | |
---|
| 1054 | TrophyIM.connection.send( delContact ); |
---|
| 1055 | |
---|
| 1056 | // Remove Contact |
---|
| 1057 | var _id = TrophyIM.connection.getUniqueId(); |
---|
| 1058 | var _delContact_ = $iq({type: 'set', id: _id}) |
---|
| 1059 | _delContact_ = _delContact_.c('query').attrs({xmlns : 'jabber:iq:private'}); |
---|
| 1060 | _delContact_ = _delContact_.c('storage').attrs({xmlns : 'storage:metacontacts'}).tree(); |
---|
| 1061 | |
---|
| 1062 | TrophyIM.connection.send( _delContact_ ); |
---|
[2437] | 1063 | } |
---|
| 1064 | }, |
---|
| 1065 | |
---|
[2397] | 1066 | /** Function: renderRoster |
---|
| 1067 | * |
---|
| 1068 | * Renders roster, looking only for jids flagged by setPresence as having |
---|
| 1069 | * changed. |
---|
| 1070 | */ |
---|
| 1071 | |
---|
| 1072 | renderRoster : function() |
---|
| 1073 | { |
---|
[2471] | 1074 | var roster_div = document.getElementById('JabberIMRoster'); |
---|
| 1075 | |
---|
| 1076 | if( roster_div ) |
---|
[2397] | 1077 | { |
---|
[2471] | 1078 | var users = new Array(); |
---|
[2397] | 1079 | |
---|
[2629] | 1080 | var loading_gif = document.getElementById("JabberIMRosterLoadingGif"); |
---|
| 1081 | |
---|
| 1082 | if( loading_gif.style.display == "block" ) |
---|
| 1083 | loading_gif.style.display = "none"; |
---|
| 1084 | |
---|
[2471] | 1085 | for( var user in TrophyIM.rosterObj.roster ) |
---|
[2397] | 1086 | { |
---|
[2670] | 1087 | if ( TrophyIM.rosterObj.roster[ user ].constructor == Function ) |
---|
| 1088 | continue; |
---|
| 1089 | |
---|
[2471] | 1090 | users[users.length] = TrophyIM.rosterObj.roster[user].contact.jid; |
---|
| 1091 | } |
---|
| 1092 | |
---|
| 1093 | users.sort(); |
---|
| 1094 | |
---|
| 1095 | var groups = new Array(); |
---|
| 1096 | var flagGeral = false; |
---|
| 1097 | |
---|
| 1098 | for (var group in TrophyIM.rosterObj.groups) |
---|
| 1099 | { |
---|
[2670] | 1100 | if ( TrophyIM.rosterObj.groups[ group ].constructor == Function ) |
---|
| 1101 | continue; |
---|
| 1102 | |
---|
[2471] | 1103 | if( group ) |
---|
| 1104 | groups[groups.length] = group; |
---|
[2397] | 1105 | |
---|
[2471] | 1106 | if( group == "Geral" ) |
---|
| 1107 | flagGeral = true; |
---|
| 1108 | } |
---|
| 1109 | |
---|
[2491] | 1110 | if( !flagGeral && users.length > 0 ) |
---|
[2471] | 1111 | groups[groups.length] = "Geral"; |
---|
[2397] | 1112 | |
---|
[2471] | 1113 | groups.sort(); |
---|
| 1114 | |
---|
| 1115 | for ( var i = 0; i < groups.length; i++ ) |
---|
| 1116 | { |
---|
| 1117 | TrophyIM.renderGroups( groups[i] , roster_div ); |
---|
[2397] | 1118 | } |
---|
[2471] | 1119 | |
---|
| 1120 | TrophyIM.renderItensGroup( users, roster_div ); |
---|
[2397] | 1121 | } |
---|
| 1122 | |
---|
[2647] | 1123 | TrophyIM._timeOut.renderRoster = setTimeout("TrophyIM.renderRoster()", 1000 ); |
---|
[2397] | 1124 | }, |
---|
| 1125 | |
---|
| 1126 | /** Function: renderGroups |
---|
| 1127 | * |
---|
| 1128 | * |
---|
| 1129 | */ |
---|
| 1130 | |
---|
| 1131 | renderGroups: function( nameGroup, element ) |
---|
| 1132 | { |
---|
| 1133 | var _addGroup = function() |
---|
| 1134 | { |
---|
[2471] | 1135 | var _nameGroup = nameGroup; |
---|
| 1136 | var _element = element; |
---|
| 1137 | |
---|
[2397] | 1138 | var paramsGroup = |
---|
| 1139 | { |
---|
[2471] | 1140 | 'nameGroup' : _nameGroup, |
---|
[2397] | 1141 | 'path_jabberit' : path_jabberit |
---|
| 1142 | } |
---|
| 1143 | |
---|
[2670] | 1144 | _element.innerHTML += loadscript.parse("group","groups.xsl", paramsGroup); |
---|
[2397] | 1145 | } |
---|
| 1146 | |
---|
| 1147 | if( !element.hasChildNodes() ) |
---|
| 1148 | { |
---|
[2471] | 1149 | _addGroup(); |
---|
[2397] | 1150 | } |
---|
| 1151 | else |
---|
| 1152 | { |
---|
[2471] | 1153 | var _NodeChild = element.firstChild; |
---|
| 1154 | var flagAdd = false; |
---|
[2397] | 1155 | |
---|
[2471] | 1156 | while( _NodeChild ) |
---|
[2397] | 1157 | { |
---|
[2471] | 1158 | if( _NodeChild.childNodes[0].nodeName.toLowerCase() === "span" ) |
---|
[2397] | 1159 | { |
---|
[2471] | 1160 | if( _NodeChild.childNodes[0].childNodes[0].nodeValue === nameGroup ) |
---|
| 1161 | { |
---|
| 1162 | flagAdd = true; |
---|
| 1163 | } |
---|
[2397] | 1164 | } |
---|
| 1165 | |
---|
[2471] | 1166 | _NodeChild = _NodeChild.nextSibling; |
---|
[2397] | 1167 | } |
---|
[2471] | 1168 | |
---|
| 1169 | if( !flagAdd ) |
---|
| 1170 | { |
---|
| 1171 | _addGroup(); |
---|
| 1172 | } |
---|
[2397] | 1173 | } |
---|
| 1174 | }, |
---|
| 1175 | |
---|
| 1176 | /** Function: renderItensGroup |
---|
| 1177 | * |
---|
| 1178 | * |
---|
| 1179 | */ |
---|
| 1180 | |
---|
| 1181 | renderItensGroup : function( users, element ) |
---|
| 1182 | { |
---|
[2471] | 1183 | var addItem = function() |
---|
[2397] | 1184 | { |
---|
| 1185 | if( arguments.length > 0 ) |
---|
| 1186 | { |
---|
| 1187 | var objContact = arguments[0]; |
---|
[2471] | 1188 | var group = arguments[1]; |
---|
| 1189 | var element = arguments[2]; |
---|
| 1190 | var index = arguments[3]; |
---|
[2706] | 1191 | var showOffline = loadscript.getShowContactsOffline(); |
---|
| 1192 | |
---|
[2471] | 1193 | var itensJid = document.getElementById( 'itenContact_' + objContact.contact.jid + '_' + index ); |
---|
[2670] | 1194 | |
---|
[2397] | 1195 | if( itensJid == null ) |
---|
| 1196 | { |
---|
| 1197 | // Name |
---|
| 1198 | var nameContact = ""; |
---|
| 1199 | |
---|
| 1200 | if ( objContact.contact.name ) |
---|
| 1201 | nameContact = objContact.contact.name; |
---|
| 1202 | else |
---|
| 1203 | { |
---|
| 1204 | nameContact = objContact.contact.jid; |
---|
| 1205 | nameContact = nameContact.substring(0, nameContact.indexOf('@')); |
---|
| 1206 | } |
---|
| 1207 | |
---|
| 1208 | // Presence e Status |
---|
| 1209 | var presence = "unavailable"; |
---|
| 1210 | var status = ""; |
---|
| 1211 | var statusDisplay = "none"; |
---|
| 1212 | |
---|
| 1213 | if (objContact.presence) |
---|
| 1214 | { |
---|
| 1215 | for (var resource in objContact.presence) |
---|
| 1216 | { |
---|
[2670] | 1217 | if ( objContact.presence[resource].constructor == Function ) |
---|
| 1218 | continue; |
---|
| 1219 | |
---|
[2471] | 1220 | if( objContact.presence[resource].show != 'invisible' ) |
---|
| 1221 | presence = objContact.presence[resource].show; |
---|
| 1222 | |
---|
[2577] | 1223 | if( objContact.contact.subscription != "both") |
---|
| 1224 | presence = 'subscription'; |
---|
| 1225 | |
---|
[2397] | 1226 | if( objContact.presence[resource].status ) |
---|
| 1227 | { |
---|
[2577] | 1228 | status = " ( " + objContact.presence[resource].status + " ) "; |
---|
[2397] | 1229 | statusDisplay = "block"; |
---|
| 1230 | } |
---|
| 1231 | } |
---|
| 1232 | } |
---|
| 1233 | |
---|
| 1234 | var paramsContact = |
---|
| 1235 | { |
---|
[2706] | 1236 | divDisplay : "block", |
---|
[2577] | 1237 | id : 'itenContact_' + objContact.contact.jid + '_' + index , |
---|
[2670] | 1238 | index : ((index == 0 ) ? "0" : index), |
---|
[2706] | 1239 | jid : objContact.contact.jid, |
---|
| 1240 | nameContact : nameContact, |
---|
[2577] | 1241 | path_jabberit : path_jabberit, |
---|
| 1242 | presence : presence, |
---|
[2706] | 1243 | spanDisplay : statusDisplay, |
---|
[2577] | 1244 | status : status, |
---|
| 1245 | statusColor : "black", |
---|
| 1246 | subscription : objContact.contact.subscription |
---|
[2397] | 1247 | } |
---|
| 1248 | |
---|
[2577] | 1249 | // Authorization |
---|
| 1250 | if( objContact.contact.subscription != "both" ) |
---|
| 1251 | { |
---|
| 1252 | |
---|
| 1253 | switch(objContact.contact.subscription) |
---|
| 1254 | { |
---|
| 1255 | case "none" : |
---|
[2863] | 1256 | |
---|
| 1257 | paramsContact.status = " (( " + i18n.ASK_AUTHO + " )) "; |
---|
[2577] | 1258 | paramsContact.statusColor = "red"; |
---|
| 1259 | break; |
---|
| 1260 | |
---|
| 1261 | case "to" : |
---|
| 1262 | |
---|
[2863] | 1263 | paramsContact.status = " (( " + i18n.CONTACT_ASK_FOR_AUTH + ")) "; |
---|
[2577] | 1264 | paramsContact.statusColor = "orange"; |
---|
| 1265 | break; |
---|
| 1266 | |
---|
| 1267 | case "from" : |
---|
| 1268 | |
---|
[2863] | 1269 | paramsContact.status = " (( " + i18n.AUTH_ASK + " )) "; |
---|
[2577] | 1270 | paramsContact.statusColor = "green"; |
---|
| 1271 | break; |
---|
| 1272 | |
---|
| 1273 | case "subscribe" : |
---|
| 1274 | |
---|
[2863] | 1275 | paramsContact.status = " (( " + i18n.AUTH_SENT + " )) "; |
---|
[2577] | 1276 | paramsContact.statusColor = "red"; |
---|
| 1277 | break; |
---|
| 1278 | |
---|
| 1279 | case "not-in-roster" : |
---|
| 1280 | |
---|
[2863] | 1281 | paramsContact.status = " (( " + i18n.ASK_FOR_AUTH_QUESTION + " )) "; |
---|
[2577] | 1282 | paramsContact.statusColor = "orange"; |
---|
| 1283 | break; |
---|
| 1284 | |
---|
| 1285 | default: |
---|
| 1286 | paramsContact.status = " ( " + objContact.contact.subscription + " ) "; |
---|
| 1287 | } |
---|
| 1288 | } |
---|
| 1289 | |
---|
[2471] | 1290 | if( group != "") |
---|
[2397] | 1291 | { |
---|
[2471] | 1292 | var _NodeChild = element.firstChild; |
---|
| 1293 | |
---|
| 1294 | while( _NodeChild ) |
---|
[2397] | 1295 | { |
---|
[2471] | 1296 | if( _NodeChild.childNodes[0].nodeName.toLowerCase() === "span" ) |
---|
| 1297 | { |
---|
| 1298 | if( _NodeChild.childNodes[0].childNodes[0].nodeValue === group ) |
---|
| 1299 | { |
---|
[2670] | 1300 | _NodeChild.innerHTML += loadscript.parse("itens_group", "itensGroup.xsl", paramsContact); |
---|
[2471] | 1301 | } |
---|
| 1302 | } |
---|
[2397] | 1303 | |
---|
[2471] | 1304 | _NodeChild = _NodeChild.nextSibling; |
---|
[2397] | 1305 | } |
---|
[2471] | 1306 | } |
---|
[2397] | 1307 | } |
---|
| 1308 | else |
---|
| 1309 | { |
---|
[2577] | 1310 | |
---|
[2397] | 1311 | // Presence e Status |
---|
| 1312 | var presence = "unavailable"; |
---|
| 1313 | var status = ""; |
---|
[2577] | 1314 | var statusColor = "black"; |
---|
[2397] | 1315 | var statusDisplay = "none"; |
---|
| 1316 | |
---|
[2584] | 1317 | if ( objContact.presence ) |
---|
[2397] | 1318 | { |
---|
[2471] | 1319 | for ( var resource in objContact.presence ) |
---|
[2397] | 1320 | { |
---|
[2670] | 1321 | if ( objContact.presence[resource].constructor == Function ) |
---|
| 1322 | continue; |
---|
| 1323 | |
---|
[2491] | 1324 | if( objContact.presence[resource].show != 'invisible' ) |
---|
| 1325 | presence = objContact.presence[resource].show; |
---|
| 1326 | |
---|
[2577] | 1327 | if( objContact.contact.subscription != "both") |
---|
| 1328 | presence = 'subscription'; |
---|
| 1329 | |
---|
[2397] | 1330 | if( objContact.presence[resource].status ) |
---|
| 1331 | { |
---|
[2577] | 1332 | status = " ( " + objContact.presence[resource].status + " ) "; |
---|
[2397] | 1333 | statusDisplay = "block"; |
---|
| 1334 | } |
---|
[2471] | 1335 | } |
---|
| 1336 | } |
---|
| 1337 | |
---|
| 1338 | var is_open = itensJid.parentNode.childNodes[0].style.backgroundImage; |
---|
| 1339 | is_open = is_open.indexOf("arrow_down.gif"); |
---|
| 1340 | |
---|
[2577] | 1341 | // Authorization |
---|
| 1342 | if( objContact.contact.subscription != "both" ) |
---|
| 1343 | { |
---|
| 1344 | switch(objContact.contact.subscription) |
---|
| 1345 | { |
---|
| 1346 | case "none" : |
---|
| 1347 | |
---|
[2863] | 1348 | status = " (( " + i18n.ASK_FOR_AUTH + " )) "; |
---|
[2577] | 1349 | statusColor = "red"; |
---|
| 1350 | break; |
---|
| 1351 | |
---|
| 1352 | case "to" : |
---|
| 1353 | |
---|
[2863] | 1354 | status = " (( " + i18n.CONTACT_ASK_FOR_AUTH + " )) "; |
---|
[2577] | 1355 | statusColor = "orange"; |
---|
| 1356 | break; |
---|
| 1357 | |
---|
| 1358 | case "from" : |
---|
| 1359 | |
---|
[2863] | 1360 | status = " (( " + i18n.AUTH_ASK + " )) "; |
---|
[2577] | 1361 | statusColor = "green"; |
---|
| 1362 | break; |
---|
| 1363 | |
---|
| 1364 | case "subscribe" : |
---|
| 1365 | |
---|
[2863] | 1366 | status = " (( " + i18n.AUTH_SENT + " )) "; |
---|
[2577] | 1367 | statusColor = "red"; |
---|
| 1368 | break; |
---|
| 1369 | |
---|
| 1370 | case "not-in-roster" : |
---|
| 1371 | |
---|
[2863] | 1372 | status = " (( " + i18n.ASK_FOR_AUTH_QUESTION + " )) "; |
---|
[2577] | 1373 | statusColor = "orange"; |
---|
| 1374 | break; |
---|
| 1375 | |
---|
| 1376 | default: |
---|
| 1377 | status = " ( " + objContact.contact.subscription + " ) "; |
---|
| 1378 | } |
---|
| 1379 | |
---|
| 1380 | statusDisplay = "block"; |
---|
| 1381 | } |
---|
| 1382 | |
---|
[2471] | 1383 | with ( document.getElementById('span_show_' + 'itenContact_' + objContact.contact.jid + '_' + index ) ) |
---|
| 1384 | { |
---|
[2706] | 1385 | if( presence == "unavailable" && !showOffline ) |
---|
| 1386 | { |
---|
| 1387 | style.display = "none"; |
---|
[2397] | 1388 | } |
---|
[2706] | 1389 | else |
---|
| 1390 | { |
---|
| 1391 | if( is_open > 0 ) |
---|
| 1392 | { |
---|
| 1393 | style.display = statusDisplay; |
---|
| 1394 | style.color = statusColor; |
---|
| 1395 | innerHTML = status; |
---|
| 1396 | } |
---|
| 1397 | } |
---|
[2397] | 1398 | } |
---|
[2471] | 1399 | |
---|
[2706] | 1400 | if( presence == "unavailable" && !showOffline ) |
---|
| 1401 | itensJid.style.display = "none"; |
---|
| 1402 | else |
---|
| 1403 | { |
---|
| 1404 | if( is_open > 0 ) |
---|
| 1405 | { |
---|
| 1406 | itensJid.style.display = "block"; |
---|
| 1407 | } |
---|
| 1408 | } |
---|
| 1409 | |
---|
[2626] | 1410 | itensJid.style.background = "url('"+path_jabberit+"templates/default/images/" + presence + ".gif') no-repeat center left"; |
---|
[2397] | 1411 | } |
---|
[2706] | 1412 | |
---|
| 1413 | // Contact OffLine |
---|
| 1414 | if( !objContact.presence && !showOffline ) |
---|
| 1415 | { |
---|
| 1416 | with ( document.getElementById('span_show_' + 'itenContact_' + objContact.contact.jid + '_' + index )) |
---|
| 1417 | { |
---|
| 1418 | style.display = "none"; |
---|
| 1419 | } |
---|
| 1420 | |
---|
| 1421 | with ( document.getElementById('itenContact_' + objContact.contact.jid + '_' + index ) ) |
---|
| 1422 | { |
---|
| 1423 | style.display = "none"; |
---|
| 1424 | } |
---|
| 1425 | } |
---|
[2397] | 1426 | } |
---|
| 1427 | } |
---|
| 1428 | |
---|
| 1429 | for( var i = 0 ; i < users.length; i++ ) |
---|
| 1430 | { |
---|
[2670] | 1431 | if( TrophyIM.rosterObj.roster[users[i]].contact.jid != Base64.decode(loadscript.getUserCurrent().jid) ) |
---|
[2471] | 1432 | { |
---|
[2511] | 1433 | if( TrophyIM.rosterObj.roster[users[i]].contact.groups ) |
---|
[2471] | 1434 | { |
---|
[2511] | 1435 | var groups = TrophyIM.rosterObj.roster[users[i]].contact.groups; |
---|
| 1436 | |
---|
| 1437 | if( groups.length > 0 ) |
---|
| 1438 | { |
---|
| 1439 | for( var j = 0; j < groups.length; j++ ) |
---|
[2670] | 1440 | { |
---|
[2511] | 1441 | addItem( TrophyIM.rosterObj.roster[users[i]], groups[j], element, j ); |
---|
[2670] | 1442 | } |
---|
[2511] | 1443 | } |
---|
| 1444 | else |
---|
| 1445 | { |
---|
| 1446 | addItem( TrophyIM.rosterObj.roster[users[i]], "Geral", element, 0 ); |
---|
| 1447 | } |
---|
[2471] | 1448 | } |
---|
[2397] | 1449 | else |
---|
[2471] | 1450 | { |
---|
| 1451 | addItem( TrophyIM.rosterObj.roster[users[i]], "Geral", element, 0 ); |
---|
| 1452 | } |
---|
| 1453 | } |
---|
[2397] | 1454 | } |
---|
| 1455 | }, |
---|
| 1456 | |
---|
| 1457 | /** Function: rosterClick |
---|
| 1458 | * |
---|
| 1459 | * Handles actions when a roster item is clicked |
---|
| 1460 | */ |
---|
| 1461 | |
---|
| 1462 | rosterClick : function(fulljid) |
---|
| 1463 | { |
---|
| 1464 | TrophyIM.makeChat(fulljid); |
---|
| 1465 | }, |
---|
| 1466 | |
---|
| 1467 | /** Function SetAutorization |
---|
| 1468 | * |
---|
| 1469 | */ |
---|
| 1470 | |
---|
[2491] | 1471 | setAutorization : function( jidTo, jidFrom, _typeSubscription ) |
---|
[2397] | 1472 | { |
---|
[2491] | 1473 | var _id = TrophyIM.connection.getUniqueId(); |
---|
| 1474 | |
---|
| 1475 | TrophyIM.connection.send($pres( ).attrs( {to: jidTo, from: jidFrom, type: _typeSubscription, id: _id}).tree()); |
---|
[2397] | 1476 | }, |
---|
| 1477 | |
---|
| 1478 | /** Function: setPresence |
---|
| 1479 | * |
---|
| 1480 | */ |
---|
| 1481 | |
---|
[2647] | 1482 | setPresence : function( _type ) |
---|
[2397] | 1483 | { |
---|
[2647] | 1484 | if( _type != 'status') |
---|
[2511] | 1485 | { |
---|
[2827] | 1486 | if( _type == "unavailable" && TrophyIM.statusConn.connected ) |
---|
[2641] | 1487 | { |
---|
[2647] | 1488 | var loading_gif = document.getElementById("JabberIMRosterLoadingGif"); |
---|
| 1489 | |
---|
| 1490 | if( TrophyIM._timeOut.renderRoster != null ) |
---|
| 1491 | clearTimeout(TrophyIM._timeOut.renderRoster); |
---|
| 1492 | |
---|
[2750] | 1493 | if( TrophyIM.statusConn.connected ) |
---|
| 1494 | TrophyIM.connection.send($pres({type : _type}).tree()); |
---|
[2647] | 1495 | |
---|
| 1496 | for( var i = 0; i < TrophyIM.connection._requests.length; i++ ) |
---|
| 1497 | { |
---|
| 1498 | if( TrophyIM.connection._requests[i] ) |
---|
| 1499 | TrophyIM.connection._removeRequest(TrophyIM.connection._requests[i]); |
---|
| 1500 | } |
---|
| 1501 | |
---|
| 1502 | TrophyIM.logout(); |
---|
| 1503 | |
---|
[2670] | 1504 | loadscript.clrAllContacts(); |
---|
[2647] | 1505 | |
---|
| 1506 | delete TrophyIM.rosterObj.roster; |
---|
| 1507 | delete TrophyIM.rosterObj.groups; |
---|
| 1508 | |
---|
| 1509 | setTimeout(function() |
---|
| 1510 | { |
---|
| 1511 | if( loading_gif.style.display == "block" ) |
---|
| 1512 | loading_gif.style.display = "none"; |
---|
| 1513 | }, 1000); |
---|
[2641] | 1514 | } |
---|
| 1515 | else |
---|
| 1516 | { |
---|
| 1517 | if( !TrophyIM.autoConnection.connect ) |
---|
| 1518 | { |
---|
| 1519 | TrophyIM.autoConnection.connect = true; |
---|
| 1520 | TrophyIM.load(); |
---|
| 1521 | } |
---|
| 1522 | else |
---|
[2711] | 1523 | { |
---|
[2738] | 1524 | if( TrophyIM.statusConn.connected ) |
---|
[2711] | 1525 | { |
---|
[2738] | 1526 | if( loadscript.getStatusMessage() != "" ) |
---|
| 1527 | { |
---|
| 1528 | var _presence = $pres( ); |
---|
| 1529 | _presence.node.appendChild( Strophe.xmlElement( 'show' ) ).appendChild( Strophe.xmlTextNode( _type ) ); |
---|
| 1530 | _presence.node.appendChild( Strophe.xmlElement( 'status' ) ).appendChild( Strophe.xmlTextNode( loadscript.getStatusMessage() )); |
---|
| 1531 | |
---|
| 1532 | TrophyIM.connection.send( _presence.tree() ); |
---|
| 1533 | } |
---|
| 1534 | else |
---|
| 1535 | { |
---|
| 1536 | TrophyIM.connection.send($pres( ).c('show').t(_type).tree()); |
---|
| 1537 | } |
---|
[2711] | 1538 | } |
---|
| 1539 | } |
---|
[2641] | 1540 | } |
---|
[2511] | 1541 | } |
---|
| 1542 | else |
---|
| 1543 | { |
---|
| 1544 | var _show = "available"; |
---|
| 1545 | var _status = ""; |
---|
| 1546 | |
---|
[2711] | 1547 | if( arguments.length < 2 ) |
---|
[2511] | 1548 | { |
---|
[2711] | 1549 | if( loadscript.getStatusMessage() != "" ) |
---|
[2863] | 1550 | _status = prompt(i18n.TYPE_YOUR_MSG, loadscript.getStatusMessage()); |
---|
[2711] | 1551 | else |
---|
[2863] | 1552 | _status = prompt(i18n.TYPE_YOUR_MSG); |
---|
[2511] | 1553 | |
---|
[2711] | 1554 | var _divStatus = document.getElementById("JabberIMStatusMessage"); |
---|
| 1555 | |
---|
| 1556 | if( ( _status = _status.replace(/^\s+|\s+$|^\n|\n$/g,"") ) != "") |
---|
| 1557 | _divStatus.firstChild.innerHTML = "( " + _status + " )"; |
---|
| 1558 | } |
---|
| 1559 | else |
---|
| 1560 | { |
---|
| 1561 | _status = arguments[1]; |
---|
[2511] | 1562 | } |
---|
[2711] | 1563 | |
---|
[2863] | 1564 | for( var resource in TrophyIM.rosterObj.roster[Base64.decode(loadscript.getUserCurrent().jid)].presence ) |
---|
[2711] | 1565 | { |
---|
| 1566 | if ( TrophyIM.rosterObj.roster[Base64.decode(loadscript.getUserCurrent().jid)].presence[ resource ].constructor == Function ) |
---|
| 1567 | continue; |
---|
| 1568 | |
---|
| 1569 | if ( TROPHYIM_RESOURCE === ("/" + resource) ) |
---|
| 1570 | _show = TrophyIM.rosterObj.roster[Base64.decode(loadscript.getUserCurrent().jid)].presence[resource].show; |
---|
| 1571 | } |
---|
| 1572 | |
---|
[2738] | 1573 | if ( TrophyIM.statusConn.connected ) |
---|
| 1574 | { |
---|
| 1575 | var _presence = $pres( ); |
---|
| 1576 | _presence.node.appendChild( Strophe.xmlElement( 'show' ) ).appendChild( Strophe.xmlTextNode( _show ) ); |
---|
| 1577 | _presence.node.appendChild( Strophe.xmlElement( 'status' ) ).appendChild( Strophe.xmlTextNode( _status ) ); |
---|
| 1578 | |
---|
| 1579 | TrophyIM.connection.send( _presence.tree() ); |
---|
| 1580 | } |
---|
[2511] | 1581 | } |
---|
[2397] | 1582 | }, |
---|
| 1583 | |
---|
| 1584 | /** Function: sendMessage |
---|
| 1585 | * |
---|
| 1586 | * Send message from chat input to user |
---|
| 1587 | */ |
---|
| 1588 | |
---|
[2821] | 1589 | sendMessage : function() |
---|
[2397] | 1590 | { |
---|
[2821] | 1591 | if (arguments.length > 0) { |
---|
| 1592 | var jidTo = arguments[0]; |
---|
| 1593 | var message_input = arguments[1]; |
---|
| 1594 | |
---|
| 1595 | |
---|
| 1596 | message_input = message_input.replace(/^\s+|\s+$|^\n|\n$/g, ""); |
---|
| 1597 | |
---|
| 1598 | if (message_input != "") { |
---|
| 1599 | |
---|
| 1600 | // Send Message |
---|
| 1601 | var newMessage = $msg({ |
---|
| 1602 | to: jidTo, |
---|
| 1603 | from: TrophyIM.connection.jid, |
---|
| 1604 | type: 'chat' |
---|
| 1605 | }); |
---|
| 1606 | newMessage = newMessage.c('body').t(message_input); |
---|
| 1607 | newMessage.up(); |
---|
| 1608 | newMessage = newMessage.c('active').attrs({ |
---|
| 1609 | xmlns: 'http://jabber.org/protocol/chatstates' |
---|
| 1610 | }); |
---|
| 1611 | // Send Message |
---|
| 1612 | TrophyIM.connection.send(newMessage.tree()); |
---|
| 1613 | |
---|
| 1614 | return true; |
---|
| 1615 | } |
---|
[2787] | 1616 | } |
---|
| 1617 | |
---|
| 1618 | return false; |
---|
[2821] | 1619 | }, |
---|
| 1620 | |
---|
| 1621 | /** Function: sendContentMessage |
---|
| 1622 | * |
---|
| 1623 | * Send a content message from chat input to user |
---|
| 1624 | */ |
---|
| 1625 | sendContentMessage : function() |
---|
| 1626 | { |
---|
| 1627 | if( arguments.length > 0 ) |
---|
| 1628 | { |
---|
| 1629 | var jidTo = arguments[0]; |
---|
| 1630 | var state = arguments[1]; |
---|
| 1631 | |
---|
| 1632 | var newMessage = $msg({to: jidTo, from: TrophyIM.connection.jid, type: 'chat'}); |
---|
| 1633 | newMessage = newMessage.c(state).attrs({xmlns : 'http://jabber.org/protocol/chatstates'}); |
---|
| 1634 | // Send content message |
---|
| 1635 | TrophyIM.connection.send(newMessage.tree()); |
---|
| 1636 | } |
---|
[2397] | 1637 | } |
---|
| 1638 | }; |
---|
| 1639 | |
---|
| 1640 | /** Class: TrophyIMRoster |
---|
| 1641 | * |
---|
| 1642 | * |
---|
| 1643 | * This object stores the roster and presence info for the TrophyIMClient |
---|
| 1644 | * |
---|
| 1645 | * roster[jid_lower]['contact'] |
---|
| 1646 | * roster[jid_lower]['presence'][resource] |
---|
| 1647 | */ |
---|
| 1648 | function TrophyIMRoster() |
---|
| 1649 | { |
---|
| 1650 | /** Constants: internal arrays |
---|
| 1651 | * (Object) roster - the actual roster/presence information |
---|
| 1652 | * (Object) groups - list of current groups in the roster |
---|
| 1653 | * (Array) changes - array of jids with presence changes |
---|
| 1654 | */ |
---|
| 1655 | if (TrophyIM.JSONStore.store_working) |
---|
| 1656 | { |
---|
| 1657 | var data = TrophyIM.JSONStore.getData(['roster', 'groups']); |
---|
| 1658 | this.roster = (data['roster'] != null) ? data['roster'] : {}; |
---|
| 1659 | this.groups = (data['groups'] != null) ? data['groups'] : {}; |
---|
| 1660 | } |
---|
| 1661 | else |
---|
| 1662 | { |
---|
| 1663 | this.roster = {}; |
---|
| 1664 | this.groups = {}; |
---|
| 1665 | } |
---|
| 1666 | this.changes = new Array(); |
---|
| 1667 | |
---|
| 1668 | if (TrophyIM.constants.stale_roster) |
---|
| 1669 | { |
---|
| 1670 | for (var jid in this.roster) |
---|
| 1671 | { |
---|
| 1672 | this.changes[this.changes.length] = jid; |
---|
| 1673 | } |
---|
| 1674 | } |
---|
[2471] | 1675 | |
---|
| 1676 | /** Function: addChange |
---|
| 1677 | * |
---|
| 1678 | * Adds given jid to this.changes, keeping this.changes sorted and |
---|
| 1679 | * preventing duplicates. |
---|
| 1680 | * |
---|
| 1681 | * Parameters |
---|
| 1682 | * (String) jid : jid to add to this.changes |
---|
| 1683 | */ |
---|
| 1684 | |
---|
| 1685 | this.addChange = function(jid) |
---|
| 1686 | { |
---|
| 1687 | for (var c = 0; c < this.changes.length; c++) |
---|
| 1688 | { |
---|
| 1689 | if (this.changes[c] == jid) |
---|
| 1690 | { |
---|
| 1691 | return; |
---|
| 1692 | } |
---|
| 1693 | } |
---|
| 1694 | |
---|
| 1695 | this.changes[this.changes.length] = jid; |
---|
| 1696 | |
---|
| 1697 | this.changes.sort(); |
---|
| 1698 | } |
---|
[2397] | 1699 | |
---|
| 1700 | /** Function: addContact |
---|
| 1701 | * |
---|
| 1702 | * Adds given contact to roster |
---|
| 1703 | * |
---|
| 1704 | * Parameters: |
---|
| 1705 | * (String) jid - bare jid |
---|
| 1706 | * (String) subscription - subscription attribute for contact |
---|
| 1707 | * (String) name - name attribute for contact |
---|
[2471] | 1708 | * (Array) groups - array of groups contact is member of |
---|
[2397] | 1709 | */ |
---|
| 1710 | |
---|
[2471] | 1711 | this.addContact = function(jid, subscription, name, groups ) |
---|
[2397] | 1712 | { |
---|
[2584] | 1713 | if( subscription !== "remove" ) |
---|
[2511] | 1714 | { |
---|
[2584] | 1715 | var contact = { jid:jid, subscription:subscription, name:name, groups:groups } |
---|
[2511] | 1716 | var jid_lower = jid.toLowerCase(); |
---|
| 1717 | |
---|
| 1718 | if ( this.roster[jid_lower] ) |
---|
| 1719 | { |
---|
| 1720 | this.roster[jid_lower]['contact'] = contact; |
---|
| 1721 | } |
---|
| 1722 | else |
---|
| 1723 | { |
---|
| 1724 | this.roster[jid_lower] = {contact:contact}; |
---|
| 1725 | } |
---|
[2584] | 1726 | |
---|
[2511] | 1727 | groups = groups ? groups : ['']; |
---|
| 1728 | |
---|
| 1729 | for ( var g = 0; g < groups.length; g++ ) |
---|
| 1730 | { |
---|
| 1731 | if ( !this.groups[groups[g]] ) |
---|
[2471] | 1732 | { |
---|
[2511] | 1733 | this.groups[groups[g]] = {}; |
---|
| 1734 | } |
---|
| 1735 | |
---|
| 1736 | this.groups[groups[g]][jid_lower] = jid_lower; |
---|
[2471] | 1737 | } |
---|
[2511] | 1738 | } |
---|
[2584] | 1739 | else |
---|
[2511] | 1740 | { |
---|
| 1741 | this.removeContact(jid); |
---|
[2471] | 1742 | } |
---|
[2397] | 1743 | } |
---|
[2471] | 1744 | |
---|
[2397] | 1745 | /** Function: getContact |
---|
| 1746 | * |
---|
| 1747 | * Returns contact entry for given jid |
---|
| 1748 | * |
---|
| 1749 | * Parameter: (String) jid - jid to return |
---|
| 1750 | */ |
---|
[2471] | 1751 | |
---|
[2397] | 1752 | this.getContact = function(jid) |
---|
| 1753 | { |
---|
| 1754 | if (this.roster[jid.toLowerCase()]) |
---|
| 1755 | { |
---|
| 1756 | return this.roster[jid.toLowerCase()]['contact']; |
---|
| 1757 | } |
---|
| 1758 | } |
---|
[2471] | 1759 | |
---|
| 1760 | /** Function: getPresence |
---|
| 1761 | * |
---|
| 1762 | * Returns best presence for given jid as Array(resource, priority, show, |
---|
| 1763 | * status) |
---|
| 1764 | * |
---|
| 1765 | * Parameter: (String) fulljid - jid to return best presence for |
---|
| 1766 | */ |
---|
| 1767 | |
---|
| 1768 | this.getPresence = function(fulljid) |
---|
| 1769 | { |
---|
| 1770 | var jid = Strophe.getBareJidFromJid(fulljid); |
---|
| 1771 | var current = null; |
---|
| 1772 | |
---|
| 1773 | if (this.roster[jid.toLowerCase()] && this.roster[jid.toLowerCase()]['presence']) |
---|
| 1774 | { |
---|
| 1775 | for (var resource in this.roster[jid.toLowerCase()]['presence']) |
---|
| 1776 | { |
---|
[2670] | 1777 | if ( this.roster[jid.toLowerCase()]['presence'][ resource ].constructor == Function ) |
---|
| 1778 | continue; |
---|
| 1779 | |
---|
[2471] | 1780 | var presence = this.roster[jid.toLowerCase()]['presence'][resource]; |
---|
| 1781 | if (current == null) |
---|
| 1782 | { |
---|
| 1783 | current = presence |
---|
| 1784 | } |
---|
| 1785 | else |
---|
| 1786 | { |
---|
| 1787 | if(presence['priority'] > current['priority'] && ((presence['show'] == "chat" |
---|
| 1788 | || presence['show'] == "available") || (current['show'] != "chat" || |
---|
| 1789 | current['show'] != "available"))) |
---|
| 1790 | { |
---|
| 1791 | current = presence |
---|
| 1792 | } |
---|
| 1793 | } |
---|
| 1794 | } |
---|
| 1795 | } |
---|
| 1796 | return current; |
---|
| 1797 | } |
---|
| 1798 | |
---|
| 1799 | /** Function: groupHasChanges |
---|
| 1800 | * |
---|
| 1801 | * Returns true if current group has members in this.changes |
---|
| 1802 | * |
---|
| 1803 | * Parameters: |
---|
| 1804 | * (String) group - name of group to check |
---|
| 1805 | */ |
---|
| 1806 | |
---|
| 1807 | this.groupHasChanges = function(group) |
---|
| 1808 | { |
---|
| 1809 | for (var c = 0; c < this.changes.length; c++) |
---|
| 1810 | { |
---|
| 1811 | if (this.groups[group][this.changes[c]]) |
---|
| 1812 | { |
---|
| 1813 | return true; |
---|
| 1814 | } |
---|
| 1815 | } |
---|
| 1816 | return false; |
---|
| 1817 | } |
---|
| 1818 | |
---|
| 1819 | /** Function removeContact |
---|
| 1820 | * |
---|
| 1821 | * Parameters |
---|
| 1822 | * (String) jid |
---|
| 1823 | */ |
---|
| 1824 | |
---|
| 1825 | this.removeContact = function(jid) |
---|
| 1826 | { |
---|
[2577] | 1827 | if( this.roster[ jid ] ) |
---|
| 1828 | { |
---|
| 1829 | var groups = this.roster[ jid ].contact.groups; |
---|
[2491] | 1830 | |
---|
[2577] | 1831 | if( groups ) |
---|
[2491] | 1832 | { |
---|
[2577] | 1833 | for ( var i = 0; i < groups.length; i++ ) |
---|
| 1834 | { |
---|
| 1835 | delete this.groups[ groups[ i ] ][ jid ]; |
---|
| 1836 | } |
---|
| 1837 | |
---|
| 1838 | for ( var i = 0; i < groups.length; i++ ) |
---|
| 1839 | { |
---|
| 1840 | var contacts = 0; |
---|
| 1841 | for ( var contact in this.groups[ groups[ i ] ] ) |
---|
[2670] | 1842 | { |
---|
| 1843 | if ( this.groups[ groups[ i ] ][ contact ].constructor == Function ) |
---|
| 1844 | continue; |
---|
| 1845 | |
---|
[2577] | 1846 | contacts++; |
---|
[2670] | 1847 | } |
---|
[2577] | 1848 | |
---|
| 1849 | if ( ! contacts ) |
---|
| 1850 | delete this.groups[ groups[ i ] ]; |
---|
| 1851 | } |
---|
[2491] | 1852 | } |
---|
| 1853 | |
---|
[2577] | 1854 | // Delete Object roster |
---|
| 1855 | if( this.roster[jid] ) |
---|
| 1856 | delete this.roster[jid]; |
---|
[2471] | 1857 | } |
---|
| 1858 | } |
---|
| 1859 | |
---|
[2397] | 1860 | /** Function: setPresence |
---|
| 1861 | * |
---|
| 1862 | * Sets presence |
---|
| 1863 | * |
---|
| 1864 | * Parameters: |
---|
| 1865 | * (String) fulljid: full jid with presence |
---|
| 1866 | * (Integer) priority: priority attribute from presence |
---|
| 1867 | * (String) show: show attribute from presence |
---|
| 1868 | * (String) status: status attribute from presence |
---|
| 1869 | */ |
---|
| 1870 | |
---|
| 1871 | this.setPresence = function(fulljid, priority, show, status) |
---|
| 1872 | { |
---|
[2577] | 1873 | var barejid = Strophe.getBareJidFromJid(fulljid); |
---|
| 1874 | var resource = Strophe.getResourceFromJid(fulljid); |
---|
| 1875 | var jid_lower = barejid.toLowerCase(); |
---|
[2397] | 1876 | |
---|
[2511] | 1877 | if( show != 'unavailable') |
---|
| 1878 | { |
---|
| 1879 | if (!this.roster[jid_lower]) |
---|
[2397] | 1880 | { |
---|
[2511] | 1881 | this.addContact(barejid, 'not-in-roster'); |
---|
| 1882 | } |
---|
| 1883 | var presence = |
---|
[2397] | 1884 | { |
---|
[2511] | 1885 | resource:resource, priority:priority, show:show, status:status |
---|
| 1886 | } |
---|
| 1887 | |
---|
| 1888 | if (!this.roster[jid_lower]['presence']) |
---|
[2397] | 1889 | { |
---|
[2584] | 1890 | this.roster[jid_lower]['presence'] = {}; |
---|
[2511] | 1891 | } |
---|
[2584] | 1892 | this.roster[jid_lower]['presence'][resource] = presence; |
---|
[2397] | 1893 | } |
---|
[2511] | 1894 | else if (this.roster[jid_lower] && this.roster[jid_lower]['presence'] && this.roster[jid_lower]['presence'][resource]) |
---|
| 1895 | { |
---|
| 1896 | delete this.roster[jid_lower]['presence'][resource]; |
---|
| 1897 | } |
---|
| 1898 | |
---|
| 1899 | this.addChange(jid_lower); |
---|
[2471] | 1900 | } |
---|
| 1901 | |
---|
| 1902 | /** Fuction: save |
---|
| 1903 | * |
---|
| 1904 | * Saves roster data to JSON store |
---|
| 1905 | */ |
---|
| 1906 | |
---|
| 1907 | this.save = function() |
---|
| 1908 | { |
---|
| 1909 | if (TrophyIM.JSONStore.store_working) |
---|
[2397] | 1910 | { |
---|
[2471] | 1911 | TrophyIM.JSONStore.setData({roster:this.roster, |
---|
| 1912 | groups:this.groups, active_chat:TrophyIM.activeChats['current'], |
---|
| 1913 | chat_history:TrophyIM.chatHistory}); |
---|
| 1914 | } |
---|
| 1915 | } |
---|
| 1916 | |
---|
[2397] | 1917 | } |
---|
| 1918 | /** Class: TrophyIMJSONStore |
---|
| 1919 | * |
---|
| 1920 | * |
---|
| 1921 | * This object is the mechanism by which TrophyIM stores and retrieves its |
---|
| 1922 | * variables from the url provided by TROPHYIM_JSON_STORE |
---|
| 1923 | * |
---|
| 1924 | */ |
---|
| 1925 | function TrophyIMJSONStore() { |
---|
| 1926 | this.store_working = false; |
---|
| 1927 | /** Function _newXHR |
---|
| 1928 | * |
---|
| 1929 | * Set up new cross-browser xmlhttprequest object |
---|
| 1930 | * |
---|
| 1931 | * Parameters: |
---|
| 1932 | * (function) handler = what to set onreadystatechange to |
---|
| 1933 | */ |
---|
| 1934 | this._newXHR = function (handler) { |
---|
| 1935 | var xhr = null; |
---|
| 1936 | if (window.XMLHttpRequest) { |
---|
| 1937 | xhr = new XMLHttpRequest(); |
---|
| 1938 | if (xhr.overrideMimeType) { |
---|
| 1939 | xhr.overrideMimeType("text/xml"); |
---|
| 1940 | } |
---|
| 1941 | } else if (window.ActiveXObject) { |
---|
| 1942 | xhr = new ActiveXObject("Microsoft.XMLHTTP"); |
---|
| 1943 | } |
---|
| 1944 | return xhr; |
---|
| 1945 | } |
---|
| 1946 | /** Function getData |
---|
| 1947 | * Gets data from JSONStore |
---|
| 1948 | * |
---|
| 1949 | * Parameters: |
---|
| 1950 | * (Array) vars = Variables to get from JSON store |
---|
| 1951 | * |
---|
| 1952 | * Returns: |
---|
| 1953 | * Object with variables indexed by names given in parameter 'vars' |
---|
| 1954 | */ |
---|
| 1955 | this.getData = function(vars) { |
---|
| 1956 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
| 1957 | Strophe.debug("Retrieving JSONStore data"); |
---|
| 1958 | var xhr = this._newXHR(); |
---|
| 1959 | var getdata = "get=" + vars.join(","); |
---|
| 1960 | try { |
---|
| 1961 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
| 1962 | } catch (e) { |
---|
| 1963 | Strophe.error("JSONStore open failed."); |
---|
| 1964 | return false; |
---|
| 1965 | } |
---|
| 1966 | xhr.setRequestHeader('Content-type', |
---|
| 1967 | 'application/x-www-form-urlencoded'); |
---|
| 1968 | xhr.setRequestHeader('Content-length', getdata.length); |
---|
| 1969 | xhr.send(getdata); |
---|
| 1970 | if (xhr.readyState == 4 && xhr.status == 200) { |
---|
| 1971 | try { |
---|
| 1972 | var dataObj = JSON.parse(xhr.responseText); |
---|
| 1973 | return this.emptyFix(dataObj); |
---|
| 1974 | } catch(e) { |
---|
| 1975 | Strophe.error("Could not parse JSONStore response" + |
---|
| 1976 | xhr.responseText); |
---|
| 1977 | return false; |
---|
| 1978 | } |
---|
| 1979 | } else { |
---|
| 1980 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
| 1981 | return false; |
---|
| 1982 | } |
---|
| 1983 | } |
---|
| 1984 | } |
---|
| 1985 | /** Function emptyFix |
---|
| 1986 | * Fix for bugs in external JSON implementations such as |
---|
| 1987 | * http://bugs.php.net/bug.php?id=41504. |
---|
| 1988 | * A.K.A. Don't use PHP, people. |
---|
| 1989 | */ |
---|
| 1990 | this.emptyFix = function(obj) { |
---|
| 1991 | if (typeof(obj) == "object") { |
---|
| 1992 | for (var i in obj) { |
---|
[2670] | 1993 | if ( obj[i].constructor == Function ) |
---|
| 1994 | continue; |
---|
| 1995 | |
---|
[2397] | 1996 | if (i == '_empty_') { |
---|
| 1997 | obj[""] = this.emptyFix(obj['_empty_']); |
---|
| 1998 | delete obj['_empty_']; |
---|
| 1999 | } else { |
---|
| 2000 | obj[i] = this.emptyFix(obj[i]); |
---|
| 2001 | } |
---|
| 2002 | } |
---|
| 2003 | } |
---|
| 2004 | return obj |
---|
| 2005 | } |
---|
| 2006 | /** Function delData |
---|
| 2007 | * Deletes data from JSONStore |
---|
| 2008 | * |
---|
| 2009 | * Parameters: |
---|
| 2010 | * (Array) vars = Variables to delete from JSON store |
---|
| 2011 | * |
---|
| 2012 | * Returns: |
---|
| 2013 | * Status of delete attempt. |
---|
| 2014 | */ |
---|
| 2015 | this.delData = function(vars) { |
---|
| 2016 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
| 2017 | Strophe.debug("Retrieving JSONStore data"); |
---|
| 2018 | var xhr = this._newXHR(); |
---|
| 2019 | var deldata = "del=" + vars.join(","); |
---|
| 2020 | try { |
---|
| 2021 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
| 2022 | } catch (e) { |
---|
| 2023 | Strophe.error("JSONStore open failed."); |
---|
| 2024 | return false; |
---|
| 2025 | } |
---|
| 2026 | xhr.setRequestHeader('Content-type', |
---|
| 2027 | 'application/x-www-form-urlencoded'); |
---|
| 2028 | xhr.setRequestHeader('Content-length', deldata.length); |
---|
| 2029 | xhr.send(deldata); |
---|
| 2030 | if (xhr.readyState == 4 && xhr.status == 200) { |
---|
| 2031 | try { |
---|
| 2032 | var dataObj = JSON.parse(xhr.responseText); |
---|
| 2033 | return dataObj; |
---|
| 2034 | } catch(e) { |
---|
| 2035 | Strophe.error("Could not parse JSONStore response"); |
---|
| 2036 | return false; |
---|
| 2037 | } |
---|
| 2038 | } else { |
---|
| 2039 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
| 2040 | return false; |
---|
| 2041 | } |
---|
| 2042 | } |
---|
| 2043 | } |
---|
| 2044 | /** Function setData |
---|
| 2045 | * Stores data in JSONStore, overwriting values if they exist |
---|
| 2046 | * |
---|
| 2047 | * Parameters: |
---|
| 2048 | * (Object) vars : Object containing named vars to store ({name: value, |
---|
| 2049 | * othername: othervalue}) |
---|
| 2050 | * |
---|
| 2051 | * Returns: |
---|
| 2052 | * Status of storage attempt |
---|
| 2053 | */ |
---|
[2736] | 2054 | this.setData = function(vars) |
---|
| 2055 | { |
---|
| 2056 | if ( typeof(TROPHYIM_JSON_STORE) != undefined ) |
---|
| 2057 | { |
---|
[2397] | 2058 | var senddata = "set=" + JSON.stringify(vars); |
---|
| 2059 | var xhr = this._newXHR(); |
---|
[2736] | 2060 | try |
---|
| 2061 | { |
---|
[2397] | 2062 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
[2736] | 2063 | } |
---|
| 2064 | catch (e) |
---|
| 2065 | { |
---|
[2397] | 2066 | Strophe.error("JSONStore open failed."); |
---|
| 2067 | return false; |
---|
| 2068 | } |
---|
| 2069 | xhr.setRequestHeader('Content-type', |
---|
| 2070 | 'application/x-www-form-urlencoded'); |
---|
| 2071 | xhr.setRequestHeader('Content-length', senddata.length); |
---|
| 2072 | xhr.send(senddata); |
---|
| 2073 | if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText == |
---|
| 2074 | "OK") { |
---|
| 2075 | return true; |
---|
| 2076 | } else { |
---|
| 2077 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
| 2078 | return false; |
---|
| 2079 | } |
---|
| 2080 | } |
---|
| 2081 | } |
---|
[2736] | 2082 | |
---|
[2397] | 2083 | var testData = true; |
---|
[2736] | 2084 | |
---|
[2397] | 2085 | if (this.setData({testData:testData})) { |
---|
| 2086 | var testResult = this.getData(['testData']); |
---|
| 2087 | if (testResult && testResult['testData'] == true) { |
---|
| 2088 | this.store_working = true; |
---|
| 2089 | } |
---|
| 2090 | } |
---|
| 2091 | } |
---|
| 2092 | /** Constants: Node types |
---|
| 2093 | * |
---|
| 2094 | * Implementations of constants that IE doesn't have, but we need. |
---|
| 2095 | */ |
---|
| 2096 | if (document.ELEMENT_NODE == null) { |
---|
| 2097 | document.ELEMENT_NODE = 1; |
---|
| 2098 | document.ATTRIBUTE_NODE = 2; |
---|
| 2099 | document.TEXT_NODE = 3; |
---|
| 2100 | document.CDATA_SECTION_NODE = 4; |
---|
| 2101 | document.ENTITY_REFERENCE_NODE = 5; |
---|
| 2102 | document.ENTITY_NODE = 6; |
---|
| 2103 | document.PROCESSING_INSTRUCTION_NODE = 7; |
---|
| 2104 | document.COMMENT_NODE = 8; |
---|
| 2105 | document.DOCUMENT_NODE = 9; |
---|
| 2106 | document.DOCUMENT_TYPE_NODE = 10; |
---|
| 2107 | document.DOCUMENT_FRAGMENT_NODE = 11; |
---|
| 2108 | document.NOTATION_NODE = 12; |
---|
| 2109 | } |
---|
| 2110 | |
---|
| 2111 | /** Function: importNode |
---|
| 2112 | * |
---|
| 2113 | * document.importNode implementation for IE, which doesn't have importNode |
---|
| 2114 | * |
---|
| 2115 | * Parameters: |
---|
| 2116 | * (Object) node - dom object |
---|
| 2117 | * (Boolean) allChildren - import node's children too |
---|
| 2118 | */ |
---|
| 2119 | if (!document.importNode) { |
---|
| 2120 | document.importNode = function(node, allChildren) { |
---|
| 2121 | switch (node.nodeType) { |
---|
| 2122 | case document.ELEMENT_NODE: |
---|
| 2123 | var newNode = document.createElement(node.nodeName); |
---|
| 2124 | if (node.attributes && node.attributes.length > 0) { |
---|
| 2125 | for(var i = 0; i < node.attributes.length; i++) { |
---|
| 2126 | newNode.setAttribute(node.attributes[i].nodeName, |
---|
| 2127 | node.getAttribute(node.attributes[i].nodeName)); |
---|
| 2128 | } |
---|
| 2129 | } |
---|
| 2130 | if (allChildren && node.childNodes && |
---|
| 2131 | node.childNodes.length > 0) { |
---|
| 2132 | for (var i = 0; i < node.childNodes.length; i++) { |
---|
| 2133 | newNode.appendChild(document.importNode( |
---|
| 2134 | node.childNodes[i], allChildren)); |
---|
| 2135 | } |
---|
| 2136 | } |
---|
| 2137 | return newNode; |
---|
| 2138 | break; |
---|
| 2139 | case document.TEXT_NODE: |
---|
| 2140 | case document.CDATA_SECTION_NODE: |
---|
| 2141 | case document.COMMENT_NODE: |
---|
| 2142 | return document.createTextNode(node.nodeValue); |
---|
| 2143 | break; |
---|
| 2144 | } |
---|
| 2145 | }; |
---|
| 2146 | } |
---|
| 2147 | |
---|
| 2148 | /** |
---|
| 2149 | * |
---|
| 2150 | * Bootstrap self into window.onload and window.onunload |
---|
| 2151 | */ |
---|
| 2152 | |
---|
[2584] | 2153 | var oldonunload = window.onunload; |
---|
[2397] | 2154 | |
---|
| 2155 | window.onunload = function() |
---|
| 2156 | { |
---|
[2584] | 2157 | if( oldonunload ) |
---|
[2397] | 2158 | { |
---|
| 2159 | oldonunload(); |
---|
| 2160 | } |
---|
[2584] | 2161 | |
---|
[2672] | 2162 | TrophyIM.setPresence('unavailable'); |
---|
[2397] | 2163 | } |
---|