[project @ 2005-05-31 05:59:57 by unknown_lamer]
[clinton/bobotpp.git] / source / Bot.C
index 45e5c6e..c3a83a1 100644 (file)
@@ -1,6 +1,6 @@
 // Bot.C  -*- C++ -*-
 // Copyright (c) 1997, 1998 Etienne BERNARD
-// Copyright (C) 2002 Clinton Ebadi
+// Copyright (C) 2002,2003,2005 Clinton Ebadi
 
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 
 #include "Bot.H"
 #include "DCCConnection.H"
+#include "DCCChatConnection.H"
 #include "StringTokenizer.H"
 #include "ServerConnection.H"
 #include "Utils.H"
 #include "UserCommands.H"
 #include "DCCManager.H"
 
+
+unsigned int Bot::MAX_MESSAGES = 2;
+
 #define DEFAULT_NICKNAME "Bobot"
 #define DEFAULT_USERNAME "bobot"
 #define DEFAULT_IRCNAME "I'm a bobot++!"
@@ -58,22 +62,28 @@ Bot::Bot(String filename, bool debug_on)
     userHost(""),
     localIP(""),
     commandChar(DEFAULT_COMMANDCHAR),
-    configFileName(filename),
     userListFileName(DEFAULT_USERLISTFILENAME),
     shitListFileName(DEFAULT_SHITLISTFILENAME),
-    logFileName(DEFAULT_LOGFILENAME),
-    logs_dir (DEFAULT_LOGDIR),
     helpFileName(DEFAULT_HELPFILENAME),
     initFileName(DEFAULT_INITFILENAME),
+    connected(false),
+    debug(debug_on), 
+    stop(false), 
+    sentPing(false),
+    startTime(time(NULL)), 
+    currentTime(startTime),
+    lastNickNameChange(startTime), 
+    lastChannelJoin(startTime),
+    serverConnection(0), 
+    sentUserhostID(0), 
+    receivedUserhostID(0),
+    logFileName(DEFAULT_LOGFILENAME),
+    logs_dir (DEFAULT_LOGDIR),
+    configFileName (filename),
 #ifdef USESCRIPTS
     scriptLogFileName(DEFAULT_SCRIPTLOGFILENAME),
-    autoexecFileName(DEFAULT_AUTOEXECFILENAME),
+    autoexecFileName(DEFAULT_AUTOEXECFILENAME)
 #endif
-    connected(false),
-    debug(debug_on), stop(false), sentPing(false),
-    startTime(time(NULL)), currentTime(startTime),
-    lastNickNameChange(startTime), lastChannelJoin(startTime),
-    serverConnection(0), sentUserhostID(0), receivedUserhostID(0)
 {
 #ifdef HAVE_STL_CLEAR
   wantedChannels.clear();
@@ -105,25 +115,25 @@ Bot::Bot(String filename, bool debug_on)
     while (initFile >> temp, temp.length() != 0) {
       line++;
       StringTokenizer st(temp);
-      temp = temp.trim();
-      if (temp[0]=='#') continue;
-      if (st.countTokens(' ') != 2) {
+      temp = Utils::trim_str (temp);
+      if (temp[(unsigned int)0]=='#') continue;
+      if (st.count_tokens (' ') != 2) {
         std::cerr << "Error when reading alias file (" << initFileName 
                  << ") line " << line << "...\n";
         continue;
       }
-      alias = st.nextToken().toUpper();
-      command = st.nextToken().toUpper();
+      alias = Utils::to_upper (st.next_token());
+      command = Utils::to_upper (st.next_token());
 
       // Does the function already exist ?
       if (!userFunctions[alias])
        {
          if (userFunction *u = userFunctions[command])
-           userFunctions[alias] = 
-             new
-             userFunction(u->function,
-                          u->minLevel,
-                          u->needsChannelName);
+           userFunctions[alias] = 
+             new
+             userFunction(u->function,
+                          u->minLevel,
+                          u->needsChannelName);
        }
     }
   }
@@ -199,14 +209,14 @@ Bot::readConfig()
 
     file >> temp;
 
-    if (temp.length() == 0 || temp[0] == '#') {
+    if (temp.length() == 0 || temp[(unsigned int)0] == '#') {
       line++;
       continue;
     }
     
     StringTokenizer st(temp);
-    String command = st.nextToken('=').trim().toUpper();
-    String parameters = st.nextToken('=').trim();
+    String command = Utils::to_upper (Utils::trim_str (st.next_token('=')));
+    String parameters = Utils::trim_str (st.next_token('='));
 
     if (command == "NICK" || command == "NICKNAME")
       nickName = wantedNickName = parameters;
@@ -215,7 +225,7 @@ Bot::readConfig()
     else if (command == "IRCNAME" || command == "REALNAME")
       ircName = parameters;
     else if (command == "CMDCHAR" || command == "COMMAND")
-      commandChar = parameters[0];
+      commandChar = parameters[(unsigned int)0];
     else if (command == "USERLIST")
       userListFileName = parameters;
     else if (command == "SHITLIST")
@@ -227,15 +237,15 @@ Bot::readConfig()
                  << " I will use compatibility mode, but you're really"
                  << " missing something.\n";
         StringTokenizer st2(parameters);
-        String name = st2.nextToken().toLower();
-        String key = st2.nextToken();
+        String name = Utils::to_lower (st2.next_token());
+        String key = st2.next_token();
         wantedChannels[name] = new wantedChannel("", "", key);
       } else {
         StringTokenizer st2(parameters);
-        String name = st2.nextToken(':').toLower();
-        String mode = st2.nextToken(':');
-        String keep = st2.nextToken(':');
-        String key = st2.nextToken(':');
+        String name = Utils::to_lower (st2.next_token(':'));
+        String mode = st2.next_token(':');
+        String keep = st2.next_token(':');
+        String key = st2.next_token(':');
         wantedChannels[name] = new wantedChannel(mode, keep, key);
       }
     }
