deekchat_renachan/client.js

635 lines
14 KiB
JavaScript
Executable File

const ws = require("websocket").client;
const ddg = require("duckduckgo-images-api");
const fetch = require("node-fetch");
const cheerio = require("cheerio");
const formdata = require("form-data");
const name = "rena_chan";
const password = "renachanlovesyou";
const client = new ws();
// get token
async function login(){
console.log("logging in...")
// login and get token
const form = new formdata();
form.append("name", name);
form.append("password", password);
form.append("submit", "log+in");
var login = await fetch(
"https://deekchat.ml/login/submit",
{
method: "POST",
redirect: 'manual',
body: form,
headers: {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
Origin: "https://deekchat.ml",
DNT: 1
}
}
)
cookie = await login.headers.get("set-cookie");
cookie = cookie.match(/(?:api_token|session_id)=[^;]+/g);
cookie = cookie[0] + "; " + cookie[1];
return {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
Origin: "https://deekchat.ml",
DNT: 1,
Cookie: cookie
};
}
function report_error(e){
console.log("error: " + e.toString());
}
client.on("connectFailed", function(e){
report_error(e);
});
client.on("connect", function(conn){
// functions
function send(msg){
conn.sendUTF(JSON.stringify({
type: "message",
data: {
message: msg
}
}));
}
async function send_img(msg, data){
var form_data = new formdata();
form_data.append("files[]", data, {
contentType: 'image/png',
name: 'file',
filename: 'file.png'
});
fetch("https://deekchat.ml/message/send", {
method: 'POST',
body: form_data,
headers: {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
Origin: "https://deekchat.ml",
DNT: 1,
"Content-Type": form_data.getHeaders()["content-type"],
Cookie: cookie
}
})
.then(async res_post => {
var response = await res_post.json();
response.message.text = msg;
conn.sendUTF(JSON.stringify({
type: "files",
data: {
message: response.message,
}
}));
});
}
console.log("Connected");
var link = "";
var ratelimitbuffer = [];
var mail = [];
conn.on("error", function(e){ report_error(e); });
conn.on("close", function(e){ console.log("Disconnected!"); connect(); });
var results_cache = [];
conn.on("message", function(msg){
// {"data":{"message":{"id":160316,"text":"Shit link don&#39;t click, I got this error accessing this shit!!!\nFetchError: request to <a class='chatLink' rel='noreferrer noopener' target='_blank' href='http://www.youtub.../'>http://www.youtub.../</a> failed, reason: getaddrinfo ENOTFOUND www.youtub... www.youtub...:80","files":null,"mentions":[],"userId":3572,"name":"rena_chan"}},"type":"message","userId":3572}
if(msg.type != "utf8"){ return; }
msg = JSON.parse(msg.utf8Data);
/*
Mail
*/
if(msg.type == "enter"){
var username = msg.name;
if(typeof mail[username] != "undefined"){
var msg = ">Whats up " + username + "!! You have unread mail:\n";
for(i=0; i<mail[username].length; i++){
msg += mail[username][i][0] + " said: " + mail[username][i][1] + "\n";
}
send(msg);
delete mail[username];
}
}
// join/leave thing
/*
if(msg.type == "enter"){
send("Hi " + msg.name + ", Rena Chan welcomes you to DeekChat!!");
return;
}
if(msg.type == "exit"){
send(msg.name + " just fucked off!! Good riddance!!!1!1");
}
*/
if(
msg.type == "message" ||
msg.type == "files"
){
if(typeof msg.data.message.name !== "undefined" && msg.data.message.name == name){
return;
}
var userid = msg.userId;
var username = msg.data.message.name;
/*
(not so) shitty embed support
*/
if(link = msg.data.message.text.match(/https?:\/\/[^ \n\r'>]+/)){
link = link[0];
getlink().catch(async err => {
send("Stupid retarded website alert!!!! I got an error accessing this shit...\n>" + err);
});
async function getlink(){
// test file:
// https://deekchat.ml/storage/files/DEK1523047738939314176.mp4
var res = await fetch(link);
var headers_arr = [...res.headers];
var headers = [];
for(i=0; i<headers_arr.length; i++){
headers[headers_arr[i][0]] = headers_arr[i][1];
}
if(
!headers["content-type"] ||
headers["content-type"].split(";")[0] != "text/html" ||
(
!headers["content-length"] &&
parseInt(headers["content-length"]) > 10485760 // 10MB
)
){
var text = "This thingy^ ";
if(headers["content-type"]){
var format = headers["content-type"].split("/");
format = format[format.length - 1].split(";")[0];
text += "is a " + format + " file ";
}else{
text += "is uhh... something.. err.. "
}
if(headers["content-length"]){
var filesize = Math.round(parseInt(headers["content-length"]) / 1048576);
text += "that weighs " + (Math.round(parseInt(headers["content-length"]) / 1048576)) + "MB!!";
}else{
text += "that uhh weighs something probably idk!!!";
}
text += " lol thats all i know fuck you";
send(text);
return;
}
// cancerous code ahead
var buffer = await res.buffer();
const parser = cheerio.load(buffer);
// get title
var title = "Unknown title lol, shit website";
var tag_title = parser("title");
var tag_name_og_title = parser("meta[name='og:title']");
var tag_property_og_title = parser("meta[property='og:title']");
if(tag_title.length !== 0){ title = tag_title.text(); }
if(tag_name_og_title.length !== 0){ title = tag_name_og_title.attr("content"); }
if(tag_property_og_title.length !== 0){ title = tag_property_og_title.attr("content"); }
// get description
var description = "Unknown description lol, stupid website do not click";
var tag_name_description = parser("meta[name='description']");
var tag_name_og_description = parser("meta[name='og:description']");
var tag_property_description = parser("meta[property='description']");
var tag_property_og_description = parser("meta[property='og:description']");
if(tag_name_description.length !== 0){ description = tag_name_description.attr("content"); }
if(tag_name_og_description.length !== 0){ description = tag_name_og_description.attr("content"); }
if(tag_property_description.length !== 0){ description = tag_property_description.attr("content"); }
if(tag_property_og_description.length !== 0){ description = tag_property_og_description.attr("content"); }
var text = "";
description = description.split("\n");
for(i=0; i<description.length; i++){
text += "\n>" + description[i];
}
send(title + text);
}
}
/*
Commands
*/
var regex = msg.data.message.text.match(/^\.([^ ]+)(?: (.*))?/);
if(
regex === null ||
typeof regex[1] == "undefined"
){
return;
}
var cmd = regex[1];
if(typeof regex[2] != "undefined"){
var value = regex[2];
}else{
var value = "";
}
var ratelimit = 0;
// ratelimit
switch(cmd){
case "ping":
ratelimit = 3;
break;
case "img":
case "im":
case "i":
ratelimit = 3;
cmd = "img";
break;
case "page":
case "p":
ratelimit = 1;
cmd = "page";
break;
case "prop":
case "help":
case "mail":
ratelimit = 5;
break;
case "mumble":
case "m":
cmd = "mumble";
ratelimit = 5;
}
// init
if(typeof ratelimitbuffer[userid] == "undefined"){
ratelimitbuffer[userid] = [];
}
// check
for(i=0; i<ratelimitbuffer[userid].length; i++){
if(ratelimitbuffer[userid] == cmd){
send("You're being ratelimited, dumbass!!");
return;
}
}
// add
ratelimitbuffer[userid].push(cmd);
// remove after x seconds
setTimeout(function(){
for(i=0; i<ratelimitbuffer[userid].length; i++){
if(ratelimitbuffer[userid][i] == cmd){
ratelimitbuffer[userid].splice(i, 1);
if(ratelimitbuffer[userid].length === 0){
delete ratelimitbuffer[userid];
}
return;
}
}
}, ratelimit * 1000);
switch(cmd){
case "ping":
send("Pong!!!!");
break;
case "img":
ddg.image_search(
{
query: value,
moderate: false
}
).then(async results => {
results_cache = results;
if(results.length === 0){
send("No results found for " + value + "!!");
return;
}
var res = await fetch(
results[0].image
);
var res_get = await res.buffer();
send_img(
"Page 0/" + (results.length - 1) + "\n[" + results[0].width + "x" + results[0].height + "] " + results[0].title + "\n" + results[0].url,
res_get
);
}).catch(async err => {
var res2 = await fetch(
results_cache[0].thumbnail
);
var res2_get = await res2.buffer();
send_img(
err + ". Downloading thumbnail instead!!" +
"\n\nPage 0/" + (results_cache.length - 1) + "\n[" + results_cache[0].width + "x" + results_cache[0].height + "] " + results_cache[0].title + "\n" + results_cache[0].url,
res2_get
);
return;
});
break;
case "page":
value = parseInt(value);
if(
isNaN(value) ||
value == ""
){
send("You didn't enter a valid number you dumbass!!");
return;
}
if(results_cache.length === 0){
send("Search buffer doesn't contain any links!!");
return;
}
if(
value >= results_cache.length ||
value <= -1
){
send("Page offset must be between 0 and " + (results_cache.length - 1) + " you dumbass!!");
return;
}
// download image
async function stupid(){
var res = await fetch(
results_cache[value].image
);
var res_get = await res.buffer();
send_img(
"Page " + value + "/" + (results_cache.length - 1) + "\n[" + results_cache[value].width + "x" + results_cache[value].height + "] " + results_cache[value].title + "\n" + results_cache[value].url,
res_get
);
}
stupid()
.catch(async err => {
var res2 = await fetch(
results_cache[value].thumbnail
);
var res2_get = await res2.buffer();
send_img(
err + ". Downloading thumbnail instead!!" +
"\n\nPage " + value + "/" + (results_cache.length - 1) + "\n[" + results_cache[value].width + "x" + results_cache[value].height + "] " + results_cache[value].title + "\n" + results_cache[value].url,
res2_get
);
return;
});
return;
case "prop":
async function stupid_the_sequel(){
var res = await fetch("https://www.hamqsl.com/solarn0nbh.php");
var res_get = await res.buffer();
send_img("Good news: the sun has not yet exploded", res_get);
}
stupid_the_sequel()
.catch(async err => {
send("Could not fetch image!!! FUCK!!!!111\n" + err);
});
break;
case "mumble":
var port = 64738;
if(value == ""){
var ip = "lolcat.ca";
}else{
var value = value.split(":");
if(value.length >= 2){
var port = value[1];
}
var ip = value[0];
}
async function mumble(){
var res = await fetch("https://lolcat.ca/api/mumble/?ip=" + ip + "&port=" + port);
var res_get = await res.json();
if(res_get.status != "ok"){
send("An error occured... " + res_get.status);
return;
}
res_get = res_get.server;
send(
"Server information for " + ip.toLowerCase() + ":" + port + ":\n\n" +
"Ping => " + res_get.ping + "\n" +
"Users => " + res_get.online + "/" + res_get.max + "\n" +
"Bandwidth => " + res_get.bandwidth + "\n" +
"Country => " + res_get.country.name + " (" + res_get.country.code + ")\n" +
"Version => " + res_get.version
);
}
mumble().catch(async err =>{
send("lolcat.ca/api/mumble/ is online, trust me. I just don't feel like giving you the status right now. (It's offline)");
});
break;
case "mail":
if(value == ""){
send("You need to specify a recipient you face fucked horse!!");
return;
}
value = value.split(" ");
var recipient = value[0];
var message = "";
for(i=1; i<value.length; i++){
message += value[i];
if(i !== value.length - 1){
message += " ";
}
}
if(msg.data.message.files != null){
if(message != ""){
message += "\n";
}
for(i=0; i<msg.data.message.files.length; i++){
message += "https://deekchat.ml/storage/files/" + msg.data.message.files[i].name;
if(i !== msg.data.message.files.length - 1){
message += "\n";
}
}
}
if(message == ""){
send("You need to send a message too, you fucked in the ass horse i hate you");
return;
}
if(typeof mail[recipient] == "undefined"){
mail[recipient] = [];
}
mail[recipient].push([username, message]);
send("I will deliver your mail to " + recipient + " when he joins back!!!!!!!! bitch!!");
break;
case "help":
send(
"== help menu ==\n\n" +
".help => lol idk\n" +
".img/.im/.i => search duckduckgo for birds\n" +
".page/.p => get page offset for image search\n" +
".mumble <[optional]ip:port> => check if lolcat died\n" +
".prop => check if the sun has exploded or smth\n" +
".mail <user> <message> => Use our worldwide AI enabled mailing system\n" +
".ping => spam deekchat"
);
break;
}
}
});
});
async function connect(){
headers = await login();
client.connect("wss://deekchat.ml/ws", "", "",
headers,
null
);
}
connect();