Enhance threadsafety of Hooks and improve generally
[clinton/bobotpp.git] / source / BotInterp.H
1 // BotInterp.H -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005,2008 Clinton Ebadi
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
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
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>
30 #include <functional>
31 #include <list>
32 #include <map>
33 #include <string>
34
35 #include <libguile.h>
36
37 #include "BotThreading.H"
38 #include "String.H"
39 #include "Utils.H"
40
41 class Bot;
42
43 struct Hook {
44 int type;
45 int priority;
46 bool fallthru;
47
48 std::string regex_str;
49 std::string name;
50 SCM regex;
51 SCM function;
52
53 Hook(int t, std::string rs, SCM r, SCM f, int p, bool ft, std::string n="DEFAULT")
54 : type(t), priority (p), fallthru (ft), regex_str(rs),
55 name (n), regex(r), function(f) { }
56
57 bool operator< (const Hook & h) const;
58
59 enum {
60 ACTION, NICKNAME, SIGNOFF, CTCP, CTCP_REPLY,
61 DISCONNECT, FLOOD, INVITE, JOIN, KICK, MODE,
62 MESSAGE, NAMES, NOTICE, PART, PUBLIC,
63 PUBLIC_NOTICE, RAW, TIMER, TOPIC,
64 // send hooks
65 SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
66 SEND_WHO, SEND_WHOIS,
67 // DCC hooks
68 DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
69 };
70 };
71
72 struct 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) { }
79
80 bool operator< (const Timer & other) const
81 { return when < other.when; }
82 };
83
84 class BotInterp {
85 typedef std::list<Timer *> TimerList;
86 typedef std::list<Hook *> HookList;
87 typedef std::map<int, HookList, std::less<int> > HookMap;
88
89 Bot * bot;
90 SCM logPort;
91 int counter;
92
93 HookMap hooks;
94 TimerList timers;
95
96 Utils::IndirectPred<Timer, std::less<Timer> > timer_sort_p;
97 Utils::IndirectPred<Hook, std::less<Hook> > hook_sort_p;
98
99 BotMutex hook_mutex;
100 BotMutex timer_mutex; // NOTE: recursive lock
101
102 public:
103 BotInterp(Bot *, String);
104
105 SCM ScriptLog();
106
107 void Execute(String);
108 void LoadScript(String);
109
110 bool AddHook(int, SCM, SCM, int, bool, std::string);
111 bool RunHooks(int, std::string, SCM);
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