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. function short_code($code){
  42. $fixedCode = substr($code, 0, 2);
  43. $fixedCode .= "0";
  44. $fixedCode .= substr($code, 2);
  45. $fixedCode .= substr($code, 4, 4);
  46. $fixedCode .= substr($code, 3);
  47. $fixedCode .= substr($code, 8);
  48. $fixedCode .= substr($code, 10, 6);
  49. $fixedCode .= substr($code, 9);
  50. $fixedCode .= substr($code, 16, strlen($code));
  51. return $fixedCode;
  52. }
  53.  
  54. $rand = substr(str_shuffle(str_repeat($x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(8/strlen($x)) )),1,8);
  55.  
  56. // announce self to channel
  57. $myCode = get_own_code();
  58. $myCode = (strlen($myCode) == 31) ? short_code($myCode) : $myCode;
  59. if(!isset($myCode) || $myCode == "")
  60. die("Please contact 0xRoM on discord for help fixing this issue!");
  61.  
  62. $socket = fsockopen("ice.uplinkcorp.net", 6667);
  63. // Send auth info
  64. // fputs($socket, "PASS " . $password . "\n");
  65. fputs($socket, "NICK Badge-" . $rand . "\n");
  66. fputs($socket, "USER Badge-" . $rand . " 0 * :DC29 Badge Bot\n");
  67. fputs($socket, "JOIN #theSignal\n"); // Join channel
  68.  
  69. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  70. echo "[+] announced arrival: ".$rand."\n";
  71.  
  72. // Force an endless while
  73. while (1) {
  74. // Continue the rest of the script here
  75. while ($data = fgets($socket, 256)) {
  76. echo $data;
  77. flush();
  78. // Separate all data
  79. $ex = explode(' ', $data);
  80. // Send PONG back to the server
  81. if ($ex[0] == "PING") {
  82. fputs($socket, "PONG " . $ex[1] . "\n");
  83. }
  84. // executes chat command
  85. if ($ex[0] != 'PING' && ISSET($ex[3])) {
  86. $command = str_replace(array(
  87. chr(10),
  88. chr(13)
  89. ), '', $ex[3]);
  90. if ($command == ":!req") {
  91. if(isset($ex[4]) && $ex[4] <> ""){ // bot name
  92. if(isset($ex[5]) && $ex[5] <> ""){ // code
  93. if(!in_array($ex[4], $collected)){ // not already added this bot
  94.  
  95. $response = get_code($ex[5]);
  96. $response = (strlen($response) == 31) ? short_code($response) : $response;
  97.  
  98. if(preg_match("/^[0-9A-Fa-f]{32}/", $response)){ // valid code back
  99. echo "[+] computing for ".$ex[4]."\n";
  100. fputs($socket, "PRIVMSG #theSignal :!rsp " . $ex[4] . " " . $response . "\n");
  101. array_push($collected, $ex[4]); // add to replied array
  102.  
  103. $myCode = get_own_code();
  104. $myCode = (strlen($myCode) == 31) ? short_code($myCode) : $myCode;
  105. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  106. echo "[+] replied to ".$ex[4]."\n";
  107. }else{echo "[-] invalid code\n";}
  108. }else{echo "[-] already exchanged\n";}
  109. }else{echo "[-] no code\n";}
  110. }else{echo "[-] no name\n";}
  111. }
  112. if ($command == ":!rsp") {
  113. if ($ex[4] == $rand && isset($ex[5]) && $ex[5] <> ""){ // response is for this bot
  114. get_code($ex[5]);
  115. echo "[+] got response from ".$ex[4]."\n";
  116. }else{echo "[-] not for me\n";}
  117. }
  118. }
  119. }
  120. }
  121. ?>
  122.  
Buy Me A Coffee