1 package rero.client.functions; 2 3 import rero.client.Feature; 4 import rero.ircfw.Channel; 5 import rero.ircfw.InternalDataList; 6 import sleep.bridges.BridgeUtilities; 7 import sleep.interfaces.Function; 8 import sleep.interfaces.Loadable; 9 import sleep.interfaces.Predicate; 10 import sleep.runtime.Scalar; 11 import sleep.runtime.ScriptInstance; 12 import sleep.runtime.SleepUtils; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.Stack ; 17 18 public class ChannelOperators extends Feature implements Predicate, Function, Loadable { 19 protected InternalDataList data; 20 21 public void init() { 22 getCapabilities().getScriptCore().addBridge(this); 23 24 data = (InternalDataList) getCapabilities().getDataStructure("clientInformation"); 25 } 26 27 public boolean scriptLoaded(ScriptInstance script) { 28 String [] contents = new String []{ 29 "ison", 30 "isop", 31 "ishalfop", 32 "isvoice", 33 "isnormal", 34 "ismode", 35 "hasmode", 36 "&getUsers", 37 "&getOps", 38 "&getHalfOps", 39 "&getVoiced", 40 "&getNormal", 41 "&getTopic", 42 "&getKey", 43 "&getMode", 44 "&getLimit", 45 "&getModeFor", 46 "&getModeChars" 47 }; 48 49 for (int x = 0; x < contents.length; x++) { 50 script.getScriptEnvironment().getEnvironment().put(contents[x], this); 51 } 52 53 return true; 54 } 55 56 public boolean scriptUnloaded(ScriptInstance script) { 57 return true; 58 } 59 60 public Scalar evaluate(String function, ScriptInstance script, Stack locals) { 61 if (function.equals("&getModeChars")) { 62 String modes = data.getPrefixInfo().getModes(); 63 char[] chars = modes.toCharArray(); 64 ArrayList ch = new ArrayList (chars.length); 65 for (int i = 0; i < chars.length; i++) { 66 char mchar = chars[i]; 67 ch.add(String.valueOf(mchar)); 68 } 69 return SleepUtils.getArrayWrapper(ch); 70 } 71 72 if (function.equals("&getModeFor")) { 73 String _nick = BridgeUtilities.getString(locals, ""); 74 String _channel = BridgeUtilities.getString(locals, ""); 75 Channel channel = data.getChannel(_channel); 76 77 if (data.getUser(_nick) == null) 78 return SleepUtils.getEmptyScalar(); 79 80 return SleepUtils.getScalar(data.getPrefixInfo().toString(data.getUser(_nick).getModeFor(channel))); 81 } 82 83 if (locals.size() != 1) { 84 return SleepUtils.getEmptyScalar(); 85 } 86 87 String _channel = ((Scalar) locals.pop()).getValue().toString(); 88 Channel channel = data.getChannel(_channel); 89 90 if (channel == null) { 91 return null; 92 } 93 94 if (function.equals("&getTopic")) { 95 return SleepUtils.getScalar(channel.getTopic()); 96 } 97 98 if (function.equals("&getMode")) { 99 return SleepUtils.getScalar(channel.getMode().toString()); 100 } 101 102 if (function.equals("&getKey")) { 103 return SleepUtils.getScalar(channel.getKey()); 104 } 105 106 if (function.equals("&getLimit")) { 107 return SleepUtils.getScalar(channel.getLimit()); 108 } 109 110 Stack rv = new Stack (); 111 Iterator i = null; 112 113 if (function.equals("&getUsers")) { 114 return SleepUtils.getArrayWrapper(channel.getAllUsers()); 115 } 116 117 if (function.equals("&getOps")) { 118 return SleepUtils.getArrayWrapper(data.getUsersWithMode(_channel, 'o')); 119 } 120 121 if (function.equals("&getHalfOps")) { 122 return SleepUtils.getArrayWrapper(data.getUsersWithMode(_channel, 'h')); 123 } 124 125 if (function.equals("&getVoiced")) { 126 return SleepUtils.getArrayWrapper(data.getUsersWithMode(_channel, 'v')); 127 } 128 129 if (function.equals("&getNormal")) { 130 return SleepUtils.getArrayWrapper(data.getUsersWithMode(_channel, ' ')); 131 } 132 133 return null; 134 } 135 136 public boolean decide(String predicate, ScriptInstance script, Stack terms) { 137 if (terms.size() != 2) { 138 return false; 139 } 140 141 String channel = ((Scalar) terms.pop()).getValue().toString(); 142 String nick = ((Scalar) terms.pop()).getValue().toString(); 143 144 if (data.getChannel(channel) == null) { 145 return false; 146 } 147 148 if (predicate.equals("ismode")) { 149 for (int x = 0; x < nick.length(); x++) { 150 if (! data.getChannel(channel).getMode().isSet(nick.charAt(x))) { 151 return false; 152 } 153 } 154 return true; 155 } 156 157 if (!data.isUser(nick)) { 158 return false; 159 } 160 161 if (predicate.equals("ison")) { 162 return data.isOn(data.getUser(nick), data.getChannel(channel)); 163 } 164 165 int temp = data.getUser(nick).getModeFor(data.getChannel(channel)); 166 167 if (predicate.equals("isop")) { 168 return data.getPrefixInfo().isMode(temp, 'o'); 169 } 170 171 if (predicate.equals("ishalfop")) { 172 return data.getPrefixInfo().isMode(temp, 'h'); 173 } 174 175 if (predicate.equals("isvoice")) { 176 return data.getPrefixInfo().isMode(temp, 'v'); 177 } 178 179 if (predicate.equals("isnormal")) { 180 return temp == 0; 181 } 182 183 if (predicate.equals("hasmode")) { 184 return temp != 0; 185 } 186 187 return false; 188 } 189 } 190 | Popular Tags |