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

Revision 171, 10.4 KB checked in by niltonneto, 16 years ago (diff)
Line 
1function Jabber()
2{
3        this.con = null;
4        this.xtools = null;
5}
6
7Jabber.prototype = {
8        'doIt' : function(pAction, pParam)
9        {
10                var _this = this;
11                function _connector()
12                {
13                        if ( _this.con == null )
14                                _this.con = new Connector;
15                }
16                function _xtools()
17                {
18                        _this.xtools = new XTools;
19                }
20
21                _connector();
22                _xtools();
23
24                if ( typeof pAction != 'string' || typeof pParam != 'object' )
25                        return false;
26
27                switch ( pAction )
28                {
29                        case 'iq' :
30                                readIq(pParam);
31                        break;
32                        case 'message' :
33                                readMessage(pParam);
34                        break;
35                        case 'presence' :
36                                readPresence(pParam);
37                        break;
38                        case 'sendMessage':
39                                sendMessage(pParam.message,pParam.from);
40                                break;
41                }
42
43                function readContacts(pContacts)
44                {
45                        var XmlDoc = "<layer>" +
46                                                        "<lang1>" + im.get_lang("Nickname") + "</lang1>" +
47                                                        "<lang2>" + im.get_lang("Add Contact") + "</lang2>" +
48                                                        "<lang3>" + im.get_lang("Preferences") + "</lang3>" +
49                                                        "<nickname>" + im.get_lang("Username") + "</nickname>" +
50                                                        "<path>" + path_im + "</path>" +
51                                                 "</layer>";
52
53                        im_window.load('_contacts_im_','instant_messenger_content','<title>.::Expresso Messenger::.</title>','window.xsl','',true,true,false);
54                        im_window.elementC('_contacts_im_').innerHTML  = _this.xtools.parse(XmlDoc,'layer.xsl');
55                        im_window.elementC('_contacts_im_').innerHTML += _this.xtools.parse(pContacts, 'contacts.xsl');
56                                               
57                        var element = func.byId('im_contact_list').firstChild;
58
59                        while(element)
60                        {
61                                if( element.nodeType == 1 )
62                                        if(element.nodeName.toLowerCase() == "img")
63                                                element.src = im_group_open.src
64                                        else if(element.nodeName.toLowerCase() == "div" && element.hasChildNodes())
65                                                element.firstChild.src = im_unavailable.src;
66
67                                element = element.nextSibling;
68                        }
69                       
70                        if(func.byId('im_avatar').src === im_photo.src)
71                        {                       
72                                im.request_vcard('this',false);
73                                im.ldap_photo('im_avatar');
74                        }
75                }
76
77                function readIq(pIq)
78                {
79                        switch ( pIq.getAttribute('id') )
80                        {
81                                case 'contacts' : readContacts(pIq); break;
82                                case 'vCard' :
83                                case 'vCard_user' :
84                                        readVcard(pIq);
85                                        break;
86                        }
87                }
88
89                function readMessage(pMessage)
90                {
91                        if ( pMessage.getElementsByTagName('body').length > 0 )
92                        {
93                                var msg = _this.xtools.parse(pMessage, 'message_new.xsl');
94                                var from = pMessage.getAttribute('from');
95                                from = from.substr(0,from.indexOf('/'));
96
97                                if(func.byId('iframe_' + from) != null)
98                                        func.byId('iframe_' + from).parentNode.removeChild(func.byId('iframe_' + from));
99
100                                var win;
101
102                                function include_iframe()
103                                {
104                                        win = im_window.elementC(from);
105                                       
106                                        var iframe = document.createElement('iframe');
107                                        iframe.id = 'iframe_' + from;
108                                        iframe.style.width = '290px';
109                                        iframe.style.height = '46px';
110                                        iframe.style.border = '0';
111                                        iframe.style.margin = '0';
112                                        iframe.style.padding = '0'
113                                       
114                                        var _messages_ = win.firstChild.firstChild.nextSibling.nextSibling;
115                                        _messages_.appendChild(iframe);
116                                       
117                                        iframe.contentWindow.document.write('<html><body id="' + from + '" style="margin:0px;padding:0px"></body></html>');
118                                        iframe.contentWindow.document.close();
119                                        iframe.contentWindow.document.designMode = "On";
120                                        iframe.contentWindow.focus();
121                               
122                                        function keypress_handler(e)
123                                        {
124                                                var evt = e.keyCode;
125                                                var pId = ( e.srcElement ) ? e.srcElement.id : e.target.getElementsByTagName('body').item(0).id;
126                                                switch ( evt )
127                                                {
128                                                        case 13 :
129                                                                if ( !e.shiftKey )
130                                                                        sendMessage(iframe.contentWindow.document.getElementsByTagName('body').item(0), from);
131                                                                return false;
132                                                        break;
133                                                        case 27 :
134                                                                im_window.window_close(pId);
135                                                        break;
136                                                        default :
137                                                        break;
138                                                }
139                                        }
140                                        function keyup_handler(e)
141                                        {
142                                                var evt = e.keyCode;
143                                                var _text = ( e.srcElement ) ? e.srcElement.id : e.target.getElementsByTagName('body').item(0);
144
145                                                switch ( evt )
146                                                {
147                                                        case 13 :
148                                                                if ( !e.shiftKey )
149                                                                        _text.innerHTML = '';
150                                                                return false;
151                                                        break;
152                                                        default :
153                                                        break;
154                                                }
155                                        }
156                                        var obj = iframe.contentWindow.document;
157
158                                        if ( obj.addEventListener )
159                                        {
160                                                obj.addEventListener('keypress', keypress_handler, false);
161                                                obj.addEventListener('keyup', keyup_handler, false);
162                                        }
163                                        else if ( obj.attachEvent )
164                                        {
165                                                obj.attachEvent('onkeypress', keypress_handler);
166                                                obj.attachEvent('onkeyup', keyup_handler);
167                                        }
168                                }                               
169
170                                if ( !(win = im_window.elementC(from)) )
171                                {
172                                        im_window.load(from, 'instant_messenger_content', '<title>.:: Expresso Messenger ::.</title>', 'chat.xsl', '310', true, true, true);
173                                        //im_window.visible(from,'block');
174                                        func.byId(from + '_avatar').src = path_im + "/templates/default/images/photo.png";
175                                        include_iframe();
176                                }
177                                else
178                                {
179                                        im_window.load(from, '', '', '', '', true, true, true);
180                                        //im_window.visible(from,'block');
181                                        include_iframe();
182                                }
183
184                                if ( win )
185                                {
186                                        if(!(im.nickname_contacts[from]))
187                                                im.request_vcard(from, false);
188
189                                        win = win.firstChild.firstChild;
190                                        var nick = (im.nickname_contacts[from]) ? im.nickname_contacts[from] : from;
191                                        win.innerHTML += "<b>" + nick + "</b>" + im.insertEmoticons(msg);
192                                    win.scrollTop = win.scrollHeight;                                   
193
194                                        im.ldap_photo(from + "_avatar");
195                                }
196                                im.newMessageNotification();
197                        }
198                }
199
200                function readPresence(pPresence)
201                {
202                        //@TODO: presenca antes da lista ocasiona erros
203                        //verificar e retirar o controle de erros abaixo
204                        try
205                        {
206                                var jidFrom = pPresence.getAttribute('from');
207                                jidFrom = jidFrom.substr(0,jidFrom.indexOf('/'));
208       
209                                if(pPresence.getAttribute('type'))
210                                {
211                                        switch(pPresence.getAttribute('type'))
212                                        {
213                                                case 'subscribe' :
214                                                                if(func.byId(pPresence.getAttribute('from')))
215                                                                {
216                                                                        if(confirm('Autorizar o usuário ' + pPresence.getAttribute('from') + '?'))
217                                                                                im.subscription(pPresence.getAttribute('from'),'subscribed');
218                                                                        else
219                                                                                im.subscription(pPresence.getAttribute('from'),'unsubscribe');
220                                                                }
221                                                                else
222                                                                {
223                                                                        if(!func.byId(pPresence.getAttribute('from')))
224                                                                        {
225                                                                                if(confirm('O usuário ' + pPresence.getAttribute('from') + ' quer adicioná-lo ?'))
226                                                                                {
227                                                                                        im.addrequest(pPresence.getAttribute('from'));
228                                                                                        im.subscription(pPresence.getAttribute('from'),'subscribed');
229                                                                                }
230                                                                                else
231                                                                                        im.subscription(pPresence.getAttribute('from'),'unsubscribe');
232                                                                        }else
233                                                                                im.subscription(pPresence.getAttribute('from'),pPresence.getAttribute('type'));
234                                                                }
235                                                                break;
236                                                case 'subscribed' :
237                                                                im.subscription(pPresence.getAttribute('from'),pPresence.getAttribute('type'));
238                                                                break;
239                                                case 'unsubscribed' :                                           
240                                                                //alert('O usuário ' + pPresence.getAttribute('from') + ' não quer mais papo !');
241                                                                break;
242                                                case 'unsubscribe' :                           
243                                                                alert('O usuário ' + pPresence.getAttribute('from') + ' negou seu pedido !');
244                                                                break;
245                                                case 'unavailable' :
246                                                                func.byId('img_' + jidFrom).src = eval('im_' + pPresence.getAttribute('type') + '.src');
247                                                                func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:none;");                                                       
248                                                                break;
249                                                case 'available' :
250                                                                func.byId('img_' + jidFrom).src = eval('im_' + pPresence.getAttribute('type') + '.src');
251                                                                func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:block;");                                                                                                               
252                                                                break;
253                                        }
254                                }
255                                else
256                                {
257                                        var node = pPresence.firstChild;
258                                        while( node )
259                                        {
260                                                if(node.hasChildNodes())
261                                                {                                               
262                                                        switch(node.nodeName)
263                                                        {
264                                                                case "show" :                                   
265                                                                                func.byId('img_' + jidFrom).src = eval('im_' + node.firstChild.nodeValue + '.src');
266                                                                                func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:block;");                                                       
267                                                                                func.byId('status_' + jidFrom).innerHTML = " ";
268                                                                                func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:none;");
269                                                                                break;
270                                                                case "status" :
271                                                                                func.byId('status_' + jidFrom).innerHTML = ( node.firstChild.nodeValue ) ? node.firstChild.nodeValue : "" ;
272                                                                                func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:block;");
273                                                                                break;
274                                                                default :
275                                                                                func.byId('img_' + jidFrom).src = im_available.src;                                                                     
276                                                                                func.byId('status_' + jidFrom).innerHTML = " ";
277                                                                                func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:none;");
278                                                                                break;
279                                                        }
280                                                }
281                                                node = node.nextSibling;                                       
282                                        }
283       
284                                        if(!im.contacts_off_line)
285                                                setTimeout("im.off_line()",3000);
286                                }
287                        }
288                        catch(e)
289                        {
290                        }
291                }
292
293                function readVcard(pVcard)
294                {
295                        switch(pVcard.getAttribute('id'))
296                        {
297                                case "vCard_user" :
298
299                                        var vcard = pVcard.firstChild;
300                                        var cc = vcard.firstChild;
301                                        while( cc )
302                                        {
303                                                if(cc.nodeName == "NICKNAME" || cc.nodeName == "nickname")
304                                                {
305                                                        if(cc.firstChild.nodeValue)
306                                                                func.byId('im_layer_nickname').innerHTML = cc.firstChild.nodeValue;                                                             
307                                                }
308                                                cc = cc.nextSibling;
309                                        }
310                                        if( im.vcard )
311                                                templates.vcard_user(pVcard);
312                                        break;
313
314                                case "vCard" :
315
316                                        var vcard = _this.xtools.parse(pVcard,'nickname.xsl');
317                                        im.nickname_contacts[vcard.substr(0,vcard.indexOf(','))] = ( vcard.substr(vcard.indexOf(',')+1) ) ? vcard.substr(vcard.indexOf(',')+1) : vcard.substr(0,vcard.indexOf(',')) ;
318                                        if( im.vcard )
319                                                templates.vcard_contact(pVcard);
320                                        break;
321                        }
322                }
323
324                function sendMessage(pMessage, pFrom)
325                {
326                        var handler_sendMessage = function(data)
327                        {
328                        };
329
330                        var sendMessage = pMessage.innerHTML;
331                        pMessage.innerHTML = pMessage.innerHTML.replace(/^( )*|( )*$/g, '');
332
333                        var send = false;
334                        var node = pMessage.firstChild;
335                       
336                        while ( node && !send )
337                        {
338                                if ( node.nodeName.toLowerCase() != 'br' )
339                                        send = true;
340                                node = node.nextSibling;
341                        }
342                        if ( send )
343                                _this.con.go('$this.Ujabber.sendMessage', {'request':handler_sendMessage},"to="+pMessage.getAttribute('id')+"&body="+escape(pMessage.innerHTML));
344                        pMessage.innerHTML = '';
345                       
346                        var win = im_window.elementC(pFrom);
347                        if ( win )
348                        {
349                                win = win.firstChild.firstChild;
350                                win.innerHTML += "<b>" + im.get_lang("says") +":</br></b>" + sendMessage + "</br></br>";
351                            win.scrollTop = win.scrollHeight;                                   
352                        }
353                }
354        }
355};
Note: See TracBrowser for help on using the repository browser.