Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SQL-based rollback

  • Loading branch information...
commit e0547ae09524db0a9d0100cb2405fcbc435da50b 1 parent 0cc322b
Shockah authored July 03, 2012
312  modulesrc/ModuleRollback.java
... ...
@@ -1,9 +1,9 @@
1  
-import java.io.File;
2 1
 import java.net.URLEncoder;
3 2
 import java.text.SimpleDateFormat;
4 3
 import java.util.*;
5  
-import java.util.Map.Entry;
6 4
 import java.util.regex.*;
  5
+import org.json.JSONArray;
  6
+import org.json.JSONObject;
7 7
 import org.pircbotx.*;
8 8
 import org.pircbotx.hooks.events.*;
9 9
 import pl.shockah.*;
@@ -14,12 +14,24 @@
14 14
 import pl.shockah.shocky.events.*;
15 15
 import pl.shockah.shocky.lines.*;
16 16
 import pl.shockah.shocky.prototypes.IRollback;
  17
+import pl.shockah.shocky.sql.Criterion;
  18
+import pl.shockah.shocky.sql.CriterionNumber;
  19
+import pl.shockah.shocky.sql.CriterionStringEquals;
  20
+import pl.shockah.shocky.sql.QueryInsert;
  21
+import pl.shockah.shocky.sql.QuerySelect;
  22
+import pl.shockah.shocky.sql.SQL;
17 23
 
