]> git.babelmonkeys.de Git - xmppchat.git/blob - main.js
Support for private messages
[xmppchat.git] / main.js
1 function doLogin(aForm) {
2         room = 'guests@conference.babelmonkeys.de';
3         domain = 'babelmonkeys.de'
4         user = 'muckl';
5         password = 'ooje0OjuJeekaek6';
6         if (!aForm.nickname.value)
7                 return false;
8         try {
9                 oArgs = new Object();
10                 oArgs.httpbase = '/http-bind/';
11                 oArgs.timerval = 2000;
12                 con = new JSJaCHttpBindingConnection(oArgs)
13
14                 nickname = aForm.nickname.value;
15                 setupHandlers(con);
16                 
17                 oArgs = new Object();
18                 oArgs.domain = domain;
19                 oArgs.username = user;
20                 oArgs.resource = randomString();
21                 oArgs.pass = password;
22                 con.connect(oArgs);
23         } catch (e) {
24                 alert(e);
25         } finally {
26                 return false;   
27         }
28 }
29
30 function setupHandlers(con) {
31         con.registerHandler('message', handleMessage);
32         con.registerHandler('presence', handlePresence);
33         con.registerHandler('iq', handleIQ);
34         con.registerHandler('onconnect', handleConnected);
35         con.registerHandler('onerror', handleError);
36         // con.registerHandler('status_changed', handleStatusChanged);
37         con.registerHandler('ondisconnect', handleDisconnected);
38
39         con.registerIQGet('query', NS_VERSION, handleIqVersion);
40         con.registerIQGet('query', NS_TIME, handleIqTime);
41 }
42
43 function handleConnected() {
44         con.send(new JSJaCPresence());
45         GCpresence = new JSJaCPresence();
46         GCpresence.setTo(room + '/' + nickname);
47         con.send(GCpresence);
48
49         // Make things (in)visible
50         document.getElementById('login').style.display = 'none';
51         document.getElementById('chat').style.display = 'block';
52         document.getElementById('roster').style.display = 'block';
53         document.getElementById('entry').style.display  = 'block';
54 }
55
56 function handleError(e) {
57         alert("An error occured:" + 
58                 "\nCode: " + e.getAttribute('code') + 
59                 "\nType: " + e.getAttribute('type') +
60                 "\nCondition: " + e.firstChild.nodeName); 
61         // Make things (in)visible
62         document.getElementById('login').style.display = 'block';
63         document.getElementById('chat').style.display = 'none';
64         document.getElementById('roster').style.display = 'none';
65         document.getElementById('entry').style.display  = 'none';
66
67   
68         if (con.connected())
69                 con.disconnect();
70 }
71
72 function handleDisconnected() {
73         // Make things (in)visible
74         document.getElementById('login').style.display = 'block';
75         document.getElementById('chat').style.display = 'none';
76         document.getElementById('roster').style.display = 'none';
77         document.getElementById('entry').style.display  = 'none';
78 }
79
80 function addBubble(nick) {
81         id = nick + 'Bubble';
82         if (!document.getElementById(id)) {
83                 root = document.getElementsByTagName('body')[0];
84                 var div = '';
85                 div += '<div id="' + id + '" class="bubble" onmousedown="startDrag(this)">';
86                 div += '<a href="#" onclick="' +"document.getElementById('" + id + "').style.display='none'" + '">Close</a>';
87                 div += '<div id="' + id + 'Chat" class="bubbleChat"></div>';
88                 div += '<form id="' + id + 'Form" class="bubbleForm" onsubmit="return sendChatMessage(this,' + "'" + nick + "');" + '" action="#">';
89                 div += '<input type="text" name="text" id="' + id + 'Text" class="bubbleForm"/>';
90                 div += '</form>';
91                 div += '</div>';
92                 root.innerHTML += div;
93         }
94         document.getElementById(id).style.display = 'block';
95 }
96
97 function handleMessage(aJSJaCPacket) {
98         var html = '';
99         sender = aJSJaCPacket.getFromJID().getResource()
100         html += '<div class="msg">';
101         if (sender) {
102                 html += '<span class="sender">';
103                 html += sender;
104                 html += ':</span> ';
105                 html += aJSJaCPacket.getBody().htmlEnc() + '</div>';
106         } else {
107                 html += '<span class="server">';
108                 html += aJSJaCPacket.getBody().htmlEnc() + '</span></div>';
109         }
110
111         if (aJSJaCPacket.getType() == 'chat') {
112                 addBubble(sender);
113                 document.getElementById(id + 'Chat').innerHTML += html;
114                 document.getElementById(id + 'Chat').lastChild.scrollIntoView();
115         } else {
116                 document.getElementById('chat').innerHTML += html;
117                 document.getElementById('chat').lastChild.scrollIntoView();
118         }
119 }
120
121 function handlePresence(aJSJaCPacket) {
122         if (aJSJaCPacket.getFromJID().toString().split('/')[0] != room)
123                 return
124         roster_list = document.getElementById('roster_list');
125         nick = aJSJaCPacket.getFromJID().getResource();
126         if (aJSJaCPacket.getType() == 'unavailable') {
127                 element = document.getElementById(nick);
128                 roster_list.removeChild(element);
129         } else {
130                 roster_list.innerHTML += '<li id="' + nick + '" onclick="addBubble(' + "'" + nick + "')" + '" >' + nick + '</li>';
131         }
132 }
133
134 function handleIQ(iq) {
135         con.send(iq.errorReply(ERR_FEATURE_NOT_IMPLEMENTED));
136 }
137
138 function handleIqVersion(iq) {
139         con.send(iq.reply([
140                      iq.buildNode('name', 'jsjacChatClient'),
141                      iq.buildNode('version', JSJaC.Version),
142                      iq.buildNode('os', navigator.userAgent)
143                      ]));
144         return true;
145 }
146
147 function handleIqTime(iq) {
148         var now = new Date();
149         con.send(iq.reply([iq.buildNode('display',
150                                   now.toLocaleString()),
151                      iq.buildNode('utc',
152                                   now.jabberDate()),
153                      iq.buildNode('tz',
154                                   now.toLocaleString().substring(now.toLocaleString().lastIndexOf(' ')+1))
155                      ]));
156         return true;
157 }
158
159 function sendMessage(aForm) {
160         if (aForm.text.value) {
161                 message = new JSJaCMessage();
162                 message.setBody(aForm.text.value);
163                 message.setType('groupchat');
164                 message.setTo(room);
165                 con.send(message);
166                 aForm.text.value = '';
167         }
168         return false;
169 }
170
171 function sendChatMessage(aForm, to) {
172         if (aForm.text.value) {
173                 message = new JSJaCMessage();
174                 message.setBody(aForm.text.value);
175                 message.setType('chat');
176                 message.setTo(room + '/' + to);
177                 con.send(message);
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 += message.getBody().htmlEnc() + '</div>';
185                 document.getElementById(to + 'BubbleChat').innerHTML += html;
186                 document.getElementById(to + 'BubbleChat').lastChild.scrollIntoView();
187
188         }
189         return false;
190 }
191
192 function randomString() {
193         var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
194         var string_length = 20;
195         var randomstring = '';
196         for (var i=0; i<string_length; i++) {
197                 var rnum = Math.floor(Math.random() * chars.length);
198                 randomstring += chars.substring(rnum,rnum+1);
199         }
200         return randomstring;
201 }
202
203 var dragElement = null;
204 var mouseX = 0;
205 var mouseY = 0;
206 var offX = 0;
207 var offY = 0;
208
209 function startDrag(element) {
210         dragElement = element;
211         offX = mouseX - dragElement.offsetLeft;
212         offY = mouseY - dragElement.offsetTop;
213 }
214
215 function doDrag(eve) {
216         mouseX = eve.pageX;
217         mouseY = eve.pageY;
218
219         if (dragElement) {
220                 dragElement.style.left = (mouseX - offX) + 'px';
221                 dragElement.style.top = (mouseY - offY) + 'px';
222         }
223 }
224
225 function stopDrag(eve) {
226         dragElement = null;
227 }
228
229 onunload = function() {
230         if (con.connected())
231                 con.disconnect();
232 }
233
234 onmousemove = doDrag;
235
236 onmouseup = stopDrag;