summaryrefslogtreecommitdiff
path: root/client/main.js
blob: acd39f8bd3fdc4586692a0967fbc23b9dfbd7e77 (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
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
require('@electron/remote/main').initialize();

const { app, BrowserWindow, globalShortcut, ipcMain, dialog, MenuItem, Menu, desktopCapturer, clipboard } = require('electron');
const path = require('path');
const os = require("os");
const {writeFileSync, existsSync, unlinkSync} = require("fs");
const fs = require("fs");

let localchatDataRoot = (os.platform() === "win32" ? os.homedir() + "/AppData/Roaming" : (os.platform() === "darwin" ? os.homedir() + "/Library/Application Support" : os.homedir())) + (os.platform() === "darwin" ? "/Chatroom" : "/.chatroom");

if (!global._localchatLauncherVersion) {
    dialog.showErrorBox("Update required", "Please update to Localchat Client Launcher version 1.4.0 or newer to continue using Localchat, you are currently running version " + app.getVersion() + ". You can get a copy of the updated launcher from your local administrator.");
    process.exit();
}

if (!fs.existsSync(localchatDataRoot)) fs.mkdirSync(localchatDataRoot);
if (!fs.existsSync(localchatDataRoot + "/client")) fs.mkdirSync(localchatDataRoot + "/client");
if (!fs.existsSync(localchatDataRoot + "/client/session")) fs.mkdirSync(localchatDataRoot + "/client/session");
if (!fs.existsSync(localchatDataRoot + "/client/data")) fs.mkdirSync(localchatDataRoot + "/client/data");
if (!fs.existsSync(localchatDataRoot + "/client/logs")) fs.mkdirSync(localchatDataRoot + "/client/logs");

app.setPath("userData", localchatDataRoot + "/client/data");
app.setPath("sessionData", localchatDataRoot + "/client/session");
app.setAppLogsPath(localchatDataRoot + "/client/logs");

if (require('os').platform() !== "darwin" && require('os').platform() !== "win32" && require('os').platform() !== "linux") return;
global.windows = [];

const createWindow = () => {
    app.setPath("userData", localchatDataRoot + "/client");
    app.setPath("sessionData", localchatDataRoot + "/client/session");
    app.setAppLogsPath(localchatDataRoot + "/client/logs");

    global.mainWindow = new BrowserWindow({
        width: 500,
        minWidth: 500,
        height: 800,
        minHeight: 800,
        icon: require('os').platform() === "darwin" ? "./icon.icns" : (require('os').platform() === "linux" ? "./icon.png" : "./icon.ico"),
        disableAutoHideCursor: true,
        backgroundColor: "#000000",
        darkTheme: true,
        titleBarStyle: "hidden",
        show: false,
        fullscreenable: false,
        vibrancy: "menu",
        frame: require('os').platform() === "linux",
        titleBarOverlay: {
            color: "#191c1c",
            symbolColor: "#e0e3e2",
            height: 34
        },
        trafficLightPosition: {
            x: 13,
            y: 10
        },
        autoHideMenuBar: true,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            additionalArguments: "--user-data-dir=\"" + localchatDataRoot + "/client" + "\""
        }
    });

    mainWindow.loadFile(global._localchatPath + "/index.html");
    windows.push(mainWindow);
    if (os.platform() === "win32") mainWindow.setContentProtection(true);

    ipcMain.on('screenSharing', (event, url, id) => {
        let screenSharingWindow = new BrowserWindow({
            width: 1280,
            minWidth: 480,
            height: 720,
            minHeight: 640,
            icon: require('os').platform() === "darwin" ? "./icon.icns" : (require('os').platform() === "linux" ? "./icon.png" : "./icon.ico"),
            disableAutoHideCursor: true,
            backgroundColor: "#000000",
            darkTheme: true,
            autoHideMenuBar: true,
            webPreferences: {
                nodeIntegration: true,
                contextIsolation: false,
                additionalArguments: "--user-data-dir=\"" + localchatDataRoot + "/client" + "\""
            }
        });

        windows.push(screenSharingWindow);
        screenSharingWindow.loadURL("file://" + encodeURI(global._localchatPath.replaceAll("\\", "/")) + "/screen.html?" + url + "#" + id);
        if (os.platform() === "win32") screenSharingWindow.setContentProtection(true);
    });

    mainWindow.send("path", app.getPath("userData"));
    mainWindow.send("launcher", global._localchatLauncherVersion);

    function createMenu(items) {
        let menuItems = [];

        for (let item of items) {
            menuItems.push(new MenuItem({
                type: item.type ?? "normal",
                label: item.label ?? "",
                enabled: item.enabled ?? true,
                click: item.submenu ? null : () => {
                    mainWindow.webContents.executeJavaScript(item.script ?? "");
                },
                submenu: item.submenu ? createMenu(item.submenu) : null
            }));
        }

        return Menu.buildFromTemplate(menuItems);
    }

    ipcMain.on('menu', (event, items) => {
        let menu = createMenu(items);
        menu.popup(mainWindow);
    });

    ipcMain.handle('screenshot', async (event) => {
        let screens = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 1920, height: 1080 } });

        if (screens[0]) {
            return screens[0].thumbnail.toJPEG(80).toString("base64");
        } else {
            return null;
        }
    });

    ipcMain.handle('screenshotRaw', async (_, id) => {
        let screens = await desktopCapturer.getSources({ types: ['screen', 'window'], thumbnailSize: { width: 1280, height: 720 } });
        let screen = screens.filter(i => i.id === id)[0];

        if (screen) {
            return screen.thumbnail.toJPEG(80);
        } else {
            return null;
        }
    });

    ipcMain.handle('sources', async (event) => {
        return await desktopCapturer.getSources({
            types: ['screen', 'window'],
            thumbnailSize: {width: 0, height: 0}
        });
    });

    ipcMain.handle('clipboard', async (event) => {
        if (clipboard.readText().trim() !== "") {
            return {
                type: "text",
                content: clipboard.readText()
            }
        } else if (clipboard.readImage().toJPEG(80).toString("base64") !== "") {
            return {
                type: "image",
                content: clipboard.readImage().toJPEG(80).toString("base64")
            }
        } else {
            return {
                type: "html",
                content: clipboard.readHTML()
            }
        }
    });

    ipcMain.handle("open-server", async (event) => {
        let select = dialog.showOpenDialogSync({
            title: "Open a .lctsc file to connect to a server",
            message: "Open a .lctsc file to connect to a server",
            defaultPath: os.homedir(),
            buttonLabel: "Connect",
            filters: [
                {
                    name: "Localchat 2.x Server Configuration",
                    extensions: [ "lctsc" ]
                }
            ],
            properties: [
                "openFile"
            ]
        });

        return select;
    });

    ipcMain.on('devmode', () => {
        mainWindow.openDevTools();
    });

    ipcMain.on('restart', () => {
        console.log("Restart requested");
        mainWindow.close();
        restart();
    });

    ipcMain.on('boop', () => {
        if (!mainWindow.isFocused()) {
            if (os.platform() === "win32" || os.platform() === "linux") {
                mainWindow.setProgressBar(1, {
                    mode: "paused"
                });
            } else {
                mainWindow.flashFrame(true);
            }

            mainWindow.once("focus", () => {
                if (os.platform() === "win32" || os.platform() === "linux") {
                    mainWindow.setProgressBar(-1);
                } else {
                    mainWindow.flashFrame(false);
                }
            })
        }
    });

    ipcMain.on('ready', () => {
        mainWindow.setResizable(false);
        mainWindow.setMaximizable(false);
        mainWindow.setSize(500, 800);

        mainWindow.show();
        try { loaderWindow.close(); } catch (e) {}

        setTimeout(() => {
            mainWindow.setTitle("");

            setTimeout(() => {
                mainWindow.setTitle("");
                mainWindow.setTitle("Localchat");
            }, 1000);
        }, 1000);
    });

    ipcMain.on('past-oobe', () => {
        mainWindow.setResizable(true);
        mainWindow.setClosable(true);
        mainWindow.setMaximizable(true);
    });
}

app.whenReady().then(() => {
    globalShortcut.register('Alt+CommandOrControl+C', () => {
        for (let window of windows) {
            try {
                if (!window.isVisible()) {
                    window.show();
                    if (process.platform === "darwin") app.dock.show();
                } else {
                    window.hide();
                    if (process.platform === "darwin") app.dock.hide();
                }
            } catch (e) {}
        }
    });

    createWindow();

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow();
    });
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit();
});