Factor out sorted list insertion into utility function
[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 <map>
31#include <list>
32#include <functional>
33
cb21075d 34#include <libguile.h>
3c37f9a8 35
36#include "BotThreading.H"
cb21075d 37#include "String.H"
3c37f9a8 38#include "Utils.H"
cb21075d 39
40class Bot;
41
42struct Hook {
43 int type;
44 int priority;
45 bool fallthru;
46
47 String regex_str;
fd7440f1 48 String name;
cb21075d 49 SCM regex;
50 SCM function;
51
fd7440f1 52 Hook(int t, String rs, SCM r, SCM f, int p, bool ft, String n="DEFAULT")
31433d27 53 : type(t), priority (p), fallthru (ft), regex_str(rs),
fd7440f1 54 name (n), regex(r), function(f) { }
cb21075d 55
56 bool operator< (const Hook &h) const
57 {
be3612f3 58 if (priority < h.priority)
cf8ea873 59 {
60 return true;
61 }
be3612f3 62 else if (priority > h.priority)
cf8ea873 63 {
64 return false;
65 }
be3612f3 66 else if (fallthru && h.fallthru)
cf8ea873 67 {
68 return false;
69 }
cb21075d 70 else if (fallthru && !h.fallthru)
cf8ea873 71 {
72 return false;
73 }
cb21075d 74 else if (!fallthru && h.fallthru)
cf8ea873 75 {
76 return true;
77 }
78 else
79 {
80 // NOTE: This should never be reached
81 return false;
82 }
cb21075d 83 }
84
85 enum {
86 ACTION, NICKNAME, SIGNOFF, CTCP, CTCP_REPLY,
ae97d6ec 87 DISCONNECT, FLOOD, INVITE, JOIN, KICK, MODE,
88 MESSAGE, NAMES, NOTICE, PART, PUBLIC,
fed59248 89 PUBLIC_NOTICE, RAW, TIMER, TOPIC,
90 // send hooks
6530edbf 91 SEND_ACTION, SEND_CTCP, SEND_PUBLIC, SEND_MESSAGE,
528799bd 92 SEND_WHO, SEND_WHOIS,
6530edbf 93 // DCC hooks
133eff7a 94 DCC_CHAT_BEGIN, DCC_CHAT_END, DCC_CHAT_MESSAGE
cb21075d 95 };
96};
97
98struct Timer {
99 int count;
100 std::time_t when;
101 SCM function;
102
103 Timer(int c, std::time_t t, SCM f)
104 : count(c), when(t), function(f) { }
3c37f9a8 105
106 bool operator< (const Timer & other) const
107 { return when < other.when; }
cb21075d 108};
109
110class BotInterp {
3c37f9a8 111 typedef std::list<Timer *> TimerList;
112
cb21075d 113 Bot * bot;
114 SCM logPort;
115 std::map<int, std::list<Hook *>, std::less<int> > hooksMap;
3c37f9a8 116 TimerList timers;
117 Utils::IndirectPred<Timer, std::less<Timer> > timer_sort_p;
cb21075d 118 int counter;
3c37f9a8 119 BotMutex hook_mutex;
120 BotMutex timer_mutex; // NOTE: recursive lock
cb21075d 121
122public:
123 BotInterp(Bot *, String);
124
d56bdd22 125 SCM ScriptLog();
cb21075d 126
127 void Execute(String);
128 void LoadScript(String);
129
fd7440f1 130 bool AddHook(int, SCM, SCM, int, bool, String);
cb21075d 131 bool RunHooks(int, String, SCM);
132
133 SCM AddTimer(int, SCM);
134 bool DelTimer(SCM);
135 bool RunTimers(int);
136
137 friend class Interp;
138 friend class ScriptCommands;
139};
140
141#endif
142#endif