[project @ 2005-07-07 21:19:26 by unknown_lamer]
[clinton/bobotpp.git] / source / BotInterp.H
1 // BotInterp.H -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 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 <libguile.h>
31 #include "String.H"
32
33 class Bot;
34
35 struct Hook {
36 int type;
37 int priority;
38 bool fallthru;
39
40 String regex_str;
41 String name;
42 SCM regex;
43 SCM function;
44
45 Hook(int t, String rs, SCM r, SCM f, int p, bool ft, String n="DEFAULT")
46 : type(t), priority (p), fallthru (ft), regex_str(rs),
47 name (n), regex(r), function(f) { }
48
49 bool operator< (const Hook &h) const
50 {
51 if (priority < h.priority)
52 {
53 return true;
54 }
55 else if (priority > h.priority)
56 {
57 return false;
58 }
59 else if (fallthru && h.fallthru)
60 {
61 return false;
62 }
63 else if (fallthru && !h.fallthru)
64 {
65 return false;
66 }
67 else if (!fallthru && h.fallthru)
68 {
69 return true;
70 }
71 else
72 {
73 // NOTE: This should never be reached
74 return false;
75 }
76 }
77
78 enum {
79 ACTION, NICKNAME, SIGNOFF, CTCP, CTCP_REPLY,
80 DISCONNECT, FLOOD, INVITE, JOIN, KICK, MODE,
81 MESSAGE, NAMES, NOTICE, PART, PUBLIC,
82 PUBLIC_NOTICE, RAW, TIMER, TOPIC,
83 // send hooks
84 SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
85 SEND_WHO, SEND_WHOIS,
86 // DCC hooks
87 DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
88 };
89 };
90
91 struct Timer {
92 int count;
93 std::time_t when;
94 SCM function;
95
96 Timer(int c, std::time_t t, SCM f)
97 : count(c), when(t), function(f) { }
98 };
99
100 class BotInterp {
101 Bot * bot;
102 SCM logPort;
103 std::map<int, std::list<Hook *>, std::less<int> > hooksMap;
104 std::list<Timer *> timersList;
105 int counter;
106
107 public:
108 BotInterp(Bot *, String);
109
110 SCM ScriptLog();
111
112 void Execute(String);
113 void LoadScript(String);
114
115 bool AddHook(int, SCM, SCM, int, bool, String);
116 bool RunHooks(int, String, SCM);
117
118 SCM AddTimer(int, SCM);
119 bool DelTimer(SCM);
120 bool RunTimers(int);
121
122 friend class Interp;
123 friend class ScriptCommands;
124 };
125
126 #endif
127 #endif