1 package rero.bridges.event; 2 3 import java.util.*; 4 import java.io.*; 5 6 import sleep.engine.*; 7 import sleep.interfaces.*; 8 import sleep.runtime.*; 9 10 import sleep.engine.atoms.*; 11 12 import rero.ircfw.*; 13 import rero.ircfw.interfaces.*; 14 15 38 39 public class EventBridge implements Loadable, Environment, PredicateEnvironment, FilterEnvironment 40 { 41 protected HashMap listeners; 43 protected ChatFramework framework; 44 45 protected HashMap registeredEvents; 46 47 protected HashMap unloadEvents; 48 49 public EventBridge() 50 { 51 listeners = new HashMap(); 52 registeredEvents = new HashMap(); 53 unloadEvents = new HashMap(); 54 } 55 56 public void announceFramework(ChatFramework f) 57 { 58 framework = f; 59 } 60 61 public boolean scriptUnloaded (ScriptInstance si) 62 { 63 if (unloadEvents.get(si) != null) 64 { 65 Block code = (Block)unloadEvents.get(si); 66 unloadEvents.remove(si); 67 68 synchronized (si.getScriptEnvironment().getScriptVariables()) 69 { 70 si.getScriptEnvironment().getScriptVariables().pushLocalLevel(); 71 SleepUtils.runCode(code, si.getScriptEnvironment()); 72 si.getScriptEnvironment().getScriptVariables().popLocalLevel(); 73 } 74 } 75 76 return true; 82 } 83 84 public boolean scriptLoaded (ScriptInstance si) 85 { 86 Hashtable _env = si.getScriptEnvironment().getEnvironment(); 87 _env.put("on", this); _env.put("wait", this); 89 90 92 return true; 93 } 94 95 97 public void registerEvent(String name, ScriptedEventListener listener) 98 { 99 registeredEvents.put(name.toUpperCase(), listener); 100 } 101 102 protected EventChatListener getNewListenerFor(String event_name) 103 { 104 if (event_name.equals("PUBLIC")) 105 { 106 return new PublicChatListener("PRIVMSG"); 107 } 108 109 if (event_name.equals("PUBLIC_ACTION")) 110 { 111 return new PublicChatListener("ACTION"); 112 } 113 114 if (event_name.equals("PRIVATE_ACTION")) 115 { 116 return new PrivateChatListener("ACTION"); 117 } 118 119 if (event_name.equals("MSG")) 120 { 121 return new PrivateChatListener("PRIVMSG"); 122 } 123 124 if (event_name.length() > 6 && event_name.substring(0, 5).equals("REPL_")) 125 { 126 return new GeneralChatListener(event_name.substring(5, event_name.length())); 127 } 128 129 return new GeneralChatListener(event_name); 130 } 131 132 protected EventChatListener getListenerFor(String event_name) 136 { 137 if (listeners.get(event_name) == null) 138 { 139 listeners.put(event_name, getNewListenerFor(event_name)); 140 framework.addChatListener((ChatListener)listeners.get(event_name)); 141 } 142 143 return (EventChatListener)listeners.get(event_name); 144 } 145 146 public void bindFilteredFunction(ScriptInstance si, String typeKeyword, String keyword, String filter, Block functionBody) 147 { 148 FilterChatListener temp = new FilterChatListener(si.getScriptEnvironment(), keyword, filter, new CodeSnippet(functionBody, si.getScriptEnvironment())); 149 150 if (typeKeyword.equals("on")) 151 { 152 framework.addChatListener((ChatListener)temp); 153 } 154 155 if (typeKeyword.equals("wait")) 156 { 157 temp.setFlags(ChatListener.REMOVE_LISTENER); 158 framework.addTemporaryListener(temp); 159 } 160 } 161 162 public void bindPredicate(ScriptInstance si, String type, Check pred, Block code) 163 { 164 PredicateChatListener temp = new PredicateChatListener(si.getScriptEnvironment(), pred, new CodeSnippet(code, si.getScriptEnvironment())); 165 166 if (type.equals("on")) 167 { 168 framework.addChatListener((ChatListener)temp); 169 } 170 171 if (type.equals("wait")) 172 { 173 temp.setFlags(ChatListener.REMOVE_LISTENER); framework.addTemporaryListener(temp); 175 } 176 } 177 178 public void bindFunction(ScriptInstance si, String type, String name, Block code) 179 { 180 name = name.toUpperCase(); 181 182 if (type.equals("on") && name.equals("UNLOAD")) 183 { 184 unloadEvents.put(si, code); 185 } 186 else if (registeredEvents.get(name) != null) 187 { 188 ScriptedEventListener temp = (ScriptedEventListener)registeredEvents.get(name); 189 if (!temp.isSetup()) 190 { 191 temp.setupListener(); 192 temp.setRegistered(); 193 } 194 195 if (type.equals("on")) 196 { 197 temp.addListener(new CodeSnippet(code, si.getScriptEnvironment())); 198 } 199 200 if (type.equals("wait")) 201 { 202 temp.addTemporaryListener(new CodeSnippet(code, si.getScriptEnvironment())); 203 } 204 205 return; 206 } 207 else if (type.equals("on")) 208 { 209 EventChatListener temp = getListenerFor(name); 211 temp.addListener(new CodeSnippet(code, si.getScriptEnvironment())); 212 } 213 else if (type.equals("wait")) 214 { 215 EventChatListener temp = getNewListenerFor(name); 216 temp.addListener(new CodeSnippet(code, si.getScriptEnvironment())); 217 temp.setFlags(ChatListener.REMOVE_LISTENER); 219 framework.addTemporaryListener(temp); 220 } 221 } 222 } 223 224 | Popular Tags |