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