Make Scheme hooks and timers threadsafe
[clinton/bobotpp.git] / source / BotInterp.H
index 3485724..4929058 100644 (file)
@@ -1,6 +1,6 @@
 // BotInterp.H  -*- C++ -*-
 // Copyright (c) 1998 Etienne BERNARD
-// Copyright (C) 2002 Clinton Ebadi
+// Copyright (C) 2002,2005,2008 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
@@ -14,7 +14,8 @@
 
 // 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.
 
 #ifndef BOTINTERP_H
 #define BOTINTERP_H
 #ifdef USESCRIPTS
 
 #include <ctime>
+#include <map>
+#include <list>
+#include <functional>
+
 #include <libguile.h>
+
+#include "BotThreading.H"
 #include "String.H"
+#include "Utils.H"
 
 class Bot;
 
@@ -37,31 +45,53 @@ struct Hook {
   bool fallthru;
 
   String regex_str;
+  String name;
   SCM regex;
   SCM function;
 
-  Hook(int t, String rs, SCM r, SCM f, int p, bool ft)
+  Hook(int t, String rs, SCM r, SCM f, int p, bool ft, String n="DEFAULT")
     : type(t), priority (p), fallthru (ft), regex_str(rs), 
-      regex(r), function(f)  { }
+      name (n), regex(r), function(f)  { }
 
   bool operator< (const Hook &h) const
   {
-    if (fallthru && h.fallthru)
-      return priority < h.priority;
-    // fallthru-thru is > non-fallthru thru ALWAYS
+    if (priority < h.priority)
+      {
+       return true;
+      }
+    else if (priority > h.priority)
+      {
+       return false;
+      }
+    else if (fallthru && h.fallthru)
+      {
+       return false;
+      }
     else if (fallthru && !h.fallthru)
-      return false;
+      {
+       return false;
+      }
     else if (!fallthru && h.fallthru)
-      return true;
+      {
+       return true;
+      }
     else
-      return priority < h.priority;
+      {
+       // NOTE: This should never be reached
+       return false;
+      }
   }
 
   enum {
     ACTION, NICKNAME, SIGNOFF, CTCP, CTCP_REPLY,
-    DISCONNECT, FLOOD, INVITE, JOIN, KICK, LEAVE,
-    MODE, MESSAGE, NAMES, NOTICE, PUBLIC,
-    PUBLIC_NOTICE, RAW, TIMER, TOPIC
+    DISCONNECT, FLOOD, INVITE, JOIN, KICK, MODE, 
+    MESSAGE, NAMES, NOTICE, PART, PUBLIC,
+    PUBLIC_NOTICE, RAW, TIMER, TOPIC,
+    // send hooks
+    SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
+    SEND_WHO, SEND_WHOIS,
+    // DCC hooks
+    DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
   };  
 };
 
@@ -72,24 +102,32 @@ struct Timer {
 
   Timer(int c, std::time_t t, SCM f)
     : count(c), when(t), function(f) { }
+
+  bool operator< (const Timer & other) const
+  { return when < other.when; }
 };
 
 class BotInterp {
+  typedef std::list<Timer *> TimerList;
+  
   Bot * bot;
   SCM logPort;
   std::map<int, std::list<Hook *>, std::less<int> > hooksMap;
-  std::list<Timer *> timersList;
+  TimerList timers;
+  Utils::IndirectPred<Timer, std::less<Timer> > timer_sort_p;
   int counter;
+  BotMutex hook_mutex;
+  BotMutex timer_mutex; // NOTE: recursive lock
 
 public:
   BotInterp(Bot *, String);
 
-  void ScriptLog(SCM);
+  SCM ScriptLog();
 
   void Execute(String);
   void LoadScript(String);
 
-  bool AddHook(int, SCM, SCM, int, bool);
+  bool AddHook(int, SCM, SCM, int, bool, String);
   bool RunHooks(int, String, SCM);
 
   SCM AddTimer(int, SCM);