Ejemplo n.º 1
0
// initialises the irc bot.
bool IrcBot::init(string channel, string password, string saveFile) {

  // make directories that might not exist
  systemUtils.runProcessWithReturn("mkdir -p history/");

  const char* touchCommand = ("touch "+saveFile).c_str();
  systemUtils.runProcessWithReturn(touchCommand);
  dataFile = saveFile;

  channelSendString = "PRIVMSG "+channel+" :";
  channelName = channel;
  this->channel = channel;

  // initialise connections to the IRC server
  struct addrinfo hints, *serverInfo;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;

  luaInterface = LuaInterface(dataFile);
  luaInterface.initState(this);

  pythonInterface = PythonInterface(dataFile);
  pythonInterface.initState(this);

  systemUtils = SystemUtils();

  cout << "connecting to server..." << endl;

  if ((getaddrinfo("irc.freenode.net","6667",&hints,&serverInfo)) != 0) {
    cout << "Error getting address information. Exiting.";
    return false;
  }

  // set up the socket
  if ((connectionSocket = socket(serverInfo->ai_family,serverInfo->ai_socktype,serverInfo->ai_protocol)) < 0) {
    cout << "Error with initialising the socket. Exiting.";
    return false;
  }

  // connect to the server
  if (connect(connectionSocket,serverInfo->ai_addr, serverInfo->ai_addrlen) < 0) {
      cout << "Error with connecting to the server. Exiting.";
      close (connectionSocket);
      return false;
  }
  
  freeaddrinfo(serverInfo);

  // send nick information
  sendMessage("NICK "+nick);
  checkAndParseMessages();

  // send user information
  sendMessage(user);
  checkAndParseMessages();

  // send authentication to NickServ if bot has been set with password
  if (password != "") {
    string authMessage = "PRIVMSG NickServ : identify " + nick + " " + password;
    sendMessage(authMessage);
  }
  checkAndParseMessages();

  // join the channel
  string joinMessage = "JOIN ";
  joinMessage += channel;
  sendMessage(joinMessage);

  checkAndParseMessages();

  /* we're now on the channel, start the pingTimer */
  pingTimer.start();
  uptimeTimer.start();

  return true;
}