@@ -243,7 +253,7 @@ Bot::readConfig()
       {
        if (parameters != logFileName)
          {
-           if (parameters[0] == '/')
+           if (parameters[(unsigned int)0] == '/')
              {
                set_log_dir ("/");
                set_log_file (parameters.subString (1));
@@ -267,11 +277,11 @@ Bot::readConfig()
         serverList->addServer(new Server(parameters));
       else {
         StringTokenizer st2(parameters);
-        String name = st2.nextToken();
-        int port = std::atoi(st2.nextToken());
+        String name = st2.next_token();
+        int port = std::atoi(st2.next_token().c_str());
         serverList->addServer(new Server(name,
                                          port,
-                                         st2.nextToken()));
+                                         st2.next_token()));
       }
     }
     else {
@@ -320,10 +330,10 @@ Bot::waitForInput()
   FD_SET(sock, &rd);
 #endif
 
-  DCC_MAP* dccmap = dccConnections->MAP ();
+  DCC_MAP* dccmap = &dccConnections->dcc_map;
   for (DCC_MAP::iterator it = dccmap->begin ();
        it != dccmap->end(); ++it) {
-    int s = it->second->DCC()->getFileDescriptor();
+    int s = it->second->dcc->getFileDescriptor();
 #ifdef _HPUX_SOURCE
     rd |= s;
 #else
@@ -350,29 +360,11 @@ Bot::waitForInput()
       if (serverConnection->handleInput())
         nextServer();
 
-//     std::list<DCCConnection *>::iterator it = dccConnections.begin();
-//     std::list<DCCConnection *>::iterator it2;
-
-//     while (it != dccConnections.end()) {
-//       it2 = it;
-//       ++it;
-// #ifdef _HPUX_SOURCE
-//       if (rd & (*it2)->getFileDescriptor()) {
-// #else
-//      if (FD_ISSET((*it2)->getFileDescriptor(), &rd)) {
-// #endif
-//         if ((*it2)->handleInput()) {
-//           delete *it2;
-//           dccConnections.erase(it2);
-//         }
-//       }
-//     }
-//     }
     dccConnections->checkInput (rd);
   }
 
-  if (currentTime < std::time(NULL)) { // Actions that we do each second
-    currentTime = std::time(NULL);
+  if (currentTime < std::time(0)) { // Actions that we do each second
+    currentTime = std::time(0);
     for (std::map<String, unsigned int, std::less<String> >::iterator
            it = ignoredUserhosts.begin();
          it != ignoredUserhosts.end(); ++it)
@@ -385,16 +377,17 @@ Bot::waitForInput()
     }
 #ifdef USESCRIPTS
     botInterp->RunTimers(currentTime);
-#endif
 
-#ifdef USESCRIPTS
     tm *thisTime = localtime(&currentTime);
-    if (thisTime->tm_sec == 0) {
-      char s[6];
-      sprintf(s, "%2d:%2d", thisTime->tm_hour, thisTime->tm_min);
-      botInterp->RunHooks(Hook::TIMER, String(s),
-                          gh_list(Utils::string2SCM(String(s)), SCM_UNDEFINED));
-    }
+    if (thisTime->tm_sec == 0)
+      {
+       char s[6];
+       std::sprintf(s, "%2d:%2d", thisTime->tm_hour, thisTime->tm_min);
+       // FIXME: uses gh_list
+       botInterp->RunHooks(Hook::TIMER, String(s),
+                           gh_list(Utils::str2scm (std::string (s)), 
+                                   SCM_UNDEFINED));
+      }
 #endif
 
   }
@@ -414,18 +407,6 @@ Bot::waitForInput()
          serverConnection->queue->sendJoin((*it).first, (*it).second->key);
   }
 
-//   std::list<DCCConnection *>::iterator it2;
-
-//   for (std::list<DCCConnection *>::iterator it = dccConnections.begin();
-//        it != dccConnections.end(); ) {
-//     it2 = it;
-//     ++it;
-//     if ((*it2)->autoRemove && currentTime >= (std::time_t)((*it2)->lastSpoken + Bot::DCC_DELAY)) {
-//       delete *it2;
-//       dccConnections.erase(it2);
-//     }
-//   }
-
   if (currentTime >= (std::time_t)(serverConnection->serverLastSpoken + Bot::PING_TIME) && !sentPing) {
     serverConnection->queue->sendPing("Testing connection");
     sentPing = true;
@@ -520,10 +501,15 @@ Bot::connect(int serverNumber)
 }
 
 void
-Bot::addDCC(Person * from, unsigned long address, int port)
+Bot::addDCC(Person * from, unsigned long address, int port, int type)
 {
-  DCCConnection * d = new DCCConnection(this, from->getAddress (),
-                                        address, port);
+  DCCConnection *d = 0;
+
+  if (type == CHAT)
+    d = new DCCChatConnection(this, from->getAddress (),
+                             address, port);
+  else
+    return;
 
   if (!d->connect())
     {
@@ -643,6 +629,8 @@ Bot::init_user_functions ()
   userFunctions["SERVER"] = uf (UserCommands::Server, User::FRIEND, false);
   userFunctions["SERVERLIST"] =
     uf (UserCommands::ServerList, User::FRIEND, false);
+  userFunctions["SETFLOODRATE"] =
+    uf (UserCommands::SetFloodRate, User::MASTER, false);
   userFunctions["SETVERSION"] =
     uf (UserCommands::SetVersion, User::MASTER, false);
   userFunctions["SHITLIST"] =