markhack512

ESPhack v1 IRC Bot

Jan 2nd, 2022
1,252
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <IRCClient.h>
  3.  
  4.  
  5. //WIFI Settings
  6. #define ssid         "WIFI_NETWORK_NAME"
  7. #define password     "WIFI_PASSWORD"
  8.  
  9. //IRC server Settings
  10. #define IRC_SERVER   "chat.freenode.net"
  11. #define IRC_PORT     6667
  12. #define IRC_NICKNAME "BOTNick"
  13. #define IRC_USER     "BOTNick"
  14. #define BOT_NICK_PW     "BOT_NICK_PASSWORD"
  15.  
  16. // Reply only to this nick
  17. #define REPLY_TO     "YOURNICK"
  18.  
  19. WiFiClient wiFiClient;
  20. IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
  21.  
  22. void setup() {
  23.  
  24. pinMode(LED_BUILTIN, OUTPUT);
  25. digitalWrite(LED_BUILTIN, HIGH);
  26. Serial.begin(115200);
  27. delay(100);
  28. Serial.println("");
  29. Serial.print("Connecting to ");
  30. Serial.println(ssid);
  31. WiFi.begin(ssid, password);
  32. while (WiFi.status() != WL_CONNECTED) {
  33. delay(500);
  34. Serial.print(".");
  35. }
  36. Serial.println("");
  37. Serial.println("WiFi connected");
  38. Serial.println("IP address: ");
  39. Serial.println(WiFi.localIP());
  40. WiFi.printDiag(Serial);
  41. client.setCallback(callback);
  42. client.setSentCallback(debugSentCallback);
  43. }
  44. void loop() {
  45.   if (!client.connected()) {
  46.     Serial.println("Attempting IRC connection...");
  47.     if (client.connect(IRC_NICKNAME, IRC_USER, BOT_NICK_PW)) {
  48.       Serial.println("connected");
  49.     } else {
  50.       Serial.println("failed... try again in 5 seconds");
  51.       // Wait 5 seconds before retrying
  52.       delay(5000);
  53.     }
  54.     return;
  55.   }
  56.   client.loop();
  57. }
  58. void callback(IRCMessage ircMessage) {
  59.   // PRIVMSG ignoring CTCP messages
  60.   if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
  61.     String message("<" + ircMessage.nick + "> " + ircMessage.text);
  62.     Serial.println(message);
  63.  
  64.     if (ircMessage.nick == REPLY_TO  && ircMessage.parameters == IRC_NICKNAME  && ircMessage.text == "!join") {
  65.       client.sendMessage(ircMessage.nick, "joining channels...");
  66.  
  67.       //Edit channels to join
  68.       client.sendRaw("JOIN #hack");
  69.       //client.sendRaw("JOIN #room");
  70.   }
  71.  
  72. if (ircMessage.text == "!die") {  
  73.   if (ircMessage.nick == REPLY_TO && ircMessage.text == "!die") {
  74.     client.sendRaw("QUIT well, i was told to die...");
  75.     delay(100000);
  76.   } else {
  77.      client.sendMessage(ircMessage.parameters, "NOU! " + ircMessage.nick + " you're not allowed to kill me...");
  78.   }
  79.   }
  80. if (ircMessage.text == "!blah") {  
  81.     client.sendMessage(ircMessage.parameters, "yeah, much blah...");  
  82.   }
  83. if (ircMessage.text == "!ledon") {
  84.   if (digitalRead(LED_BUILTIN) == 0) {
  85.       client.sendMessage(ircMessage.parameters, "Onboard LED is already On");
  86.     }else{
  87.       digitalWrite(LED_BUILTIN, 0);
  88.       client.sendMessage(ircMessage.parameters, "Onboard LED: On");
  89. }
  90. }
  91. if (ircMessage.text == "!ledoff") {
  92.   if (digitalRead(LED_BUILTIN) == 1) {
  93.       client.sendMessage(ircMessage.parameters, "Onboard LED is already Off");
  94.     }else{
  95.       digitalWrite(LED_BUILTIN, 1);
  96.       client.sendMessage(ircMessage.parameters, "Onboard LED: Off");
  97. }
  98. }
  99.  
  100. // IRC INFO
  101.  
  102.     if (ircMessage.text == "!register") {  
  103.       client.sendMessage(ircMessage.parameters, "You can Register your Nick by visiting iRC.com and creating an account or by using the command '/msg nickserv register password email' then check said email to verifiy the account.");
  104.       delay(5000);
  105.   }
  106.     if (ircMessage.text == "!umode") {  
  107.       client.sendMessage(ircMessage.parameters, "For a list of User Modes visit: https://wiki.freenode.net/view/User_Modes");
  108.       delay(5000);  
  109.   }
  110.     if (ircMessage.text == "!cmode") {  
  111.       client.sendMessage(ircMessage.parameters, "For a list of Channel Modes visit: https://wiki.freenode.net/view/Channel_Modes");
  112.       delay(5000);  
  113.   }
  114.     if (ircMessage.text == "!buc") {  
  115.       client.sendMessage(ircMessage.parameters, "For a list of Basic User Commands visit: https://wiki.freenode.net/view/User_Commands");
  116.       delay(5000);  
  117.   }
  118.  
  119.     if (ircMessage.text == "!flags") {  
  120.       client.sendMessage(ircMessage.parameters, "For a list of Flags type '/ChanServ help flags' to use flags type '/cs flags #room nick +/-FLAG'");
  121.       delay(5000);  
  122.   }
  123.  
  124.     if (ircMessage.text == "!mc") {  
  125.       client.sendMessage(ircMessage.parameters, "To use multiple commands on one line try: /mode #room -o+v nick nick");
  126.       delay(5000);  
  127.   }
  128.  
  129.     if (ircMessage.text == "!ch340") {  
  130.       client.sendMessage(ircMessage.parameters, "Download for the Windows Ch340 Driver https://sparks.gogo.co.nz/ch340.html");
  131.       delay(5000);  
  132.   }
  133.  
  134. // NodeMCU ESP8266 INFO (unlisted)
  135.  
  136.   if (ircMessage.text == "!freeheap") {
  137.       float freeheap = ESP.getFreeHeap();
  138.       client.sendMessage(ircMessage.parameters, "ESP8266 Free Heap: " + String((freeheap),0) + " bytes");
  139.       delay(5000);
  140.   }
  141.   if (ircMessage.text == "!cycles") {
  142.       float cycles = ESP.getCycleCount();
  143.       client.sendMessage(ircMessage.parameters, "ESP8266 Instruction Cycles since startup: " + String((cycles),0));
  144.       delay(5000);
  145.   }
  146.   if (ircMessage.text == "!cpu") {
  147.       float freq = ESP.getCpuFreqMHz();
  148.       client.sendMessage(ircMessage.parameters, "ESP8266 CPU Frequency: " + String((freq),0) + "MHz");
  149.       delay(5000);
  150.      
  151.   }
  152.   if (ircMessage.text == "!resetreason") {
  153.       String reason = ESP.getResetReason();
  154.       client.sendMessage(ircMessage.parameters, "ESP8266 Restart Reason: " + String(reason));
  155.       delay(5000);
  156.   }
  157.   if (ircMessage.text == "!chipid") {
  158.       float chipid = ESP.getChipId();
  159.       client.sendMessage(ircMessage.parameters, "ESP8266 ChipID: 0x" + String((chipid),HEX));
  160.       delay(5000);
  161.   }
  162.   if (ircMessage.text == "!resetinfo") {
  163.       String resetinfo = ESP.getResetInfo();
  164.       client.sendMessage(ircMessage.parameters, "ESP8266 Reset Info: " + resetinfo);
  165.       delay(5000);
  166.   }
  167.   if (ircMessage.text == "!sketchsize") {
  168.       float sketchsize = ESP.getSketchSize();
  169.       client.sendMessage(ircMessage.parameters, "ESP8266 Sketch Size: " + String((sketchsize),0));
  170.       delay(5000);
  171.   }
  172. if (ircMessage.text == "!version") {
  173.       const char* sdkversion = ESP.getSdkVersion();
  174.       String coreversion = ESP.getCoreVersion();
  175.       String fullversion = ESP.getFullVersion();
  176.       uint8_t bootversion = ESP.getBootVersion();
  177.       client.sendMessage(ircMessage.parameters, "ESP8266 SDK Version: " + String(sdkversion));
  178.       client.sendMessage(ircMessage.parameters, "ESP8266 Core Version: " + String(coreversion));
  179.       //client.sendMessage(ircMessage.parameters, "ESP8266 Full Version: " + String(fullversion));
  180.       //client.sendMessage(ircMessage.parameters, "ESP8266 Boot Version: " + String(bootversion));
  181.       delay(5000);
  182.   }
  183.  
  184.  
  185.  
  186.   if (ircMessage.text == "!help") {  
  187.       client.sendMessage(ircMessage.parameters, ircMessage.nick + " Check your PM for a list of commands.");
  188.       client.sendMessage(ircMessage.nick, "ESP8266 IRC-Bot Commands");
  189.       client.sendMessage(ircMessage.nick, " ");
  190.       client.sendMessage(ircMessage.nick, "!register :shows someone how to create an account on irc.com");
  191.       client.sendMessage(ircMessage.nick, "!umode :shows a user where to view a list of user modes");
  192.       client.sendMessage(ircMessage.nick, "!cmode :shows a user where to view a list of channel modes");
  193.       client.sendMessage(ircMessage.nick, "!buc :shows a user where to view a list of basic user commands");
  194.       client.sendMessage(ircMessage.nick, "!flags :shows a user how to view/use flags");
  195.       client.sendMessage(ircMessage.nick, "!ch340 :shows a user where to download the ch340 driver");
  196.       client.sendMessage(ircMessage.nick, "!mc :shows a user how to use multiple commands in one line");
  197.  
  198.       client.sendMessage(ircMessage.nick, " ");
  199.       client.sendMessage(ircMessage.nick, "NodeMCU ESP8266 INFO");
  200.       client.sendMessage(ircMessage.nick, " ");
  201.       client.sendMessage(ircMessage.nick, "!cpu :shows ESP8266 CPU Frequency");
  202.       client.sendMessage(ircMessage.nick, "!ledon :turn on onboard LED");
  203.       client.sendMessage(ircMessage.nick, "!ledoff :turn off onboard LED");
  204.       //client.sendMessage(ircMessage.nick, "!freeheap :shows ESP8266 free heap memory");
  205.       //client.sendMessage(ircMessage.nick, "!cycles :shows ESP8266 cpu instruction cycle count since start as an unsigned 32-bit");
  206.       //client.sendMessage(ircMessage.nick, "!chipid :shows ESP8266 Chip ID");
  207.       client.sendMessage(ircMessage.nick, "!sketchsize :shows ESP8266 Arduino Sketch Size");
  208.       client.sendMessage(ircMessage.nick, "!version :shows ESP8266 SDK and Core Versions");
  209.       client.sendMessage(ircMessage.nick, "!source :sends user ESP8266 IRC Bot source links");
  210.       delay(5000);
  211.  
  212.     }
  213.  
  214.   if (ircMessage.text == "!source") {  
  215.       client.sendMessage(ircMessage.parameters, ircMessage.nick + " Check your PM for source links. :)");
  216.       client.sendMessage(ircMessage.nick, "ESP8266 IRC-Bot Source");
  217.       client.sendMessage(ircMessage.nick, "Arduino Library: https://www.arduino.cc/reference/en/libraries/arduinoirc/");
  218.       client.sendMessage(ircMessage.nick, "GitHub: https://github.com/fredimachado/ArduinoIRC");
  219.       client.sendMessage(ircMessage.nick, "ESPhack Source: https://pastebin.com/ek5w8V6A");
  220.  
  221.  
  222.       delay(5000);
  223.  
  224.     }
  225.  
  226.  
  227.     return;
  228.   }
  229.   Serial.println(ircMessage.original);
  230. }
  231.  
  232. void debugSentCallback(String data) {
  233.   Serial.println(data);
  234. }
  235.  
RAW Paste Data