<commit_before>#!/usr/bin/env node
var login = require("facebook-chat-api"),
    fs = require('fs'),
    exec = require('child_process').exec;

var loginInfo = {appState: JSON.parse(fs.readFileSync("appstate.json"))};
var cds = [];
var lastDate = Date.now();
var lastMessage = "";

var directory = __dirname;
console.log(__dirname);
login(loginInfo,{logLevel: "silent"},function callback (err, api) {

    if(err) return console.error(err);
    console.log("Logged in.");
    // fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState())); // save app state for future launches

    api.changeNickname("fbterm", api.getCurrentUserID(), api.getCurrentUserID(), function(err){});   // set user's message-to-self nickname to 'fbterm'
    api.sendMessage("@fbterm connected", api.getCurrentUserID());
    api.setOptions({selfListen: true});

    api.listen(function callback(err, message) {
    	if(message.senderID != message.threadID) // do not accept messages from group chats unless preceded by '/fbterm'
    	    if(message.body.startsWith("/fbterm"))
                message.body = message.body.substring(7);
            else
                return;

        if(message.senderID != api.getCurrentUserID()) // do not accept messages from other people
    	    return;

        if(message.body.startsWith("@fbterm"))	// do not accept messages that were sent by the bot
    	    return;

    	if(Date.now()-lastDate < 300 && lastMessage == message.body)	// prevent duplicate calls
	    return;
    	lastDate = Date.now();
	   lastMessage = message.body;
    	console.log("\nexecuting command @ "+Date.now());

        if(message.body.trim() == "cls" || message.body.trim() == "clear"){     // deletes thread (clears messages)
            api.sendMessage("@fbterm\nReload page to finish clearing thread.", message.threadID, function(err, messageInfo){
                api.deleteThread(message.threadID);
                api.sendMessage("@fbterm :)", message.threadID);
            });
            return;
        }

         if(message.body.toLowerCase().startsWith("sendfile ")){
            var filename = message.body.substring(9).trim();
            console.log("Attempting to send file: "+filename);
            fs.stat(directory+"/"+filename, function(err, stat) { //check if file exists
                if(err == null) {
                    api.sendMessage({body: "@fbterm "+filename, attachment: fs.createReadStream(directory+"/"+filename)}, message.threadID);
                    console.log('File exists');
                } else if(err.code == 'ENOENT') {
                    api.sendMessage("@fbterm ERR:\nFile "+filename+" not found.", message.threadID);
                } else {
                    console.log('Some other error: ', err.code);
                }
            });
            return;
        }

    	if(message.body.trim().startsWith("cd")){
            var success = true;
            var relativeDir = message.body.trim().substring(2).trim();
            if(relativeDir == ".."){
                directory = directory.substring(0, directory.lastIndexOf("\\"));
            }
            else if(relativeDir != "."){
                try{
                    var stat = fs.statSync(directory+"\\"+relativeDir);
                    if(stat.isDirectory()){
                        directory+="\\"+relativeDir;
                    }else{
                        success = false;
                    }
                }catch(e){
                    success = false;
                }
            }
            if(success){
                api.sendMessage("@fbterm\n"+directory, message.threadID);
                console.log("cwd: "+directory);
            }else{
                api.sendMessage("@fbterm\nThe system could not find the path specified.", message.threadID);
            }
            return;
    		/*console.log(message.body);
    		cds.push(message.body+" && ");<commit_msg>Improved cd handling using 'path' module<commit_after>#!/usr/bin/env node
var login = require("facebook-chat-api"),
    fs = require('fs'),
    exec = require('child_process').exec,
    path = require('path');

var loginInfo = {appState: JSON.parse(fs.readFileSync("appstate.json"))};
var cds = [];
var lastDate = Date.now();
var lastMessage = "";

var directory = __dirname;
console.log(__dirname);
login(loginInfo,{logLevel: "silent"},function callback (err, api) {

    if(err) return console.error(err);
    console.log("Logged in.");
    // fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState())); // save app state for future launches

    api.changeNickname("fbterm", api.getCurrentUserID(), api.getCurrentUserID(), function(err){});   // set user's message-to-self nickname to 'fbterm'
    api.sendMessage("@fbterm connected", api.getCurrentUserID());
    api.setOptions({selfListen: true});

    api.listen(function callback(err, message) {
    	if(message.senderID != message.threadID) // do not accept messages from group chats unless preceded by '/fbterm'
    	    if(message.body.startsWith("/fbterm"))
                message.body = message.body.substring(7);
            else
                return;

        if(message.senderID != api.getCurrentUserID()) // do not accept messages from other people
    	    return;

        if(message.body.startsWith("@fbterm"))	// do not accept messages that were sent by the bot
    	    return;

    	if(lastMessage == message.body){	// prevent duplicate calls
            lastMessage = "";
	       return;
       }
    	lastDate = Date.now();
	   lastMessage = message.body;
    	console.log("\nexecuting command @ "+Date.now());

        if(message.body.trim() == "cls" || message.body.trim() == "clear"){     // deletes thread (clears messages)
            api.sendMessage("@fbterm\nReload page to finish clearing thread.", message.threadID, function(err, messageInfo){
                api.deleteThread(message.threadID);
                api.sendMessage("@fbterm :)", message.threadID);
            });
            return;
        }

         if(message.body.toLowerCase().startsWith("sendfile ")){
            var filename = message.body.substring(9).trim();
            console.log("Attempting to send file: "+filename);
            fs.stat(directory+"/"+filename, function(err, stat) { //check if file exists
                if(err == null) {
                    api.sendMessage({body: "@fbterm "+filename, attachment: fs.createReadStream(directory+"/"+filename)}, message.threadID);
                    console.log('File exists');
                } else if(err.code == 'ENOENT') {
                    api.sendMessage("@fbterm ERR:\nFile "+filename+" not found.", message.threadID);
                } else {
                    console.log('Some other error: ', err.code);
                }
            });
            return;
        }

    	if(message.body.trim().startsWith("cd")){
            var success = false;
            var relativeDir = message.body.trim().substring(2).trim();
            try{
                var stat = fs.statSync(path.join(directory, relativeDir));
                if(stat.isDirectory()){
                    directory = path.join(directory, relativeDir);
                    success = true;
                }
            }catch(e){}
            if(success){
                api.sendMessage("@fbterm\n"+directory, message.threadID);
                console.log("cwd: "+directory);
            }else{
                api.sendMessage("@fbterm\nThe system could not find the path specified.", message.threadID);
            }
            return;
    		/*console.log(message.body);
    		cds.push(message.body+" && ");
{'fraction_non_alphanumeric': 0.14489528795811518, 'fraction_numerical': 0.0013089005235602095, 'mean_word_length': 2.134126333059885, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4162366', 'n_tokens_mistral': 2122, 'n_tokens_neox': 2049, 'n_words': 440}