Newer
Older
DC29BadgeBot / bot.php
root on 5 Aug 2021 3 KB slightly better instructions
  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.  
  57.  
  58. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  59.  
  60. // Force an endless while
  61. while (1) {
  62. // Continue the rest of the script here
  63. while ($data = fgets($socket, 128)) {
  64. echo $data;
  65. flush();
  66. // Separate all data
  67. $ex = explode(' ', $data);
  68. // Send PONG back to the server
  69. if ($ex[0] == "PING") {
  70. fputs($socket, "PONG " . $ex[1] . "\n");
  71. }
  72. // executes chat command
  73. if ($ex[0] != 'PING' && ISSET($ex[3])) {
  74. $command = str_replace(array(
  75. chr(10),
  76. chr(13)
  77. ), '', $ex[3]);
  78. if ($command == ":!req" && isset($ex[4]) && isset($ex[5]) && $ex[4] <> "" && $ex[5] <> "") {
  79. // 4 = bot name, 5 = their code
  80. if(!in_array($ex[4], $collected)){ // not already added their code
  81. $response = get_code($ex[5]);
  82. if(preg_match("/^[0-9A-Fa-f]{32}/", $response)){ // valid code back
  83. fputs($socket, "PRIVMSG #theSignal :!rsp " . $ex[4] . " " . $response . "\n");
  84. array_push($collected, $ex[4]); // add to replied array
  85. $myCode = get_own_code();
  86. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  87.  
  88. }
  89. }
  90. }
  91. if ($command == ":!rsp") {
  92. if ($ex[4] == $rand && isset($ex[5]) && $ex[5] <> ""){ // response is for this bot
  93. get_code($ex[5]);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. ?>
Buy Me A Coffee