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

Revision 227, 10.8 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                        function handler(Xmldata)
77                        {
78                        };
79                        con.go('$this.Ujabber.setStatus', {'request':handler});
80
81                        var status = "";
82                        if( status = cookies_im.checkCookie('status'))
83                        {
84                                status = status.substr(status.indexOf("=") + 1,status.length);
85                                im.status = status;
86                        }
87                        else
88                        {
89                                im.status = 'type=available';
90                                im.statusFlag = 'available';
91                                func.byId('im_status_src').src = im_available.src;
92                        }
93       
94                        im.presence(im.status);
95                }
96
97                function readIq(pIq)
98                {
99                        switch ( pIq.getAttribute('id') )
100                        {
101                                case 'contacts' : readContacts(pIq); break;
102                                case 'vCard' :
103                                case 'vCard_user' :
104                                        readVcard(pIq);
105                                        break;
106                        }
107                }
108
109                function readMessage(pMessage)
110                {
111                        if ( pMessage.getElementsByTagName('body').length > 0 )
112                        {
113                                var msg = _this.xtools.parse(pMessage, 'message_new.xsl');
114                                var from = pMessage.getAttribute('from');
115                                from = from.substr(0,from.indexOf('/'));
116
117                                if(func.byId('iframe_' + from) != null)
118                                        func.byId('iframe_' + from).parentNode.removeChild(func.byId('iframe_' + from));
119
120                                var win;
121
122                                function include_iframe()
123                                {
124                                        win = im_window.elementC(from);
125                                       
126                                        var iframe = document.createElement('iframe');
127                                        iframe.id = 'iframe_' + from;
128                                        iframe.style.width = '290px';
129                                        iframe.style.height = '46px';
130                                        iframe.style.border = '0';
131                                        iframe.style.margin = '0';
132                                        iframe.style.padding = '0'
133                                       
134                                        var _messages_ = win.firstChild.firstChild.nextSibling.nextSibling;
135                                        _messages_.appendChild(iframe);
136                                       
137                                        iframe.contentWindow.document.write('<html><body id="' + from + '" style="margin:0px;padding:0px"></body></html>');
138                                        iframe.contentWindow.document.close();
139                                        iframe.contentWindow.document.designMode = "On";
140                                        iframe.contentWindow.focus();
141                               
142                                        function keypress_handler(e)
143                                        {
144                                                var evt = e.keyCode;
145                                                var pId = ( e.srcElement ) ? e.srcElement.id : e.target.getElementsByTagName('body').item(0).id;
146                                                switch ( evt )
147                                                {
148                                                        case 13 :
149                                                                if ( !e.shiftKey )
150                                                                        sendMessage(iframe.contentWindow.document.getElementsByTagName('body').item(0), from);
151                                                                return false;
152                                                        break;
153                                                        case 27 :
154                                                                im_window.window_close(pId);
155                                                        break;
156                                                        default :
157                                                        break;
158                                                }
159                                        }
160                                        function keyup_handler(e)
161                                        {
162                                                var evt = e.keyCode;
163                                                var _text = ( e.srcElement ) ? e.srcElement.id : e.target.getElementsByTagName('body').item(0);
164
165                                                switch ( evt )
166                                                {
167                                                        case 13 :
168                                                                if ( !e.shiftKey )
169                                                                        _text.innerHTML = '';
170                                                                return false;
171                                                        break;
172                                                        default :
173                                                        break;
174                                                }
175                                        }
176                                        var obj = iframe.contentWindow.document;
177
178                                        if ( obj.addEventListener )
179                                        {
180                                                obj.addEventListener('keypress', keypress_handler, false);
181                                                obj.addEventListener('keyup', keyup_handler, false);
182                                        }
183                                        else if ( obj.attachEvent )
184                                        {
185                                                obj.attachEvent('onkeypress', keypress_handler);
186                                                obj.attachEvent('onkeyup', keyup_handler);
187                                        }
188                                }                               
189
190                                if ( !(win = im_window.elementC(from)) )
191                                {
192                                        im_window.load(from, 'instant_messenger_content', '<title>.:: Expresso Messenger ::.</title>', 'chat.xsl', '310', true, true, true);
193                                        //im_window.visible(from,'block');
194                                        func.byId(from + '_avatar').src = path_im + "/templates/default/images/photo.png";
195                                        include_iframe();
196                                }
197                                else
198                                {
199                                        im_window.load(from, '', '', '', '', true, true, true);
200                                        //im_window.visible(from,'block');
201                                        include_iframe();
202                                }
203
204                                if ( win )
205                                {
206                                        if(!(im.nickname_contacts[from]))
207                                                im.request_vcard(from, false);
208
209                                        win = win.firstChild.firstChild;
210                                        var nick = (im.nickname_contacts[from]) ? im.nickname_contacts[from] : from;
211                                        win.innerHTML += "<b>" + nick + "</b>" + im.insertEmoticons(msg);
212                                    win.scrollTop = win.scrollHeight;                                   
213
214                                        im.ldap_photo(from + "_avatar");
215                                }
216                                im.newMessageNotification();
217                        }
218                }
219
220                function readPresence(pPresence)
221                {
222                        //@TODO: presenca antes da lista ocasiona erros
223                        //verificar e retirar o controle de erros abaixo
224                        try
225                        {
226                                var jidFrom = pPresence.getAttribute('from');
227                                jidFrom = jidFrom.substr(0,jidFrom.indexOf('/'));
228       
229                                if(pPresence.getAttribute('type'))
230                                {
231                                        switch(pPresence.getAttribute('type'))
232                                        {
233                                                case 'subscribe' :
234                                                                if(func.byId(pPresence.getAttribute('from')))
235                                                                {
236                                                                        if(confirm('Autorizar o usuário ' + pPresence.getAttribute('from') + '?'))
237                                                                                im.subscription(pPresence.getAttribute('from'),'subscribed');
238                                                                        else
239                                                                                im.subscription(pPresence.getAttribute('from'),'unsubscribe');
240                                                                }
241                                                                else
242                                                                {
243                                                                        if(!func.byId(pPresence.getAttribute('from')))
244                                                                        {
245                                                                                if(confirm('O usuário ' + pPresence.getAttribute('from') + ' quer adicioná-lo ?'))
246                                                                                {
247                                                                                        im.addrequest(pPresence.getAttribute('from'));
248                                                                                        im.subscription(pPresence.getAttribute('from'),'subscribed');
249                                                                                }
250                                                                                else
251                                                                                        im.subscription(pPresence.getAttribute('from'),'unsubscribe');
252                                                                        }else
253                                                                                im.subscription(pPresence.getAttribute('from'),pPresence.getAttribute('type'));
254                                                                }
255                                                                break;
256                                                case 'subscribed' :
257                                                                im.subscription(pPresence.getAttribute('from'),pPresence.getAttribute('type'));
258                                                                break;
259                                                case 'unsubscribed' :                                           
260                                                                //alert('O usuário ' + pPresence.getAttribute('from') + ' não quer mais papo !');
261                                                                break;
262                                                case 'unsubscribe' :                           
263                                                                alert('O usuário ' + pPresence.getAttribute('from') + ' negou seu pedido !');
264                                                                break;
265                                                case 'unavailable' :
266                                                                func.byId('img_' + jidFrom).src = eval('im_' + pPresence.getAttribute('type') + '.src');
267                                                                func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:none;");                                                       
268                                                                break;
269                                                case 'available' :
270                                                                func.byId('img_' + jidFrom).src = eval('im_' + pPresence.getAttribute('type') + '.src');
271                                                                func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:block;");                                                                                                               
272                                                                break;
273                                        }
274                                }
275                                else
276                                {
277                                        var node = pPresence.firstChild;
278                                        while( node )
279                                        {
280                                                if(node.hasChildNodes())
281                                                {                                               
282                                                        switch(node.nodeName)
283                                                        {
284                                                                case "show" :                                   
285                                                                                func.byId('img_' + jidFrom).src = eval('im_' + node.firstChild.nodeValue + '.src');
286                                                                                func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:block;");                                                       
287                                                                                func.byId('status_' + jidFrom).innerHTML = " ";
288                                                                                func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:none;");
289                                                                                break;
290                                                                case "status" :
291                                                                                func.byId('status_' + jidFrom).innerHTML = ( node.firstChild.nodeValue ) ? node.firstChild.nodeValue : "" ;
292                                                                                func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:block;");
293                                                                                break;
294                                                                default :
295                                                                                func.byId('img_' + jidFrom).src = im_available.src;                                                                     
296                                                                                func.byId('status_' + jidFrom).innerHTML = " ";
297                                                                                func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:none;");
298                                                                                break;
299                                                        }
300                                                }
301                                                node = node.nextSibling;                                       
302                                        }
303       
304                                        if(!im.contacts_off_line)
305                                                setTimeout("im.off_line()",3000);
306                                }
307                        }
308                        catch(e)
309                        {
310                        }
311                }
312
313                function readVcard(pVcard)
314                {
315                        switch(pVcard.getAttribute('id'))
316                        {
317                                case "vCard_user" :
318
319                                        var vcard = pVcard.firstChild;
320                                        var cc = vcard.firstChild;
321                                        while( cc )
322                                        {
323                                                if(cc.nodeName == "NICKNAME" || cc.nodeName == "nickname")
324                                                {
325                                                        if(cc.firstChild.nodeValue)
326                                                                func.byId('im_layer_nickname').innerHTML = cc.firstChild.nodeValue;                                                             
327                                                }
328                                                cc = cc.nextSibling;
329                                        }
330                                        if( im.vcard )
331                                                templates.vcard_user(pVcard);
332                                        break;
333
334                                case "vCard" :
335
336                                        var vcard = _this.xtools.parse(pVcard,'nickname.xsl');
337                                        im.nickname_contacts[vcard.substr(0,vcard.indexOf(','))] = ( vcard.substr(vcard.indexOf(',')+1) ) ? vcard.substr(vcard.indexOf(',')+1) : vcard.substr(0,vcard.indexOf(',')) ;
338                                        if( im.vcard )
339                                                templates.vcard_contact(pVcard);
340                                        break;
341                        }
342                }
343
344                function sendMessage(pMessage, pFrom)
345                {
346                        var handler_sendMessage = function(data)
347                        {
348                        };
349
350                        var sendMessage = pMessage.innerHTML;
351                        pMessage.innerHTML = pMessage.innerHTML.replace(/^( )*|( )*$/g, '');
352
353                        var send = false;
354                        var node = pMessage.firstChild;
355                       
356                        while ( node && !send )
357                        {
358                                if ( node.nodeName.toLowerCase() != 'br' )
359                                        send = true;
360                                node = node.nextSibling;
361                        }
362                        if ( send )
363                                _this.con.go('$this.Ujabber.sendMessage', {'request':handler_sendMessage},"to="+pMessage.getAttribute('id')+"&body="+escape(pMessage.innerHTML));
364                        pMessage.innerHTML = '';
365                       
366                        var win = im_window.elementC(pFrom);
367                        if ( win )
368                        {
369                                win = win.firstChild.firstChild;
370                                win.innerHTML += "<b>" + im.get_lang("says") +":</br></b>" + sendMessage + "</br></br>";
371                            win.scrollTop = win.scrollHeight;                                   
372                        }
373                }
374        }
375};
Note: See TracBrowser for help on using the repository browser.