Enhance threadsafety of Hooks and improve generally
[clinton/bobotpp.git] / source / BotInterp.H
CommitLineData
cb21075d 1// BotInterp.H -*- C++ -*-
2// Copyright (c) 1998 Etienne BERNARD
3c37f9a8 3// Copyright (C) 2002,2005,2008 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
528799bd 17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18// 02110-1301, USA.
cb21075d 19
20#ifndef BOTINTERP_H
21#define BOTINTERP_H
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#ifdef USESCRIPTS
28
29#include <ctime>
3c37f9a8 30#include <functional>
7a9d1172 31#include <list>
32#include <map>
33#include <string>
3c37f9a8 34
cb21075d 35#include <libguile.h>
3c37f9a8 36
37#include "BotThreading.H"
cb21075d 38#include "String.H"
3c37f9a8 39#include "Utils.H"
cb21075d 40
41class Bot;
42
43struct Hook {
44 int type;
45 int priority;
46 bool fallthru;
47
7a9d1172 48 std::string regex_str;
49 std::string name;
cb21075d 50 SCM regex;
51 SCM function;
52
7a9d1172 53 Hook(int t, std::string rs, SCM r, SCM f, int p, bool ft, std::string n="DEFAULT")
31433d27 54 : type(t), priority (p), fallthru (ft), regex_str(rs),
fd7440f1 55 name (n), regex(r), function(f) { }
cb21075d 56
2d3af8a4 57 bool operator< (const Hook & h) const;
cb21075d 58
59 enum {
60 ACTION, NICKNAME, SIGNOFF, CTCP, CTCP_REPLY,
ae97d6ec 61 DISCONNECT, FLOOD, INVITE, JOIN, KICK, MODE,
62 MESSAGE, NAMES, NOTICE, PART, PUBLIC,
fed59248 63 PUBLIC_NOTICE, RAW, TIMER, TOPIC,
64 // send hooks
6530edbf 65 SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
528799bd 66 SEND_WHO, SEND_WHOIS,
6530edbf 67 // DCC hooks
133eff7a 68 DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
cb21075d 69 };
70};
71
72struct Timer {
73 int count;
74 std::time_t when;
75 SCM function;
76
77 Timer(int c, std::time_t t, SCM f)
78 : count(c), when(t), function(f) { }
3c37f9a8 79
80 bool operator< (const Timer & other) const
81 { return when < other.when; }
cb21075d 82};
83
84class BotInterp {
3c37f9a8 85 typedef std::list<Timer *> TimerList;
7a9d1172 86 typedef std::list<Hook *> HookList;
87 typedef std::map<int, HookList, std::less<int> > HookMap;
3c37f9a8 88
cb21075d 89 Bot * bot;
90 SCM logPort;
7a9d1172 91 int counter;
92
93 HookMap hooks;
3c37f9a8 94 TimerList timers;
7a9d1172 95
3c37f9a8 96 Utils::IndirectPred<Timer, std::less<Timer> > timer_sort_p;
7a9d1172 97 Utils::IndirectPred<Hook, std::less<Hook> > hook_sort_p;
98
3c37f9a8 99 BotMutex hook_mutex;
100 BotMutex timer_mutex; // NOTE: recursive lock
cb21075d 101
102public:
103 BotInterp(Bot *, String);
104
d56bdd22 105 SCM ScriptLog();
cb21075d 106
107 void Execute(String);
108 void LoadScript(String);
109
7a9d1172 110 bool AddHook(int, SCM, SCM, int, bool, std::string);
111 bool RunHooks(int, std::string, SCM);
cb21075d 112
113 SCM AddTimer(int, SCM);
114 bool DelTimer(SCM);
115 bool RunTimers(int);
116
117 friend class Interp;
118 friend class ScriptCommands;
119};
120
121#endif
122#endif