diff --git a/bot.php b/bot.php index fcbe90e..46a5d24 100644 --- a/bot.php +++ b/bot.php @@ -15,12 +15,31 @@ $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; @@ -31,7 +50,7 @@ $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 @@ -167,8 +186,13 @@ if ($command == ":!rsp") { if ($ex[4] == $rand){ // response is for this bot if(isset($ex[5]) && $ex[5] <> ""){ // code for me - get_code($ex[5]); echo "[+] got response from ".$ex[4]."\n"; + if(get_code_rsp($ex[5]) == "alreadyConnected"){ + echo "[-] already connected with ".$ex[4]."\n"; + }else{ + echo "[+] new connection with ".$ex[4]."\n"; + } + }else{ echo "[-] no code set\n"; } }else{ echo "[-] not for me\n"; } }