1 // BotInterp.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005,2008 Clinton Ebadi
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "BotInterp.H"
35 #include <libguile/regex-posix.h>
38 BotInterp::BotInterp(Bot *b, String fn)
39 : bot(b), counter(0), timer_mutex (true)
41 logPort = scm_open_file (Utils::str2scm (fn),
42 Utils::str2scm ("a"));
44 scm_gc_protect_object(logPort);
48 BotInterp::Execute(String command)
50 Interp::Execute(bot, command);
54 BotInterp::LoadScript(String filename)
56 Interp::LoadScript(bot, filename);
60 BotInterp::ScriptLog()
67 bool hptr_lt (const Hook* h, const Hook* h1)
68 // Hook Pointer less than
69 // Used to sort the Hooks list
74 BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall,
77 if (scm_string_p(regex) == SCM_BOOL_F)
79 String rx = Utils::to_upper (Utils::scm2str (regex));
80 // fixme: really ought to use scm_c_module_lookup with bot module
81 SCM r = scm_make_regexp (regex,
82 scm_list_n (scm_variable_ref (scm_c_lookup ("regexp/icase")),
84 scm_gc_protect_object(r);
85 scm_gc_protect_object(function);
87 BotLock hook_lock (hook_mutex);
89 // First, we check if an hook doesn't exist yet
90 std::list<Hook *>::iterator it = hooksMap[hooktype].begin();
91 std::list<Hook *>::iterator it2 = hooksMap[hooktype].end();
93 for ( ; it != it2; ++it)
94 // It exists, we replace it.
95 if ((*it)->regex_str == rx && (*it)->name == name) {
96 scm_gc_unprotect_object((*it)->function);
97 scm_gc_unprotect_object (r);
98 (*it)->function = function;
99 (*it)->priority = pri;
100 (*it)->fallthru = fall;
101 hooksMap[hooktype].sort (hptr_lt);
104 // It does not exist, we create it
105 hooksMap[hooktype].push_back (new Hook(hooktype, rx, r,
106 function, pri, fall, name));
107 hooksMap[hooktype].sort (hptr_lt);
112 BotInterp::RunHooks(int hooktype, String match, SCM args)
114 BotLock hook_lock (hook_mutex);
118 // We want to execute higher priority hooks first, so we start at
119 // the end of the list instead of the beggining
120 std::list<Hook *>::reverse_iterator it = hooksMap[hooktype].rbegin();
121 std::list<Hook *>::reverse_iterator it2 = hooksMap[hooktype].rend();
126 for ( ; it != it2; ++it) {
127 if (scm_regexp_exec((*it)->regex, Utils::str2scm (match),
128 SCM_UNDEFINED, SCM_UNDEFINED) != SCM_BOOL_F)
130 wd.func = (*it)->function;
131 result = scm_internal_catch(SCM_BOOL_T,
133 Interp::LazyApplyWrapper,
134 static_cast<void *> (&wd),
135 (scm_t_catch_handler) Interp::EmptyHandler, 0);
136 if (! (*it)->fallthru)
145 BotInterp::AddTimer(int delay, SCM function)
147 BotLock timer_lock (timer_mutex);
148 int when = time(NULL) + delay;
150 scm_gc_protect_object(function);
152 Timer *timer = new Timer (++counter, when, function);
153 TimerList::iterator it = std::find_if (timers.begin (), timers.end (),
154 std::bind1st (timer_sort_p, timer));
156 if (it != timers.end ())
157 timers.insert (it, timer);
159 timers.push_back (timer);
161 return scm_from_int (counter);
166 BotInterp::DelTimer(SCM timer)
168 BotLock timer_lock (timer_mutex);
170 int count = scm_to_int (timer);
171 TimerList::iterator it = timers.begin();
172 TimerList::iterator end = timers.end();
174 for ( ; it != end; ++it)
176 if ((*it)->count == count)
178 scm_gc_unprotect_object((*it)->function);
190 BotInterp::RunTimers(int now)
192 BotLock timer_lock (timer_mutex);
193 struct wrapper_data wd;
194 wd.args = scm_list_n (SCM_UNDEFINED);
196 while (!timers.empty ())
198 // Keep a stack allocated copy of the front of the timer queue
199 // just in case the timer is deleted while being executed (which
200 // is very unlikely as the only place this could occur is if the
201 // timer deleted itself)
202 Timer current_timer = *timers.front () ;
204 if (current_timer.when <= now)
206 wd.func = current_timer.function;
208 scm_internal_catch (SCM_BOOL_T,
209 (scm_t_catch_body) Interp::LazyApplyWrapper,
211 (scm_t_catch_handler) Interp::EmptyHandler,
214 // The timer list may have been modified by the timer
215 // callback; if it has in such a way that the first queue
216 // item has changed (adding a timer in the past) then we
217 // switch the slow path for deleting a timer
218 if (current_timer.count == timers.front()->count)
220 scm_gc_unprotect_object (current_timer.function);
221 delete timers.front ();
226 DelTimer (scm_from_int (current_timer.count));