Newer
Older
DC29BadgeBot / bot.php
root on 6 Aug 2021 8 KB fixed shortcode issue
<?php
set_time_limit(0);
include 'php_serial.class.php';
$badgeInterface = "/dev/ttyACM0";// EDIT THIS TO MATCH YOUR INTERFACE
$collected = array();

function get_code($code){
    global $badgeInterface;
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", strtoupper(substr(trim($code), 0, 32)));
    $serial = new PhpSerial;
    $serial->deviceSet($badgeInterface); 
    $serial->confBaudRate(9600);
    $serial->deviceOpen('w+');
    stream_set_timeout($serial->_dHandle, 3);
    $serial->sendMessage("\r\n\r\n"."5"."$result"."\r\n");
    $devResponse = $serial->readPort();
    $serial->deviceClose();
//echo "DEBUG: ".$devResponse."\n";
    $toReturn = trim(str_replace("Press ENTER to continue...", "", $devResponse));
    $toReturn = substr($toReturn, -32);
    return $toReturn;
}
function get_code_rsp($code){
    global $badgeInterface;
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", strtoupper(substr(trim($code), 0, 32)));
    $serial = new PhpSerial;
    $serial->deviceSet($badgeInterface); 
    $serial->confBaudRate(9600);
    $serial->deviceOpen('w+');
    stream_set_timeout($serial->_dHandle, 3);
    $serial->sendMessage("\r\n\r\n"."5"."$result"."\r\n");
    $devResponse = $serial->readPort();
    $serial->deviceClose();
//echo "DEBUG: ".$devResponse."\n";
    if(strpos($devResponse, "Already connected to this badge") !== false){ // already connected
        $toReturn = "alreadyConnected";
    }else{
        $toReturn = trim(str_replace("Press ENTER to continue...", "", $devResponse));
        $toReturn = substr($toReturn, -32);
    }
    return $toReturn;
}
function get_own_code(){
    global $badgeInterface;
    $serial = new PhpSerial;
    $serial->deviceSet($badgeInterface); 
    $serial->confBaudRate(9600);
    $serial->deviceOpen('w+');
    stream_set_timeout($serial->_dHandle, 3);
    $serial->sendMessage("\r\n\r\n4\r\n");
    $devResponse = $serial->readPort();
    $serial->deviceClose();
//echo "DEBUG: ".$devResponse."\n";
    $toReturn = trim(str_replace("Invalid Input. Please try again:", "", $devResponse));
    $toReturn = trim(str_replace("Choose an option:", "", $toReturn));
    //echo $toReturn; // DEBUG
    $toReturn = substr($toReturn, -32);
    return $toReturn;
}
function short_code($code){
    $fixedCode = substr($code, 0, 2);
    $fixedCode .= "0";
    $fixedCode .= substr($code, 2,1);
    $fixedCode .= substr($code, 4, 4);
    $fixedCode .= substr($code, 3,1);
    $fixedCode .= substr($code, 8,1);
    $fixedCode .= substr($code, 10, 6);
    $fixedCode .= substr($code, 9,1);
    $fixedCode .= substr($code, 16, 16);
    return $fixedCode;
}
function get_stats_connected(){
    global $badgeInterface;
    $serial = new PhpSerial;
    $serial->deviceSet($badgeInterface); 
    $serial->confBaudRate(9600);
    $serial->deviceOpen('w+');
    stream_set_timeout($serial->_dHandle, 3);
    $serial->sendMessage("\r\n\r\n5\r\n\r\n");
    $devResponse = $serial->readPort();
    $serial->deviceClose();

    $toReturn = trim(preg_replace('/\s+/', ' ', $devResponse));
    $toReturn = trim(string_between_two_string($toReturn, 'Number of Badges Connected: ', 'Badge Types'));
    //echo $toReturn; // DEBUG

    return $toReturn;
}
function get_stats_signal(){
    global $badgeInterface;
    $serial = new PhpSerial;
    $serial->deviceSet($badgeInterface); 
    $serial->confBaudRate(9600);
    $serial->deviceOpen('w+');
    stream_set_timeout($serial->_dHandle, 3);
    $serial->sendMessage("\r\n\r\n5\r\n\r\n");
    $devResponse = $serial->readPort();
    $serial->deviceClose();

    $toReturn = trim(preg_replace('/\s+/', ' ', $devResponse));
    if(strpos($toReturn, "Shared the Signal") !== false){ // are sharing the signal
        if(strpos($toReturn, "Thank you for doing your part") !== false){ // have sent more than 20 signals
            $toReturn = trim(string_between_two_string($toReturn, 'Shared the Signal: ', 'Thank you for doing your part'));
        }else{
            $toReturn = trim(string_between_two_string($toReturn, 'Shared the Signal: ', 'se try again'));
        }
        //echo $toReturn; // DEBUG
    }else{ // you havent collected all badge types
        $toReturn = 0;
    }
    return $toReturn;
}
function string_between_two_string($str, $starting_word, $ending_word){
    $subtring_start = strpos($str, $starting_word);
    $subtring_start += strlen($starting_word);  
    $size = strpos($str, $ending_word, $subtring_start) - $subtring_start;  
    return substr($str, $subtring_start, $size);  
}

