Bump version to 2.2.3 and update copyright notice
[clinton/bobotpp.git] / source / BotInterp.C
CommitLineData
cb21075d 1// BotInterp.C -*- C++ -*-
2// Copyright (c) 1998 Etienne BERNARD
ae97d6ec 3// Copyright (C) 2002,2005 Clinton Ebadi
cb21075d 4
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.
9
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.
14
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
ae97d6ec 17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18// 02110-1301, USA.
cb21075d 19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include "Utils.H"
25#include "Bot.H"
26#include "BotInterp.H"
27
28#ifdef USESCRIPTS
29
30#include <libguile.h>
31extern "C"
32{
33#include <libguile/regex-posix.h>
34}
35
36BotInterp::BotInterp(Bot *b, String fn)
37 : bot(b), counter(0)
38{
db098a03 39 logPort = scm_open_file (Utils::str2scm (fn),
40 Utils::str2scm ("a"));
41
cb21075d 42 scm_gc_protect_object(logPort);
43}
44
45void
46BotInterp::Execute(String command)
47{
48 Interp::Execute(bot, command);
49}
50
51void
52BotInterp::LoadScript(String filename)
53{
54 Interp::LoadScript(bot, filename);
55}
56
d56bdd22 57SCM
58BotInterp::ScriptLog()
cb21075d 59{
d56bdd22 60 return logPort;
cb21075d 61}
62
be3612f3 63namespace
64{
65 bool hptr_lt (const Hook* h, const Hook* h1)
66 // Hook Pointer less than
67 // Used to sort the Hooks list
68 { return *h < *h1; }
69}
70
cb21075d 71bool
fd7440f1 72BotInterp::AddHook(int hooktype, SCM regex, SCM function, int pri, bool fall,
73 String name) {
cb21075d 74 if (scm_string_p(regex) == SCM_BOOL_F)
75 return false;
a6339323 76 String rx = Utils::to_upper (Utils::scm2str (regex));
cb21075d 77 SCM r = scm_make_regexp(regex,
78 scm_listify (gh_lookup("regexp/icase"),
79 SCM_UNDEFINED));
80 scm_gc_protect_object(r);
81 scm_gc_protect_object(function);
82 // First, we check if an hook doesn't exist yet
83 std::list<Hook *>::iterator it = hooksMap[hooktype].begin();
84 std::list<Hook *>::iterator it2 = hooksMap[hooktype].end();
be3612f3 85
cb21075d 86 for ( ; it != it2; ++it)
87 // It exists, we replace it.
fd7440f1 88 if ((*it)->regex_str == rx && (*it)->name == name) {
cb21075d 89 scm_gc_unprotect_object((*it)->function);
90 scm_gc_unprotect_object (r);
91 (*it)->function = function;
92 (*it)->priority = pri;
93 (*it)->fallthru = fall;
be3612f3 94 hooksMap[hooktype].sort (hptr_lt);
cb21075d 95 return true;
96 }
97 // It does not exist, we create it
98 hooksMap[hooktype].push_back (new Hook(hooktype, rx, r,
fd7440f1 99 function, pri, fall, name));
be3612f3 100 hooksMap[hooktype].sort (hptr_lt);
cb21075d 101 return true;
102}
103
104bool
105BotInterp::RunHooks(int hooktype, String match, SCM args)
106{
107 SCM result;
ad529fde 108 // We want to execute higher priority hooks first, so we start at
109 // the end of the list instead of the beggining
110 std::list<Hook *>::reverse_iterator it = hooksMap[hooktype].rbegin();
111 std::list<Hook *>::reverse_iterator it2 = hooksMap[hooktype].rend();
cb21075d 112 wrapper_data wd;
113 wd.args = args;
114 for ( ; it != it2; ++it) {
a6339323 115 if (scm_regexp_exec((*it)->regex, Utils::str2scm (match),
cb21075d 116 SCM_UNDEFINED, SCM_UNDEFINED) != SCM_BOOL_F)
117 {
118 wd.func = (*it)->function;
d56bdd22 119 result = scm_internal_catch(SCM_BOOL_T,
ae97d6ec 120 (scm_t_catch_body)
121 Interp::LazyApplyWrapper,
d56bdd22 122 static_cast<void *> (&wd),
ae97d6ec 123 (scm_t_catch_handler) Interp::EmptyHandler, 0);
cb21075d 124 if (! (*it)->fallthru)
125 break;
126 }
127 }
128 return true;
129}
130
131SCM
132BotInterp::AddTimer(int delay, SCM function)
133{
134 int when = time(NULL) + delay;
135 int c = ++counter;
136 scm_gc_protect_object(function);
137 Timer *t = new Timer(c, when, function);
138 timersList.push_back(t);
139 return scm_long2num (c);
140}
141
142bool
143BotInterp::DelTimer(SCM timer)
144{
145 int count = scm_num2long(timer, SCM_ARG1, "BotInterp::DelTimer");
146 std::list<Timer *>::iterator it = timersList.begin();
147 std::list<Timer *>::iterator it2 = timersList.end();
148
149 for ( ; it != it2; ++it) {
150 if ((*it)->count == count) {
151 scm_gc_unprotect_object((*it)->function);
152 delete (*it);
153 timersList.erase(it);
154 return true;
155 }
156 }
157 return false;
158}
159
160bool
161BotInterp::RunTimers(int now)
162{
163 std::list<Timer *>::iterator it = timersList.begin();
164 std::list<Timer *>::iterator it2 = timersList.end();
165 std::list<Timer *>::iterator it3;
cf8ea873 166
cb21075d 167 struct wrapper_data wd;
168 wd.args = scm_listify (SCM_UNDEFINED);
169
170 while (it != it2) {
171 if ((*it)->when <= now) {
172 wd.func = (*it)->function;
d56bdd22 173 scm_internal_catch(SCM_BOOL_T,
ae97d6ec 174 (scm_t_catch_body) Interp::LazyApplyWrapper, (void *)&wd,
175 (scm_t_catch_handler) Interp::EmptyHandler, 0);
cb21075d 176 scm_gc_unprotect_object(wd.func);
177 it3 = it;
178 ++it3;
179 delete (*it);
180 timersList.erase(it);
181 it = it3;
182 } else {
183 ++it;
184 }
185 }
186 return true;
187}
188
189#endif