public
Description: An easy Javascript Client for XMPP Multi-User Chat (XEP-0045).
Homepage:
Clone URL: git://github.com/superfeedr/aristochat.git
aristochat / chat.js
100644 178 lines (155 sloc) 6.263 kb
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
AristoChat = {
connection: null,
inputField: "message_input",
onEnterNewMessageBefore: function(msg) { return msg },
onEnterNewMessageAfter: function(msg) { }
}
 
var connection = AristoChat.connection; //
 
function log(msg) {
  if(DEBUG) $('#log').append('<div></div><br /><br />').append(document.createTextNode(msg)); }
function rawInput(data) { log('RECV: ' + data); }
function rawOutput(data) { log('SENT: ' + data); }
 
function onMessage(msg) {
var from = msg.getAttribute('from');
var type = msg.getAttribute('type');
var body = msg.getElementsByTagName("body")[0].childNodes[0];
var user = Strophe.getResourceFromJid(from);
 
if (type == "groupchat" && body) {
body = body.nodeValue.replace(/(<([^>]+)>)/ig,"");
if(user == AristoChat.nick) {
$('#chat').append("<tr class='message'><td class='me'>"+ user + "</td><td class='body'>" + body +"</td></tr>");
} else {
$('#chat').append("<tr class='message'><td class='nick'>"+ user + "</td><td class='body'>" + body +"</td></tr>");
}
$('html, body').animate({scrollTop: $('.message:last').offset().top }, 5);
}
// we must return true to keep the handler alive.
// returning false would remove it after it finishes.
return true;
}
 
function user_connected(user) {
  $('#' + user).hide("slow").remove();
  $('#users').append("<li id='" + user + "'>"+ user +"</li>")
  $('#chat').append("<tr class='presence'><td class='nick'>"+ user +
"</td><td class='body'>has entered the room</td></tr>");
  $('html, body').animate({scrollTop: $('.message:last').offset().top }, 5);
}
 
function user_disconnected(user) {
  $('#' + user).hide("slow").remove();
  $('#chat').append("<tr class='presence'><td class='nick'>"+ user +
"</td><td class='body'>has left the room</td></tr>");
  $('html, body').animate({scrollTop: $('.message:last').offset().top }, 5);
}
 
function onPresence(msg) {
  var user = Strophe.getResourceFromJid(msg.getAttribute('from'));
  var type = msg.getAttribute('type');
    
  if (type == "unavailable") {
    user_disconnected(user);
  } else if (type == "error") {
onDisconnect();
alert(msg.getElementsByTagName("text")[0].childNodes[0].nodeValue);
  } else {
    user_connected(user);
  }
  // we must return true to keep the handler alive.
  // returning false would remove it after it finishes.
  return true;
}
 
function onConnect(status) {
    if (status == Strophe.Status.CONNECTING) {
        $('#loader').show();
        log('Strophe is connecting.');
        $('#status').text('Connecting...');
    } else if (status == Strophe.Status.DISCONNECTING) {
        $('#loader').show();
        $('#status').text('Disconnecting...');
        log('Strophe is disconnecting.');
    } else if (status == Strophe.Status.AUTHENTICATING) {
        $('#loader').show();
        $('#status').text('Authenticating...');
        log('Strophe is authenticating.');
    } else if (status == Strophe.Status.CONNFAIL) {
        log('Strophe failed to connect.');
        $('#status').text('Connection failed');
        onDisconnect();
        $('#loader').hide();
    } else if (status == Strophe.Status.DISCONNECTED) {
        log('Strophe is disconnected.');
        $('#status').text('Disconnected');
        onDisconnect();
        $('#loader').hide();
    } else if (status == Strophe.Status.AUTHFAIL) {
        $('#status').text('Authentication failed');
        alert("Authentication failed, please retry.");
        onDisconnect();
        $('#loader').hide();
    } else if (status == Strophe.Status.CONNECTED) {
        // Handlers
        connection.addHandler(onMessage, null, 'message', null, null, null);
        connection.addHandler(onPresence, null, 'presence', null, null, null);
        connected();
        log('Strophe is connected.');
        // Entrering the room
        connection.send($pres({to: MUC_ROOM + "@" + MUC_COMPONENT + "/" + AristoChat.nick}).
c("x", {xmlns: "http://jabber.org/protocol/muc"}).tree());
        $('#loader').hide();
    }
}
 
function connected() {
    user_connected(AristoChat.nick);
    $('#status').text('Connected');
    $('#connect').get(0).value = 'disconnect';
    $('#login').hide();
    $('#roster').show()
    $('#connect').removeAttr("disabled"); // To enable
    $('#post').removeAttr("disabled"); // To enable
}
 
function onDisconnect() {
    if(AristoChat.nick != "")
      user_disconnected(AristoChat.nick);
   
    $('#post').attr("disabled", "disabled"); // To disable
    $('#connect').get(0).value = 'connect';
    $('#login').show();
    $('#roster').hide()
    $('#connect').removeAttr("disabled"); // To enable
}
 
 
$(document).ready(function () {
    var last = new Date();
    var items = 0;
    var max_items = 5;
    connection = new Strophe.Connection(BOSH_SERVICE);
    connection.rawInput = rawInput;
    connection.rawOutput = rawOutput;
    
    $('#post').bind('click', sendMessageWithCallbacks);
    
    $('#connect').bind('click', function () {
        var button = $('#connect').get(0);
        if (button.value == 'Connect') {
            $('#connect').attr("disabled", "disabled"); // To disable
            $('#loader').show();
            if($('#nick').get(0).value == "") {
              AristoChat.nick = Strophe.getNodeFromJid($('#jid').get(0).value);
            }
            else {
             AristoChat.nick = $('#nick').get(0).value;
            }
            AristoChat.jid = $('#jid').get(0).value;
            AristoChat.password = $('#pass').get(0).value;
            connection.connect(AristoChat.jid, AristoChat.password, onConnect);
        } else {
            $('#connect').attr("disabled", "disabled"); // To disable
            $('#loader').show();
            connection.disconnect();
        }
    });
});
 
 
function sendMessageWithCallbacks() {
var msg = $('#'+AristoChat.inputField).get(0).value.replace(/(<([^>]+)>)/ig,"");
 
msg = AristoChat.onEnterNewMessageBefore(msg);
connection.send($msg({to: MUC_ROOM + "@" + MUC_COMPONENT,
type: "groupchat", id: connection.getUniqueId
}).c("body").t(msg).up().
c("nick", {xmlns: "http://jabber.org/protocol/nick"}).
t(AristoChat.nick).tree());
  $('#'+AristoChat.inputField).get(0).value = "";
 
AristoChat.onEnterNewMessageAfter(msg);
}