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

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