aboutsummaryrefslogtreecommitdiffstats
path: root/webclient/lib/basic.js
blob: eb65ecf25b4c17c6b9c9d35304023be4d588184a (plain)
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
var BOSH_SERVICE = 'http://alias.fr.nf/http-bind';
var server_component = 'object.alias.fr.nf';
var connection = null;

jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

function log(msg, color)
{
    $('#log').append($('<div></div>').css('background-color', color).text(msg));
}

function rawInput(data)
{
    log('RECV: ' + data, '#FBB6B4');
}

function rawOutput(data)
{
    log('SENT: ' + data, '#B5BBFB');
}

function getRoster()
{
    var roster = $iq({type : 'get'}).c('query', {xmlns : Strophe.NS.ROSTER});
    connection.sendIQ(roster, onRoster);
}

function getPresence(contact)
{
    if ( contact.hasClass("online") )
        return 2;
    else if ( contact.hasClass("away") )
        return 1;
    else
        return 0;
}

function jid_to_id(jid) {
    return Strophe.getBareJidFromJid(jid).replace(/[@.]/g,'-');
}

function getJID(contact)
{
    return contact.find('.roster-jid').text();
}

function getHome(contact)
{
    var name = Base64.encode(getJID(contact)) + '@' + server_component;
    var iq = $iq({type : 'get', to : name}).c('query', {xmlns : 'alias:query', type:'content'});
    connection.sendIQ(iq, onHome);
}

function onHome(iq)
{
    var content = $(iq).find('content').text();
    $('#main').html(content);
}

function insertContact(contact)
{
    var presence = getPresence(contact);
    var jid = getJID(contact);
    var contacts = $('#roster li');
    if (contacts.length > 0) {
        var inserted = false;
        contacts.each(function () {
            var locpres = getPresence($(this));
            var locjid = getJID($(this));
            if (presence > locpres)
            {
                $(this).before(contact);
                inserted = true;
                return false;
            }
            else if ( (presence == locpres) && (jid < locjid) )
            {
                $(this).before(contact);
                inserted = true;
                return false;
            }
        });

        if (!inserted) 
            $('#roster ul').append(contact);
    }
    else
        $('#roster ul').append(contact);
    contact.click(function(){getHome($(this));});
}

function onPresence(presence)
{
    var who = $(presence).attr('from');
    var type = $(presence).attr('type');
    if (type !== 'error')
    {
        var contact = $('#'+jid_to_id(who));
        contact.removeClass('online away offline');
        if (type === 'unavailable')
        {
            contact.addClass('offline');
        }
        else
        {
            var show = $(presence).find('show').text();
            if (show === '' || show === '')
            {
                contact.addClass('online');
            }
            else
            {
                contact.addClass('away');
            }
        }
        contact.remove();
        insertContact(contact);
    }
    return true;
}

function onRoster(iq)
{
    $('#roster li').remove();
    var elems = iq.getElementsByTagName('query');
    var query = elems[0];
    Strophe.forEachChild(query, 'item', function(item)
    {
        var jid = item.getAttribute('jid');
        var name = item.getAttribute('name') || jid;
        var id = jid_to_id(jid);
        var contact = $("<li id='" + id + "' class='roster-contact offline'>"
                        + "<div class='roster-name'>" + name + "</div>"
                        + "<div class='roster-jid'>" + jid + "</div></li>");
        insertContact(contact);
    });
    connection.addHandler(onPresence,null,'presence', null, null, null, null);
    connection.send($pres());
    return true;
}

function onConnect(status)
{
    var jid = $('#jid').get(0).value;
    if ( status == Strophe.Status.CONNECTING )
    {
        log('Strophe is connecting.');
    } else if ( status == Strophe.Status.CONNFAIL )
    {
        log('Strophe failed to connect.');
    } else if ( status == Strophe.Status.DISCONNECTING )
    {
        log('Strophe is disconnecting.');
    } else if ( status == Strophe.Status.DISCONNECTED )
    {
        log('Strophe is disconnected.');
    } else if ( status == Strophe.Status.CONNECTED )
    {
        log('Strophe is connected.');
        getRoster();
        $('#password').val('');
        $('#login').dialog('close');
        $('#status').append($('<a href="#" id="connect">Disconnect</a>'));
        $('#connect').click(function(){
            connection.disconnect();
            $('#roster ul').empty();
            $('#login').dialog('open');
            $(this).remove();
         });
    }
}

$(document).ready(function(){
   connection = new Strophe.Connection(BOSH_SERVICE);
   connection.rawInput = rawInput;
   connection.rawOutput = rawOutput;

   $('#login').dialog({
       autoOpen: true,
       modal: true,
       title: 'Connect',
       buttons: { 
           'Connect' : function () {
               connection.connect($('#jid').get(0).value,
                                  $('#pass').get(0).value,
                                  onConnect);}
       }
   });
   
   $('#bottomup').click(function(){
       $(this).next().slideToggle();
   });
   
   $('#rosterfilter').keyup(function() {
       var filter = $(this).val();
       $('#roster ul li div.roster-name:not(:Contains("' + filter + '"))').parent().hide();
       $('#roster ul li div.roster-name:Contains("' + filter + '")').parent().show();
   });
});