Newer
Older
DC29BadgeBot / bot.php
  1. <?php
  2. set_time_limit(0);
  3. include 'php_serial.class.php';
  4. $badgeInterface = "/dev/ttyACM0";// EDIT THIS TO MATCH YOUR INTERFACE
  5. $collected = array();
  6.  
  7. function get_code($code){
  8. global $badgeInterface;
  9. $result = preg_replace("/[^a-zA-Z0-9]+/", "", strtoupper(substr(trim($code), 0, 32)));
  10. $serial = new PhpSerial;
  11. $serial->deviceSet($badgeInterface);
  12. $serial->confBaudRate(9600);
  13. $serial->deviceOpen('w+');
  14. stream_set_timeout($serial->_dHandle, 3);
  15. $serial->sendMessage("\r\n\r\n"."5"."$result"."\r\n");
  16. $devResponse = $serial->readPort();
  17. $serial->deviceClose();
  18.  
  19. $toReturn = trim(str_replace("Press ENTER to continue...", "", $devResponse));
  20. $toReturn = substr($toReturn, -32);
  21. return $toReturn;
  22. }
  23.  
  24. function get_own_code(){
  25. global $badgeInterface;
  26. $serial = new PhpSerial;
  27. $serial->deviceSet($badgeInterface);
  28. $serial->confBaudRate(9600);
  29. $serial->deviceOpen('w+');
  30. stream_set_timeout($serial->_dHandle, 3);
  31. $serial->sendMessage("\r\n\r\n4\r\n");
  32. $devResponse = $serial->readPort();
  33. $serial->deviceClose();
  34.  
  35. $toReturn = trim(str_replace("Invalid Input. Please try again:", "", $devResponse));
  36. $toReturn = trim(str_replace("Choose an option:", "", $toReturn));
  37. //echo $toReturn; // DEBUG
  38. $toReturn = substr($toReturn, -32);
  39. return $toReturn;
  40. }
  41.  
  42. $rand = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(8/strlen($x)) )),1,8);
  43.  
  44. // announce self to channel
  45. $myCode = get_own_code();
  46. if(!isset($myCode) || $myCode == "")
  47. die("Please contact 0xRoM on discord for help fixing this issue!");
  48.  
  49. $socket = fsockopen("ice.uplinkcorp.net", 6667);
  50. // Send auth info
  51. // fputs($socket, "PASS " . $password . "\n");
  52. fputs($socket, "NICK Badge-" . $rand . "\n");
  53. fputs($socket, "USER Badge-" . $rand . " 0 * :DC29 Badge Bot\n");
  54. fputs($socket, "JOIN #theSignal\n"); // Join channel
  55.  
  56. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  57. echo "[+] announced arrival: ".$rand."\n";
  58.  
  59. // Force an endless while
  60. while (1) {
  61. // Continue the rest of the script here
  62. while ($data = fgets($socket, 128)) {
  63. echo $data;
  64. flush();
  65. // Separate all data
  66. $ex = explode(' ', $data);
  67. // Send PONG back to the server
  68. if ($ex[0] == "PING") {
  69. fputs($socket, "PONG " . $ex[1] . "\n");
  70. }
  71. // executes chat command
  72. if ($ex[0] != 'PING' && ISSET($ex[3])) {
  73. $command = str_replace(array(
  74. chr(10),
  75. chr(13)
  76. ), '', $ex[3]);
  77. if ($command == ":!req") {
  78. if(isset($ex[4]) && $ex[4] <> ""){ // bot name
  79. if(isset($ex[5]) && $ex[5] <> ""){ // code
  80. if(!in_array($ex[4], $collected)){ // not already added this bot
  81. $response = get_code($ex[5]);
  82. if(preg_match("/^[0-9A-Fa-f]{32}/", $response)){ // valid code back
  83. echo "[+] computing for ".$ex[4]."\n";
  84. fputs($socket, "PRIVMSG #theSignal :!rsp " . $ex[4] . " " . $response . "\n");
  85. array_push($collected, $ex[4]); // add to replied array
  86. $myCode = get_own_code();
  87. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  88. echo "[+] replied to ".$ex[4]."\n";
  89. }else{echo "[-] invalid code\n";}
  90. }else{echo "[-] already exchanged\n";}
  91. }else{echo "[-] no code\n";}
  92. }else{echo "[-] no name\n";}
  93. }
  94. if ($command == ":!rsp") {
  95. if ($ex[4] == $rand && isset($ex[5]) && $ex[5] <> ""){ // response is for this bot
  96. get_code($ex[5]);
  97. echo "[+] got response from ".$ex[4]."\n";
  98. }else{echo "[-] not for me\n";}
  99. }
  100. }
  101. }
  102. }
  103. ?>
Buy Me A Coffee