source: trunk/instant_messenger/js/jabber.js @ 323

Revision 323, 21.4 KB checked in by niltonneto, 16 years ago (diff)
Line 
1(function()
2{
3        var _conn;
4        var _xtools;
5        var _window;
6        var _ldap;
7        var _menu;
8        var _cookies;
9
10        var _vcards = [];
11        var _info = [];
12        var _chats = [];
13
14        var _win_position = 0;
15
16        function _next_position()
17        {
18                if ( _win_position > 200 )
19                        _win_position = 0;
20                return (_win_position += 30);
21        }
22
23        function _chat(_pJid)
24        {
25                if ( !_chats[_pJid] )
26                        _chats[_pJid] = [];
27
28                var _win_pos = _next_position();
29                var _win_chat = _window.load('chat' + _pJid);
30                        _win_chat.title('.::Expresso Messenger::.');
31                        _win_chat.size(310);
32                        _win_chat.content(_xtools.parse('chat.xsl'));
33                        _win_chat.position(_win_pos, _win_pos);
34                        _win_chat.wc.style.padding = "3px";
35
36                _ldap.photo(
37                        _pJid.substr(0,_pJid.indexOf('@')),
38                        _win_chat.wc.childNodes[2]
39                );
40
41                var _iframe = top.document.createElement('iframe');
42                        _iframe.id = 'iframe_' + _pJid;
43
44                _win_chat.wc.childNodes[2].appendChild(_iframe);
45
46                _iframe.style.width = '250px';
47                _iframe.style.height = '100%';
48                _iframe.style.border = '0';
49                _iframe.style.margin = '0';
50
51                var _contentWindow = _iframe.contentWindow;
52
53                _iframe = _contentWindow.document;
54                _iframe.write('<html><body style="padding:0;margin:0;"></body></html>');
55                _iframe.close();
56                _iframe.designMode = "On";
57
58                _contentWindow.focus();
59
60                _config(
61                        _win_chat.wc.childNodes[1].childNodes[5],
62                        'onclick',
63                        function()
64                        {
65                                _info[_pJid] = true;
66                                _vcard(_pJid);
67                        }
68                );
69
70                _config(
71                        _win_chat.wc.childNodes[1].lastChild.previousSibling,
72                        'onclick',
73                        function()
74                        {
75                                _sendMessage(
76                                        _pJid,
77                                        _iframe,
78                                        _win_chat.wc.firstChild
79                                );
80                                _iframe.body.innerHTML = '';
81                                _contentWindow.focus();
82                        }
83                );
84
85                var _composing_paused = null;
86                function _execKeyAction(e)
87                {
88                        if ( e.type.indexOf('keydown') != -1 )
89                        {
90                                if ( (e.keyCode > 48 && e.keyCode < 112) || e.keyCode > 186 )
91                                {
92                                        if ( _chats[_pJid]['composing_paused'] )
93                                        {
94                                                clearTimeout(_chats[_pJid]['composing_paused']);
95                                                _chats[_pJid]['composing_paused'] = null;
96                                        }
97                                        else
98                                                _conn.go('t.composing','jid='+_pJid);
99
100                                        _chats[_pJid]['composing_paused'] = setTimeout(
101                                        function()
102                                        {
103                                                if ( _chats[_pJid]['composing_paused'] )
104                                                {
105                                                        clearTimeout(_chats[_pJid]['composing_paused']);
106                                                        _chats[_pJid]['composing_paused'] = null;
107                                                }
108                                                _conn.go('t.paused','jid='+_pJid);
109                                        }, 5000);
110                                }
111                        }
112                        switch ( e.keyCode )
113                        {
114                                case 13 :
115                                        if ( !(e.shiftKey) )
116                                        {
117                                                if ( e.type.indexOf('keydown') != -1 )
118                                                {
119                                                        if ( _chats[_pJid]['composing_paused'] )
120                                                        {
121                                                                clearTimeout(_chats[_pJid]['composing_paused']);
122                                                                _chats[_pJid]['composing_paused'] = null;
123                                                        }
124
125                                                        _sendMessage(
126                                                                _pJid,
127                                                                _iframe,
128                                                                _win_chat.wc.firstChild
129                                                        );
130                                                }
131                                                else
132                                                        _iframe.body.innerHTML = '';
133
134                                                return false;
135                                        }
136                                break;
137                                case 27 :
138                                        if ( e.type.indexOf('keyup') != -1 )
139                                        {
140                                                if ( _chats[_pJid]['composing_paused'] )
141                                                {
142                                                        clearTimeout(_chats[_pJid]['composing_paused']);
143                                                        _chats[_pJid]['composing_paused'] = null;
144                                                }
145                                                _win_chat.button(_win_chat.bc);
146                                        }
147                                break;
148                        }
149                }
150                _config(_contentWindow.document, 'onkeyup', _execKeyAction);
151                _config(_contentWindow.document, 'onkeydown', _execKeyAction);
152
153                if ( !(_vcards[_pJid]) )
154                        _conn.go('$this.Ujabber.getVcard','jid='+_pJid);
155                else
156                        _readVCard(_vcards[_pJid]);
157
158                return _win_chat;
159        }
160
161        function _config(pObj, pEvent, pHandler)
162        {
163                if ( typeof pObj == 'object' )
164                {
165                        if ( pEvent.substring(0, 2) == 'on' )
166                                pEvent = pEvent.substring(2, pEvent.length);
167
168                        if ( pObj.addEventListener )
169                                pObj.addEventListener(pEvent, pHandler, false);
170                        else if ( pObj.attachEvent )
171                                pObj.attachEvent('on' + pEvent, pHandler);
172                }
173        }
174
175        function _openChat(el)
176        {
177                var id = el.getAttribute('jid');
178
179                if ( id.indexOf('/') > -1 )
180                        id = id.substr(0, id.indexOf('/'));
181
182                var _win = _window.get('chat' + id);
183                if ( !_win )
184                {
185                        _win = _chat(id);
186                        _win.title(el.getAttribute('idname'));
187                        _win.icon(eval('im_' + el.getAttribute('status') + '.src'));
188                }
189                else
190                {
191                        _win.wm.style.display = 'block';
192                        _win.focus();
193                        _win.wc.childNodes[2].firstChild.contentWindow.focus();
194                }
195        }
196
197        function _readContacts(_pContacts)
198        {
199                var _win_contact = _window.get('contact_list');
200                if ( !_win_contact )
201                {
202                        _win_contact = _window.load('contact_list');
203                        _win_contact.hidden();
204                        _win_contact.title('.::Expresso Messenger::.');
205                        _win_contact.size(170, 350);
206                        _win_contact.position(30, 30, true);
207                        _win_contact.icon(im_unavailable.src);
208                        _win_contact.bc.style.display = 'none';
209                }
210
211                var _params = {
212                        'lang1':imGetLang('nickname'),
213                        'lang2':imGetLang('add contact'),
214                        'lang3':imGetLang('preferences'),
215                        'nickname':imGetLang('username')
216                };
217                _win_contact.content(
218                        _xtools.parse(_xtools.xml('layer'),'layer.xsl', _params)
219                        + _xtools.parse(_pContacts, 'contacts.xsl', {'path':path_im})
220                );
221
222                _config(_win_contact.wc.firstChild.childNodes[5], 'onclick', _ldap.add);
223
224                var _contact_list = top.document.getElementById('im_contact_list');
225                if ( _contact_list )
226                {
227                        _conn.go('$this.Ujabber.setPresence', 'type=unavailable');
228                        _setPresence();
229                        _ldap.photo(
230                                'im_avatar',
231                                _win_contact.wc.firstChild
232                        );
233                        _conn.go('$this.Ujabber.getVcard','jid=this');
234                        function _click(e)
235                        {
236                                var el = ( e.target ) ? e.target : e.srcElement;
237                                //alert(e.button);
238                                if ( (e.button == 0 && !document.all) || (e.button == 1 && document.all) )
239                                        _openChat(el);
240                                else
241                                {
242                                        var _options = [
243                                                imGetLang("Nickname"),
244                                                imGetLang("Group"),
245                                                imGetLang("Remove"),
246                                                imGetLang("It requisition permission"),
247                                                imGetLang("See Info")
248                                        ];
249
250                                        var _style_text = 'background: no-repeat url(' + path_im
251                                                                + 'templates/default/images/group_close.gif);'
252                                                                + 'padding:2px 4px 2px 16px;';
253
254                                        var _xml = _xtools.xml('menu');
255                                        var _option;
256                                        var _item;
257                                        var _action;
258                                        var _style;
259
260                                        for ( var i in _options )
261                                        {
262                                                _option = _xml.createElement('option');
263
264                                                _item = _xml.createElement('item');
265                                                _item.appendChild(_xml.createTextNode(_options[i]));
266
267                                                _style = _xml.createElement('style');
268                                                _style.appendChild(_xml.createTextNode(_style_text));
269
270                                                _option.appendChild(_item);
271                                                _option.appendChild(_style);
272
273                                                _xml.documentElement.appendChild(_option);
274                                        }
275
276                                        var _sub_menu = top.document.getElementById('sub_' + el.getAttribute('jid'));
277                                                _sub_menu.innerHTML = _xtools.parse(_xml, 'options.xsl');
278
279                                        _menu.action('onmouseover', 'onmouseout', _sub_menu);
280
281                                        _sub_menu = _sub_menu.firstChild;
282                                        _sub_menu.childNodes[0].style.display = 'none';
283                                        _sub_menu.childNodes[1].style.display = 'none';
284                                        _sub_menu.childNodes[3].style.display = 'none';
285                                        _config(
286                                                _sub_menu.childNodes[2],
287                                                'onclick',
288                                                function(e)
289                                                {
290                                                        var el = ( e.target ) ? e.target : e.srcElement;
291                                                        var jid = el.parentNode.parentNode.id.substr(4);
292                                                        var _contact = el.parentNode.parentNode.previousSibling;
293                                                        _contact.parentNode.removeChild(_contact.nextSibling);
294                                                        _contact.parentNode.removeChild(_contact);
295                                                        _conn.go('$this.Ujabber.removeContact','jid='+jid);
296                                                }
297                                        );
298                                        _config(
299                                                _sub_menu.childNodes[4],
300                                                'onclick',
301                                                function()
302                                                {
303                                                        _info[el.getAttribute('jid')] = true;
304                                                        _vcard(el.getAttribute('jid'));
305                                                }
306                                        );
307                                }
308                        }
309                        function _hover(e)
310                        {
311                                var el = ( e.target ) ? e.target : e.srcElement;
312                                var color = "#fff";
313                                if ( e.type.indexOf('mouseover') != -1 )
314                                        color = "#b4cfe5";
315                                el.style.backgroundColor = color;
316                        }
317                        var _contact = _contact_list.firstChild;
318                        var _show_all = _cookies.get('IM_unavailable');
319                        while ( _contact )
320                        {
321                                if ( _contact.nodeName.toLowerCase() == 'div' )
322                                {
323                                        _config(_contact, 'onmouseover', _hover);
324                                        _config(_contact, 'onmouseout', _hover);
325                                        if ( _contact.getAttribute('jid') )
326                                        {
327                                                if ( _show_all == 'hidden' )
328                                                        _contact.style.display = 'none';
329                                                //_config(_contact, 'onmousedown', _click);
330                                                _config(_contact, 'onmouseup', _click);
331                                        }
332                                        else
333                                        {
334                                                _config(_contact, 'onclick',
335                                                function(e)
336                                                {
337                                                        var el = ( e.target ) ? e.target : e.srcElement;
338                                                        var _display = "none";
339                                                        var _image = im_group_close.src;
340
341                                                        if ( el.style.backgroundImage.indexOf('group_open') < 0 )
342                                                        {
343                                                                _display = "block";
344                                                                _image = im_group_open.src;
345                                                        }
346
347                                                        el.style.backgroundImage = 'url(' + _image + ')';
348
349                                                        el = el.nextSibling;
350                                                        while ( el.getAttribute('jid') )
351                                                        {
352                                                                if ( _display == 'block' )
353                                                                {
354                                                                        var _show_all = _cookies.get('IM_unavailable');
355                                                                        if ( (el.style.backgroundImage.indexOf('unavailable') > -1) && (_show_all == 'hidden') )
356                                                                                _display = 'none';
357                                                                }
358                                                                el.style.display = _display;
359                                                                el = el.nextSibling;
360                                                                if ( el && el.nextSibling )
361                                                                        el = el.nextSibling;
362                                                        }
363                                                });
364                                        }
365                                }
366                                _contact = _contact.nextSibling;
367                        }
368                }
369        }
370
371        function _setPresence()
372        {
373                var _presence = false;
374                var _pPresence;
375                if ( arguments.length == 0 )
376                {
377                        if ( !(_pPresence = _cookies.get('IM_presence')) )
378                                _pPresence = 'available';
379                }
380                else
381                        _pPresence = arguments[0];
382
383                if ( _pPresence == 'auto' )
384                {
385                        if ( _cookies.get('IM_presence') == 'available' )
386                                _pPresence = 'xa';
387                }
388                else
389                        _cookies.set('IM_presence', _pPresence);
390
391                switch ( _pPresence )
392                {
393                        case 'away':
394                        case 'dnd':
395                        case 'xa':
396                                _presence = 'show=' + _pPresence;
397                        break;
398                        case 'available':
399                        case 'unavailable':
400                                _presence = 'type=' + _pPresence;
401                        break;
402                }
403
404                if ( _presence )
405                {
406                        var _img_status = eval('im_' + _pPresence + '.src');
407
408                        var _status = top.document.getElementById('im_status');
409                        if ( _status )
410                                _status.style.backgroundImage = 'url(' + _img_status + ')';
411
412                        var _win_contacts = _window.get('contact_list');
413                        if ( _win_contacts )
414                                _win_contacts.icon(_img_status);
415
416                        _conn.go('$this.Ujabber.setPresence', _presence);
417                }
418        }
419
420        function _vcard(_pJid)
421        {
422                var _win_vcard;
423                if ( !(_win_vcard = _window.get('vcard_' + _pJid)) )
424                {
425                        var _win_pos = _next_position();
426
427                        _win_vcard = _window.load('vcard_' + _pJid);
428                        _win_vcard.title('.:: ' + imGetLang('Information Contacts') + ' ::.');
429                        _win_vcard.size(300);
430                        _win_vcard.position(_win_pos, _win_pos);
431
432                        _win_vcard.wc.style.padding = "3px";
433                        _win_vcard.bx.style.display = "none";
434                }
435                else
436                        _win_vcard.focus();
437
438                if ( _vcards[_pJid] )
439                {
440                        if ( _info[_pJid] )
441                                delete _info[_pJid];
442
443                                var _params = {
444                                        'fullname' : imGetLang('Full Name'),
445                                        'nickname' : imGetLang('Nickname'),
446                                        'organization' : imGetLang('Organization'),
447                                        'sector' : imGetLang('Sector'),
448                                        'role' : imGetLang('Role'),
449                                        'birthday' : imGetLang('Birthday')
450                                };
451
452                                _win_vcard.content(
453                                        _xtools.parse(
454                                                _vcards[_pJid],
455                                                'vcard_contacts.xsl',
456                                                _params
457                                        )
458                                );
459                }
460                else
461                {
462                        _win_vcard.loading();
463                        _conn.go('$this.Ujabber.getVcard', 'jid='+_pJid);
464                }
465        }
466
467        function _readVCard(_pVCard)
468        {
469                _vcards[_pVCard.getAttribute('from')] = _pVCard;
470
471                switch ( _pVCard.getAttribute('id') )
472                {
473                        case "vCard_user" :
474                                var _layer_nickname = top.document.getElementById('im_layer_nickname');
475                                var _nickname = _pVCard.getElementsByTagName('NICKNAME');
476
477                                if ( _nickname.length && _layer_nickname )
478                                        _layer_nickname.innerHTML = _nickname[0].firstChild.nodeValue;
479                        break;
480                        case "vCard" :
481                                if ( _info[_pVCard.getAttribute('from')] )
482                                        _vcard(_pVCard.getAttribute('from'));
483                        break;
484                }
485        }
486
487        function _readIq(pIq)
488        {
489                switch ( pIq.getAttribute('id') )
490                {
491                        case 'contacts' :
492                                _readContacts(pIq);
493                        break;
494                        case 'vCard' :
495                        case 'vCard_user' :
496                                _readVCard(pIq);
497                        break;
498                        case 'last_time_user' :
499                                _readLastTimeUser(pIq);
500                        break;
501                        default : //alert('readIq : ' + pIq.getAttribute('id'));
502                }
503        }
504
505        function _readLastTimeUser(pIq)
506        {
507                if ( pIq.firstChild.getAttribute("seconds") != 0 )
508                        _conn.go('$this.Ujabber.getContacts');
509        }
510
511        function _readMessage(_pMessage)
512        {
513                var _from = _pMessage.getAttribute('from');
514
515                if ( _from.indexOf('/') > -1 )
516                        _from = _from.substr(0, _from.indexOf('/'));
517
518                var _contact_list = top.document.getElementById('contact_' + _from);
519
520                var _win = _window.get('chat' + _from);
521
522                if ( _contact_list && _pMessage.getElementsByTagName('composing').length > 0 )
523                {
524                        _contact_list.style.backgroundImage = 'url(' + im_composing.src + ')';
525                        setTimeout(function(){_contact_list.style.backgroundImage = 'url(' + eval('im_' + _contact_list.getAttribute('status') + '.src') + ')';}, 10000);
526                }
527
528                if ( _win && _pMessage.getElementsByTagName('composing').length > 0 )
529                {
530                        _win.wc.childNodes[1].lastChild.style.display = 'block';
531                        setTimeout(function(){_win.wc.childNodes[1].lastChild.style.display = 'none';}, 10000);
532                }
533
534                if ( _pMessage.getElementsByTagName('body').length > 0 )
535                {
536                        if ( !_win )
537                        {
538                                _win = _chat(_from);
539                                var _st = top.document.getElementById('contact_' + _from);
540                                if ( _st )
541                                {
542                                        _st = eval('im_' + _st.getAttribute('status') + '.src');
543                                        _win.icon(_st);
544                                }
545                        }
546                        _win.show();
547
548                        var _nickname;
549                        if ( _vcards[_from] )
550                        {
551                                _nickname = _vcards[_from].getElementsByTagName('NICKNAME');
552                                if ( _nickname.length )
553                                        _nickname = _nickname[0].firstChild.nodeValue;
554                                else
555                                        _nickname = _from;
556                        }
557                        else
558                                _nickname = _from;
559
560                        var _msg = _xtools.parse(_pMessage, 'message_new.xsl', {'nickname':_nickname,'time':Date().substr(16,5)});
561
562                        var _history = _win.wc.firstChild;
563                        if ( _history.scrollHeight == (_history.scrollTop + _history.clientHeight) )
564                        {
565                                _history.innerHTML += _msg;
566                                _history.scrollTop = _history.scrollHeight;
567                        }
568                        else
569                                // this "else" is to prevent the creation of a flag and the need to use another conditional
570                                _history.innerHTML += _msg;
571
572                        _win.wc.childNodes[1].lastChild.style.display = 'none';
573                        if ( _contact_list )
574                                _contact_list.style.backgroundImage = 'url(' + eval('im_' + _contact_list.getAttribute('status') + '.src') + ')';
575                }
576
577                if ( _contact_list && _pMessage.getElementsByTagName('paused').length > 0 )
578                        _contact_list.style.backgroundImage = 'url(' + eval('im_' + _contact_list.getAttribute('status') + '.src') + ')';
579
580                if ( _win && _pMessage.getElementsByTagName('paused').length > 0 )
581                        _win.wc.childNodes[1].lastChild.style.display = 'none';
582        }
583
584        function _readPresence(_pPresence)
585        {
586                var _from = _pPresence.getAttribute('from');
587                if ( _from.indexOf('/') > -1 )
588                        _from = _from.substr(0, _from.indexOf('/'));
589
590                var _presence_type = _pPresence.getAttribute('type');
591                var _img_status_contact = top.document.getElementById('contact_' + _from);
592                var _mensagem_status_contact = top.document.getElementById('status_' + _from);
593                var _win_chat = _window.get('chat' + _from);
594
595                _img_status_contact.setAttribute('status', _presence_type);
596
597                if ( _mensagem_status_contact )
598                {
599                        _mensagem_status_contact.innerHTML = '';
600                        _mensagem_status_contact.style.display = 'none';
601                }
602
603                if ( _presence_type )
604                {
605                        switch( _presence_type )
606                        {
607                                case 'available' :
608                                case 'unavailable' :
609                                        if ( _img_status_contact )
610                                                _img_status_contact.style.backgroundImage = 'url(' + eval('im_' + _presence_type + '.src') + ')';
611                                        if ( _win_chat )
612                                                _win_chat.icon(eval('im_' + _presence_type + '.src'));
613                                        if ( _presence_type == 'unavailable' )
614                                        {
615                                                var _show_all = _cookies.get('IM_unavailable');
616                                                if ( _show_all == 'hidden' )
617                                                        _img_status_contact.style.display = 'none';
618                                        }
619                                        else
620                                                _img_status_contact.style.display = 'block';
621                                break;
622                                case 'subscribe' :
623                                        if ( top.document.getElementById('contact_' + _from) )
624                                        {
625                                                _conn.go('$this.Ujabber.subscription', 'jid=' + _from + '&type=subscribed' );
626                                        }
627                                        else
628                                        {
629                                                if ( !_window.get('subscription' + _from) )
630                                                {
631                                                        var _win_pos = _next_position();
632                                                        var _win_subscription = _window.load('subscription' + _from);
633                                                                _win_subscription.title('.::Expresso Messenger::.');
634                                                                _win_subscription.position(_win_pos, _win_pos);
635                                                                _win_subscription.size(200);
636                                                                _win_subscription.wc.style.padding = "3px";
637                                                                _win_subscription.bc.style.display = 'none';
638                                                                _win_subscription.bx.style.display = 'none';
639                                                                _win_subscription.bz.style.display = 'none';
640
641                                                        var _params = {
642                                                                'jid' : _from,
643                                                                'lang1' : 'deseja adiciona-lo como contato.',
644                                                                'lang2' : imGetLang('nickname'),
645                                                                'lang3' : imGetLang('group'),
646                                                                'lang4' : imGetLang('allow'),
647                                                                'lang5' : imGetLang('deny')
648                                                        };
649
650                                                        _win_subscription.content(
651                                                                _xtools.parse(_xtools.xml('subscribe'), 'subscribe.xsl', _params)
652                                                        );
653
654                                                        _win_subscription.wc.childNodes[2].style.margin = '5px';
655                                                        _win_subscription.wc.childNodes[3].style.margin = '5px';
656
657                                                        _config(
658                                                                _win_subscription.wc.childNodes[2],
659                                                                'onclick',
660                                                                function()
661                                                                {
662                                                                        _win_subscription.wc.childNodes[1].style.display = 'block';
663                                                                        _win_subscription.size(450);
664                                                                        _config(
665                                                                                _win_subscription.wc.childNodes[2],
666                                                                                'onclick',
667                                                                                function()
668                                                                                {
669
670                                                                                        var _status = top.document.getElementById('im_status_add_' + _from);
671                                                                                        var _name = top.document.getElementById('im_name_' + _from);
672                                                                                        var _group = top.document.getElementById('im_group_' + _from);
673
674                                                                                        _name.value = _name.value.replace(/^( )*|( )*$/g, '');
675                                                                                        _group.value = _group.value.replace(/^( )*|( )*$/g, '');
676
677                                                                                        if ( !(_name.value) || !(_group.value) )
678                                                                                        {
679                                                                                                _status.innerHTML = '<br/>O campo destacado em vermelho é obrigatório!<br/>';
680
681                                                                                                if ( !(_name.value) )
682                                                                                                {
683                                                                                                        _name.previousSibling.style.color = "#f00";
684                                                                                                        _status.innerHTML += '<br/>** Informe um "NOME" para o contato.';
685                                                                                                }
686
687                                                                                                if ( !(_group.value) )
688                                                                                                {
689                                                                                                        _group.previousSibling.style.color = "#f00";
690                                                                                                        _status.innerHTML += '<br/>** Informe um "GRUPO" para o contato.';
691                                                                                                }
692                                                                                        }
693                                                                                        else
694                                                                                        {
695                                                                                                _status.innerHTML = '<br/>Adicionado contanto, aguarde.<br/>';
696
697                                                                                                _conn.go(
698                                                                                                        '$this.Ujabber.allowContact',
699                                                                                                        function()
700                                                                                                        {
701                                                                                                                _status.innerHTML = '<br/>Contato autorizado com "SUCESSO"!<br/>';
702                                                                                                                _win_subscription.button(_win_subscription.bc);
703                                                                                                        },
704                                                                                                        'uid='+_from.substr(0,_from.indexOf('@'))+'&name='+_name.value+'&group='+_group.value
705                                                                                                );
706
707                                                                                                _name.value = '';
708                                                                                                _group.value = '';
709                                                                                        }
710                                                                                }
711                                                                        );
712                                                                }
713                                                        );
714                                                }
715                                        }
716                                break;
717                                case 'unsubscribe' :
718                                case 'unsubscribed' :
719                                        _conn.go('$this.Ujabber.removeContact','jid=' + _from);
720                                break;
721                                default : //alert('readPresence : ' + _presence_type);
722                        }
723                }
724                else
725                {
726                        var _node = _pPresence.firstChild;
727                        while( _node )
728                        {
729                                if ( _node.hasChildNodes() )
730                                {
731                                        _img_status_contact.style.display = 'block';
732                                        switch ( _node.nodeName )
733                                        {
734                                                case "show" :
735                                                        if ( _img_status_contact )
736                                                                _img_status_contact.style.backgroundImage = 'url(' + eval('im_' + _node.firstChild.nodeValue + '.src') + ')';
737                                                        if ( _win_chat )
738                                                                _win_chat.icon(eval('im_' + _node.firstChild.nodeValue + '.src'));
739                                                break;
740                                                case "status" :
741                                                        if ( _mensagem_status_contact )
742                                                        {
743                                                                if ( _node.firstChild.nodeValue )
744                                                                        _mensagem_status_contact.innerHTML = _node.firstChild.nodeValue;
745                                                                _mensagem_status_contact.style.display = 'block';
746                                                        }
747                                                break;
748                                                default :
749                                                        if ( _img_status_contact )
750                                                                _img_status_contact.style.backgroundImage = 'url(' + im_available.src + ')';
751                                                        if ( _win_chat )
752                                                                _win_chat.icon(im_available.src);
753                                        }
754                                }
755                                _node = _node.nextSibling;
756                        }
757                }
758        }
759
760        function _sendMessage(_pJid, _pMessage, _pHistory)
761        {
762                var m1 = _pMessage = _pMessage.body.innerHTML;
763                var m2 = _pMessage = _pMessage.replace(/(&nbsp;)+|( )+/ig, ' ');
764                var m3 = _pMessage = _pMessage.replace(/(<br[^>]*>)+/ig, '<br/>');
765                var m4 = _pMessage = _pMessage.replace(/^( )+|( )+$|^(<br\/>)|(<br\/>)$/g, '');
766
767                //_pMessage = _pMessage.replace(/( )+(<br[^>]*>)+/g, '<br/>');
768                //_pMessage = _pMessage.replace(/(<br[^>]*>)*( )+(<br[^>]*>)+/g, '');
769                //_pMessage = _pMessage.replace(/^(<br[^>]*>)*|(<br[^>]*>)*$/g, '');
770
771                if ( _pMessage != '' )
772                {
773                        _conn.go('$this.Ujabber.sendMessage', "to=" + _pJid + "&body=" + escape(_pMessage));
774
775                        if ( _pHistory.scrollHeight == (_pHistory.scrollTop + _pHistory.clientHeight) )
776                        {
777                                _pHistory.innerHTML += '<b>' + imGetLang('says') + ':</b><br/>' + _pMessage + '<br/><br/>';
778                            _pHistory.scrollTop = _pHistory.scrollHeight;
779                        }
780                        else
781                        // this "else" is to prevent the creation of a flag and the need to use another conditional
782                                _pHistory.innerHTML += '<b>' + imGetLang('says') + ':</b><br/>' + _pMessage + '<br/><br/>';
783                }
784        }
785
786        function _action(_pAction)
787        {
788                //alert ( typeof pAction + "\n\n" + pAction.nodeName )
789                if ( typeof _pAction == 'object' && _pAction.nodeName )
790                {
791                        switch ( _pAction.nodeName )
792                        {
793                                case 'iq' :
794                                        _readIq(_pAction);
795                                break;
796                                case 'message' :
797                                        _readMessage(_pAction);
798                                break;
799                                case 'presence' :
800                                        _readPresence(_pAction);
801                                break;
802                                //default : alert('action : ' + _pAction.nodeName);
803                        }
804                }
805        }
806
807        function Jabber()
808        {
809                _conn = arguments[0];
810                _xtools = arguments[1];
811                _window = arguments[2];
812                _ldap = arguments[3];
813                _menu = arguments[4];
814                _cookies = arguments[5];
815        }
816
817        Jabber.prototype.action = _action;
818        Jabber.prototype.setPresence = _setPresence;
819        window.Jabber = Jabber;
820        window.InstantMessengerPresence = _setPresence;
821}
822)();
Note: See TracBrowser for help on using the repository browser.