1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
package com.earth2me.essentials.update;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.jibble.pircbot.User;
public class EssentialsHelp extends PlayerListener
{
private transient Player chatUser;
private final transient Server server;
private final transient Plugin plugin;
private final static Charset UTF8 = Charset.forName("utf-8");
private transient IrcBot ircBot;
public EssentialsHelp(final Plugin plugin)
{
super();
this.plugin = plugin;
this.server = plugin.getServer();
}
public void registerEvents()
{
final PluginManager pluginManager = server.getPluginManager();
pluginManager.registerEvent(Type.PLAYER_QUIT, this, Priority.Low, plugin);
pluginManager.registerEvent(Type.PLAYER_CHAT, this, Priority.Low, plugin);
}
public void onCommand(CommandSender sender)
{
if (sender instanceof Player && sender.hasPermission("essentials.helpchat"))
{
if (chatUser == null)
{
chatUser = (Player)sender;
ircBot = null;
sender.sendMessage("You will be connected to the Essentials Help Chat.");
sender.sendMessage("All your chat messages will be forwarded to the channel. You can't chat with other players on your server while in help chat, but you can use commands.");
sender.sendMessage("Please be patient, if noone is available, check back later.");
sender.sendMessage("Type !help to get a list of all commands.");
sender.sendMessage("Type !quit to leave the channel.");
sender.sendMessage("Do you want to join the channel now? (yes/no)");
}
if (!chatUser.equals(sender))
{
sender.sendMessage("The player " + chatUser.getDisplayName() + " is already using the essentialshelp.");
}
}
else
{
sender.sendMessage("Please run the command as op from in game.");
}
}
public void onDisable()
{
if ( ircBot != null)
{
ircBot.quit();
ircBot = null;
}
}
private boolean sendChatMessage(final Player player, final String message)
{
final String messageCleaned = message.trim();
if (messageCleaned.isEmpty())
{
return false;
}
if (ircBot == null)
{
if (messageCleaned.equalsIgnoreCase("yes"))
{
player.sendMessage("Connecting...");
connectToIRC(player);
return true;
}
if (messageCleaned.equalsIgnoreCase("no") || message.equalsIgnoreCase("!quit"))
{
chatUser = null;
return true;
}
return false;
}
else
{
if (ircBot.isKicked()) {
chatUser = null;
ircBot.quit();
ircBot = null;
return false;
}
final String lowMessage = messageCleaned.toLowerCase();
if (lowMessage.startsWith("!quit"))
{
chatUser = null;
if (ircBot != null) {
ircBot.quit();
ircBot = null;
}
player.sendMessage("Connection closed.");
return true;
}
if (!ircBot.isConnected() || ircBot.getChannels().length == 0)
{
return false;
}
if (lowMessage.startsWith("!list"))
{
final User[] members = ircBot.getUsers();
final StringBuilder sb = new StringBuilder();
for (User user : members)
{
if (sb.length() > 0)
{
sb.append("§f, ");
}
if (user.isOp() || user.hasVoice())
{
sb.append("§6");
}
else
{
sb.append("§7");
}
sb.append(user.getPrefix()).append(user.getNick());
}
player.sendMessage(sb.toString());
return true;
}
if (lowMessage.startsWith("!help"))
{
player.sendMessage("Commands: (Note: Files send to the chat will be public viewable.)");
player.sendMessage("!errors - Send the last server errors to the chat.");
player.sendMessage("!startup - Send the last startup messages to the chat.");
player.sendMessage("!config - Sends your Essentials config to the chat.");
player.sendMessage("!list - List all players in chat.");
player.sendMessage("!quit - Leave chat.");
return true;
}
if (lowMessage.startsWith("!errors"))
{
sendErrors();
return true;
}
if (lowMessage.startsWith("!startup"))
{
sendStartup();
return true;
}
if (lowMessage.startsWith("!config"))
{
sendConfig();
return true;
}
ircBot.sendMessage(messageCleaned);
chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + messageCleaned);
return true;
}
}
private String buildIrcName()
{
final StringBuilder nameBuilder = new StringBuilder();
nameBuilder.append(chatUser.getName());
final Matcher versionMatch = Pattern.compile("git-Bukkit-([0-9]+).([0-9]+).([0-9]+)-[0-9]+-[0-9a-z]+-b([0-9]+)jnks.*").matcher(server.getVersion());
if (versionMatch.matches())
{
nameBuilder.append(" CB");
nameBuilder.append(versionMatch.group(4));
}
final Plugin essentials = server.getPluginManager().getPlugin("Essentials");
if (essentials != null)
{
nameBuilder.append(" ESS");
nameBuilder.append(essentials.getDescription().getVersion());
}
final Plugin groupManager = server.getPluginManager().getPlugin("GroupManager");
if (groupManager != null)
{
nameBuilder.append(" GM");
if (!groupManager.isEnabled())
{
nameBuilder.append('!');
}
}
final Plugin pex = server.getPluginManager().getPlugin("PermissionsEx");
if (pex != null)
{
nameBuilder.append(" PEX");
if (!pex.isEnabled())
{
nameBuilder.append('!');
}
nameBuilder.append(pex.getDescription().getVersion());
}
final Plugin pb = server.getPluginManager().getPlugin("PermissionsBukkit");
if (pb != null)
{
nameBuilder.append(" PB");
if (!pb.isEnabled())
{
nameBuilder.append('!');
}
nameBuilder.append(pb.getDescription().getVersion());
}
final Plugin bp = server.getPluginManager().getPlugin("bPermissions");
if (bp != null)
{
nameBuilder.append(" BP");
if (!bp.isEnabled())
{
nameBuilder.append('!');
}
nameBuilder.append(bp.getDescription().getVersion());
}
final Plugin perm = server.getPluginManager().getPlugin("Permissions");
if (perm != null)
{
nameBuilder.append(" P");
if (!perm.isEnabled())
{
nameBuilder.append('!');
}
nameBuilder.append(perm.getDescription().getVersion());
}
return nameBuilder.toString();
}
private void connectToIRC(final Player player)
{
ircBot = new IrcBot(player, "Ess_" + player.getName(), buildIrcName());
}
private void sendErrors()
{
BufferedReader page = null;
try
{
File bukkitFolder = plugin.getDataFolder().getAbsoluteFile().getParentFile().getParentFile();
if (bukkitFolder == null || !bukkitFolder.exists())
{
chatUser.sendMessage("Bukkit folder not found.");
return;
}
File logFile = new File(bukkitFolder, "server.log");
if (!logFile.exists())
{
chatUser.sendMessage("Server log not found.");
return;
}
FileInputStream fis = new FileInputStream(logFile);
if (logFile.length() > 1000000)
{
fis.skip(logFile.length() - 1000000);
}
page = new BufferedReader(new InputStreamReader(fis));
final StringBuilder input = new StringBuilder();
String line;
Pattern pattern = Pattern.compile("^[0-9 :-]+\\[INFO\\].*");
while ((line = page.readLine()) != null)
{
if (!pattern.matcher(line).matches())
{
input.append(line).append("\n");
}
}
if (input.length() > 10000)
{
input.delete(0, input.length() - 10000);
}
final PastieUpload pastie = new PastieUpload();
final String url = pastie.send(input.toString());
String message = "Errors: " + url;
chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message);
ircBot.sendMessage(message);
}
catch (IOException ex)
{
Bukkit.getLogger().log(Level.SEVERE, null, ex);
chatUser.sendMessage(ex.getMessage());
}
finally
{
try
{
if (page != null)
{
page.close();
}
}
catch (IOException ex)
{
Logger.getLogger(EssentialsHelp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void sendStartup()
{
BufferedReader page = null;
try
{
File bukkitFolder = plugin.getDataFolder().getAbsoluteFile().getParentFile().getParentFile();
if (bukkitFolder == null || !bukkitFolder.exists())
{
chatUser.sendMessage("Bukkit folder not found.");
return;
}
File logFile = new File(bukkitFolder, "server.log");
if (!logFile.exists())
{
chatUser.sendMessage("Server log not found.");
return;
}
FileInputStream fis = new FileInputStream(logFile);
if (logFile.length() > 1000000)
{
fis.skip(logFile.length() - 1000000);
}
page = new BufferedReader(new InputStreamReader(fis));
final StringBuilder input = new StringBuilder();
String line;
Pattern patternStart = Pattern.compile("^[0-9 :-]+\\[INFO\\] Starting minecraft server version.*");
Pattern patternEnd = Pattern.compile("^[0-9 :-]+\\[INFO\\] Done \\([0-9.,]+s\\)! For help, type \"help\".*");
boolean log = false;
while ((line = page.readLine()) != null)
{
if (patternStart.matcher(line).matches())
{
if (input.length() > 0)
{
input.delete(0, input.length());
}
log = true;
}
if (log)
{
input.append(line).append("\n");
}
if (patternEnd.matcher(line).matches())
{
log = false;
}
}
if (input.length() > 10000)
{
input.delete(0, input.length() - 10000);
}
final PastieUpload pastie = new PastieUpload();
final String url = pastie.send(input.toString());
String message = "Startup: " + url;
chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message);
ircBot.sendMessage(message);
}
catch (IOException ex)
{
Bukkit.getLogger().log(Level.SEVERE, null, ex);
chatUser.sendMessage(ex.getMessage());
}
finally
{
try
{
if (page != null)
{
page.close();
}
}
catch (IOException ex)
{
Logger.getLogger(EssentialsHelp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void sendConfig()
{
BufferedReader page = null;
try
{
File configFolder = new File(plugin.getDataFolder().getParentFile(), "Essentials");
if (!configFolder.exists())
{
chatUser.sendMessage("Essentials plugin folder not found.");
return;
}
File configFile = new File(configFolder, "config.yml");
if (!configFile.exists())
{
chatUser.sendMessage("Essentials config file not found.");
return;
}
page = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), UTF8));
final StringBuilder input = new StringBuilder();
String line;
while ((line = page.readLine()) != null)
{
input.append(line).append("\n");
}
final PastieUpload pastie = new PastieUpload();
final String url = pastie.send(input.toString());
String message = "Essentials config.yml: " + url;
chatUser.sendMessage("§6" + ircBot.getNick() + ": §7" + message);
ircBot.sendMessage(message);
}
catch (IOException ex)
{
Bukkit.getLogger().log(Level.SEVERE, null, ex);
chatUser.sendMessage(ex.getMessage());
}
finally
{
try
{
if (page != null)
{
page.close();
}
}
catch (IOException ex)
{
Logger.getLogger(EssentialsHelp.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public void onPlayerChat(PlayerChatEvent event)
{
if (event.getPlayer() == chatUser)
{
boolean success = sendChatMessage(event.getPlayer(), event.getMessage());
event.setCancelled(success);
return;
}
}
@Override
public void onPlayerQuit(PlayerQuitEvent event)
{
chatUser = null;
if (ircBot != null) {
ircBot.quit();
ircBot = null;
}
return;
}
}
|