18 24
 public class ModuleRollback extends Module implements IRollback {
19  
-	public final Map<String,ArrayList<Line>> rollback = Collections.synchronizedMap(new HashMap<String,ArrayList<Line>>()), rollbackTmp = Collections.synchronizedMap(new HashMap<String,ArrayList<Line>>());
20 25
 	public final ArrayList<PasteService> services = new ArrayList<ModuleRollback.PasteService>();
21 26
 	protected Command cmd;
22 27
 	
  28
+	public static final int
  29
+		TYPE_MESSAGE = 1,
  30
+		TYPE_ACTION = 2,
  31
+		TYPE_ENTERLEAVE = 3,
  32
+		TYPE_KICK = 4,
  33
+		TYPE_OTHER = 0;
  34
+	
23 35
 	public static void appendLines(StringBuilder sb, ArrayList<Line> lines) {
24 36
 		try {
25 37
 			for (int i = 0; i < lines.size(); i++) {
@@ -31,33 +43,12 @@ public static void appendLines(StringBuilder sb, ArrayList<Line> lines) {
31 43
 	public static String toString(Line line) {
32 44
 		SimpleDateFormat sdf = new SimpleDateFormat(Data.config.getString("rollback-dateformat"));
33 45
 		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
34  
-		return "["+sdf.format(line.time)+"] "+line.getMessage();
  46
+		return (Line.getWithChannels() ? "["+line.channel+"] " : " ")+"["+sdf.format(line.time)+"] "+line.getMessage();
35 47
 	}
36 48
 	
37 49
 	public String name() {return "rollback";}
38 50
 	public boolean isListener() {return true;}
39 51
 	public void onEnable() {
40  
-		File dir = new File("data","rollback"); dir.mkdir();
41  
-		File[] files = dir.listFiles();
42  
-		for (File f : files) {
43  
-			if (f.isDirectory()) return;
44  
-			
45  
-			String channel = f.getName();
46  
-			BinBuffer binb = new BinFile(f).read(); binb.setPos(0);
47  
-			int count = binb.readInt();
48  
-			for (int i = 0; i < count; i++) {
49  
-				try {
50  
-					Line line = Line.readLine(binb);
51  
-					if (line != null)
52  
-						addRollbackLine(channel,line);
53  
-				} catch (Exception e) {
54  
-					e.printStackTrace();
55  
-				}
56  
-			}
57  
-		}
58  
-		
59  
-		rollbackTmp.clear();
60  
-		
61 52
 		Data.config.setNotExists("rollback-dateformat","dd.MM.yyyy HH:mm:ss");
62 53
 		Command.addCommands(this, cmd = new CmdPastebin());
63 54
 		Command.addCommand(this, "pb", cmd);
@@ -65,159 +56,156 @@ public void onEnable() {
65 56
 		services.add(new ServicePasteKdeOrg());
66 57
 		services.add(new ServicePastebinCom());
67 58
 		services.add(new ServicePastebinCa());
  59
+		
  60
+		SQL.raw("CREATE TABLE IF NOT EXISTS "+SQL.getTable("rollback")+" (channel TEXT NOT NULL,user TEXT NOT NULL,user2 TEXT NOT NULL,type INT(1) UNSIGNED NOT NULL,stamp BIGINT UNSIGNED NOT NULL,txt TEXT NOT NULL)");
  61
+		SQL.raw("ALTER TABLE "+SQL.getTable("rollback")+" ADD INDEX (channel(5),stamp)");
68 62
 	}
69 63
 	public void onDisable() {
70 64
 		Command.removeCommands(cmd);
71 65
 		services.clear();
72 66
 	}
73  
-	public void onDataSave() {
74  
-		File dir = new File("data","rollback"); dir.mkdir();
75  
-		BinBuffer binb = new BinBuffer();
76  
-		
77  
-		Iterator<Entry<String, ArrayList<Line>>> it = rollbackTmp.entrySet().iterator();
78  
-		while (it.hasNext()) {
79  
-			binb.clear();
80  
-			Map.Entry<String, ArrayList<Line>> pair = it.next();
81  
-			
82  
-			ArrayList<Line> lines = pair.getValue();
83  
-			binb.writeInt(lines.size());
84  
-			for (Line line : lines) {
85  
-				byte type = Line.getLineID(line);
86  
-				if (type != -1) {
87  
-					binb.writeByte(type);
88  
-					line.save(binb);
89  
-				}
90  
-			}
91  
-			
92  
-			binb.setPos(0);
93  
-			new BinFile(new File(dir,pair.getKey())).append(binb);
94  
-		}
95  
-		
96  
-		rollbackTmp.clear();
97  
-	}
98 67
 	
99 68
 	public void onMessage(MessageEvent<PircBotX> event) {
100  
-		addRollbackLine(event.getChannel().getName(),new LineMessage(event.getUser().getNick(),event.getMessage()));
  69
+		addRollbackLine(event.getChannel().getName(),new LineMessage(event.getChannel().getName(),event.getUser().getNick(),event.getMessage()));
101 70
 	}
102 71
 	public void onMessageOut(MessageOutEvent<PircBotX> event) {
103  
-		addRollbackLine(event.getChannel().getName(),new LineMessage(event.getBot().getNick(),event.getMessage()));
  72
+		addRollbackLine(event.getChannel().getName(),new LineMessage(event.getChannel().getName(),event.getBot().getNick(),event.getMessage()));
104 73
 	}
105 74
 	public void onAction(ActionEvent<PircBotX> event) {
106  
-		addRollbackLine(event.getChannel().getName(),new LineAction(event.getUser().getNick(),event.getMessage()));
  75
+		addRollbackLine(event.getChannel().getName(),new LineAction(event.getChannel().getName(),event.getUser().getNick(),event.getMessage()));
107 76
 	}
108 77
 	public void onActionOut(ActionOutEvent<PircBotX> event) {
109  
-		addRollbackLine(event.getChannel().getName(),new LineAction(event.getBot().getNick(),event.getMessage()));
  78
+		addRollbackLine(event.getChannel().getName(),new LineAction(event.getChannel().getName(),event.getBot().getNick(),event.getMessage()));
110 79
 	}
111 80
 	public void onTopic(TopicEvent<PircBotX> event) {
112 81
 		if (!event.isChanged()) return;
113  
-		addRollbackLine(event.getChannel().getName(),new LineOther("* "+event.getUser().getNick()+" has changed the topic to: "+event.getTopic()));
  82
+		addRollbackLine(event.getChannel().getName(),new LineOther(event.getChannel().getName(),"* "+event.getUser().getNick()+" has changed the topic to: "+event.getTopic()));
114 83
 	}
115 84
 	public void onJoin(JoinEvent<PircBotX> event) {
116  
-		addRollbackLine(event.getChannel().getName(),new LineEnterLeave(event.getUser().getNick(),"("+event.getUser().getHostmask()+") has joined"));
  85
+		addRollbackLine(event.getChannel().getName(),new LineEnterLeave(event.getChannel().getName(),event.getUser().getNick(),"("+event.getUser().getHostmask()+") has joined"));
117 86
 	}
118 87
 	public void onPart(PartEvent<PircBotX> event) {
119  
-		addRollbackLine(event.getChannel().getName(),new LineEnterLeave(event.getUser().getNick(),"("+event.getUser().getHostmask()+") has left"));
  88
+		addRollbackLine(event.getChannel().getName(),new LineEnterLeave(event.getChannel().getName(),event.getUser().getNick(),"("+event.getUser().getHostmask()+") has left"));
120 89
 	}
121 90
 	public void onQuit(QuitEvent<PircBotX> event) {
122  
-		for (Channel channel : event.getUser().getChannels()) addRollbackLine(channel.getName(),new LineEnterLeave(event.getUser().getNick(),"has quit ("+event.getReason()+")"));
  91
+		for (Channel channel : event.getUser().getChannels()) addRollbackLine(channel.getName(),new LineEnterLeave(channel.getName(),event.getUser().getNick(),"has quit ("+event.getReason()+")"));
123 92
 	}
124 93
 	public void onKick(KickEvent<PircBotX> event) {
125 94
 		addRollbackLine(event.getChannel().getName(),new LineKick(event));
126 95
 	}
127 96
 	public void onNickChange(NickChangeEvent<PircBotX> event) {
128  
-		for (Channel channel : event.getBot().getChannels(event.getUser())) addRollbackLine(channel.getName(),new LineOther("* "+event.getOldNick()+" is now known as "+event.getNewNick()));
  97
+		for (Channel channel : event.getBot().getChannels(event.getUser())) addRollbackLine(channel.getName(),new LineOther(channel.getName(),"* "+event.getOldNick()+" is now known as "+event.getNewNick()));
129 98
 	}
130 99
 	public void onMode(ModeEvent<PircBotX> event) {
131 100
 		String mode = event.getMode();
132 101
 		if (mode.charAt(0) == ' ') mode = "+"+mode.substring(1);
133  
-		addRollbackLine(event.getChannel().getName(),new LineOther("* "+event.getUser().getNick()+" sets mode "+mode));
  102
+		addRollbackLine(event.getChannel().getName(),new LineOther(event.getChannel().getName(),"* "+event.getUser().getNick()+" sets mode "+mode));
134 103
 	}
135 104
 	public void onUserMode(UserModeEvent<PircBotX> event) {
136 105
 		String mode = event.getMode();
137 106
 		if (mode.charAt(0) == ' ') mode = "+"+mode.substring(1);
138  
-		for (Channel channel : event.getBot().getChannels(event.getTarget())) addRollbackLine(channel.getName(),new LineOther("* "+event.getSource().getNick()+" sets mode "+mode+" "+event.getTarget().getNick()));
  107
+		for (Channel channel : event.getBot().getChannels(event.getTarget())) addRollbackLine(channel.getName(),new LineOther(channel.getName(),"* "+event.getSource().getNick()+" sets mode "+mode+" "+event.getTarget().getNick()));
139 108
 	}
140 109
 	
141 110
 	public synchronized void addRollbackLine(String channel, Line line) {
142  
-		if (!channel.startsWith("#")) return;
  111
+		if (channel == null || !channel.startsWith("#")) return;
143 112
 		channel = channel.toLowerCase();
144  
-		if (!rollback.containsKey(channel)) rollback.put(channel,new ArrayList<Line>());
145  
-		if (!rollbackTmp.containsKey(channel)) rollbackTmp.put(channel,new ArrayList<Line>());
146  
-		rollback.get(channel).add(line);
147  
-		rollbackTmp.get(channel).add(line);
  113
+		
  114
+		int intType = TYPE_OTHER;
  115
+		if (line.getClass() == LineMessage.class) intType = TYPE_MESSAGE;
  116
+		if (line.getClass() == LineAction.class) intType = TYPE_ACTION;
  117
+		if (line.getClass() == LineEnterLeave.class) intType = TYPE_ENTERLEAVE;
  118
+		if (line.getClass() == LineKick.class) intType = TYPE_KICK;
  119
+		
  120
+		QueryInsert q = new QueryInsert(SQL.getTable("rollback"));
  121
+		q.add("channel",channel);
  122
+		q.add("stamp",new Date().getTime());
  123
+		q.add("type",intType);
  124
+		switch (intType) {
  125
+			case TYPE_MESSAGE: {
  126
+				LineMessage lm = (LineMessage)line;
  127
+				q.add("user",lm.sender);
  128
+				q.add("user2","");
  129
+				q.add("txt",lm.text);
  130
+			} break;
  131
+			case TYPE_ACTION: {
  132
+				LineAction la = (LineAction)line;
  133
+				q.add("user",la.sender);
  134
+				q.add("user2","");
  135
+				q.add("txt",la.text);
  136
+			} break;
  137
+			case TYPE_ENTERLEAVE: {
  138
+				LineEnterLeave lel = (LineEnterLeave)line;
  139
+				q.add("user",lel.sender);
  140
+				q.add("user2","");
  141
+				q.add("txt",lel.text);
  142
+			} break;
  143
+			case TYPE_KICK: {
  144
+				LineKick lk = (LineKick)line;
  145
+				q.add("user",lk.sender);
  146
+				q.add("user2",lk.target);
  147
+				q.add("txt",lk.text);
  148
+			} break;
  149
+			default: {
  150
+				q.add("user",""); q.add("user2","");
  151
+				q.add("txt",((LineOther)line).text);
  152
+			} break;
  153
+		}
  154
+		SQL.insert(q);
148 155
 	}
149 156
 	
150 157
 	public ArrayList<Line> getRollbackLines(String channel, String user, String regex, String cull, boolean newest, int lines, int seconds) {
151 158
 		return getRollbackLines(Line.class, channel, user, regex, cull, newest, lines, seconds);
152 159
 	}
153 160
 	
154  
-	public synchronized <T extends Line> ArrayList<T> getRollbackLines(Class<T> type, String channel, String user, String regex, String cull, boolean newest, int lines, int seconds) {
  161
+	@SuppressWarnings("unchecked") public synchronized <T extends Line> ArrayList<T> getRollbackLines(Class<T> type, String channel, String user, String regex, String cull, boolean newest, int lines, int seconds) {
155 162
 		ArrayList<T> ret = new ArrayList<T>();
156  
-		ArrayList<Line> linesChannel = rollback.get(channel);
157  
-		if (linesChannel == null || linesChannel.isEmpty()) return ret;
158  
-		Pattern pat = regex == null ? null : Pattern.compile(regex);
  163
+		int intType = TYPE_OTHER;
  164
+		if (type == LineMessage.class) intType = TYPE_MESSAGE;
  165
+		if (type == LineAction.class) intType = TYPE_ACTION;
  166
+		if (type == LineEnterLeave.class) intType = TYPE_ENTERLEAVE;
  167
+		if (type == LineKick.class) intType = TYPE_KICK;
159 168
 		
160  
-		int i = newest ? linesChannel.size() : -1;
161  
-		if (lines != 0) {
162  
-			while (lines > ret.size()) {
163  
-				i += newest ? -1 : 1;
164  
-				if (i < 0 || i >= linesChannel.size()) break;
165  
-				
166  
-				Line line = linesChannel.get(i);
167  
-				if (!type.isAssignableFrom(line.getClass()))
168  
-					continue;
169  
-				@SuppressWarnings("unchecked")
170  
-				T generic = (T) linesChannel.get(i);
171  
-				if (line.containsUser(user)) {
172  
-					String tmp = null;
173  
-					if (line instanceof LineMessage) tmp = ((LineMessage)line).text;
174  
-					if (line instanceof LineAction) tmp = ((LineAction)line).text;
175  
-					if (tmp != null) {
176  
-						if (cull != null && tmp.contentEquals(cull))
177  
-							continue;
178  
-						if (pat != null && !pat.matcher(tmp).find())
179  
-							continue;
180  
-						ret.add(generic);
181  
-					} else {
182  
-						if (pat == null)
183  
-							ret.add(generic);
184  
-					}
185  
-				}
  169
+		try {
  170
+			QuerySelect q = new QuerySelect(SQL.getTable("rollback"));
  171
+			if (channel != null) q.addCriterions(new CriterionStringEquals("channel",channel.toLowerCase()));
  172
+			if (user != null) q.addCriterions(new CriterionStringEquals("user",user.toLowerCase()));
  173
+			if (lines != 0) q.setLimitCount(lines);
  174
+			if (seconds != 0) {
  175
+				if (!newest) {
  176
+					QuerySelect q2 = new QuerySelect(SQL.getTable("rollback"));
  177
+					if (channel != null) q2.addCriterions(new CriterionStringEquals("channel",channel.toLowerCase()));
  178
+					q2.setLimitCount(1);
  179
+					JSONObject j = SQL.select(q2);
  180
+					if (j == null || j.length() == 0) return ret;
  181
+					q.addCriterions(new CriterionNumber("stamp","<=",j.getLong("stamp")+seconds));
  182
+				} else q.addCriterions(new CriterionNumber("stamp",">=",new Date().getTime()-(seconds*1000)));
186 183
 			}
187  
-		} else {
188  
-			Date check = newest ? new Date(new Date().getTime()-(seconds*1000l)) : new Date(linesChannel.get(0).time.getTime()+(seconds*1000l));
  184
+			if (regex != null && !regex.isEmpty()) q.addCriterions(new Criterion("'HEX(txt) RLIKE CONCAT('(..)*',HEX('"+regex.replace("\\","\\\\").replace("'","\\'")+"'),'(..)*')'"));
  185
+			if (cull != null && !cull.isEmpty()) q.addCriterions(new CriterionStringEquals("txt",cull,false));
  186
+			if (type != Line.class) q.addCriterions(new CriterionNumber("type","=",intType));
  187
+			q.addOrder("stamp",!newest);
189 188
 			
190  
-			while (true) {
191  
-				i += newest ? -1 : 1;
192  
-				if (i < 0 || i >= linesChannel.size()) break;
193  
-				
194  
-				Line line = linesChannel.get(i);
195  
-				if (!type.isAssignableFrom(line.getClass()))
196  
-					continue;
197  
-				@SuppressWarnings("unchecked")
198  
-				T generic = (T) linesChannel.get(i);
199  
-				if (newest && line.time.before(check)) break;
200  
-				if (!newest && line.time.after(check)) break;
201  
-				
202  
-				if (line.containsUser(user)) {
203  
-					String tmp = null;
204  
-					if (line instanceof LineMessage) tmp = ((LineMessage)line).text;
205  
-					if (line instanceof LineAction) tmp = ((LineAction)line).text;
206  
-					if (tmp != null) {
207  
-						if (cull != null && tmp.contentEquals(cull))
208  
-							continue;
209  
-						if (pat != null && !pat.matcher(tmp).find())
210  
-							continue;
211  
-						ret.add(generic);
212  
-					} else {
213  
-						if (pat == null)
214  
-							ret.add(generic);
215  
-					}
  189
+			JSONObject j = SQL.select(q);
  190
+			if (j == null || j.length() == 0) return ret;
  191
+			ArrayList<JSONObject> results = new ArrayList<JSONObject>();
  192
+			if (j.length() == 1 && j.has("___")) {
  193
+				JSONArray ja = j.getJSONArray("___");
  194
+				for (int i = 0; i < ja.length(); i++) results.add(ja.getJSONObject(i));
  195
+			} else results.add(j);
  196
+			
  197
+			for (JSONObject result : results) {
  198
+				switch (result.getInt("type")) {
  199
+					case TYPE_MESSAGE: ret.add((T)new LineMessage(result.getLong("stamp"),result.getString("channel"),result.getString("user"),result.getString("txt"))); break;
  200
+					case TYPE_ACTION: ret.add((T)new LineAction(result.getLong("stamp"),result.getString("channel"),result.getString("user"),result.getString("txt"))); break;
  201
+					case TYPE_ENTERLEAVE: ret.add((T)new LineEnterLeave(result.getLong("stamp"),result.getString("channel"),result.getString("user"),result.getString("txt"))); break;
  202
+					case TYPE_KICK: ret.add((T)new LineKick(result.getLong("stamp"),result.getString("channel"),result.getString("user"),result.getString("user2"),result.getString("txt"))); break;
  203
+					default: ret.add((T)new LineOther(result.getLong("stamp"),result.getString("channel"),result.getString("txt"))); break;
216 204
 				}
217 205
 			}
218  
-		}
  206
+		} catch (Exception e) {e.printStackTrace();}
219 207
 		
220  
-		if (newest) Collections.reverse(ret);
  208
+		Collections.reverse(ret);
221 209
 		return ret;
222 210
 	}
223 211
 	
@@ -232,9 +220,9 @@ public String help(PircBotX bot, EType type, Channel channel, User sender) {
232 220
 				sb.append("\npastebin [channel] [user] -{lines} - uploads first lines to paste.kde.org/pastebin.com/pastebin.ca");
233 221
 				sb.append("\npastebin [channel] [user] {time}{d/h/m/s} - uploads last lines from set time to paste.kde.org/pastebin.com/pastebin.ca");
234 222
 			} else {
235  
-				sb.append("\npastebin {channel} [user] {lines} - uploads last lines to paste.kde.org/pastebin.com/pastebin.ca");
236  
-				sb.append("\npastebin {channel} [user] -{lines} - uploads first lines to paste.kde.org/pastebin.com/pastebin.ca");
237  
-				sb.append("\npastebin {channel} [user] {time}{d/h/m/s} - uploads last lines from set time to paste.kde.org/pastebin.com/pastebin.ca");
  223
+				sb.append("\npastebin [channel] [user] {lines} - uploads last lines to paste.kde.org/pastebin.com/pastebin.ca");
  224
+				sb.append("\npastebin [channel] [user] -{lines} - uploads first lines to paste.kde.org/pastebin.com/pastebin.ca");
  225
+				sb.append("\npastebin [channel] [user] {time}{d/h/m/s} - uploads last lines from set time to paste.kde.org/pastebin.com/pastebin.ca");
238 226
 			}
239 227
 			
240 228
 			return sb.toString();
@@ -259,7 +247,7 @@ public void doCommand(PircBotX bot, EType type, CommandCallback callback, Channe
259 247
 			}
260 248
 			
261 249
 			if (args.length >= 2) {
262  
-				String aChannel = type == EType.Channel ? channel.getName() : null, aUser = null, aLines;
  250
+				String aChannel = null, aUser = null, aLines;
263 251
 				
264 252
 				if (args.length == 2) {
265 253
 					aLines = args[1];
@@ -271,40 +259,40 @@ public void doCommand(PircBotX bot, EType type, CommandCallback callback, Channe
271 259
 					aUser = args[2];
272 260
 					aLines = args[3];
273 261
 				}
274  
-				if (aChannel == null) {
275  
-					callback.append(help(bot,type,channel,sender));
276  
-					return;
  262
+				if (aChannel == null && aUser == null) {
  263
+					if (type == EType.Channel) aChannel = channel.getName(); else {
  264
+						callback.append(help(bot,type,channel,sender));
  265
+						return;
  266
+					}
277 267
 				}
278  
-				aChannel = aChannel.toLowerCase();
  268
+				if (aChannel != null) aChannel = aChannel.toLowerCase();
279 269
 				
280  
-				if (rollback.containsKey(aChannel) && !rollback.get(aChannel).isEmpty()) {
281  
-					ArrayList<Line> list;
282  
-					if (aLines.toLowerCase().matches("^\\-?(?:[0-9]+?[smhd])+$")) {
283  
-						boolean additive = aLines.charAt(0) != '-';
284  
-						if (!additive) aLines = aLines.substring(1);
285  
-						Pattern p = Pattern.compile("([0-9]+[smhd])");
286  
-						Matcher m = p.matcher(aLines);
287  
-						
288  
-						int time = 0;
289  
-						while (m.find()) {
290  
-							char c = m.group(1).charAt(m.group(1).length()-1);
291  
-							int i = Integer.parseInt(m.group(1).substring(0,m.group(1).length()-1));
292  
-							switch (c) {
293  
-								case 's': time += i; break;
294  
-								case 'm': time += i*60; break;
295  
-								case 'h': time += i*3600; break;
296  
-								case 'd': time += i*86400; break;
297  
-							}
298  
-						}
299  
-						list = getRollbackLines(aChannel,aUser,regex,null,additive,0,time);
300  
-					} else list = getRollbackLines(aChannel,aUser,regex,null,aLines.charAt(0) != '-',Math.abs(Integer.parseInt(aLines)),0);
  270
+				ArrayList<Line> list;
  271
+				if (aLines.toLowerCase().matches("^\\-?(?:[0-9]+?[smhd])+$")) {
  272
+					boolean additive = aLines.charAt(0) != '-';
  273
+					if (!additive) aLines = aLines.substring(1);
  274
+					Pattern p = Pattern.compile("([0-9]+[smhd])");
  275
+					Matcher m = p.matcher(aLines);
301 276
 					
302  
-					if (list.isEmpty()) {
303  
-						callback.append("Nothing to upload");
304  
-						return;
  277
+					int time = 0;
  278
+					while (m.find()) {
  279
+						char c = m.group(1).charAt(m.group(1).length()-1);
  280
+						int i = Integer.parseInt(m.group(1).substring(0,m.group(1).length()-1));
  281
+						switch (c) {
  282
+							case 's': time += i; break;
  283
+							case 'm': time += i*60; break;
  284
+							case 'h': time += i*3600; break;
  285
+							case 'd': time += i*86400; break;
  286
+						}
305 287
 					}
306  
-					pbLink = getLink(list);
307  
-				} else callback.append("No "+aChannel+" archive");
  288
+					list = getRollbackLines(aChannel,aUser,regex,null,additive,0,time);
  289
+				} else list = getRollbackLines(aChannel,aUser,regex,null,aLines.charAt(0) != '-',Math.abs(Integer.parseInt(aLines)),0);
  290
+				
  291
+				if (list.isEmpty()) {
  292
+					callback.append("Nothing to upload");
  293
+					return;
  294
+				}
  295
+				pbLink = getLink(list,aUser != null && aChannel == null);
308 296
 			} else {
309 297
 				callback.append(help(bot,type,channel,sender));
310 298
 				return;
@@ -316,10 +304,12 @@ public void doCommand(PircBotX bot, EType type, CommandCallback callback, Channe
316 304
 			}
317 305
 		}
318 306
 		
319  
-		public String getLink(ArrayList<Line> lines) {
  307
+		public String getLink(ArrayList<Line> lines, boolean withChannel) {
320 308
 			String link;
321 309
 			for (PasteService service : services) {
  310
+				Line.setWithChannels(withChannel);
322 311
 				link = service.paste(lines);
  312
+				Line.setWithChannels(false);
323 313
 				if (link == null) continue;
324 314
 				if (link.isEmpty() || link.startsWith("http://")) return link;
325 315
 			}
4  modulesrc/ModuleTell.java
@@ -34,7 +34,7 @@ public void onEnable() {
34 34
 		Command.addCommands(this, cmd = new CmdTell());
35 35
 		
36 36
 		ArrayList<String> lines = FileLine.read(new File("data","tell.cfg"));
37  
-		for (int i = 0; i < lines.size(); i += 4) addTell(lines.get(i),new LineMessage(Long.parseLong(lines.get(i+2)),lines.get(i+1),lines.get(i+3)));
  37
+		for (int i = 0; i < lines.size(); i += 4) addTell(lines.get(i),new LineMessage(Long.parseLong(lines.get(i+2)),"",lines.get(i+1),lines.get(i+3)));
38 38
 	}
39 39
 	public void onDisable() {
40 40
 		Command.removeCommands(cmd);
@@ -92,7 +92,7 @@ public void doCommand(PircBotX bot, EType type, CommandCallback callback, Channe
92 92
 			String[] args = message.split(" ");
93 93
 			callback.type = EType.Notice;
94 94
 			if (args.length >= 3) {
95  
-				addTell(args[1],new LineMessage(sender.getNick(),StringTools.implode(args,2," ")));
  95
+				addTell(args[1],new LineMessage("",sender.getNick(),StringTools.implode(args,2," ")));
96 96
 				callback.append("I'll pass that along");
97 97
 				return;
98 98
 			}
4  php/sql.php
@@ -60,7 +60,7 @@ function sqlArray($result) {
60 60
 function sql2json($data_sql) {
61 61
 	$json_str = "";
62 62
 	if ($total = sqlRows($data_sql)) {
63  
-		if ($total > 1) $json_str .= "[";
  63
+		if ($total > 1) $json_str .= "{\"___\":[";
64 64
 		$row_count = 0;	
65 65
 		while ($data = sqlArray($data_sql)) {
66 66
 			if (count($data) > 1) $json_str .= "{";
@@ -80,7 +80,7 @@ function sql2json($data_sql) {
80 80
 			if (count($data) > 1) $json_str .= "}";
81 81
 			if ($row_count < $total) $json_str .= ",";
82 82
 		}
83  
-		if ($total > 1) $json_str .= "]";
  83
+		if ($total > 1) $json_str .= "]}";
84 84
 	} else $json_str = "{}";
85 85
 	return $json_str;
86 86
 }
1  src/pl/shockah/shocky/Data.java
@@ -168,6 +168,7 @@ public static Config forChannel(String chan) {
168 168
 		return Data.config.getConfig(chan);
169 169
 	}
170 170
 	public static Config forChannel(Channel chan) {
  171
+		if (chan == null) return Data.config;
171 172
 		return forChannel(chan.getName());
172 173
 	}
173 174
 }
21  src/pl/shockah/shocky/lines/Line.java
@@ -11,30 +11,41 @@
11 11
 	private static final HashMap<Byte,Class<? extends Line>> lineIDMap = new HashMap<Byte,Class<? extends Line>>();
12 12
 	private static final HashMap<Class<? extends Line>,Byte> lineClassMap = new HashMap<Class<? extends Line>,Byte>();
13 13
 	private static final SimpleDateFormat sdf;
  14
+	protected static boolean withChannels = false;
14 15
 	
15 16
 	static {
16 17
 		sdf = new SimpleDateFormat("[HH:mm:ss]");
17 18
 		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
18 19
 	}
19 20
 	
  21
+	public static void setWithChannels(boolean b) {
  22
+		withChannels = b;
  23
+	}
  24
+	public static boolean getWithChannels() {
  25
+		return withChannels;
  26
+	}
  27
+	
20 28
 	public final Date time;
  29
+	public final String channel;
21 30
 	
22  
-	public Line() {this(new Date());}
23  
-	public Line(long ms) {this(new Date(ms));}
24  
-	public Line(Date time) {
  31
+	public Line(String channel) {this(new Date(),channel);}
  32
+	public Line(long ms, String channel) {this(new Date(ms),channel);}
  33
+	public Line(Date time, String channel) {
25 34
 		this.time = time;
  35
+		this.channel = channel.toLowerCase();
26 36
 	}
27 37
 	
28 38
 	public Line(BinBuffer buffer) {
29  
-		this(buffer.readXBytes(8));
  39
+		this(buffer.readXBytes(8),buffer.readString());
30 40
 	}
31 41
 	
32 42
 	public void save(BinBuffer buffer) {
33 43
 		buffer.writeXBytes(time.getTime(),8);
  44
+		buffer.writeString(channel);
34 45
 	}
35 46
 	
36 47
 	public String toString() {
37  
-		return sdf.format(time)+" "+getMessage();
  48
+		return (withChannels ? "["+channel+"] " : " ")+sdf.format(time)+" "+getMessage();
38 49
 	}
39 50
 	public abstract String getMessage();
40 51
 	
10  src/pl/shockah/shocky/lines/LineAction.java
@@ -9,11 +9,11 @@
9 9
 public class LineAction extends LineWithSender {
10 10
 	public final String text;
11 11
 	
12  
-	public LineAction(String sender, String text) {this(new Date(),sender,text);}
13  
-	public LineAction(long ms, String sender, String text) {this(new Date(ms),sender,text);}
14  
-	public LineAction(ActionEvent<PircBotX> event) {this(new Date(),event.getUser().getNick(),event.getAction());}
15  
-	public LineAction(Date time, String sender, String text) {
16  
-		super(time,sender);
  12
+	public LineAction(String channel, String sender, String text) {this(new Date(),channel,sender,text);}
  13
+	public LineAction(long ms, String channel, String sender, String text) {this(new Date(ms),channel,sender,text);}
  14
+	public LineAction(ActionEvent<PircBotX> event) {this(new Date(),event.getChannel().getName(),event.getUser().getNick(),event.getAction());}
  15
+	public LineAction(Date time, String channel, String sender, String text) {
  16
+		super(time,channel,sender);
17 17
 		this.text = text;
18 18
 	}
19 19
 	
10  src/pl/shockah/shocky/lines/LineEnterLeave.java
@@ -9,11 +9,11 @@
9 9
 public class LineEnterLeave extends LineWithSender {
10 10
 	public final String text;
11 11
 	
12  
-	public LineEnterLeave(String sender, String text) {this(new Date(),sender,text);}
13  
-	public LineEnterLeave(long ms, String sender, String text) {this(new Date(ms),sender,text);}
14  
-	public LineEnterLeave(ActionEvent<PircBotX> event) {this(new Date(),event.getUser().getNick(),event.getAction());}
15  
-	public LineEnterLeave(Date time, String sender, String text) {
16  
-		super(time,sender);
  12
+	public LineEnterLeave(String channel, String sender, String text) {this(new Date(),channel,sender,text);}
  13
+	public LineEnterLeave(long ms, String channel, String sender, String text) {this(new Date(ms),channel,sender,text);}
  14
+	public LineEnterLeave(ActionEvent<PircBotX> event) {this(new Date(),event.getChannel().getName(),event.getUser().getNick(),event.getAction());}
  15
+	public LineEnterLeave(Date time, String channel, String sender, String text) {
  16
+		super(time,channel,sender);
17 17
 		this.text = text;
18 18
 	}
19 19
 	
10  src/pl/shockah/shocky/lines/LineKick.java
@@ -10,11 +10,11 @@
10 10
 	public final String target;
11 11
 	public final String text;
12 12
 	
13  
-	public LineKick(String sender, String target, String text) {this(new Date(),sender,target,text);}
14  
-	public LineKick(long ms, String sender, String target, String text) {this(new Date(ms),sender,target,text);}
15  
-	public LineKick(KickEvent<PircBotX> event) {this(new Date(),event.getSource().getNick(),event.getRecipient().getNick(),event.getReason());}
16  
-	public LineKick(Date time, String sender, String target, String text) {
17  
-		super(time,sender);
  13
+	public LineKick(String channel, String sender, String target, String text) {this(new Date(),channel,sender,target,text);}
  14
+	public LineKick(long ms, String channel, String sender, String target, String text) {this(new Date(ms),channel,sender,target,text);}
  15
+	public LineKick(KickEvent<PircBotX> event) {this(new Date(),event.getChannel().getName(),event.getSource().getNick(),event.getRecipient().getNick(),event.getReason());}
  16
+	public LineKick(Date time, String channel, String sender, String target, String text) {
  17
+		super(time,channel,sender);
18 18
 		this.target = target;
19 19
 		this.text = text;
20 20
 	}
10  src/pl/shockah/shocky/lines/LineMessage.java
@@ -9,11 +9,11 @@
9 9
 public class LineMessage extends LineWithSender {
10 10
 	public final String text;
11 11
 	
12  
-	public LineMessage(String sender, String text) {this(new Date(),sender,text);}
13  
-	public LineMessage(long ms, String sender, String text) {this(new Date(ms),sender,text);}
14  
-	public LineMessage(MessageEvent<PircBotX> event) {this(new Date(),event.getUser().getNick(),event.getMessage());}
15  
-	public LineMessage(Date time, String sender, String text) {
16  
-		super(time,sender);
  12
+	public LineMessage(String channel, String sender, String text) {this(new Date(),channel,sender,text);}
  13
+	public LineMessage(long ms, String channel, String sender, String text) {this(new Date(ms),channel,sender,text);}
  14
+	public LineMessage(MessageEvent<PircBotX> event) {this(new Date(),event.getChannel().getName(),event.getUser().getNick(),event.getMessage());}
  15
+	public LineMessage(Date time, String channel, String sender, String text) {
  16
+		super(time,channel,sender);
17 17
 		this.text = text;
18 18
 	}
19 19
 	
8  src/pl/shockah/shocky/lines/LineOther.java
@@ -7,10 +7,10 @@
7 7
 public class LineOther extends Line {
8 8
 	public final String text;
9 9
 	
10  
-	public LineOther(String text) {this(new Date(),text);}
11  
-	public LineOther(long ms, String text) {this(new Date(ms),text);}
12  
-	public LineOther(Date time, String text) {
13  
-		super(time);
  10
+	public LineOther(String channel, String text) {this(new Date(),channel,text);}
  11
+	public LineOther(long ms, String channel, String text) {this(new Date(ms),channel,text);}
  12
+	public LineOther(Date time, String channel, String text) {
  13
+		super(time,channel);
14 14
 		this.text = text;
15 15
 	}
16 16
 	
8  src/pl/shockah/shocky/lines/LineWithSender.java
@@ -7,10 +7,10 @@
7 7
 public abstract class LineWithSender extends Line {
8 8
 	public final String sender;
9 9
 	
10  
-	public LineWithSender(String sender) {this(new Date(),sender);}
11  
-	public LineWithSender(long ms, String sender) {this(new Date(ms),sender);}
12  
-	public LineWithSender(Date time, String sender) {
13  
-		super(time);
  10
+	public LineWithSender(String channel, String sender) {this(new Date(),channel,sender);}
  11
+	public LineWithSender(long ms, String channel, String sender) {this(new Date(ms),channel,sender);}
  12
+	public LineWithSender(Date time, String channel, String sender) {
  13
+		super(time,channel);
14 14
 		this.sender = sender;
15 15
 	}
16 16
 	
3  src/pl/shockah/shocky/sql/CriterionNumber.java
@@ -2,6 +2,9 @@
2 2
 
3 3
 public class CriterionNumber extends Criterion {
4 4
 	public CriterionNumber(String column, Operation o, long value) {
  5
+		this(column,o.toString(),value);
  6
+	}
  7
+	public CriterionNumber(String column, String o, long value) {
5 8
 		super(column+o+value);
6 9
 	}
7 10
 	
5  src/pl/shockah/shocky/sql/CriterionStringEquals.java
@@ -2,6 +2,9 @@
2 2
 
3 3
 public class CriterionStringEquals extends Criterion {
4 4
 	public CriterionStringEquals(String column, String value) {
5  
-		super(column+"='"+value.replace("\\","\\\\").replace("'","\\'")+"'");
  5
+		this(column,value,true);
  6
+	}
  7
+	public CriterionStringEquals(String column, String value, boolean equals) {
  8
+		super(column+(equals ? "=" : "<>")+"'"+value.replace("\\","\\\\").replace("'","\\'")+"'");
6 9
 	}
7 10
 }

0 notes on commit e0547ae

Please sign in to comment.
Something went wrong with that request. Please try again.