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"
33 #include <libguile/regex-posix.h>
36 BotInterp::BotInterp(Bot *b, String fn)
39 logPort = scm_open_file (Utils::str2scm (fn),
40 Utils::str2scm ("a"));
42 scm_gc_protect_object(logPort);
46 BotInterp::Execute(String command)
48 Interp::Execute(bot, command);
52 BotInterp::LoadScript(String filename)
54 Interp::LoadScript(bot, filename);
58 BotInterp::ScriptLog()
65 bool hptr_lt (const Hook* h, const Hook* h1)
66 // Hook Pointer less than
67 // Used to sort the Hooks list
72 BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall,
74 if (scm_string_p(regex) == SCM_BOOL_F)
76 String rx = Utils::to_upper (Utils::scm2str (regex));
77 // fixme: really ought to use scm_c_module_lookup with bot module
78 SCM r = scm_make_regexp (regex,
79 scm_list_n (scm_variable_ref (scm_c_lookup ("regexp/icase")),
81 scm_gc_protect_object(r);
82 scm_gc_protect_object(function);
83 // First, we check if an hook doesn't exist yet
84 std::list<Hook *>::iterator it = hooksMap[hooktype].begin();
85 std::list<Hook *>::iterator it2 = hooksMap[hooktype].end();
87 for ( ; it != it2; ++it)
88 // It exists, we replace it.
89 if ((*it)->regex_str == rx && (*it)->name == name) {
90 scm_gc_unprotect_object((*it)->function);
91 scm_gc_unprotect_object (r);
92 (*it)->function = function;
93 (*it)->priority = pri;
94 (*it)->fallthru = fall;
95 hooksMap[hooktype].sort (hptr_lt);
98 // It does not exist, we create it
99 hooksMap[hooktype].push_back (new Hook(hooktype, rx, r,
100 function, pri, fall, name));
101 hooksMap[hooktype].sort (hptr_lt);
106 BotInterp::RunHooks(int hooktype, String match, SCM args)
109 // We want to execute higher priority hooks first, so we start at
110 // the end of the list instead of the beggining
111 std::list<Hook *>::reverse_iterator it = hooksMap[hooktype].rbegin();
112 std::list<Hook *>::reverse_iterator it2 = hooksMap[hooktype].rend();
115 for ( ; it != it2; ++it) {
116 if (scm_regexp_exec((*it)->regex, Utils::str2scm (match),
117 SCM_UNDEFINED, SCM_UNDEFINED) != SCM_BOOL_F)
119 wd.func = (*it)->function;
120 result = scm_internal_catch(SCM_BOOL_T,
122 Interp::LazyApplyWrapper,
123 static_cast<void *> (&wd),
124 (scm_t_catch_handler) Interp::EmptyHandler, 0);
125 if (! (*it)->fallthru)
133 BotInterp::AddTimer(int delay, SCM function)
135 int when = time(NULL) + delay;
137 scm_gc_protect_object(function);
138 Timer *t = new Timer(c, when, function);
139 timersList.push_back(t);
140 return scm_from_int (c);
144 BotInterp::DelTimer(SCM timer)
146 int count = scm_to_int (timer);
147 std::list<Timer *>::iterator it = timersList.begin();
148 std::list<Timer *>::iterator it2 = timersList.end();
150 for ( ; it != it2; ++it) {
151 if ((*it)->count == count) {
152 scm_gc_unprotect_object((*it)->function);
154 timersList.erase(it);
162 BotInterp::RunTimers(int now)
164 std::list<Timer *>::iterator it = timersList.begin();
165 std::list<Timer *>::iterator it2 = timersList.end();
166 std::list<Timer *>::iterator it3;
168 struct wrapper_data wd;
169 wd.args = scm_list_n (SCM_UNDEFINED);
172 if ((*it)->when <= now) {
173 wd.func = (*it)->function;
174 scm_internal_catch(SCM_BOOL_T,
175 (scm_t_catch_body) Interp::LazyApplyWrapper, (void *)&wd,
176 (scm_t_catch_handler) Interp::EmptyHandler, 0);
177 scm_gc_unprotect_object(wd.func);
181 timersList.erase(it);