-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgab.js
444 lines (361 loc) · 13.7 KB
/
gab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
var Gab = {
connection: null,
jid_to_id: function (jid) {
return Strophe.getBareJidFromJid(jid)
.replace(/@/g, "-")
.replace(/\./g, "-");
},
on_roster: function (iq) {
$(iq).find('item').each(function () {
var jid = $(this).attr('jid');
var name = $(this).attr('name') || jid;
// transform jid into an id
var jid_id = Gab.jid_to_id(jid);
var contact = $("<li id='" + jid_id + "'>" +
"<div class='roster-contact offline'>" +
"<div class='roster-name'>" +
name +
"</div><div class='roster-jid'>" +
jid +
"</div></div></li>");
Gab.insert_contact(contact);
});
// set up presence handler and send initial presence
Gab.connection.addHandler(Gab.on_presence, null, "presence");
Gab.connection.send($pres());
},
pending_subscriber: null,
on_presence: function (presence) {
var ptype = $(presence).attr('type');
var from = $(presence).attr('from');
var jid_id = Gab.jid_to_id(from);
if (ptype === 'subscribe') {
// populate pending_subscriber, the approve-jid span, and
// open the dialog
Gab.pending_subscriber = from;
$('#approve-jid').text(Strophe.getBareJidFromJid(from));
$('#approve_dialog').dialog('open');
} else if (ptype !== 'error') {
var contact = $('#roster-area li#' + jid_id + ' .roster-contact')
.removeClass("online")
.removeClass("away")
.removeClass("offline");
if (ptype === 'unavailable') {
contact.addClass("offline");
} else {
var show = $(presence).find("show").text();
if (show === "" || show === "chat") {
contact.addClass("online");
} else {
contact.addClass("away");
}
}
var li = contact.parent();
li.remove();
Gab.insert_contact(li);
}
// reset addressing for user since their presence changed
var jid_id = Gab.jid_to_id(from);
$('#chat-' + jid_id).data('jid', Strophe.getBareJidFromJid(from));
return true;
},
on_roster_changed: function (iq) {
$(iq).find('item').each(function () {
var sub = $(this).attr('subscription');
var jid = $(this).attr('jid');
var name = $(this).attr('name') || jid;
var jid_id = Gab.jid_to_id(jid);
if (sub === 'remove') {
// contact is being removed
$('#' + jid_id).remove();
} else {
// contact is being added or modified
var contact_html = "<li id='" + jid_id + "'>" +
"<div class='" +
($('#' + jid_id).attr('class') || "roster-contact offline") +
"'>" +
"<div class='roster-name'>" +
name +
"</div><div class='roster-jid'>" +
jid +
"</div></div></li>";
if ($('#' + jid_id).length > 0) {
$('#' + jid_id).replaceWith(contact_html);
} else {
Gab.insert_contact($(contact_html));
}
}
});
return true;
},
on_message: function (message) {
var full_jid = $(message).attr('from');
var jid = Strophe.getBareJidFromJid(full_jid);
var jid_id = Gab.jid_to_id(jid);
if ($('#chat-' + jid_id).length === 0) {
$('#chat-area').tabs('add', '#chat-' + jid_id, jid);
$('#chat-' + jid_id).append(
"<div class='chat-messages'></div>" +
"<input type='text' class='chat-input'>");
}
$('#chat-' + jid_id).data('jid', full_jid);
$('#chat-area').tabs('select', '#chat-' + jid_id);
$('#chat-' + jid_id + ' input').focus();
var composing = $(message).find('composing');
if (composing.length > 0) {
$('#chat-' + jid_id + ' .chat-messages').append(
"<div class='chat-event'>" +
Strophe.getNodeFromJid(jid) +
" is typing...</div>");
Gab.scroll_chat(jid_id);
}
var body = $(message).find("html > body");
if (body.length === 0) {
body = $(message).find('body');
if (body.length > 0) {
body = body.text()
} else {
body = null;
}
} else {
body = body.contents();
var span = $("<span></span>");
body.each(function () {
if (document.importNode) {
$(document.importNode(this, true)).appendTo(span);
} else {
// IE workaround
span.append(this.xml);
}
});
body = span;
}
if (body) {
// remove notifications since user is now active
$('#chat-' + jid_id + ' .chat-event').remove();
// add the new message
$('#chat-' + jid_id + ' .chat-messages').append(
"<div class='chat-message'>" +
"<<span class='chat-name'>" +
Strophe.getNodeFromJid(jid) +
"</span>><span class='chat-text'>" +
"</span></div>");
$('#chat-' + jid_id + ' .chat-message:last .chat-text')
.append(body);
Gab.scroll_chat(jid_id);
}
return true;
},
scroll_chat: function (jid_id) {
var div = $('#chat-' + jid_id + ' .chat-messages').get(0);
div.scrollTop = div.scrollHeight;
},
presence_value: function (elem) {
if (elem.hasClass('online')) {
return 2;
} else if (elem.hasClass('away')) {
return 1;
}
return 0;
},
insert_contact: function (elem) {
var jid = elem.find('.roster-jid').text();
var pres = Gab.presence_value(elem.find('.roster-contact'));
var contacts = $('#roster-area li');
if (contacts.length > 0) {
var inserted = false;
contacts.each(function () {
var cmp_pres = Gab.presence_value(
$(this).find('.roster-contact'));
var cmp_jid = $(this).find('.roster-jid').text();
if (pres > cmp_pres) {
$(this).before(elem);
inserted = true;
return false;
} else if (pres === cmp_pres) {
if (jid < cmp_jid) {
$(this).before(elem);
inserted = true;
return false;
}
}
});
if (!inserted) {
$('#roster-area ul').append(elem);
}
} else {
$('#roster-area ul').append(elem);
}
}
};
$(document).ready(function () {
$('#login_dialog').dialog({
autoOpen: true,
draggable: false,
modal: true,
title: 'Connect to XMPP',
buttons: {
"Connect": function () {
$(document).trigger('connect', {
jid: $('#jid').val().toLowerCase(),
password: $('#password').val()
});
$('#password').val('');
$(this).dialog('close');
}
}
});
$('#contact_dialog').dialog({
autoOpen: false,
draggable: false,
modal: true,
title: 'Add a Contact',
buttons: {
"Add": function () {
$(document).trigger('contact_added', {
jid: $('#contact-jid').val().toLowerCase(),
name: $('#contact-name').val()
});
$('#contact-jid').val('');
$('#contact-name').val('');
$(this).dialog('close');
}
}
});
$('#new-contact').click(function (ev) {
$('#contact_dialog').dialog('open');
});
$('#approve_dialog').dialog({
autoOpen: false,
draggable: false,
modal: true,
title: 'Subscription Request',
buttons: {
"Deny": function () {
Gab.connection.send($pres({
to: Gab.pending_subscriber,
"type": "unsubscribed"}));
Gab.pending_subscriber = null;
$(this).dialog('close');
},
"Approve": function () {
Gab.connection.send($pres({
to: Gab.pending_subscriber,
"type": "subscribed"}));
Gab.connection.send($pres({
to: Gab.pending_subscriber,
"type": "subscribe"}));
Gab.pending_subscriber = null;
$(this).dialog('close');
}
}
});
$('#chat-area').tabs().find('.ui-tabs-nav').sortable({axis: 'x'});
$('.roster-contact').live('click', function () {
var jid = $(this).find(".roster-jid").text();
var name = $(this).find(".roster-name").text();
var jid_id = Gab.jid_to_id(jid);
if ($('#chat-' + jid_id).length === 0) {
$('#chat-area').tabs('add', '#chat-' + jid_id, name);
$('#chat-' + jid_id).append(
"<div class='chat-messages'></div>" +
"<input type='text' class='chat-input'>");
$('#chat-' + jid_id).data('jid', jid);
}
$('#chat-area').tabs('select', '#chat-' + jid_id);
$('#chat-' + jid_id + ' input').focus();
});
$('.chat-input').live('keypress', function (ev) {
var jid = $(this).parent().data('jid');
if (ev.which === 13) {
ev.preventDefault();
var body = $(this).val();
var message = $msg({to: jid,
"type": "chat"})
.c('body').t(body).up()
.c('active', {xmlns: "http://jabber.org/protocol/chatstates"});
Gab.connection.send(message);
$(this).parent().find('.chat-messages').append(
"<div class='chat-message'><" +
"<span class='chat-name me'>" +
Strophe.getNodeFromJid(Gab.connection.jid) +
"</span>><span class='chat-text'>" +
body +
"</span></div>");
Gab.scroll_chat(Gab.jid_to_id(jid));
$(this).val('');
$(this).parent().data('composing', false);
} else {
var composing = $(this).parent().data('composing');
if (!composing) {
var notify = $msg({to: jid, "type": "chat"})
.c('composing', {xmlns: "http://jabber.org/protocol/chatstates"});
Gab.connection.send(notify);
$(this).parent().data('composing', true);
}
}
});
$('#disconnect').click(function () {
Gab.connection.disconnect();
Gab.connection = null;
});
$('#chat_dialog').dialog({
autoOpen: false,
draggable: false,
modal: true,
title: 'Start a Chat',
buttons: {
"Start": function () {
var jid = $('#chat-jid').val().toLowerCase();
var jid_id = Gab.jid_to_id(jid);
$('#chat-area').tabs('add', '#chat-' + jid_id, jid);
$('#chat-' + jid_id).append(
"<div class='chat-messages'></div>" +
"<input type='text' class='chat-input'>");
$('#chat-' + jid_id).data('jid', jid);
$('#chat-area').tabs('select', '#chat-' + jid_id);
$('#chat-' + jid_id + ' input').focus();
$('#chat-jid').val('');
$(this).dialog('close');
}
}
});
$('#new-chat').click(function () {
$('#chat_dialog').dialog('open');
});
});
$(document).bind('connect', function (ev, data) {
var conn = new Strophe.Connection(
'http://bosh.metajack.im:5280/xmpp-httpbind');
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
}
});
Gab.connection = conn;
});
$(document).bind('connected', function () {
var iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
Gab.connection.sendIQ(iq, Gab.on_roster);
Gab.connection.addHandler(Gab.on_roster_changed,
"jabber:iq:roster", "iq", "set");
Gab.connection.addHandler(Gab.on_message,
null, "message", "chat");
});
$(document).bind('disconnected', function () {
Gab.connection = null;
Gab.pending_subscriber = null;
$('#roster-area ul').empty();
$('#chat-area ul').empty();
$('#chat-area div').remove();
$('#login_dialog').dialog('open');
});
$(document).bind('contact_added', function (ev, data) {
var iq = $iq({type: "set"}).c("query", {xmlns: "jabber:iq:roster"})
.c("item", data);
Gab.connection.sendIQ(iq);
var subscribe = $pres({to: data.jid, "type": "subscribe"});
Gab.connection.send(subscribe);
});