repl.it
@eliyah/

chatti Backup Server

Nodejs

No description

fork
loading
Files
  • index.js
  • cookies.json
  • msgs.json
  • package-lock.json
index.js
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
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static('public'));

app.get('/', (req, res) => {
	res.send('Hello Express app');
});

app.listen(3000, () => console.log('server started'));

const WebSocket = require('ws');
const fs = require("fs");

const ws = new WebSocket("ws://connect.websocket.in/chatii?room_id=1");

var save = () => {
  fs.writeFile("msgs.json", JSON.stringify(msgs), () => {});
  fs.writeFile("cookies.json", JSON.stringify(cookies), () => {});
};

ws.onopen = () => {
  console.log("OPEN");
}
var reload = () => {
  ws.send(JSON.stringify({type:"Delete",msgs:msgs}));
}
var cookies = JSON.parse(fs.readFileSync("cookies.json"));
var msgs = JSON.parse(fs.readFileSync("msgs.json"));
ws.onmessage = message => {
  console.log(message.data);
  var data = JSON.parse(message.data);
  if (data.type == "SendMessage") {
    if (data.raw.startsWith("/")) {
      if (data.raw.startsWith("/msg")) {
        data.raw = data.raw.split(" ");
        data.raw.shift();
        var toa = data.raw.shift();
        data.raw = data.raw.join(" ");
        msgs.push(JSON.stringify({type:"PrivateMessage", raw:data.raw,user:data.user,to:toa,reports:[]}));
        console.log(msgs);
      }
      if (data.raw.startsWith("/nick")) {
        data.raw = data.raw.split(" ");
        data.raw.shift();
        var toa = data.raw.shift();
        data.raw = data.raw.join(" ");
        ws.send(JSON.stringify({type:"nick",user:data.user,nick:toa,reports:[]}));
      }
      if (data.raw.startsWith("/unnick")) {
        ws.send(JSON.stringify({type:"unnick",user:data.user,reports:[]}));
      }
      if (data.raw.startsWith("/colors")) {
        msgs.push(JSON.stringify({type:"PrivateMessage", raw:"&0&&00 &1&&11 &2&&22 &3&&33 &4&&44 &5&&55 &6&&66 &7&&77 &8&&88 &9&&99 &a&&aa &b&&bb &c&&cc &d&&dd &e&&ee &f&&ff&r.",user:"Console",to:data.user,reports:[]}));
      }
      if (data.raw.startsWith("/help")) {
        msgs.push(JSON.stringify({type:"PrivateMessage", raw:"/nick /unnick /msg /help /colors",user:"Console",to:data.user,reports:[]}));
      }
    } else {
      msgs.push(JSON.stringify({type:"Message", message:data.message,raw:data.raw,user:data.user,reports:[]}));
    }
    reload();
  }
  if (data.type == "GetRefresh") {
    reload();
  }
  if (data.type == "Remove") {
    msgs.splice(data.id,1);
    reload();
  }
  if (data.type == "RemoveCookie") {
    ws.send(JSON.stringify({type:"kick",name:cookies[data.id].name}));
    console.log(cookies);
    cookies.splice(data.id,1);
    console.log(cookies);
    ws.send(JSON.stringify({type:"DeleteCookies",cookies:cookies}));
  }
  if (data.type == "signup") {
    console.log(data.cookie);
    cookies.push({cookie:data.id, name:data.name});
    msgs.push(JSON.stringify({type:"Message", message:data.name+" &1Have just signup",raw:data.name+" Have just signup",user:"Console",reports:[]}));
    reload();
  }
  if (data.type == "kickCookie") {
    ws.send(JSON.stringify({type:"kick",name:cookies[data.id].name}));
  }
  if (data.type == "ban") {
    cookies[data.id].banned = true;
    ws.send(JSON.stringify({type:"DeleteCookies",cookies:cookies}));
  }
  if (data.type == "unban") {
    cookies[data.id].banned = false;
    ws.send(JSON.stringify({type:"DeleteCookies",cookies:cookies}));
  }
  if (data.type == "cookies") {
    ws.send(JSON.stringify({type:"DeleteCookies",cookies:cookies}));
  }
  if (data.type == "report") {
    if (msgs.length > data.id) {
      var msg = JSON.parse(msgs[data.id]);
      if (msg.reports.indexOf(data.user) < 0) {
        msg.reports.push(data.user);
      }
      msgs[data.id] = JSON.stringify(msg);
    }
    reload();
  }
  save();
};
var replace = (text, a, b) => {
  return text.split(a).join(b);
};
ws.close = () => {
  process.exit(0);
};
node v10.16.0