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

Revision 287, 11.2 KB checked in by niltonneto, 16 years ago (diff)

Verificar Wiki/Trac? do módulo.

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