summaryrefslogtreecommitdiffstats
path: root/comm/suite/chatzilla/js/lib/ident.js
blob: 5110d70165d09cb20eae03fc9281cd1cb3ea74f8 (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
202
203
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// RFC1413-ish identification server for ChatZilla
// One Ident Server is used for all networks. When multiple networks are in the
// process of connecting, it won't stop listening until they're all done.

function IdentServer(parent)
{
    this.responses = new Array();
    this.listening = false;
    this.dns = getService("@mozilla.org/network/dns-service;1","nsIDNSService");

    this.parent = parent;
    this.eventPump = parent.eventPump;
}

IdentServer.prototype.start =
function ident_start()
{
    if (this.listening)
        return true;

    if (!this.socket)
        this.socket = new CBSConnection();

    if (!this.socket.listen(113, this))
        return false;

    this.listening = true;
    return true;
}

IdentServer.prototype.stop =
function ident_stop()
{
    if (!this.socket || !this.listening)
        return;

    // No need to destroy socket, listen() will work again.
    this.socket.close();
    this.listening = false;
}

IdentServer.prototype.addNetwork =
function ident_add(net, serv)
{
    var addr, dnsRecord = this.dns.resolve(serv.hostname, 0);

    while (dnsRecord.hasMore())
    {
        addr = dnsRecord.getNextAddrAsString();
        this.responses.push({net: net, ip: addr, port: serv.port,
                             username: net.INITIAL_NAME || net.INITIAL_NICK});
    }

    if (this.responses.length == 0)
        return false;

    // Start the ident server if necessary.
    if (!this.listening)
        return this.start();

    return true;
}

IdentServer.prototype.removeNetwork =
function ident_remove(net)
{
    var newResponses = new Array();

    for (var i = 0; i < this.responses.length; ++i)
    {
        if (this.responses[i].net != net)
            newResponses.push(this.responses[i]);
    }
    this.responses = newResponses;

    // Only stop listening if responses is empty - no networks need us anymore.
    if (this.responses.length == 0)
        this.stop();
}

IdentServer.prototype.onSocketAccepted =
function ident_gotconn(serv, transport)
{
    // Using the listening CBSConnection to accept would stop it listening.
    // A new CBSConnection does exactly what we want.
    var connection = new CBSConnection();
    connection.accept(transport);

    connection.startAsyncRead(new IdentListener(this, connection));
}

function IdentListener(server, connection)
{
    this.server = server;
    this.connection = connection;
}

IdentListener.prototype.onStreamDataAvailable =
function ident_listener_sda(request, inStream, sourceOffset, count)
{
    var ev = new CEvent("ident-listener", "data-available", this,
                        "onDataAvailable");
    ev.line = this.connection.readData(0, count);
    this.server.eventPump.routeEvent(ev);
}

IdentListener.prototype.onStreamClose =
function ident_listener_sclose(status)
{
}

IdentListener.prototype.onDataAvailable =
function ident_listener_dataavailable(e)
{
    var incomplete = (e.line.substr(-2) != "\r\n");
    var lines = e.line.split(/\r\n/);

    if (this.savedLine)
    {
        lines[0] = this.savedLine + lines[0];
        this.savedLine = "";
    }

    if (incomplete)
        this.savedLine = lines.pop()

    for (var i in lines)
    {
        var ev = new CEvent("ident-listener", "rawdata", this, "onRawData");
        ev.line = lines[i];
        this.server.eventPump.routeEvent(ev);
    }
}

IdentListener.prototype.onRawData =
function ident_listener_rawdata(e)
{
    var ports = e.line.match(/(\d+) *, *(\d+)/);
    // <port-on-server> , <port-on-client>
    // (where "server" is the ident server)

    if (!ports)
    {
        this.connection.disconnect(); // same meaning as "ERROR : UNKNOWN-ERROR"
        return;
    }

    e.type = "parsedrequest";
    e.destObject = this;
    e.destMethod = "onParsedRequest";
    e.localPort = ports[1];
    e.remotePort = ports[2];
}

IdentListener.prototype.onParsedRequest =
function ident_listener_request(e)
{
    function response(str)
    {
        return e.localPort + " , " + e.remotePort + " : " + str + "\r\n";
    };

    function validPort(p)
    {
        return (p >= 1) && (p <= 65535);
    };

    if (!validPort(e.localPort) || !validPort(e.remotePort))
    {
        this.connection.sendData(response("ERROR : INVALID-PORT"));
        this.connection.disconnect();
        return;
    }

    var found, responses = this.server.responses;
    for (var i = 0; i < responses.length; ++i)
    {
        if ((e.remotePort == responses[i].port) &&
            (this.connection._transport.host == responses[i].ip))
        {
            // charset defaults to US-ASCII
            // anything except an OS username should use OTHER
            // however, ircu sucks, so we can't do that.
            this.connection.sendData(response("USERID : CHATZILLA :" + 
                                              responses[i].username));
            found = true;
            break;
        }
    }

    if (!found)
        this.connection.sendData(response("ERROR : NO-USER"));

    // Spec gives us a choice: drop the connection, or listen for more queries.
    // Since IRC servers will only ever want one name, we disconnect.
    this.connection.disconnect();
}