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

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