Remove String->const char* conversion operator in favor of c_str method
[clinton/bobotpp.git] / source / Bot.C
index 741e2e1..cc860b9 100644 (file)
 
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301, USA.
 
-#include <fstream>
-#include <algorithm>
-#include <iomanip>
 #include <cstring>
 #include <cstdlib>
 #include <cstdio>
+
+#include <fstream>
+#include <algorithm>
+#include <iomanip>
+
+#include <dirent.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
+#ifdef USESCRIPTS
+#include <libguile.h>
+#endif
+
 #include "Bot.H"
 #include "DCCConnection.H"
 #include "DCCChatConnection.H"
-#include "StringTokenizer.H"
+#include "ChannelList.H"
+#include "DCCManager.H"
+#include "DCCPerson.H"
+#include "Parser.H"
+#include "Person.H"
+#include "Server.H"
 #include "ServerConnection.H"
-#include "Utils.H"
+#include "ServerList.H"
+#include "ShitList.H"
+#include "StringTokenizer.H"
+#include "User.H"
 #include "UserCommands.H"
-#include "DCCManager.H"
+#include "UserList.H"
+#include "Utils.H"
 
 
+#ifdef USESCRIPTS
+#include "BotInterp.H"
+#include "Interp.H"
+#endif
+
 unsigned int Bot::MAX_MESSAGES = 2;
+unsigned int Bot::MAX_NICKLENGTH = 9;
 
 #define DEFAULT_NICKNAME "Bobot"
 #define DEFAULT_USERNAME "bobot"
@@ -79,9 +103,9 @@ Bot::Bot(String filename, bool debug_on)
     receivedUserhostID(0),
     logFileName(DEFAULT_LOGFILENAME),
     logs_dir (DEFAULT_LOGDIR),
-    configFileName (filename),
+    configFileName (filename)
 #ifdef USESCRIPTS
-    scriptLogFileName(DEFAULT_SCRIPTLOGFILENAME),
+    ,scriptLogFileName(DEFAULT_SCRIPTLOGFILENAME),
     autoexecFileName(DEFAULT_AUTOEXECFILENAME)
 #endif
 {
@@ -103,11 +127,10 @@ Bot::Bot(String filename, bool debug_on)
   readConfig();
   userList = new UserList(userListFileName);
   shitList = new ShitList(shitListFileName);
-  todoList = new TodoList();
   dccConnections = new DCCManager ();
 
   // Let's read the alias file
-  std::ifstream initFile(initFileName);
+  std::ifstream initFile(initFileName.c_str ());
 
   if (initFile) 
     {
@@ -158,6 +181,7 @@ Bot::Bot(String filename, bool debug_on)
 
 #ifdef USESCRIPTS
   botInterp = new BotInterp(this, logs_dir + scriptLogFileName);
+  Interp::Startup2 (this);
   botInterp->LoadScript(autoexecFileName);
 #endif
 }
@@ -183,7 +207,6 @@ Bot::~Bot()
   shitList->save();
   delete channelList;
   delete userList;
-  delete todoList;
   delete serverList;
   delete shitList;
   delete serverConnection;
@@ -212,7 +235,7 @@ Bot::logLine(String line)
 void
 Bot::readConfig()
 {
-  std::ifstream file(configFileName);
+  std::ifstream file(configFileName.c_str ());
   String temp;
   int line = 1;
 
@@ -271,8 +294,20 @@ Bot::readConfig()
          {
            if (parameters[(unsigned int)0] == '/')
              {
-               set_log_dir ("/");
-               set_log_file (parameters.subString (1));
+               StringTokenizer log_st (parameters);
+               std::string log_dir = "/";
+
+               for (unsigned int m = log_st.count_tokens ('/');
+                    --m;
+                    m > 0)
+                 {
+                   log_dir += log_st.next_token ('/') + "/";
+                 }
+
+               std::cerr << "==" << log_dir << std::endl;
+
+               set_log_dir (log_dir);
+               set_log_file (log_st.rest ());
              }
            else
              set_log_file (parameters);
@@ -288,6 +323,8 @@ Bot::readConfig()
       initFileName = parameters;
     else if (command == "LOCALIP")
       localIP = parameters;
+    else if (command == "MAXNICKLENGTH")
+      MAX_NICKLENGTH = std::atoi (parameters.c_str ());
     else if (command == "SERVER") {
       if (parameters.indexOf(' ') == -1)
         serverList->addServer(new Server(parameters));
@@ -300,6 +337,10 @@ Bot::readConfig()
                                          st2.next_token()));
       }
     }
+    else if (command == "")
+      {
+       // do nothing
+      }
     else {
       logLine(String("Syntax error in file ") + configFileName +
               ", line " + String((long)line));
@@ -327,12 +368,13 @@ Bot::run()
        {
          // Disconnected
 #ifdef USESCRIPTS
-         // Run hooks/disconnected
+         // Run hooks/disconnect
          this->botInterp->RunHooks 
            (Hook::DISCONNECT, 
             serverConnection->server->getHostName (),
             scm_list_n 
-            (Utils::str2scm  (serverConnection->server->getHostName ())));
+            (Utils::str2scm  (serverConnection->server->getHostName ()),
+             SCM_BOOL_F));
 #endif
          nextServer();
        }
@@ -400,10 +442,12 @@ Bot::waitForInput()
       if ((*it).second > 0)
         (*it).second--;
 
-    String line;
-    while ((line = todoList->getNext()) != "") {
-      serverConnection->queue->sendChannelMode(line);
-    }
+    for (std::map<String, Channel *, std::less<String> >::iterator it = channelList->begin ();
+        it != channelList->end ();
+        ++it)
+      {
+       it->second->purge_expired_bans ();
+      }
 #ifdef USESCRIPTS
     botInterp->RunTimers(currentTime);
 
@@ -411,7 +455,7 @@ Bot::waitForInput()
     if (thisTime->tm_sec == 0)
       {
        char s[6];
-       std::sprintf(s, "%2d:%2d", thisTime->tm_hour, thisTime->tm_min);
+       std::snprintf(s, 6, "%02d:%02d", thisTime->tm_hour, thisTime->tm_min);
        
        botInterp->RunHooks(Hook::TIMER, String(s),
                            scm_list_n (Utils::str2scm (std::string (s)), 
@@ -709,14 +753,14 @@ Bot::destroy_user_functions ()
 void
 Bot::set_log_file (String name)
 {
-  logFileName = name;
+  logFileName = logs_dir + name;
   logFile.close ();
   logFile.clear ();
 #if HAVE_IOSBASE
-  logFile.open(logs_dir + logFileName, std::ios_base::out | 
+  logFile.open(logFileName.c_str (), std::ios_base::out | 
               std::ios_base::ate | std::ios_base::app);
 #else
-  logFile.open(logs_dir + logFileName, ios::out | ios::ate 
+  logFile.open(logFileName.c_str (), ios::out | ios::ate 
              | ios::app);
 #endif
 
@@ -727,4 +771,15 @@ void
 Bot::set_log_dir (String dir)
 {
   logs_dir = dir;
+
+  DIR *temp = opendir (logs_dir.c_str ());
+
+  if (!temp)
+    {
+      mkdir (logs_dir.c_str (), S_IRWXU);
+    }
+  else
+    {
+      closedir (temp);
+    }
 }