[project @ 2005-06-01 00:27:44 by unknown_lamer]
[clinton/bobotpp.git] / source / Interp.C
index 7f82cfe..3bd757b 100644 (file)
@@ -29,7 +29,9 @@
 
 #include <libguile.h>
 
+// static class member initial definitions
 Bot * Interp::bot = 0;
+SCM Interp::bot_module = 0;
 #ifdef MULTITHREAD
 pthread_mutex_t Interp::mutex = PTHREAD_MUTEX_INITIALIZER;
 #endif
@@ -39,16 +41,28 @@ typedef SCM (*SCMFunc)();
 SCM
 scm_apply_wrapper(void *data)
 {
+#ifdef MULTITHREAD
+  //  pthread_mutex_lock (&Interp::mutex);
+#endif
+
   wrapper_data * wd = static_cast<wrapper_data *> (data);
   scm_apply(wd->func, wd->args, SCM_EOL);
+
+#ifdef MULTITHREAD
+  //  pthread_mutex_unlock (&Interp::mutex);
+#endif
+
   return SCM_BOOL_T;
 }
 
-#define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b)
+#define bot_new_procedure(a, b, c, d, e) scm_c_define_gsubr (a, c, d, e, b); scm_c_export (a, 0)
+#define scm_c_define_gsubr(a, b, c, d, e) scm_c_define_gsubr (a, b, c, d, e); scm_c_export (a, 0)
+#define scm_c_define(a, b) scm_c_define (a, b); scm_c_export (a, 0)
 
 void
-Interp::Startup()
+interp_init_helper (void* unused)
 {
+  scm_c_use_module ("guile-user");
   // Hooks
   scm_c_define ("bot:exit-hook", scm_make_hook (scm_long2num (0)));
 
@@ -104,6 +118,8 @@ Interp::Startup()
   scm_c_define_gsubr ("bot:addshit", 3, 2, 0, 
                      (SCMFunc)ScriptCommands::AddShit);
   bot_new_procedure ("bot:ban", (SCMFunc)ScriptCommands::Action, 2, 0, 0);
+  bot_new_procedure ("bot:change-command-level", (SCMFunc)ScriptCommands::ChangeCommandLevel,
+                    2, 0, 0);
   bot_new_procedure ("bot:cycle", (SCMFunc)ScriptCommands::Action, 1, 0, 0);
   bot_new_procedure ("bot:deban", (SCMFunc)ScriptCommands::Deban, 2, 0, 0);
   bot_new_procedure ("bot:delserver", (SCMFunc)ScriptCommands::DelServer, 
@@ -131,6 +147,8 @@ Interp::Startup()
                     0, 0, 0);
   bot_new_procedure ("bot:say", (SCMFunc)ScriptCommands::Say, 2, 0, 0);
   bot_new_procedure ("bot:server", (SCMFunc)ScriptCommands::Server, 1, 0, 0);
+  scm_c_define_gsubr ("bot:setfloodrate", 1, 0, 0,
+                     (SCMFunc)ScriptCommands::SetFloodRate);
   bot_new_procedure ("bot:setversion", (SCMFunc)ScriptCommands::SetVersion, 
                     1, 0, 0);
   bot_new_procedure ("bot:tban", (SCMFunc)ScriptCommands::TBan, 3, 0, 0);
@@ -167,13 +185,34 @@ Interp::Startup()
   // "Low Level" Message functuions
   scm_c_define_gsubr ("bot:send-CTCP", 3, 0, 0,
                      (SCMFunc)ScriptCommands::sendCTCP);
+}
+
+#undef bot_new_procedure
+#undef scm_c_define_gsubr
+#undef scm_c_define
 
+
+SCM
+interp_post_startup_helper (void *bot_module)
+{
+  SCM module = static_cast<SCM> (bot_module);
+  scm_c_define ("the-bot-module", module);
+  scm_c_export ("the-bot-module", 0);
   // load bobot-utils
   scm_primitive_load 
     (scm_makfrom0str (String(PREFIX) + 
                      "/share/bobotpp/scripts/bobot-utils.scm"));
+  return 0;
+}
 
-
+void
+Interp::Startup()
+{
+  bot_module = scm_c_define_module ("the-bot-module", 
+                                   interp_init_helper, 0);
+  scm_c_call_with_current_module (bot_module,
+                                 interp_post_startup_helper,
+                                 bot_module);
 }
 
 void
@@ -189,14 +228,23 @@ Interp::Execute(Bot *b, String command)
   // We get the lock
   pthread_mutex_lock(&mutex);
 #endif
+  
   bot = b;
-  gh_eval_str_with_catch(command, ErrorHandler);
+  gh_eval_str_with_catch (command, ErrorHandler);
+  
 #ifdef MULTITHREAD
   // We release the lock
   pthread_mutex_unlock(&mutex);
 #endif
 }
 
+void load_script_helper (void* file)
+{
+  String filename = *static_cast<String*> (file);
+  scm_c_use_module ("guile-user");
+  gh_eval_file_with_catch(filename, Interp::ErrorHandler);
+}
+
 void
 Interp::LoadScript(Bot *b, String filename)
 {
@@ -205,6 +253,7 @@ Interp::LoadScript(Bot *b, String filename)
   pthread_mutex_lock(&mutex);
 #endif
   bot = b;
+  //scm_c_define_module ("", load_script_helper, &filename);
   gh_eval_file_with_catch(filename, ErrorHandler);
 #ifdef MULTITHREAD
   // We release the lock