1cd397598bc917d5184787ab59c4fc95f9bb9716
[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 <functional>
30 #include <list>
31 #include <map>
32 #include <string>
33
34 #include <ctime>
35
36 #include <libguile.h>
37
38 #include "BotThreading.H"
39 #include "String.H"
40 #include "Utils.H"
41
42 class Bot;
43
44 struct Hook {
45 int type;
46 int priority;
47 bool fallthru;
48
49 std::string regex_str;
50 std::string name;
51 SCM regex;
52 SCM function;
53
54 Hook(int t, std::string rs, SCM r, SCM f, int p, bool ft, std::string n="DEFAULT")
55 : type(t), priority (p), fallthru (ft), regex_str(rs),
56 name (n), regex(r), function(f) { }
57
58 bool operator< (const Hook & h) const;
59
60 enum {
61 ACTION, NICKNAME, SIGNOFF, CTCP, CTCP_REPLY,
62 DISCONNECT, FLOOD, INVITE, JOIN, KICK, MODE,
63 MESSAGE, NAMES, NOTICE, PART, PUBLIC,
64 PUBLIC_NOTICE, RAW, TIMER, TOPIC,
65 // send hooks
66 SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
67 SEND_WHO, SEND_WHOIS,
68 // DCC hooks
69 DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
70 };
71 };
72
73 struct Timer {
74 int count;
75 std::time_t when;
76 SCM function;
77
78 Timer(int c, std::time_t t, SCM f)
79 : count(c), when(t), function(f) { }
80
81 bool operator< (const Timer & other) const
82 { return when < other.when; }
83 };
84
85 class BotInterp {
86 typedef std::list<Timer *> TimerList;
87 typedef std::list<Hook *> HookList;
88 typedef std::map<int, HookList, std::less<int> > HookMap;
89
90 Bot * bot;
91 SCM logPort;
92 int counter;
93
94 HookMap hooks;
95 TimerList timers;
96
97 Utils::IndirectPred<Timer, std::less<Timer> > timer_sort_p;
98 Utils::IndirectPred<Hook, std::less<Hook> > hook_sort_p;
99
100 BotMutex hook_mutex;
101 BotMutex timer_mutex; // NOTE: recursive lock
102
103 public:
104 BotInterp(Bot *, String);
105
106 SCM ScriptLog();
107
108 void Execute(String);
109 void LoadScript(String);
110
111 bool AddHook(int, SCM, SCM, int, bool, std::string);
112 bool RunHooks(int, std::string, SCM);
113
114 SCM AddTimer(int, SCM);
115 bool DelTimer(SCM);
116 bool RunTimers(int);
117
118 friend class Interp;
119 friend class ScriptCommands;
120 };
121
122 #endif
123 #endif