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

Revision 151, 9.5 KB checked in by niltonneto, 16 years ago (diff)

Commit da nova versão do módulo, usando agente em C.
Vide Página do módulo do Trac:
http://www.expressolivre.org/dev/wiki/messenger

A versão anterior encontra-se na subpasta bkp (32/64).

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                        im_window.load('_contacts_im_','instant_messenger_content','<title>.::Expresso Messenger::.</title>','window.xsl','',true,true,false);
53                        im_window.elementC('_contacts_im_').innerHTML  = _this.xtools.parse(XmlDoc,'layer.xsl');
54                        im_window.elementC('_contacts_im_').innerHTML += _this.xtools.parse(pContacts, 'contacts.xsl');
55                       
56                        var element = func.byId('im_contact_list').firstChild;
57
58                        while(element)
59                        {
60                                if( element.nodeType == 1 )
61                                        if(element.nodeName.toLowerCase() == "img")
62                                                element.src = im_group_open.src
63                                        else if(element.nodeName.toLowerCase() == "div" && element.hasChildNodes())
64                                                element.firstChild.src = im_unavailable.src;
65
66                                element = element.nextSibling;
67                        }
68                       
69                        if(im.nickname == null)
70                        {
71                                im.request_vcard('this','layer');
72                                im.ldap_photo('im_avatar');
73                        }
74                        else
75                                func.byId('im_layer_nickname').innerHTML = im.nickname;
76                }
77
78                function readIq(pIq)
79                {
80                        switch ( pIq.getAttribute('id') )
81                        {
82                                case 'contacts' : readContacts(pIq); break;
83                                case 'vCard' : readVcard(pIq); break;
84                        }
85                }
86
87                function readMessage(pMessage)
88                {
89                        if ( pMessage.getElementsByTagName('body').length > 0 )
90                        {
91                                var msg = _this.xtools.parse(pMessage, 'message_new.xsl');
92                                var from = pMessage.getAttribute('from');
93                                from = from.substr(0,from.indexOf('/'));
94
95                                var win;
96                                if ( !(win = im_window.elementC(from)) )
97                                {
98                                        im_window.load(from, 'instant_messenger_content', '<title>.:: Expresso Messenger ::.</title>', 'chat.xsl', '310', true, true, true);
99                                        func.byId(from + '_avatar').src = path_im + "/templates/default/images/photo.png";                                 
100                                        win = im_window.elementC(from);
101
102                                        var iframe = document.createElement('iframe');
103                                        iframe.id = 'iframe_' + from;
104                                        iframe.style.width = '290px';
105                                        iframe.style.height = '46px';
106                                        iframe.style.border = '0';
107                                        iframe.style.margin = '0';
108                                        iframe.style.padding = '0'
109
110                                        var _messages_ = win.firstChild.firstChild.nextSibling.nextSibling;
111                                        _messages_.appendChild(iframe);
112
113                                        iframe.contentWindow.document.write('<html><body id="' + from + '" style="margin:0px;padding:0px"></body></html>');
114                                        iframe.contentWindow.document.close();
115                                        iframe.contentWindow.document.designMode = "On";
116                                        iframe.contentWindow.focus();
117
118                                        function keypress_handler(e)
119                                        {
120                                                var evt = e.keyCode;
121                                                var pId = ( e.srcElement ) ? e.srcElement.id : e.target.getElementsByTagName('body').item(0).id;
122                                                switch ( evt )
123                                                {
124                                                        case 13 :
125                                                                if ( !e.shiftKey )
126                                                                        sendMessage(iframe.contentWindow.document.getElementsByTagName('body').item(0), from);
127                                                                return false;
128                                                        break;
129                                                        case 27 :
130                                                                im_window.window_close(pId);
131                                                        break;
132                                                        default :
133                                                        break;
134                                                }
135                                        }
136                                        function keyup_handler(e)
137                                        {
138                                                var evt = e.keyCode;
139                                                var _text = ( e.srcElement ) ? e.srcElement.id : e.target.getElementsByTagName('body').item(0);
140
141                                                switch ( evt )
142                                                {
143                                                        case 13 :
144                                                                if ( !e.shiftKey )
145                                                                        _text.innerHTML = '';
146                                                                return false;
147                                                        break;
148                                                        default :
149                                                        break;
150                                                }
151                                        }
152                                        var obj = iframe.contentWindow.document;
153
154                                        if ( obj.addEventListener )
155                                        {
156                                                obj.addEventListener('keypress', keypress_handler, false);
157                                                obj.addEventListener('keyup', keyup_handler, false);
158                                        }
159                                        else if ( obj.attachEvent )
160                                        {
161                                                obj.attachEvent('onkeypress', keypress_handler);
162                                                obj.attachEvent('onkeyup', keyup_handler);
163                                        }
164                                }else
165                                        im_window.load(from, '', '', '', '', true, true, true);
166
167                                if ( win )
168                                {
169                                        if(!(im.nickname_contacts[from]))
170                                                im.request_vcard(from,'nickname');
171
172                                        win = win.firstChild.firstChild;
173                                        var nick = (im.nickname_contacts[from]) ? im.nickname_contacts[from] : from;
174                                        win.innerHTML += "<b>" + nick + "</b>" + im.insertEmoticons(msg);
175                                    win.scrollTop = win.scrollHeight;                                   
176
177                                        im.ldap_photo(from + "_avatar");
178                                }
179                        }
180                }
181
182                function readPresence(pPresence)
183                {
184                        var jidFrom = pPresence.getAttribute('from');
185                        jidFrom = jidFrom.substr(0,jidFrom.indexOf('/'));
186
187                        if(pPresence.getAttribute('type'))
188                        {
189                                switch(pPresence.getAttribute('type'))
190                                {
191                                        case 'subscribe' :
192                                                        if(!func.byId(pPresence.getAttribute('from')))
193                                                        {
194                                                                if(confirm('O usuario ' + pPresence.getAttribute('from') + ' quer adicioná-lo ?'))
195                                                                        im.addrequest(pPresence.getAttribute('from'));
196                                                                else
197                                                                        im.subscription(pPresence.getAttribute('from'),'unsubscribe');
198                                                        }else
199                                                                im.subscription(pPresence.getAttribute('from'),pPresence.getAttribute('type'));
200                                                        break;
201                                        case 'subscribed' :
202                                                        im.subscription(pPresence.getAttribute('from'),pPresence.getAttribute('type'));
203                                                        break;
204                                        case 'unsubscribed' :                                           
205                                                        //alert('O usuário ' + pPresence.getAttribute('from') + ' não quer mais papo !');
206                                                        break;
207                                        case 'unsubscribe' :                           
208                                                        alert('O usuário ' + pPresence.getAttribute('from') + ' negou seu pedido !');
209                                                        break;
210                                        case 'unavailable' :
211                                                        func.byId('img_' + jidFrom).src = eval('im_' + pPresence.getAttribute('type') + '.src');
212                                                        func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:none;");                                                       
213                                                        break;
214                                        case 'available' :
215                                                        func.byId('img_' + jidFrom).src = eval('im_' + pPresence.getAttribute('type') + '.src');
216                                                        break;
217                                }
218                        }
219                        else
220                        {
221                                var node = pPresence.firstChild;
222                                while( node )
223                                {
224                                        if(node.hasChildNodes())
225                                        {                                               
226                                                switch(node.nodeName)
227                                                {
228                                                        case "show" :                                   
229                                                                        func.byId('img_' + jidFrom).src = eval('im_' + node.firstChild.nodeValue + '.src');
230                                                                        func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:block;");                                                       
231                                                                        func.byId('status_' + jidFrom).innerHTML = " ";
232                                                                        func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:none;");
233                                                                        break;
234                                                        case "status" :
235                                                                        func.byId('status_' + jidFrom).innerHTML = ( node.firstChild.nodeValue ) ? node.firstChild.nodeValue : "" ;
236                                                                        func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:block;");
237                                                                        break;
238                                                        default :
239                                                                        func.byId('img_' + jidFrom).src = im_available.src;                                                                     
240                                                                        func.confEl(func.byId(jidFrom),"style","margin-left:15px;cursor:pointer;display:block;");                                                       
241                                                                        func.byId('status_' + jidFrom).innerHTML = " ";
242                                                                        func.confEl(func.byId('status_' + jidFrom), "style", "font-size:8pt; font-family:Verdana, Arial, Helvetica, sans-serif; font-style:italic; display:none;");
243                                                                        break;
244                                                }
245                                        }
246                                        node = node.nextSibling;                                       
247                                }
248
249                                /*if(!im.contacts_off_line)
250                                        setTimeout("im.off_line('none')",2000);*/
251                        }
252                }
253
254                function readVcard(pVcard)
255                {
256                        switch(im.vcard)
257                        {
258                                case "layer" :
259                                        var vcard = pVcard.firstChild;
260                                        var cc = vcard.firstChild;
261                                       
262                                        while( cc )
263                                        {
264                                                if(cc.nodeName == "NICKNAME" || cc.nodeName == "nickname")
265                                                {
266                                                        func.byId('im_layer_nickname').innerHTML = cc.firstChild.nodeValue;
267                                                        im.nickname = " ";
268                                                        if(cc.firstChild.nodeValue)
269                                                                im.nickname = cc.firstChild.nodeValue;
270                                                }
271
272                                                cc = cc.nextSibling;
273                                        }
274
275                                        break;
276
277                                case "user"     :
278                                        templates.vcard_user(pVcard);
279                                        im.vcard = "layer";                                     
280                                        break;
281
282                                case "contact" :
283                                        templates.vcard_contact(pVcard);
284                                        im.vcard = "layer";                                     
285                                        break;
286                                       
287                                case "nickname" :
288                                        var vcard = _this.xtools.parse(pVcard,'nickname.xsl');
289                                        im.vcard = "layer";     
290                                        im.nickname_contacts[vcard.substr(0,vcard.indexOf(','))] = vcard.substr(vcard.indexOf(',')+1);
291                                        break;
292                        }
293                }
294
295                function sendMessage(pMessage, pFrom)
296                {
297                        var handler_sendMessage = function(data)
298                        {
299                        };
300
301                        var sendMessage = pMessage.innerHTML;
302                        pMessage.innerHTML = pMessage.innerHTML.replace(/^( )*|( )*$/g, '');
303
304                        var send = false;
305                        var node = pMessage.firstChild;
306                       
307                        while ( node && !send )
308                        {
309                                if ( node.nodeName.toLowerCase() != 'br' )
310                                        send = true;
311                                node = node.nextSibling;
312                        }
313                        if ( send )
314                                _this.con.go('$this.Ujabber.sendMessage', {'request':handler_sendMessage},"to="+pMessage.getAttribute('id')+"&body="+escape(pMessage.innerHTML));
315                        pMessage.innerHTML = '';
316                       
317                        var win = im_window.elementC(pFrom);
318                        if ( win )
319                        {
320                                win = win.firstChild.firstChild;
321                                win.innerHTML += "<b>" + im.get_lang("says") +":</br></b>" + sendMessage + "</br></br>";
322                            win.scrollTop = win.scrollHeight;                                   
323                        }
324                }
325        }
326};
Note: See TracBrowser for help on using the repository browser.