source: sandbox/jabberit_messenger/trophy_expresso/js/trophyim.js @ 2397

Revision 2397, 41.4 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Importacao do modulo trophy integrado ao expresso.

  • Property svn:executable set to *
Line 
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
8var TROPHYIM_BOSH_SERVICE       = "/proxy/xmpp-httpbind";  //Change to suit
9
10var TROPHYIM_LOG_LINES          = 200;
11
12var TROPHYIM_LOGLEVEL           = 0; //0=debug, 1=info, 2=warn, 3=error, 4=fatal
13
14var TROPHYIM_VERSION            = "0.3";
15
16var 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 */
24DOMObjects = {
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 */
132TrophyIM = {
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(';');
200        for (var c in cookies) {
201            while (cookies[c].charAt(0)==' ') {
202                cookies[c] = cookies[c].substring(1,cookies[c].length);
203            }
204            if (cookies[c].substr(0, 8) == "trophyim") {
205                var nvpair = cookies[c].split("=", 2);
206                cObj[nvpair[0]] = nvpair[1];
207                TrophyIM.setCookie(nvpair[0], nvpair[1]);
208            }
209        }
210        return cObj;
211    },
212       
213    /** Function: load
214     *
215     *  This function searches for the trophyimclient div and loads the client
216     *  into it.
217     */
218
219        load : function()
220        {
221                if (!TrophyIM.statusConn.connected)
222                {
223                        TrophyIM.cookies = TrophyIM.getCookies();
224                       
225                        //Load other .js scripts needed
226                        document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/strophe.js'));
227                        document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/md5.js'));
228                        document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/sha1.js'));
229                        document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'strophejs/b64.js'));
230                        document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript(path_jabberit + 'js/json2.js')); //Keep this script last
231                        //Wait a second to give scripts time to load
232                        setTimeout("TrophyIM.showLogin()", 500);
233                }
234                else
235                {
236                        loadIM.rosterDiv();
237                }
238   },
239   
240    /** Function: storeData
241     *
242     *  Store all our data in the JSONStore, if it is active
243     */
244   storeData : function()
245   {
246        if ( TrophyIM.connection && TrophyIM.connection.connected )
247                {
248            TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" +
249            TrophyIM.connection.sid + "|" +  TrophyIM.connection.rid);
250            TrophyIM.rosterObj.save();
251        }
252    },
253    /**  Function: showlogin
254     *
255     *   This function clears out the IM box and either redisplays the login
256     *   page, or re-attaches to Strophe, preserving the logging div if it
257     *   exists, or creating a new one of we are re-attaching.
258     */
259    showLogin : function()
260        {
261        //JSON is the last script to load, so we wait on it
262        //Added Strophe check too because of bug where it's sometimes missing
263        if (typeof(JSON) != undefined && typeof(Strophe) != undefined)
264        {
265                TrophyIM.JSONStore = new TrophyIMJSONStore();
266               
267            if ( TrophyIM.JSONStore.store_working && TrophyIM.cookies['trophyim_bosh_xid'] )
268            {
269                var xids = TrophyIM.cookies['trophyim_bosh_xid'].split("|");
270                TrophyIM.delCookie('trophyim_bosh_xid');
271                TrophyIM.constants.stale_roster = true;
272                       
273                                TrophyIM.connection                             = new Strophe.Connection(TROPHYIM_BOSH_SERVICE);
274                TrophyIM.connection.rawInput    = TrophyIM.rawInput;
275                TrophyIM.connection.rawOutput   = TrophyIM.rawOutput;
276                Strophe.log = TrophyIM.log;
277                Strophe.info('Attempting Strophe attach.');
278                TrophyIM.connection.attach(xids[0], xids[1], xids[2], TrophyIM.onConnect);
279                TrophyIM.onConnect(Strophe.Status.CONNECTED);
280            }
281            else
282            {
283                                // List Contact
284                                loadIM.rosterDiv();
285                               
286                                if ( loadIM.getUserCurrent() != null )
287                                {
288                                        TrophyIM.login( loadIM.getUserCurrent().jid, loadIM.getUserCurrent().password );
289                                }
290                                else
291                                {
292                                        loadIM.loginPage();
293                                }
294            }
295        }
296        else
297        {
298                setTimeout("TrophyIM.showLogin()", 500);
299        }
300    },
301   
302        /** Function: log
303     *
304     *  This function logs the given message in the trophyimlog div
305     *
306     *  Parameter: (String) msg - the message to log
307     */
308   
309    log : function(level, msg)
310    {
311        if (TrophyIM.logging_div && level >= TROPHYIM_LOGLEVEL) {
312            while(TrophyIM.logging_div.childNodes.length > TROPHYIM_LOG_LINES) {
313                TrophyIM.logging_div.removeChild(
314                TrophyIM.logging_div.firstChild);
315            }
316            var msg_div = document.createElement('div');
317            msg_div.className = 'trophyimlogitem';
318            msg_div.appendChild(document.createTextNode(msg));
319            TrophyIM.logging_div.appendChild(msg_div);
320            TrophyIM.logging_div.scrollTop = TrophyIM.logging_div.scrollHeight;
321        }
322    },
323       
324    /** Function: rawInput
325     *
326     *  This logs the packets actually recieved by strophe at the debug level
327     */
328    rawInput : function (data)
329        {
330        Strophe.debug("RECV: " + data);
331    },
332       
333    /** Function: rawInput
334     *
335     *  This logs the packets actually recieved by strophe at the debug level
336     */
337    rawOutput : function (data)
338        {
339        Strophe.debug("SEND: " + data);
340    },
341       
342    /** Function: login
343     *
344     *  This function logs into server using information given on login page.
345     *  Since the login page is where the logging checkbox is, it makes or
346     *  removes the logging div and cookie accordingly.
347     *
348     */
349    login : function()
350        {
351                if (TrophyIM.JSONStore.store_working)
352                {
353                   //In case they never logged out
354            TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', 'chat_history']);
355        }
356
357                TrophyIM.connection                             = new Strophe.Connection(TROPHYIM_BOSH_SERVICE);
358        TrophyIM.connection.rawInput    = TrophyIM.rawInput;
359        TrophyIM.connection.rawOutput   = TrophyIM.rawOutput;
360        Strophe.log                                     = TrophyIM.log;
361       
362                if ( arguments.length > 0 )
363                {
364                        var barejid = arguments[0];
365                        var password = arguments[1];
366                       
367                        TrophyIM.connection.connect(barejid + TROPHYIM_RESOURCE, password, TrophyIM.onConnect);
368                }
369                else
370                {
371                        var barejid             = document.getElementById('trophyimjid').value
372                        var fulljid             = barejid + TROPHYIM_RESOURCE;
373                        var password    = document.getElementById('trophyimpass').value;
374                        var button              = document.getElementById('trophyimconnect');
375                       
376                        loadIM.setUserCurrent( barejid, password);
377                       
378                        if ( button.value == 'connect' )
379                        {
380                                button.value = 'disconnect';
381                                TrophyIM.connection.connect(fulljid, password, TrophyIM.onConnect);
382                        }
383                        else
384                        {
385                                button.value = 'connect';
386                                TrophyIM.connection.disconnect();
387                        }
388                }
389
390                TrophyIM.setCookie('trophyimjid', barejid);
391
392    },
393       
394    /** Function: logout
395     *
396     *  Logs into fresh session through Strophe, purging any old data.
397     */
398    logout : function()
399        {
400        TrophyIM.delCookie('trophyim_bosh_xid');
401        delete TrophyIM['cookies']['trophyim_bosh_xid'];
402       
403                if (TrophyIM.JSONStore.store_working)
404                {
405            TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', 'chat_history']);
406        }
407       
408                for (var chat in TrophyIM.activeChats['divs'])
409                {
410            delete TrophyIM.activeChats['divs'][chat];
411        }
412               
413        TrophyIM.activeChats = {current: null, divs: {}},
414        TrophyIM.connection.disconnect();
415        TrophyIM.showLogin();
416               
417    },
418       
419    /** Function onConnect
420     *
421     *  Callback given to Strophe upon connection to BOSH proxy.
422     */
423    onConnect : function(status)
424        {
425                if (status == Strophe.Status.CONNECTING)
426                {
427                        Strophe.info('Strophe is connecting.');
428        }
429               
430                if (status == Strophe.Status.CONNFAIL)
431                {
432                        Strophe.info('Strophe failed to connect.');
433            TrophyIM.delCookie('trophyim_bosh_xid');
434            TrophyIM.showLogin();
435        }
436               
437                if (status == Strophe.Status.DISCONNECTING)
438                {
439                        Strophe.info('Strophe is disconnecting.');
440        }
441               
442                if (status == Strophe.Status.DISCONNECTED)
443                {
444                        Strophe.info('Strophe is disconnected.');
445            TrophyIM.delCookie('trophyim_bosh_xid');
446            TrophyIM.showLogin();
447        }
448               
449                if (status == Strophe.Status.CONNECTED)
450                {
451                        loadIM.setStatusJabber('Disponível','available');
452                        TrophyIM.statusConn.connected = true;
453            TrophyIM.showClient();
454                        Strophe.info('Strophe is connected.');
455        }
456    },
457
458    /** Function: showClient
459     *
460     *  This clears out the main div and puts in the main client.  It also
461     *  registers all the handlers for Strophe to call in the client.
462     */
463    showClient : function()
464        {
465        TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" +
466        TrophyIM.connection.sid + "|" +  TrophyIM.connection.rid);
467               
468        TrophyIM.rosterObj = new TrophyIMRoster();
469        TrophyIM.connection.addHandler(TrophyIM.onVersion, Strophe.NS.VERSION, 'iq', null, null, null);
470        TrophyIM.connection.addHandler(TrophyIM.onRoster, Strophe.NS.ROSTER, 'iq', null, null, null);
471        TrophyIM.connection.addHandler(TrophyIM.onPresence, null, 'presence', null, null, null);
472        TrophyIM.connection.addHandler(TrophyIM.onMessage, null, 'message', null, null,  null);
473       
474                //Get roster then announce presence.
475        TrophyIM.connection.send($iq({type: 'get', xmlns: Strophe.NS.CLIENT}).c('query', {xmlns: Strophe.NS.ROSTER}).tree());
476        TrophyIM.connection.send($pres().tree());
477                setTimeout("TrophyIM.renderRoster()", 1000);
478    },
479       
480    /** Function: clearClient
481     *
482     *  Clears out client div, preserving and returning existing logging_div if
483     *  one exists
484     */
485    clearClient : function() {
486        if(TrophyIM.logging_div) {
487            var logging_div = TrophyIM.client_div.removeChild(
488            document.getElementById('trophyimlog'));
489        } else {
490            var logging_div = null;
491        }
492        while(TrophyIM.client_div.childNodes.length > 0) {
493            TrophyIM.client_div.removeChild(TrophyIM.client_div.firstChild);
494        }
495        return logging_div;
496    },
497    /** Function: onVersion
498     *
499     *  jabber:iq:version query handler
500     */
501    onVersion : function(msg) {
502        Strophe.debug("Version handler");
503        if (msg.getAttribute('type') == 'get') {
504            var from = msg.getAttribute('from');
505            var to = msg.getAttribute('to');
506            var id = msg.getAttribute('id');
507            var reply = $iq({type: 'result', to: from, from: to, id: id}).c('query',
508            {name: "TrophyIM", version: TROPHYIM_VERSION, os:
509            "Javascript-capable browser"});
510            TrophyIM.connection.send(reply.tree());
511        }
512        return true;
513    },
514    /** Function: onRoster
515     *
516     *  Roster iq handler
517     */
518    onRoster : function(msg)
519        {
520                //Strophe.debug("Roster handler");
521               
522        var roster_items = msg.firstChild.getElementsByTagName('item');
523               
524                for (var i = 0; i < roster_items.length; i++)
525                {
526                        var groups = roster_items[i].getElementsByTagName('group');
527            var group_array = new Array();
528           
529                        for (var g = 0; g < groups.length; g++)
530                        {
531                                group_array[group_array.length] = groups[g].firstChild.nodeValue;
532            }
533                        with ( roster_items[i] )
534                        {
535                                TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('subscription'), getAttribute('name'), group_array);
536                        }
537        }
538       
539                if ( msg.getAttribute('type') == 'set' )
540                {
541                        TrophyIM.connection.send($iq({type: 'reply', id:
542            msg.getAttribute('id'), to: msg.getAttribute('from')}).tree());
543        }
544       
545                return true;
546    },
547    /** Function: onPresence
548     *
549     *  Presence handler
550     */
551    onPresence : function(msg)
552        {
553                Strophe.debug("Presence handler");
554        var type = msg.getAttribute('type') ? msg.getAttribute('type') : 'available';
555        var show = msg.getElementsByTagName('show').length ? Strophe.getText(msg.getElementsByTagName('show')[0]) : type;
556        var status = msg.getElementsByTagName('status').length ? Strophe.getText(msg.getElementsByTagName('status')[0]) : '';
557        var priority = msg.getElementsByTagName('priority').length ? parseInt(Strophe.getText(msg.getElementsByTagName('priority')[0])) : 0;
558                TrophyIM.rosterObj.setPresence(msg.getAttribute('from'), priority, show, status);
559               
560        return true;
561    },
562    /** Function: onMessage
563     *
564     *  Message handler
565     */
566    onMessage : function(msg)
567    {
568        Strophe.debug("Message handler");
569   
570        var from = msg.getAttribute('from');
571        var type = msg.getAttribute('type');
572        var elems = msg.getElementsByTagName('body');
573
574        if ( (type == 'chat' || type == 'normal') && elems.length > 0 )
575        {
576            var barejid         = Strophe.getBareJidFromJid(from);
577            var jid_lower       = barejid.toLowerCase();
578            var contact = "";
579               
580            if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] )
581            {
582                contact = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'];
583            }
584            else
585            {
586                contact = barejid.toLowerCase();
587                contact = contact.substring(0, contact.indexOf('@'));
588            }   
589           
590            var message =
591            {
592                contact : "<font style='font-weight:bold; color:black;'>" + contact + "</font>",
593                msg             : Strophe.getText(elems[0])     
594            };
595
596            TrophyIM.makeChat(from); //Make sure we have a chat window
597            TrophyIM.addMessage(message, jid_lower);
598        }
599        return true;
600    },
601    /** Function: makeChat
602     *
603     *  Make sure chat window to given fulljid exists, switching chat context to
604     *  given resource
605     */
606    makeChat : function(fulljid)
607    {
608       
609        var barjid = Strophe.getBareJidFromJid(fulljid);
610       
611        var paramsChatBox =
612        {
613                        'idChatBox'     : barjid + "__chatBox",
614                        'jidTo'                 : barjid,
615                                'path_jabberit' : path_jabberit
616        };
617       
618        var winChatBox =
619        {
620                         id_window              : "window_chat_area_" + barjid,
621                         width                  : 387,
622                         height                 : 365,
623                         top                    : 100,
624                         left                   : 400,
625                         draggable              : true,
626                         visible                : "display",
627                         resizable              : true,
628                         zindex                 : loadIM.getZIndex(),
629                         title                  : barjid.substring(0, barjid.indexOf('@')),
630                         closeAction    : "hidden",
631                         content                : loadIM.parse("chat_box","chatBox.xsl", paramsChatBox)
632        }
633       
634        _winBuild(winChatBox)
635               
636                loadIM.configEvents(
637                        document.getElementById( barjid + '__sendBox'),
638                        'onkeyup', function(e)
639                        {
640                                if( e.keyCode == 13 )
641                                {
642                                        TrophyIM.sendMessage( barjid );
643                                        document.getElementById( barjid + '__sendBox').value = '';
644                                        return false;
645                                }
646                        }
647                );
648    },
649
650        /** Function addContacts
651         *
652         *  Parameters:
653         *      (string) jid
654         *              (string) name
655         *              (string) group   
656         */
657       
658        addContacts : function( jidFrom, jidTo, name, group )
659        {
660                var newPresence = $pres({from: jidFrom, to: jidTo, type: 'subscribe'}).tree();
661               
662                TrophyIM.connection.send(newPresence);
663
664                var newContact = $iq({type: 'set', id: 'set1'});
665                        newContact = newContact.c('query').attrs({xmlns : 'jabber:iq:roster'});
666                        newContact = newContact.c('item').attrs({jid: jid, name: name, ask:'subscribe', subscription :'none'});
667                        newContact = newContact.c('group').t(group).tree();
668
669                TrophyIM.connection.send(newContact);
670        },
671
672    /** Function: addMessage
673     *
674     *  Parameters:
675     *    (string) msg - the message to add
676     *    (string) jid - the jid of chat box to add the message to.
677     */
678       
679    addMessage : function(msg, jid)
680    {
681        var chatBox             = document.getElementById(jid + "__chatBox");
682        var messageDiv  = document.createElement("div");
683       
684                messageDiv.style.margin = "3px 0px 3px 3px";
685        messageDiv.innerHTML    = msg.contact + " : " + msg.msg ;
686               
687        chatBox.appendChild(messageDiv);
688        chatBox.scrollTop = chatBox.scrollHeight;
689    },
690       
691    /** Function: renderRoster
692     *
693     *  Renders roster, looking only for jids flagged by setPresence as having
694     *  changed.
695     */
696   
697        renderRoster : function()
698        {
699                if( TrophyIM.rosterObj.changes.length > 0 )
700                {
701                        var roster_div = document.getElementById('JabberIMRoster');
702                       
703                        if( roster_div )
704                        {
705                                var users = new Array();
706                               
707                                for( var user in TrophyIM.rosterObj.roster )
708                                {
709                                        users[users.length] = TrophyIM.rosterObj.roster[user].contact.jid;
710                                }
711                               
712                                users.sort();
713                               
714                                var groups = new Array();
715                               
716                                for (var group in TrophyIM.rosterObj.groups)
717                                {
718                                        if( group )
719                                                groups[groups.length] = group;
720                }
721               
722                                groups.sort();
723                               
724                                for ( var i = 0; i < groups.length; i++ )
725                                {
726                                        TrophyIM.renderGroups( groups[i] , roster_div.lastChild );     
727                                }
728                               
729                                TrophyIM.renderItensGroup(users, roster_div);
730                        }
731                }
732                       
733                setTimeout("TrophyIM.renderRoster()", 1000 );           
734        },
735       
736    /** Function: renderGroups
737     *
738     *
739     */
740       
741        renderGroups: function( nameGroup, element )
742        {
743                var _addGroup = function()
744                {
745                        var paramsGroup =
746                        {
747                                'nameGroup'     : arguments[0],
748                                'path_jabberit' : path_jabberit
749                        }
750                       
751                        element.innerHTML += loadIM.parse("group","groups.xsl", paramsGroup);
752                       
753                }
754
755                if( !element.hasChildNodes() )
756                {
757                        _addGroup(nameGroup);   
758                }
759                else
760                {
761                        var elementChild = element;
762                        var flag = false;
763                       
764                        while ( elementChild )
765                        {
766                                if ( elementChild.childNodes[0].childNodes[0].firstChild.nodeValue === nameGroup )
767                                {
768                                        flag = true;
769                                }
770                               
771                                elementChild = elementChild.nextSibling;
772                        }
773                       
774                        if( !flag )
775                                _addGroup( nameGroup );
776                }
777               
778        },
779
780    /** Function: renderItensGroup
781     *
782     *
783     */
784
785        renderItensGroup : function( users, element )
786        {
787                var addItemGroup = function()
788                {
789                        if( arguments.length > 0 )
790                        {
791                                var objContact  = arguments[0];
792                                var element             = arguments[1];
793                                var itensJid    = document.getElementById( 'itenContact_' + objContact.contact.jid );
794                               
795                                if( itensJid == null )
796                                {
797                                        // Name
798                                        var nameContact = "";                                   
799                                       
800                                        if ( objContact.contact.name )
801                                                nameContact = objContact.contact.name;
802                                        else
803                                        {
804                                                nameContact = objContact.contact.jid;
805                                                nameContact = nameContact.substring(0, nameContact.indexOf('@'));
806                                        }
807                                       
808                                        // Presence e Status
809                                        var presence            = "unavailable";
810                                        var status                      = "";
811                                        var statusDisplay       = "none";
812                                       
813                                        if (objContact.presence)
814                                        {
815                                                for (var resource in objContact.presence)
816                                                {
817                                                        presence = objContact.presence[resource].show;
818                                                       
819                                                        if( objContact.presence[resource].status )
820                                                        {
821                                                                status                  = " ( " + objContact.presence[resource].status + " ) ";
822                                                                statusDisplay   = "block";
823                                                        }
824                                                }
825                                        }
826                                       
827                                        var paramsContact =
828                                        {
829                                                'nameContact'   : nameContact,
830                                                'jid'                   : objContact.contact.jid,       
831                                                'id'                    : 'itenContact_' + objContact.contact.jid,
832                                                'presence'              : presence,
833                                                'path_jabberit' : path_jabberit,
834                                                'status'                : status,
835                                                'statusDisplay' : statusDisplay
836                                        }
837                                       
838                                        if( element.id == "rosterIM_with_groups" )
839                                        {
840                                                var elementChild = element;
841                               
842                                                while ( elementChild )
843                                                {
844                                                        if( elementChild.childNodes[0].childNodes[0].firstChild.nodeValue == objContact.contact.groups[0] )
845                                                                elementChild.childNodes[0].innerHTML += loadIM.parse("itens_group", "itensGroup.xsl", paramsContact);
846
847                                                        elementChild = elementChild.nextSibling;
848                                                }
849                                        }
850                                        else
851                                        {
852                                                element.innerHTML += loadIM.parse("itens_group", "itensGroup.xsl", paramsContact);                                                     
853                                        }
854                                }
855                                else
856                                {
857                                       
858                                        // Presence e Status
859                                        var presence            = "unavailable";
860                                        var status                      = "";
861                                        var statusDisplay       = "none";
862                                       
863                                        if (objContact.presence)
864                                        {
865                                                for (var resource in objContact.presence)
866                                                {
867                                                        presence = objContact.presence[resource].show;
868                                                       
869                                                        if( objContact.presence[resource].status )
870                                                        {
871                                                                status                  = objContact.presence[resource].status;
872                                                                statusDisplay   = "block";
873                                                        }
874                                                }
875                                        }
876
877                                        itensJid.style.background                       = "url('"+path_jabberit+"templates/default/images/" + presence + ".gif')no-repeat center left";
878                                        itensJid.lastChild.style.display        = statusDisplay;
879                                        itensJid.lastChild.innerHTML            = " ( " + status + " ) ";
880                                }
881                        }
882                }
883               
884                for( var i = 0 ; i < users.length; i++ )
885                {
886                        if( TrophyIM.rosterObj.roster[users[i]].contact.groups )
887                                if( TrophyIM.rosterObj.roster[users[i]].contact.groups[0] )
888                                        addItemGroup(TrophyIM.rosterObj.roster[users[i]], element.lastChild );
889                                else
890                                        addItemGroup(TrophyIM.rosterObj.roster[users[i]], element.firstChild );
891                        else
892                                addItemGroup(TrophyIM.rosterObj.roster[users[i]], element.firstChild );
893                }       
894        },
895
896    /** Function: rosterClick
897     *
898     *  Handles actions when a roster item is clicked
899     */
900   
901        rosterClick : function(fulljid)
902        {
903        TrophyIM.makeChat(fulljid);
904    },
905
906
907        /** Function SetAutorization
908         *
909         */
910
911        setAutorization : function( jid )
912        {
913           //<presence to='user@example.com' from='contact@example.org' type='subscribed'/>
914           //var _autorization = $pres( ).attrs( {to: jid, from: loadIM.getUserCurrent(), type:'subscribed'}).tree();
915               
916           TrophyIM.connection.send($pres( ).attrs( {to: jid, from: loadIM.getUserCurrent().jid, type:'subscribed'}).tree());
917        },
918   
919        /** Function: setPresence
920     *
921     */
922
923        setPresence : function( show )
924        {
925                TrophyIM.connection.send($pres( ).c('show').t(show).tree());
926        },
927       
928        /** Function: sendMessage
929     *
930     *  Send message from chat input to user
931     */
932     
933    sendMessage : function()
934    {
935
936        if( arguments.length > 0 )
937        {
938                var jidTo = arguments[0];
939                var message_input = document.getElementById(jidTo + "__sendBox").value;
940           
941                if( ( message_input = message_input.replace(/^\s+|\s+$|^\n|\n$/g,"") ) != "" )
942                {
943                        // Send Message
944                        TrophyIM.connection.send($msg({to: jidTo, from: TrophyIM.connection.jid, type: 'chat'}).c('body').t(message_input).tree());
945                       
946                        var message =
947                        {
948                                        contact : "<font style='font-weight:bold; color:red;'>" + "Eu" + "</font>",
949                                        msg : message_input
950                        }
951                       
952                        // Add Message in chatBox;
953                        TrophyIM.addMessage( message, jidTo);
954                                document.getElementById(jidTo + "__sendBox").value = "";
955                                document.getElementById(jidTo + "__sendBox").focus();
956                }
957        }
958    }
959};
960
961/** Class: TrophyIMRoster
962 *
963 *
964 *  This object stores the roster and presence info for the TrophyIMClient
965 *
966 *  roster[jid_lower]['contact']
967 *  roster[jid_lower]['presence'][resource]
968 */
969function TrophyIMRoster()
970{
971    /** Constants: internal arrays
972     *    (Object) roster - the actual roster/presence information
973     *    (Object) groups - list of current groups in the roster
974     *    (Array) changes - array of jids with presence changes
975     */
976    if (TrophyIM.JSONStore.store_working)
977        {
978        var data = TrophyIM.JSONStore.getData(['roster', 'groups']);
979        this.roster = (data['roster'] != null) ? data['roster'] : {};
980        this.groups = (data['groups'] != null) ? data['groups'] : {};
981    }
982        else
983        {
984        this.roster = {};
985        this.groups = {};
986    }
987    this.changes = new Array();
988   
989        if (TrophyIM.constants.stale_roster)
990        {
991        for (var jid in this.roster)
992                {
993                        this.changes[this.changes.length] = jid;
994        }
995    }
996       
997    /** Function: addContact
998     *
999     *  Adds given contact to roster
1000     *
1001     *  Parameters:
1002     *    (String) jid - bare jid
1003     *    (String) subscription - subscription attribute for contact
1004     *    (String) name - name attribute for contact
1005     *    (Array) groups - array of groups contact is member of
1006     */
1007   
1008        this.addContact = function(jid, subscription, name, groups)
1009        {
1010        var contact = { jid:jid, subscription:subscription, name:name, groups:groups }
1011        var jid_lower = jid.toLowerCase();
1012
1013                if ( this.roster[jid_lower] )
1014                {
1015            this.roster[jid_lower]['contact'] = contact;
1016        }
1017                else
1018                {
1019            this.roster[jid_lower] = {contact:contact};
1020        }
1021
1022                groups = groups ? groups : [''];
1023       
1024                for ( var g = 0; g < groups.length; g++ )
1025                {
1026                        if ( !this.groups[groups[g]] )
1027                        {
1028                this.groups[groups[g]] = {};
1029            }
1030           
1031                        this.groups[groups[g]][jid_lower] = jid_lower;
1032        }
1033    }
1034    /** Function: getContact
1035     *
1036     *  Returns contact entry for given jid
1037     *
1038     *  Parameter: (String) jid - jid to return
1039     */
1040    this.getContact = function(jid)
1041        {
1042        if (this.roster[jid.toLowerCase()])
1043                {
1044            return this.roster[jid.toLowerCase()]['contact'];
1045        }
1046    }
1047    /** Function: setPresence
1048     *
1049     *  Sets presence
1050     *
1051     *  Parameters:
1052     *    (String) fulljid: full jid with presence
1053     *    (Integer) priority: priority attribute from presence
1054     *    (String) show: show attribute from presence
1055     *    (String) status: status attribute from presence
1056     */
1057   
1058        this.setPresence = function(fulljid, priority, show, status)
1059        {
1060                var barejid = Strophe.getBareJidFromJid(fulljid);
1061        var resource = Strophe.getResourceFromJid(fulljid);
1062        var jid_lower = barejid.toLowerCase();
1063       
1064                if( show != 'unavailable')
1065                {
1066            if (!this.roster[jid_lower])
1067                        {
1068                this.addContact(barejid, 'not-in-roster');
1069            }
1070            var presence =
1071                        {
1072                resource:resource, priority:priority, show:show, status:status
1073            }
1074           
1075                        if (!this.roster[jid_lower]['presence'])
1076                        {
1077                this.roster[jid_lower]['presence'] = {}
1078            }
1079            this.roster[jid_lower]['presence'][resource] = presence
1080        }
1081                else if (this.roster[jid_lower] && this.roster[jid_lower]['presence'] && this.roster[jid_lower]['presence'][resource])
1082                {
1083            delete this.roster[jid_lower]['presence'][resource];
1084        }
1085       
1086                this.addChange(jid_lower);
1087       
1088                if (TrophyIM.activeChats['divs'][jid_lower])
1089                {
1090            TrophyIM.setTabPresence(jid_lower, TrophyIM.activeChats['divs'][jid_lower]['tab']);
1091        }
1092    }
1093    /** Function: addChange
1094     *
1095     *  Adds given jid to this.changes, keeping this.changes sorted and
1096     *  preventing duplicates.
1097     *
1098     *  Parameters
1099     *    (String) jid : jid to add to this.changes
1100     */
1101    this.addChange = function(jid) {
1102        for (var c = 0; c < this.changes.length; c++) {
1103            if (this.changes[c] == jid) {
1104                return;
1105            }
1106        }
1107        this.changes[this.changes.length] = jid;
1108        this.changes.sort();
1109    }
1110    /** Function: getPresence
1111     *
1112     *  Returns best presence for given jid as Array(resource, priority, show,
1113     *  status)
1114     *
1115     *  Parameter: (String) fulljid - jid to return best presence for
1116     */
1117    this.getPresence = function(fulljid) {
1118        var jid = Strophe.getBareJidFromJid(fulljid);
1119        var current = null;
1120        if (this.roster[jid.toLowerCase()] &&
1121        this.roster[jid.toLowerCase()]['presence']) {
1122            for (var resource in this.roster[jid.toLowerCase()]['presence']) {
1123                var presence = this.roster[jid.toLowerCase()]['presence'][resource];
1124                if (current == null) {
1125                    current = presence
1126                } else {
1127                    if(presence['priority'] > current['priority'] && ((presence['show'] == "chat"
1128                    || presence['show'] == "available") || (current['show'] != "chat" ||
1129                    current['show'] != "available"))) {
1130                        current = presence
1131                    }
1132                }
1133            }
1134        }
1135        return current;
1136    }
1137    /** Function: groupHasChanges
1138     *
1139     *  Returns true if current group has members in this.changes
1140     *
1141     *  Parameters:
1142     *    (String) group - name of group to check
1143     */
1144    this.groupHasChanges = function(group) {
1145        for (var c = 0; c < this.changes.length; c++) {
1146            if (this.groups[group][this.changes[c]]) {
1147                return true;
1148            }
1149        }
1150        return false;
1151    }
1152    /** Fuction: save
1153     *
1154     *  Saves roster data to JSON store
1155     */
1156    this.save = function() {
1157        if (TrophyIM.JSONStore.store_working) {
1158            TrophyIM.JSONStore.setData({roster:this.roster,
1159            groups:this.groups, active_chat:TrophyIM.activeChats['current'],
1160            chat_history:TrophyIM.chatHistory});
1161        }
1162    }
1163}
1164/** Class: TrophyIMJSONStore
1165 *
1166 *
1167 *  This object is the mechanism by which TrophyIM stores and retrieves its
1168 *  variables from the url provided by TROPHYIM_JSON_STORE
1169 *
1170 */
1171function TrophyIMJSONStore() {
1172    this.store_working = false;
1173    /** Function _newXHR
1174     *
1175     *  Set up new cross-browser xmlhttprequest object
1176     *
1177     *  Parameters:
1178     *    (function) handler = what to set onreadystatechange to
1179     */
1180     this._newXHR = function (handler) {
1181        var xhr = null;
1182        if (window.XMLHttpRequest) {
1183            xhr = new XMLHttpRequest();
1184            if (xhr.overrideMimeType) {
1185            xhr.overrideMimeType("text/xml");
1186            }
1187        } else if (window.ActiveXObject) {
1188            xhr = new ActiveXObject("Microsoft.XMLHTTP");
1189        }
1190        return xhr;
1191    }
1192    /** Function getData
1193     *  Gets data from JSONStore
1194     *
1195     *  Parameters:
1196     *    (Array) vars = Variables to get from JSON store
1197     *
1198     *  Returns:
1199     *    Object with variables indexed by names given in parameter 'vars'
1200     */
1201    this.getData = function(vars) {
1202        if (typeof(TROPHYIM_JSON_STORE) != undefined) {
1203            Strophe.debug("Retrieving JSONStore data");
1204            var xhr = this._newXHR();
1205            var getdata = "get=" + vars.join(",");
1206            try {
1207                xhr.open("POST", TROPHYIM_JSON_STORE, false);
1208            } catch (e) {
1209                Strophe.error("JSONStore open failed.");
1210                return false;
1211            }
1212            xhr.setRequestHeader('Content-type',
1213            'application/x-www-form-urlencoded');
1214            xhr.setRequestHeader('Content-length', getdata.length);
1215            xhr.send(getdata);
1216            if (xhr.readyState == 4 && xhr.status == 200) {
1217                try {
1218                    var dataObj = JSON.parse(xhr.responseText);
1219                    return this.emptyFix(dataObj);
1220                } catch(e) {
1221                    Strophe.error("Could not parse JSONStore response" +
1222                    xhr.responseText);
1223                    return false;
1224                }
1225            } else {
1226                Strophe.error("JSONStore open failed. Status: " + xhr.status);
1227                return false;
1228            }
1229        }
1230    }
1231    /** Function emptyFix
1232     *    Fix for bugs in external JSON implementations such as
1233     *    http://bugs.php.net/bug.php?id=41504.
1234     *    A.K.A. Don't use PHP, people.
1235     */
1236    this.emptyFix = function(obj) {
1237        if (typeof(obj) == "object") {
1238            for (var i in obj) {
1239                if (i == '_empty_') {
1240                    obj[""] = this.emptyFix(obj['_empty_']);
1241                    delete obj['_empty_'];
1242                } else {
1243                    obj[i] = this.emptyFix(obj[i]);
1244                }
1245            }
1246        }
1247        return obj
1248    }
1249    /** Function delData
1250     *    Deletes data from JSONStore
1251     *
1252     *  Parameters:
1253     *    (Array) vars  = Variables to delete from JSON store
1254     *
1255     *  Returns:
1256     *    Status of delete attempt.
1257     */
1258    this.delData = function(vars) {
1259        if (typeof(TROPHYIM_JSON_STORE) != undefined) {
1260            Strophe.debug("Retrieving JSONStore data");
1261            var xhr = this._newXHR();
1262            var deldata = "del=" + vars.join(",");
1263            try {
1264                xhr.open("POST", TROPHYIM_JSON_STORE, false);
1265            } catch (e) {
1266                Strophe.error("JSONStore open failed.");
1267                return false;
1268            }
1269            xhr.setRequestHeader('Content-type',
1270            'application/x-www-form-urlencoded');
1271            xhr.setRequestHeader('Content-length', deldata.length);
1272            xhr.send(deldata);
1273            if (xhr.readyState == 4 && xhr.status == 200) {
1274                try {
1275                    var dataObj = JSON.parse(xhr.responseText);
1276                    return dataObj;
1277                } catch(e) {
1278                    Strophe.error("Could not parse JSONStore response");
1279                    return false;
1280                }
1281            } else {
1282                Strophe.error("JSONStore open failed. Status: " + xhr.status);
1283                return false;
1284            }
1285        }
1286    }
1287    /** Function setData
1288     *    Stores data in JSONStore, overwriting values if they exist
1289     *
1290     *  Parameters:
1291     *    (Object) vars : Object containing named vars to store ({name: value,
1292     *    othername: othervalue})
1293     *
1294     *  Returns:
1295     *    Status of storage attempt
1296     */
1297    this.setData = function(vars) {
1298        if (typeof(TROPHYIM_JSON_STORE) != undefined) {
1299            Strophe.debug("Storing JSONStore data");
1300            var senddata = "set=" + JSON.stringify(vars);
1301            var xhr = this._newXHR();
1302            try {
1303                xhr.open("POST", TROPHYIM_JSON_STORE, false);
1304            } catch (e) {
1305                Strophe.error("JSONStore open failed.");
1306                return false;
1307            }
1308            xhr.setRequestHeader('Content-type',
1309            'application/x-www-form-urlencoded');
1310            xhr.setRequestHeader('Content-length', senddata.length);
1311            xhr.send(senddata);
1312            if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText ==
1313            "OK") {
1314                return true;
1315            } else {
1316                Strophe.error("JSONStore open failed. Status: " + xhr.status);
1317                return false;
1318            }
1319        }
1320    }
1321    var testData = true;
1322    if (this.setData({testData:testData})) {
1323        var testResult = this.getData(['testData']);
1324        if (testResult && testResult['testData'] == true) {
1325            this.store_working = true;
1326        }
1327    }
1328}
1329/** Constants: Node types
1330 *
1331 * Implementations of constants that IE doesn't have, but we need.
1332 */
1333if (document.ELEMENT_NODE == null) {
1334    document.ELEMENT_NODE = 1;
1335    document.ATTRIBUTE_NODE = 2;
1336    document.TEXT_NODE = 3;
1337    document.CDATA_SECTION_NODE = 4;
1338    document.ENTITY_REFERENCE_NODE = 5;
1339    document.ENTITY_NODE = 6;
1340    document.PROCESSING_INSTRUCTION_NODE = 7;
1341    document.COMMENT_NODE = 8;
1342    document.DOCUMENT_NODE = 9;
1343    document.DOCUMENT_TYPE_NODE = 10;
1344    document.DOCUMENT_FRAGMENT_NODE = 11;
1345    document.NOTATION_NODE = 12;
1346}
1347
1348/** Function: importNode
1349 *
1350 *  document.importNode implementation for IE, which doesn't have importNode
1351 *
1352 *  Parameters:
1353 *    (Object) node - dom object
1354 *    (Boolean) allChildren - import node's children too
1355 */
1356if (!document.importNode) {
1357    document.importNode = function(node, allChildren) {
1358        switch (node.nodeType) {
1359            case document.ELEMENT_NODE:
1360                var newNode = document.createElement(node.nodeName);
1361                if (node.attributes && node.attributes.length > 0) {
1362                    for(var i = 0; i < node.attributes.length; i++) {
1363                        newNode.setAttribute(node.attributes[i].nodeName,
1364                        node.getAttribute(node.attributes[i].nodeName));
1365                    }
1366                }
1367                if (allChildren && node.childNodes &&
1368                node.childNodes.length > 0) {
1369                    for (var i = 0; i < node.childNodes.length; i++) {
1370                        newNode.appendChild(document.importNode(
1371                        node.childNodes[i], allChildren));
1372                    }
1373                }
1374                return newNode;
1375                break;
1376            case document.TEXT_NODE:
1377            case document.CDATA_SECTION_NODE:
1378            case document.COMMENT_NODE:
1379                return document.createTextNode(node.nodeValue);
1380                break;
1381        }
1382    };
1383}
1384
1385/**
1386 *
1387 * Bootstrap self into window.onload and window.onunload
1388 */
1389
1390/*
1391var oldonload = window.onload;
1392window.onload = function()
1393{
1394    if(oldonload)
1395        {
1396        oldonload();
1397    }
1398   
1399        TrophyIM.load();
1400};
1401*/
1402
1403var oldonunload = window.onunload;
1404window.onunload = function()
1405{
1406    if(oldonunload)
1407        {
1408        oldonunload();
1409    }
1410   
1411        TrophyIM.storeData();
1412}
Note: See TracBrowser for help on using the repository browser.