$rand = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(8/strlen($x)) )),1,8);

// announce self to channel
$myCode = get_own_code();
$myCode = (strlen($myCode) == 31) ? "".short_code($myCode) : $myCode;
if(!isset($myCode) || $myCode == "")
    die("Please contact 0xRoM on discord for help fixing this issue!");

$badgeConn = get_stats_connected();
$badgeSig = get_stats_signal();
echo "[+] connected: $badgeConn, Sig shared: $badgeSig\n";

$socket = fsockopen("ice.uplinkcorp.net", 6667);
// Send auth info
// fputs($socket, "PASS " . $password . "\n");
fputs($socket, "NICK Badge-" . $rand . "\n");
fputs($socket, "USER Badge-" . $rand . " 0 * :DC29 Badge Bot\n");
fputs($socket, "JOIN #theSignal\n"); // Join channel

fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n"); 
echo "[+] announced arrival: ".$rand."\n";

// Force an endless while
while (1) {
    // Continue the rest of the script here
    while ($data = fgets($socket, 256)) {
        echo $data;
        flush();
        
        // Separate all data
        $ex = explode(' ', $data);
        
        // Send PONG back to the server
        if ($ex[0] == "PING") {
            fputs($socket, "PONG " . $ex[1] . "\n");
        }
        
        // executes chat command
        if ($ex[0] != 'PING' && ISSET($ex[3])) {
            $command = str_replace(array(
                chr(10),
                chr(13)
            ), '', $ex[3]);
            if ($command == ":!req") {
                if(isset($ex[4]) && $ex[4] <> ""){ // bot name
                    if(isset($ex[5]) && $ex[5] <> ""){ // code
                        if(!in_array($ex[4], $collected)){ // not already added this bot

                            $response = get_code($ex[5]);
                            $response = (strlen($response) == 31) ? "".short_code($response) : $response;

                            if(preg_match("/^[0-9A-Fa-f]{32}/", $response)){ // valid code back
                                echo "[+] computing for ".$ex[4]."\n";
                                fputs($socket, "PRIVMSG #theSignal :!rsp " . $ex[4] . " " . $response . "\n");
                                array_push($collected, $ex[4]); // add to replied array

                                $myCode = get_own_code();
                                $myCode = (strlen($myCode) == 31) ? "".short_code($myCode) : $myCode;

                                fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
                                echo "[+] replied to ".$ex[4]."\n";
                            }else{ echo "[-] invalid code\n"; }
                        }else{ echo "[-] already exchanged\n"; }
                    }else{ echo "[-] no code\n"; }
                }else{ echo "[-] no name\n"; }
            }
            if ($command == ":!rsp") {
                if ($ex[4] == $rand){ // response is for this bot
                    if(isset($ex[5]) && $ex[5] <> ""){ // code for me
                        echo "[+] got response from ".$ex[4]."\n";
                        if(get_code_rsp($ex[5]) == "alreadyConnected"){
                            echo "[-] already connected (rsp code)\n";
                        }else{
                            echo "[+] new connection (rsp code)\n";
                        }
                        
                    }else{ echo "[-] no code set\n"; }
                }else{ echo "[-] not for me\n"; }
            }
            if ($command == ":!stats") {
                if (trim($ex[4]) === $rand){ // response is for this bot
                    $badgeConn = get_stats_connected();
                    $badgeSig = get_stats_signal();
                    echo "[+] connected: $badgeConn, Sig shared: $badgeSig\n";
                    fputs($socket, "PRIVMSG #theSignal :Connected = $badgeConn, Sig shared = $badgeSig\n");
                }else{ echo "[-] not for me\n"; }
            }
        }
    }
}
?>