summaryrefslogtreecommitdiff
path: root/src/bootstrap.js
blob: b01697fd5c8a464e0936dc89d6c176d9c006c615 (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
const PORT = 26001;

if (typeof EIB === "undefined")
    var EIB = {
        headers: {},
        pending: []
    };

const Cc = Components.classes, Ci = Components.interfaces, Cu = Components.utils, CC = Components.Constructor, Cr = Components.results;
const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init");

Cu.import("resource://gre/modules/Services.jsm");

function install() {}
function uninstall() {}

EIB.listen = function () {
    this.serverSocket = Cc["@mozilla.org/network/server-socket;1"]
                       .createInstance(Ci.nsIServerSocket);
    this.serverSocket.init(PORT, true, -1);
    const tm = Cc["@mozilla.org/thread-manager;1"].getService();
    this.serverSocket.asyncListen({
        onSocketAccepted: function (socket, transport) {
            var is = transport.openInputStream(0, 0, 0);
            var os = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
            var sis = new ScriptableInputStream(is);
            var buf = "";
            // TODO: run on separate thread
            is.asyncWait({
                onInputStreamReady: function () {
                    while (sis.available()) {
                        buf += sis.read(2048);
                    }
                    if (buf.indexOf("\n\n") !== 2) {
                        var headers = {};
                        var rx = /\n(EVE_[^:]*): (.*)$/gm, arr;
                        while ((arr = rx.exec(buf)) !== null) {
                            headers[arr[1]] = arr[2];
                        }
                        EIB.headers = headers;

                        var resp = "HTTP/1.0 200 OK\r\n";
                        resp += "Content-Type: text/html\r\n\r\n";
                        resp += "<!DOCTYPE html><html><head><title>EVE-IGB Bridge</title>";
                        if (headers["EVE_TRUSTED"]) {
                            resp += "<script>";
                            if (headers["EVE_TRUSTED"] == "Yes") {
                                resp += EIB.pending.join("\r\n");
                                EIB.pending = [];
                                resp += 'setTimeout(function () {';
                                resp += '    window.location.reload();';
                                resp += '}, 100);';
                            } else {
                                resp += 'CCPEVE.requestTrust("http://127.0.0.1:' + EIB.serverSocket.port + '");';
                            }
                            resp += "</script></head><body>";
                            resp += headers["EVE_TRUSTED"] == "Yes" ?
                                    "Connected to the EVE-IGB Bridge. Please do not close this window. You may minimize it, however. Note that this window will refresh repeatedly; this does not indicate a malfunction and will not count towards your internet data usage if you have one." :
                                    "Please grant trust to this site, then refresh the page.";
                            resp += "</body></html>";
                        } else {
                            resp += "</head><body>Please open this page in the EVE in-game browser.</body></html>";
                        }
                        os.write(resp, resp.length);

                        is.close();
                        os.close();
                    }
                }
            }, 0, 0, tm.mainThread);
        }
    });
}

var HttpObserver = {
    observe: function (subject, topic, data) {
        var channel = subject.QueryInterface(Ci.nsIHttpChannel);
        // TODO: is this secure?
        if (checkTrusted(channel.URI.specIgnoringRef)) {
            for (var header in EIB.headers) {
                channel.setRequestHeader(header, EIB.headers[header], false);
            }
        }
    }
};

function forEachOpenWindow(todo) {
    var windows = Services.wm.getEnumerator("navigator:browser");
    while (windows.hasMoreElements())
        todo(windows.getNext().QueryInterface(Ci.nsIDOMWindow));
}

var WindowListener = {
    onOpenWindow: function(xulWindow)
    {
        var window = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                              .getInterface(Ci.nsIDOMWindow);
        function onWindowLoad()
        {
            window.removeEventListener("load", onWindowLoad);
            if (window.document.documentElement.getAttribute("windowtype") == "navigator:browser")
                loadIntoWindow(window);
        }
        window.addEventListener("load", onWindowLoad);
    },
};

var TrustedReparser = {
    observe: function () {
        if (!EIB.prefs.prefHasUserValue("trusted")) {
            EIB.prefs.setCharPref("trusted", "[]");
        }
        EIB.trusted = JSON.parse(EIB.prefs.getCharPref("trusted"));
    }
};

function startup(data, reason) {
    EIB.listen();

    EIB.prefs = Services.prefs.getBranch("extensions.eib.");
    EIB.prefs.addObserver("trusted", TrustedReparser, false);
    TrustedReparser.observe();

    forEachOpenWindow(loadIntoWindow);
    Services.wm.addListener(WindowListener);

    Services.obs.addObserver(HttpObserver, "http-on-modify-request", false);
}

function shutdown(data, reason) {
    if (reason === APP_SHUTDOWN)
        return;

    if (EIB.serverSocket)
        EIB.serverSocket.close();
    delete EIB.serverSocket;

    EIB.prefs.removeObserver("trusted", TrustedReparser);

    forEachOpenWindow(unloadFromWindow);
    Services.wm.removeListener(WindowListener);

    Services.obs.removeObserver(HttpObserver, "http-on-modify-request");
}

function checkTrusted(href) {
    for (var i = 0; i < EIB.trusted.length; i++)
        if (href.indexOf(EIB.trusted[i]) === 0)
            return true;
    return false;
}

function injectCCPEVE(e) {
    var window = e.originalTarget.defaultView;
    if (checkTrusted(window.location.href)) {
        var CCPEVE = Cu.createObjectIn(window, {defineAs: "CCPEVE"});
        ["openEveMail", "showInfo", "showPreview", "showRouteTo", "showMap", "showFitting", "showContract", "showMarketDetails", "setDestination", "addWaypoint", "joinChannel", "joinMailingList", "createContract", "buyType", "findInContracts", "addToMarketQuickBar", "addContact", "removeContact", "addCorpContact", "removeCorpContact", "block", "addBounty", "inviteToFleet", "startConversation", "showContracts", "showOnMap", "editMember", "awardDecoration", "sendMail", "showContents", "bookmark"].forEach(function (n) {
            Object.defineProperty(CCPEVE, n, {
                value: Cu.exportFunction(function () {
                    EIB.pending.push('CCPEVE.' + n + '(' + Array.prototype.join.call(arguments, ',') + ');');
                    return null;
                }, CCPEVE)
            });
        });
    }
}

function loadIntoWindow(window) {
    window.document.getElementById("appcontent").addEventListener("DOMContentLoaded", injectCCPEVE, false);
}

function unloadFromWindow(window) {
    window.document.getElementById("appcontent").removeEventListener("DOMContentLoaded", injectCCPEVE, false);
}