1 // BotInterp.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 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"));
41 scm_gc_protect_object(logPort);
45 BotInterp::Execute(String command)
47 Interp::Execute(bot, command);
51 BotInterp::LoadScript(String filename)
53 Interp::LoadScript(bot, filename);
57 BotInterp::ScriptLog()
64 bool hptr_lt (const Hook* h, const Hook* h1)
65 // Hook Pointer less than
66 // Used to sort the Hooks list
71 BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall,
73 if (scm_string_p(regex) == SCM_BOOL_F)
75 String rx = Utils::to_upper (Utils::scm2str (regex));
76 SCM r = scm_make_regexp(regex,
77 scm_listify (gh_lookup("regexp/icase"),
79 scm_gc_protect_object(r);
80 scm_gc_protect_object(function);
81 // First, we check if an hook doesn't exist yet
82 std::list<Hook *>::iterator it = hooksMap[hooktype].begin();
83 std::list<Hook *>::iterator it2 = hooksMap[hooktype].end();
85 for ( ; it != it2; ++it)
86 // It exists, we replace it.
87 if ((*it)->regex_str == rx && (*it)->name == name) {
88 scm_gc_unprotect_object((*it)->function);
89 scm_gc_unprotect_object (r);
90 (*it)->function = function;
91 (*it)->priority = pri;
92 (*it)->fallthru = fall;
93 hooksMap[hooktype].sort (hptr_lt);
96 // It does not exist, we create it
97 hooksMap[hooktype].push_back (new Hook(hooktype, rx, r,
98 function, pri, fall, name));
99 hooksMap[hooktype].sort (hptr_lt);
104 BotInterp::RunHooks(int hooktype, String match, SCM args)
107 // We want to execute higher priority hooks first, so we start at
108 // the end of the list instead of the beggining
109 std::list<Hook *>::reverse_iterator it = hooksMap[hooktype].rbegin();
110 std::list<Hook *>::reverse_iterator it2 = hooksMap[hooktype].rend();
113 for ( ; it != it2; ++it) {
114 if (scm_regexp_exec((*it)->regex, Utils::str2scm (match),
115 SCM_UNDEFINED, SCM_UNDEFINED) != SCM_BOOL_F)
117 wd.func = (*it)->function;
118 result = scm_internal_catch(SCM_BOOL_T,
120 Interp::LazyApplyWrapper,
121 static_cast<void *> (&wd),
122 (scm_t_catch_handler) Interp::EmptyHandler, 0);
123 if (! (*it)->fallthru)
131 BotInterp::AddTimer(int delay, SCM function)
133 int when = time(NULL) + delay;
135 scm_gc_protect_object(function);
136 Timer *t = new Timer(c, when, function);
137 timersList.push_back(t);
138 return scm_long2num (c);
142 BotInterp::DelTimer(SCM timer)
144 int count = scm_num2long(timer, SCM_ARG1, "BotInterp::DelTimer");
145 std::list<Timer *>::iterator it = timersList.begin();
146 std::list<Timer *>::iterator it2 = timersList.end();
148 for ( ; it != it2; ++it) {
149 if ((*it)->count == count) {
150 scm_gc_unprotect_object((*it)->function);
152 timersList.erase(it);
160 BotInterp::RunTimers(int now)
162 std::list<Timer *>::iterator it = timersList.begin();
163 std::list<Timer *>::iterator it2 = timersList.end();
164 std::list<Timer *>::iterator it3;
166 struct wrapper_data wd;
167 wd.args = scm_listify (SCM_UNDEFINED);
170 if ((*it)->when <= now) {
171 wd.func = (*it)->function;
172 scm_internal_catch(SCM_BOOL_T,
173 (scm_t_catch_body) Interp::LazyApplyWrapper, (void *)&wd,
174 (scm_t_catch_handler) Interp::EmptyHandler, 0);
175 scm_gc_unprotect_object(wd.func);
179 timersList.erase(it);