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. $socket = fsockopen("ice.uplinkcorp.net", 6667);
  45. // Send auth info
  46. // fputs($socket, "PASS " . $password . "\n");
  47. fputs($socket, "NICK Badge-" . $rand . "\n");
  48. fputs($socket, "USER Badge-" . $rand . " 0 * :DC29 Badge Bot\n");
  49. fputs($socket, "JOIN #theSignal\n"); // Join channel
  50.  
  51. // announce self to channel
  52. $myCode = get_own_code();
  53. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  54.  
  55. // Force an endless while
  56. while (1) {
  57. // Continue the rest of the script here
  58. while ($data = fgets($socket, 128)) {
  59. echo $data;
  60. flush();
  61. // Separate all data
  62. $ex = explode(' ', $data);
  63. // Send PONG back to the server
  64. if ($ex[0] == "PING") {
  65. fputs($socket, "PONG " . $ex[1] . "\n");
  66. }
  67. // executes chat command
  68. if ($ex[0] != 'PING' && ISSET($ex[3])) {
  69. $command = str_replace(array(
  70. chr(10),
  71. chr(13)
  72. ), '', $ex[3]);
  73. if ($command == ":!req") {
  74. // 4 = bot name, 5 = their code
  75. if(!in_array($ex[4], $collected)){ // not already added their code
  76. $response = get_code($ex[5]);
  77. if(preg_match("/^[0-9A-Fa-f]{32}/", $response)){ // valid code back
  78. fputs($socket, "PRIVMSG #theSignal :!rsp " . $ex[4] . " " . $response . "\n");
  79. array_push($collected, $ex[4]); // add to replied array
  80. $myCode = get_own_code();
  81. fputs($socket, "PRIVMSG #theSignal :!req " . $rand . " " . $myCode . "\n");
  82.  
  83. }
  84. }
  85. }
  86. if ($command == ":!rsp") {
  87. if ($ex[4] == $rand){ // response is for this bot
  88. get_code($ex[5]);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. ?>
Buy Me A Coffee