]> git.babelmonkeys.de Git - xmppchat.git/blob - js/main.js
/me support
[xmppchat.git] / js / main.js
1 var connection = null;
2 var nickname;
3
4 function doLogin(aForm) {
5         if (!aForm.nickname.value)
6                 return false;
7         try {
8                 connection = new Strophe.Connection(BOSH_LOCATION);
9                 connection.connect(jid, password, onConnect);
10
11                 nickname = aForm.nickname.value;
12         } catch (e) {
13                 alert(e);
14         } finally {
15                 return false;   
16         }
17 }
18
19 function onConnect(status) {
20         if (status == Strophe.Status.CONNFAIL) {
21                 handleError('Failed to connect');
22         } else if (status == Strophe.Status.DISCONNECTED) {
23                 handleDisconnected();
24         } else if (status == Strophe.Status.CONNECTED) {
25                 // Add handlers connection.addHandler(callback, namespace, stanza_name, type, id, from)
26                 connection.addHandler(handleMessage, null, 'message', null, null, null);
27                 connection.addHandler(handlePresence, null, 'presence', null, null, null);
28                 connection.addHandler(handleIQ, null, 'iq', null, null, null);
29
30                 connection.addHandler(handleIqVersion, Strophe.NS.VERSION, 'iq', null, null, null);
31                 connection.addHandler(handleIqVersion, 'urn:xmpp:time', 'iq', null, null, null);
32
33                 connection.send($pres().tree());
34                 connection.send($pres({to: room + '/' + nickname}).tree());
35
36                 // Make things (in)visible
37                 $('#login').hide();
38                 $('#chat').show();
39                 $('#roster').show();
40                 $('#entry').show();
41         }
42 }
43
44 function handleError(error) {
45         alert("An error occured:" + error);
46         handleDisconnected();
47 }
48
49 function handleDisconnected() {
50         // Make things (in)visible
51         $('#login').show();
52         $('#chat').hide();
53         $('#roster').hide();
54         $('#entry').hide();
55 }
56
57 function addBubble(nick) {
58         id = nick + 'Bubble';
59         if (!document.getElementById(id)) {
60                 var div = '';
61                 div += '<div id="' + id + '" class="bubble" onmousedown="startDrag(this)" style="display: none">';
62                 div += '<a href="#" onclick="' +"$('#" + id + "').hide('slow')" + '">Close</a>';
63                 div += '<div id="' + id + 'Chat" class="bubbleChat"></div>';
64                 div += '<form id="' + id + 'Form" class="bubbleForm" onsubmit="return sendChatMessage(this,' + "'" + nick + "');" + '" action="#">';
65                 div += '<input type="text" name="text" id="' + id + 'Text" class="bubbleForm"/>';
66                 div += '</form>';
67                 div += '</div>';
68                 $('body').append(div);
69         }
70         $('#'+id).show('slow');
71 }
72
73 function handleMessage(msg) {
74         var html = '';
75         var sender = Strophe.xmlescape(Strophe.getResourceFromJid(msg.getAttribute('from')));
76         var type = msg.getAttribute('type');
77         var body = Strophe.xmlescape(Strophe.getText(msg.getElementsByTagName('body')[0]));
78
79         html += '<div class="msg">';
80         if (sender) {
81                 if (body.search(/^\/me/) == 0) {
82                         body = body.replace(/^\/me/, sender);
83                         html += '<span class="sender">';
84                         html += body;
85                         html += '</span></div>';
86                 } else {
87                         html += '<span class="sender">';
88                         html += sender;
89                         html += ':</span> ';
90                         html += body + '</div>';
91                 }
92         } else {
93                 html += '<span class="server">';
94                 html += body + '</span></div>';
95         }
96
97         if (type == 'chat') {
98                 addBubble(sender);
99                 $('#' + id + 'Chat').append(html);
100                 document.getElementById(id + 'Chat').lastChild.scrollIntoView();
101         } else {
102                 $('#chat').append(html);
103                 document.getElementById('chat').lastChild.scrollIntoView();
104         }
105
106         return true;
107 }
108
109 function handlePresence(presence) {
110         if (Strophe.getBareJidFromJid(presence.getAttribute('from')) != room)
111                 return true
112         roster_list = document.getElementById('roster_list');
113         nick = Strophe.getResourceFromJid(presence.getAttribute('from'));
114         type = presence.getAttribute('type');
115         if (type == 'unavailable') {
116                 element = document.getElementById(nick);
117                 roster_list.removeChild(element);
118                 $('#chat').append('<div class="msg"><span class="server">' + nick + ' left the groupchat</span></div>');
119         } else {
120                 roster_list.innerHTML += '<li id="' + nick + '" onclick="addBubble(' + "'" + nick + "')" + '" >' + nick + '</li>';
121                 $('#chat').append('<div class="msg"><span class="server">' + nick + ' joined the groupchat</span></div>');
122         }
123
124         return true;
125 }
126
127 function handleIQ(iq) {
128         var to = iq.getAttribute('to');
129         var from = iq.getAttribute('from');
130         var type = iq.getAttribute('type');
131
132         //FIXME: Clients SHOULD send the content of the original stanza back for analysis
133
134         var reply = $iq({to: from, from: to, type: 'error'}).c('error', {type: 'cancel'}).c('feature-not-implemented', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'});
135
136         connection.send(reply.tree());
137
138         return true;
139 }
140
141 function handleIqVersion(iq) {
142         var to = iq.getAttribute('to');
143         var from = iq.getAttribute('from');
144
145         var reply = $iq({type: 'result', to: from, from: to}).c('query', {xmlns: Strophe.NS.VERSION}).c('name').t('XMPPChat').up().c('version').t('preAny').up().c('os').t(navigator.userAgent);
146
147         connection.send(reply.tree());
148
149         return true;
150 }
151
152 function handleIqTime(iq) {
153         var now = new Date();
154         var to = iq.getAttribute('to');
155         var from = iq.getAttribute('from');
156
157         var reply = $iq({type: 'result', from: to, to: from}).c('time', {xmlns: 'urn:xmpp:time'}).c('utc').t(now.getUTCFullYear() + '-' + now.getUTCMonth() + '-' + now.getUTCDate() + 'T' + now.getUTCHours() + ':' + now.getUTCMinutes() + ':' + now.getUTCSeconds + '.' + now.getUTCMilliseconds() + 'Z').up().c('tzo').t(now.getTimezoneOffset()/60 + ':' + now.getTimezoneOffset()%60);
158
159         connection.send(reply.tree());
160
161         return true;
162 }
163
164 function sendMessage(aForm) {
165         if (aForm.text.value) {
166                 message = $msg({type: 'groupchat', to: room}).c('body').t(aForm.text.value);
167                 connection.send(message.tree());
168                 aForm.text.value = '';
169         }
170         return false;
171 }
172
173 function sendChatMessage(aForm, to) {
174         if (aForm.text.value) {
175                 body = aForm.text.value
176                 message = $msg({type: 'chat', to: room + '/' + to}).c('body').t(body);
177                 connection.send(message.tree());
178                 aForm.text.value = '';
179                 var html = '';
180                 html += '<div class="msg">';
181                 html += '<span class="sender">';
182                 html += nickname;
183                 html += ':</span> ';
184                 html += body + '</div>';
185                 document.getElementById(to + 'BubbleChat').innerHTML += html;
186                 document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
187
188         }
189         return false;
190 }
191
192 var dragElement = null;
193 var mouseX = 0;
194 var mouseY = 0;
195 var offX = 0;
196 var offY = 0;
197
198 function startDrag(element) {
199         dragElement = element;
200         offX = mouseX - dragElement.offsetLeft;
201         offY = mouseY - dragElement.offsetTop;
202 }
203
204 function doDrag(eve) {
205         mouseX = eve.pageX;
206         mouseY = eve.pageY;
207
208         if (dragElement) {
209                 dragElement.style.left = (mouseX - offX) + 'px';
210                 dragElement.style.top = (mouseY - offY) + 'px';
211         }
212 }
213
214 function stopDrag(eve) {
215         dragElement = null;
216 }
217
218 onunload = function() {
219         if (connection) {
220                 connection.disconnect();
221         }
222 }
223
224 onmousemove = doDrag;
225
226 onmouseup = stopDrag;