Untitled

 avatar
unknown
python
2 months ago
14 kB
4
Indexable
import Stdio;                                                                                               class BotConfig {                                         string host;                                          int port;                                             string nick;                                          string realname;                                      array(string) channels;                               string sasl_user;                                     string sasl_pass;                                                                                           void create() {                                           write("BotConfig: Initializing\n");                   host = "irc.deft.com";                                port = 6667;                                          nick = "PikeBot" + random(1000);                      realname = "Pike IRC Bot";                            channels = ({"#pike-test"});                          sasl_user = "YourNickServUsername";  // Replace if needed                                                   sasl_pass = "YourNickServPassword";  // Replace if needed                                                   write("BotConfig: Initialized successfully\n");                                                         }                                                 }                                                                                                           class IRCBot {                                            BotConfig config;                                     Stdio.File socket;                                    bool connected;                                                                                             void create(BotConfig _config) {                          write("IRCBot: Starting create\n");                   if (!_config) {                                           write("Error: No config provided\n");                 return;                                           }                                                     config = _config;                                     connected = false;                                    socket = 0;                                           write("IRCBot: Config assigned\n");                                                                         connect();                                            write("IRCBot: Create completed\n");              }                                                                                                           protected void connect() {                                write("IRCBot: Starting connect\n");                  socket = Stdio.File();                                if (!socket) {                                            write("Error: Failed to create socket\n");            return;                                           }                                                                                                           write("IRCBot: Attempting connection to %s:%d\n", config->host, config->port);                              mixed err = catch {                                       if (!socket->connect(config->host, config->port)) {                                                             write("Error: Failed to connect to IRC server, errno: %d\n", socket->errno());                              socket->close();                                      socket = 0;                                           return;                                           }                                                 };                                                    if (err) {                                                write("Error: Connection attempt failed: %O\n", err);                                                       socket->close();                                      socket = 0;                                           return;                                           }                                                     write("IRCBot: Socket connection succeeded\n");                                                                                                                   write("IRCBot: Socket connected, sending initial commands\n");                                              err = catch {                                             socket->write("NICK %s\r\n", config->nick);                                                                 socket->write("USER %s 0 * :%s\r\n", config->nick, config->realname);                                   };                                                    if (err) {                                                write("Error: Failed to send NICK/USER commands: %O\n", err);                                               cleanup();                                            return;                                           }                                                     write("IRCBot: NICK/USER commands sent\n");                                                                 write("IRCBot: Waiting for connection completion\n");                                                       int timeout = 60;                                     while (timeout > 0) {                                     string buffer = socket->read(4096, 1);                if (buffer && buffer != "") {                             array(string) lines = buffer / "\n";                  foreach(lines; ; string line) {                           if (line && line != "") {                                 write("IRCBot: Received: %s\n", line);                                                                      array(string) parts = line / " ";                                                                           if (sizeof(parts) >= 2 && parts[1] == "001") {                                                                  write("IRCBot: Connection completed (001 received)\n");                                                     connected = true;                                     break;                                            }                                                 }                                                 }                                                     if (connected) break;                             } else {                                                  write("IRCBot: Received empty buffer\n");                                                               }                                                     sleep(0.1);                                           timeout--;                                        }                                                     if (!connected) {                                         write("Error: Connection timeout waiting for response\n");                                                  cleanup();                                            return;                                           }                                                                                                           write("IRCBot: Joining channels\n");                  err = catch {                                             foreach(config->channels; ; string channel) {                                                                   socket->write("JOIN %s\r\n", channel);                write("IRCBot: Sent JOIN for %s\n", channel);                                                           }                                                 };                                                    if (err) {                                                write("Error: Failed to join channels: %O\n", err);                                                         cleanup();                                            return;                                           }                                                                                                           write("IRCBot: Connect fully completed\n");       }                                                                                                           protected void cleanup() {                                if (socket) {                                             socket->write("QUIT :Bot shutting down\r\n");                                                               socket->close();                                      socket = 0;                                       }                                                 }                                                                                                           protected void process_message(string line) {             write("Main: Processing RAW: %s\n", line);            array(string) parts = line / " ";                     if (sizeof(parts) < 2) {                                  write("Main: Skipping malformed line\n");             return;                                           }                                                                                                           string prefix = parts[0];                             string cmd = parts[1];                                                                                      if (cmd == "PING") {                                      socket->write("PONG %s\r\n", parts[2..] * " ");                                                             write("Main: Responded to PING\n");               } else if (cmd == "PRIVMSG" && sizeof(parts) >= 4) {                                                            string channel = parts[2];                            string message = parts[3..] * " ";                    if (message[0] == ':') message = message[1..];  // Strip leading colon                                      if (prefix[0] == ':') prefix = prefix[1..];                                                                 string nick = (prefix / "!")[0];                                                                            write(sprintf("Main: Received PRIVMSG from <%s> in %s: %s\n", nick, channel, message));                                                                           if (has_prefix(message, "!hello")) {                      mixed err = catch {                                       socket->write("PRIVMSG %s :Hello %s!\r\n", channel, nick);                                                  write("Main: Sent hello response to %s\n", nick);                                                       };                                                    if (err) {                                                write("Main: Error sending hello response: %O\n", err);                                                 }                                                 }                                                     if (has_prefix(message, "!quit")) {                       write("Main: Received quit command, shutting down\n");                                                      socket->write("QUIT :Bot shutting down\r\n");                                                               connected = false;                                }                                                 }                                                 }                                                                                                           protected void _destruct() {                              if (connected && socket) {                                socket->write("QUIT :Bot shutting down\r\n");                                                           }                                                     cleanup();                                        }                                                                                                           void run() {                                              while (connected && socket) {                             mixed err = catch {                                       string buffer = socket->read(4096, 1);                if (buffer && buffer != "") {                             write("Main: Read buffer: %s\n", buffer);                                                                   array(string) lines = buffer / "\n";                                                                        foreach(lines; ; string line) {                           if (line && line != "") {                                 write("Main: Received line: %s\n", line);                                                                   process_message(line);                            }                                                 }                                                 } else {                                                  write("Main: Read empty buffer\n");                                                                     }                                                 };                                                    if (err) {                                                write("Main: Error in read: %O\n", err);                                                                    connected = false;                                    break;                                            }                                                     sleep(0.01);  // Tight loop for responsiveness                                                          }                                                     write("Main: Bot shutting down\n");               }                                                 }                                                                                                           int main() {                                              write("Main: Starting\n");                            BotConfig config = BotConfig();                       if (!config) {                                            write("Error: Failed to create config\n");            return 1;                                         }                                                     write("Main: Config created\n");                                                                            IRCBot bot = IRCBot(config);                          if (!bot) {                                               write("Error: Failed to create bot\n");               return 1;                                         }                                                     write("Main: Bot created\n");                                                                               bot->run();                                                                                                 return 0;                                         }
Editor is loading...
Leave a Comment