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
26 #include "BotInterp.H"
38 #include <libguile/regex-posix.h>
42 Hook::operator< (const Hook & h) const
44 if (priority < h.priority)
48 else if (priority > h.priority)
52 else if (fallthru && h.fallthru)
56 else if (fallthru && !h.fallthru)
60 else if (!fallthru && h.fallthru)
66 // NOTE: This should never be reached
71 BotInterp::BotInterp(Bot *b, String fn)
73 hook_mutex (true), timer_mutex (true)
75 logPort = scm_open_file (Utils::str2scm (fn),
76 Utils::str2scm ("a"));
78 scm_gc_protect_object(logPort);
82 BotInterp::Execute(String command)
84 Interp::Execute(bot, command);
88 BotInterp::LoadScript(String filename)
90 Interp::LoadScript(bot, filename);
94 BotInterp::ScriptLog()
106 HookFind (std::string r, std::string n)
110 bool operator() (const Hook * hook) const
111 { return hook->regex_str == rx && hook->name == name; }
116 BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall,
119 if (scm_string_p(regex) == SCM_BOOL_F)
122 BotLock hook_lock (hook_mutex);
123 std::string rx = Utils::to_upper (Utils::scm2str (regex));
124 SCM r = scm_make_regexp (regex,
125 scm_list_n (scm_variable_ref (scm_c_lookup ("regexp/icase")),
127 HookFind hook_find (rx, name);
128 HookList& hook_list = hooks[hooktype];
130 scm_gc_protect_object(r);
131 scm_gc_protect_object(function);
133 HookList::iterator it = std::find_if (hook_list.begin (),
137 if (it != hook_list.end())
141 scm_gc_unprotect_object(found->function);
142 scm_gc_unprotect_object (r);
144 found->function = function;
145 found->priority = pri;
146 found->fallthru = fall;
148 hook_list.erase (it);
149 Utils::push_sorted (hook_list, found, hook_sort_p);
155 Utils::push_sorted (hook_list,
156 new Hook(hooktype, rx, r, function, pri, fall, name),
164 BotInterp::RunHooks(int hooktype, std::string match, SCM args)
166 BotLock hook_lock (hook_mutex);
172 // We want to execute higher priority hooks first, so we start at
173 // the end of the list instead of the beginning
175 for (HookList::reverse_iterator it = hooks[hooktype].rbegin();
176 it != hooks[hooktype].rend();
179 std::cerr << "Matching...\n";
180 if (scm_regexp_exec((*it)->regex, Utils::str2scm (match),
181 SCM_UNDEFINED, SCM_UNDEFINED) != SCM_BOOL_F)
183 std::cerr << " Match " << (*it)->regex_str << std::endl;
184 bool fallthru_p = (*it)->fallthru;
185 wd.func = (*it)->function;
186 result = scm_internal_catch(SCM_BOOL_T,
188 Interp::LazyApplyWrapper,
189 static_cast<void *> (&wd),
190 (scm_t_catch_handler) Interp::EmptyHandler, 0);
200 BotInterp::AddTimer(int delay, SCM function)
202 BotLock timer_lock (timer_mutex);
203 int when = time(NULL) + delay;
205 scm_gc_protect_object(function);
207 Timer *timer = new Timer (++counter, when, function);
208 Utils::push_sorted (timers, timer, timer_sort_p);
210 return scm_from_int (counter);
215 BotInterp::DelTimer(SCM timer)
217 BotLock timer_lock (timer_mutex);
219 int count = scm_to_int (timer);
220 TimerList::iterator it = timers.begin();
221 TimerList::iterator end = timers.end();
223 for ( ; it != end; ++it)
225 if ((*it)->count == count)
227 scm_gc_unprotect_object((*it)->function);
239 BotInterp::RunTimers(int now)
241 BotLock timer_lock (timer_mutex);
242 struct wrapper_data wd;
243 wd.args = scm_list_n (SCM_UNDEFINED);
245 while (!timers.empty ())
247 // Keep a stack allocated copy of the front of the timer queue
248 // just in case the timer is deleted while being executed (which
249 // is very unlikely as the only place this could occur is if the
250 // timer deleted itself)
251 Timer current_timer = *timers.front () ;
253 if (current_timer.when <= now)
255 wd.func = current_timer.function;
257 scm_internal_catch (SCM_BOOL_T,
258 (scm_t_catch_body) Interp::LazyApplyWrapper,
260 (scm_t_catch_handler) Interp::EmptyHandler,
263 // The timer list may have been modified by the timer
264 // callback; if it has in such a way that the first queue
265 // item has changed (adding a timer in the past) then we
266 // switch the slow path for deleting a timer
267 if (current_timer.count == timers.front()->count)
269 scm_gc_unprotect_object (current_timer.function);
270 delete timers.front ();
275 DelTimer (scm_from_int (current_timer.count));