[project @ 2002-08-02 04:31:30 by unknown_lamer]
[clinton/bobotpp.git] / source / BotInterp.C
index cedd0ac..e6659a3 100644 (file)
@@ -61,8 +61,17 @@ BotInterp::ScriptLog(SCM throw_args)
   scm_flush(logPort);
 }
 
+namespace
+{
+  bool hptr_lt (const Hook* h, const Hook* h1)
+    // Hook Pointer less than
+    // Used to sort the Hooks list
+  { return *h < *h1; }
+}
+
 bool
-BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall) {
+BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall,
+                  String name) {
   if (scm_string_p(regex) == SCM_BOOL_F)
     return false;
   String rx = Utils::scm2String(regex).toUpper();
@@ -74,21 +83,22 @@ BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall) {
   // First, we check if an hook doesn't exist yet
   std::list<Hook *>::iterator it = hooksMap[hooktype].begin();
   std::list<Hook *>::iterator it2 = hooksMap[hooktype].end();
+
   for ( ; it != it2; ++it)
     // It exists, we replace it.
-    if ((*it)->regex_str == rx) {
+    if ((*it)->regex_str == rx && (*it)->name == name) {
       scm_gc_unprotect_object((*it)->function);
       scm_gc_unprotect_object (r);
       (*it)->function = function;
       (*it)->priority = pri;
       (*it)->fallthru = fall;
-      hooksMap[hooktype].sort ();
+      hooksMap[hooktype].sort (hptr_lt);
       return true;
     }
   // It does not exist, we create it
   hooksMap[hooktype].push_back (new Hook(hooktype, rx, r, 
-                                        function, pri, fall));
-  hooksMap[hooktype].sort ();
+                                        function, pri, fall, name));
+  hooksMap[hooktype].sort (hptr_lt);
   return true;
 }
 
@@ -96,8 +106,10 @@ bool
 BotInterp::RunHooks(int hooktype, String match, SCM args)
 {
   SCM result;
-  std::list<Hook *>::iterator it = hooksMap[hooktype].begin();
-  std::list<Hook *>::iterator it2 = hooksMap[hooktype].end();
+  // We want to execute higher priority hooks first, so we start at
+  // the end of the list instead of the beggining
+  std::list<Hook *>::reverse_iterator it = hooksMap[hooktype].rbegin();
+  std::list<Hook *>::reverse_iterator it2 = hooksMap[hooktype].rend();
   wrapper_data wd;
   wd.args = args;
   for ( ; it != it2; ++it) {