From 4515318dbb86b77773350aec78c8ef00cf361aa7 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Wed, 12 Dec 2012 16:44:27 -0800 Subject: [PATCH] More player-level actions --- minecraft/edit_users.cgi | 2 +- minecraft/lang/en | 17 +++++++- minecraft/minecraft-lib.pl | 83 +++++++++++++++++++++++++++++++++----- minecraft/save_conn.cgi | 61 ++++++++++++++++++++++++++++ minecraft/save_users.cgi | 43 ++++++++++++++++++++ minecraft/view_conn.cgi | 22 ++++++---- 6 files changed, 208 insertions(+), 20 deletions(-) create mode 100644 minecraft/save_users.cgi diff --git a/minecraft/edit_users.cgi b/minecraft/edit_users.cgi index 385eb87c9..f0ff60c9b 100644 --- a/minecraft/edit_users.cgi +++ b/minecraft/edit_users.cgi @@ -20,7 +20,7 @@ my @white = &list_whitelist_users(); print &ui_form_start("save_users.cgi", "post"); print &ui_hidden('mode', 'white'); print $text{'users_whitedesc'},"

\n"; -print &ui_textarea('white', join("\n", @white), 10, 80); +print &ui_textarea('white', join("\n", @white), 10, 80),"
\n"; my $enabled = &find_value("white-list", $conf); print &ui_checkbox("enabled", 1, $text{'users_enabled'}, $enabled =~ /true|yes/i); diff --git a/minecraft/lang/en b/minecraft/lang/en index 66968b72a..539f3eb25 100644 --- a/minecraft/lang/en +++ b/minecraft/lang/en @@ -22,7 +22,7 @@ users_tabop=Operators users_apply=Save and Apply users_whitedesc=When enabled, only users listed here will be allowed to connect to this Minecraft server. users_opdesc=Only users listed here are allowed to run administration commands on this Minecraft server. -users_enable=Whitelist enforcement enabled? +users_enabled=Whitelist enforcement enabled? conns_title=Connected Players conns_desc=The following players are currently connected to your server. Click on a player name to perform actions on it. @@ -85,6 +85,21 @@ conn_banb=Ban Player conn_oplist=Operator state conn_opb=Grant Operator conn_deopb=Revoke Operator +conn_ex=X position must be an integer +conn_ey=Y position must be an integer +conn_ez=Z position must be an integer +conn_spawndone=Moved spawn point to $1, $2, $3 +conn_tpdone=Teleported player to $1, $2, $3 +conn_tppdone=Teleported player to location of $1 +conn_bandone=Banned player $1 +conn_pardondone=Pardoned player $1 +conn_reason=for reason +conn_opdone=Granted operator status +conn_deopdone=Revoked operator status +conn_ebutton=No button clicked! +conn_kick=Disconnect player +conn_kickb=Kick Now +conn_kickdone=Disconnected player worlds_title=Manage Worlds diff --git a/minecraft/minecraft-lib.pl b/minecraft/minecraft-lib.pl index d227002dc..b3a4ef759 100644 --- a/minecraft/minecraft-lib.pl +++ b/minecraft/minecraft-lib.pl @@ -48,6 +48,11 @@ foreach my $p (@procs) { return undef; } +sub get_minecraft_config_file +{ +return $config{'minecraft_dir'}."/server.properties"; +} + # get_minecraft_config() # Parses the config into an array ref of hash refs sub get_minecraft_config @@ -55,8 +60,7 @@ sub get_minecraft_config my @rv; my $fh = "CONFIG"; my $lnum = 0; -&open_readfile($fh, $config{'minecraft_dir'}."/server.properties") || - return [ ]; +&open_readfile($fh, &get_minecraft_config_file()) || return [ ]; while(<$fh>) { s/\r|\n//g; s/#.*$//; @@ -71,6 +75,57 @@ close($fh); return \@rv; } +# find(name, &config) +# Returns all objects with some name in the config +sub find +{ +my ($name, $conf) = @_; +my @rv = grep { lc($_->{'name'}) eq lc($name) } @$conf; +return wantarray ? @rv : $rv[0]; +} + +# find_value(name, &config) +# Returns the values of all objects with some name in the config +sub find_value +{ +my ($name, $conf) = @_; +my @rv = map { $_->{'value'} } &find($name, $conf); +return wantarray ? @rv : $rv[0]; +} + +# save_directive(name, value, &config) +# Update one directive in the config +sub save_directive +{ +my ($name, $value, $conf) = @_; +my $old = &find($name, $conf); +my $lref = &read_file_lines(&get_minecraft_config_file()); +if ($old && defined($value)) { + # Update existing line + $lref->[$old->{'line'}] = $name."=".$value; + $old->{'value'} = $value; + } +elsif ($old && !defined($value)) { + # Delete existing line + splice(@$lref, $old->{'line'}, 1); + my $idx = &indexof($old, @$conf); + splice(@$conf, $idx, 1) if ($idx >= 0); + foreach my $c (@$conf) { + if ($c->{'line'} > $old->{'line'}) { + $c->{'line'}--; + } + } + } +elsif (!$old && defined($value)) { + # Add new line + my $n = { 'name' => $name, + 'value' => $value, + 'line' => scalar(@$lref) }; + push(@$lref, $name."=".$value); + push(@$conf, $n); + } +} + # get_start_command() # Returns a command to start the server sub get_start_command @@ -299,11 +354,16 @@ foreach my $l (@out) { return @rv; } +sub get_whitelist_file +{ +return $config{'minecraft_dir'}.'/white-list.txt'; +} + # list_whitelist_users() # Returns a list of usernames on the whitelist sub list_whitelist_users { -my $lref = &read_file_lines($config{'minecraft_dir'}.'/white-list.txt', 1); +my $lref = &read_file_lines(&get_whitelist_file(), 1); return @$lref; } @@ -312,16 +372,21 @@ return @$lref; sub save_whitelist_users { my ($users) = @_; -my $lref = &read_file_lines($config{'minecraft_dir'}.'/white-list.txt'); +my $lref = &read_file_lines(&get_whitelist_file()); @$lref = @$users; -&flush_file_lines($config{'minecraft_dir'}.'/white-list.txt'); +&flush_file_lines(&get_whitelist_file()); +} + +sub get_op_file +{ +return $config{'minecraft_dir'}.'/ops.txt'; } # list_op_users() # Returns a list of usernames on the operator list sub list_op_users { -my $lref = &read_file_lines($config{'minecraft_dir'}.'/ops.txt', 1); +my $lref = &read_file_lines(&get_op_file(), 1); return @$lref; } @@ -330,11 +395,9 @@ return @$lref; sub save_op_users { my ($users) = @_; -my $lref = &read_file_lines($config{'minecraft_dir'}.'/ops.txt'); +my $lref = &read_file_lines(&get_op_file()); @$lref = @$users; -&flush_file_lines($config{'minecraft_dir'}.'/ops.txt'); +&flush_file_lines(&get_op_file()); } - - 1; diff --git a/minecraft/save_conn.cgi b/minecraft/save_conn.cgi index 7388f7626..f2b8a58d3 100644 --- a/minecraft/save_conn.cgi +++ b/minecraft/save_conn.cgi @@ -20,6 +20,11 @@ elsif ($in{'kill'}) { &send_server_command("/kill $in{'name'}"); $msg = $text{'conn_killdone'}; } +elsif ($in{'kick'}) { + # Disconnect this player + &send_server_command("/kick $in{'name'}"); + $msg = $text{'conn_kickdone'}; + } elsif ($in{'give'}) { # Give an item $in{'id'} =~ /^\d+$/ || &error($text{'conn_eid'}); @@ -33,6 +38,62 @@ elsif ($in{'give'}) { $msg = &text('conn_givedone', $i ? $i->{'name'} : $in{'id'}, $in{'count'}); } +elsif ($in{'spawn'}) { + # Change spawn point + $in{'spawnx'} =~ /^\-?([0-9]+)$/ || &error($text{'conn_ex'}); + $in{'spawny'} =~ /^\-?([0-9]+)$/ || &error($text{'conn_ey'}); + $in{'spawnz'} =~ /^\-?([0-9]+)$/ || &error($text{'conn_ez'}); + my $out = &execute_minecraft_command( + "/spawnpoint $in{'name'} $in{'spawnx'} $in{'spawny'} $in{'spawnz'}"); + $out =~ /Set\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_spawndone', $in{'spawnx'}, $in{'spawny'}, $in{'spawnz'}); + } +elsif ($in{'tp'}) { + $in{'tpx'} =~ /^\~?\-?([0-9]+)$/ || &error($text{'conn_ex'}); + $in{'tpy'} =~ /^\~?\-?([0-9]+)$/ || &error($text{'conn_ey'}); + $in{'tpz'} =~ /^\~?\-?([0-9]+)$/ || &error($text{'conn_ez'}); + my $out = &execute_minecraft_command( + "/tp $in{'name'} $in{'tpx'} $in{'tpy'} $in{'tpz'}"); + $out =~ /Teleported\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_tpdone', $in{'tpx'}, $in{'tpy'}, $in{'tpz'}); + } +elsif ($in{'tpp'}) { + my $out = &execute_minecraft_command( + "/tp $in{'name'} $in{'player'}"); + $out =~ /Teleported\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_tppdone', $in{'player'}); + } +elsif ($in{'ban'}) { + my $out = &execute_minecraft_command( + "/ban $in{'name'} $in{'reason'}"); + $out =~ /Banned\s+player\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_bandone'); + } +elsif ($in{'pardon'}) { + my $out = &execute_minecraft_command( + "/pardon $in{'name'}"); + $out =~ /Unbanned\s+player\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_pardondone'); + } +elsif ($in{'op'}) { + my $out = &execute_minecraft_command( + "/op $in{'name'}"); + $out =~ /Opped\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_opdone'); + } +elsif ($in{'deop'}) { + my $out = &execute_minecraft_command( + "/deop $in{'name'}"); + $out =~ /De-opped\s+\Q$in{'name'}\E/ || + &error(&html_escape($out)); + $msg = &text('conn_deopdone'); + } else { # No button clicked! &error($text{'conn_ebutton'}); diff --git a/minecraft/save_users.cgi b/minecraft/save_users.cgi new file mode 100644 index 000000000..8a01567ff --- /dev/null +++ b/minecraft/save_users.cgi @@ -0,0 +1,43 @@ +#!/usr/local/bin/perl +# Update whitelisted or operator users + +use strict; +use warnings; +require './minecraft-lib.pl'; +our (%in, %text, %config); +&ReadParse(); +my $conf = &get_minecraft_config(); +&error_setup($text{'users_err'}); + +if ($in{'mode'} eq 'white') { + # Update whitelist, and maybe apply it + &lock_file(&get_minecraft_config_file()); + &lock_file(&get_whitelist_file()); + my @users = split(/\r?\n/, $in{'white'}); + &save_whitelist_users(\@users); + &save_directive("white-list", $in{'enabled'} ? 'true' : 'false', $conf); + &flush_file_lines(); + &unlock_file(&get_whitelist_file()); + &unlock_file(&get_minecraft_config_file()); + + if ($in{'apply'}) { + &send_server_command("/whitelist reload"); + &send_server_command("/whitelist ". + ($in{'enabled'} ? "on" : "off")); + } + + &webmin_log('white'); + } +elsif ($in{'mode'} eq 'op') { + # Update operator list + &lock_file(&get_op_file()); + my @users = split(/\r?\n/, $in{'op'}); + &save_op_users(\@users); + &unlock_file(&get_op_file()); + &webmin_log('op'); + } +else { + &error($text{'users_emode'}); + } +&redirect(""); + diff --git a/minecraft/view_conn.cgi b/minecraft/view_conn.cgi index e87c54a42..97ea93cbc 100644 --- a/minecraft/view_conn.cgi +++ b/minecraft/view_conn.cgi @@ -57,6 +57,10 @@ if ($c || 1) { print &ui_table_row($text{'conn_kill'}, &ui_submit($text{'conn_killb'}, 'kill')); + # Disconnect player + print &ui_table_row($text{'conn_kick'}, + &ui_submit($text{'conn_kickb'}, 'kick')); + # Grant item print &ui_table_row($text{'conn_give'}, &ui_textbox("id", undef, 5). @@ -70,22 +74,22 @@ if ($c || 1) { # Change spawn point print &ui_table_row($text{'conn_spawn'}, - "X:".&ui_textbox("spawnx", $x, 20)." ". - "Y:".&ui_textbox("spawny", $y, 20)." ". - "Z:".&ui_textbox("spawnz", $z, 20)." ". + "X:".&ui_textbox("spawnx", int($x), 20)." ". + "Y:".&ui_textbox("spawny", int($y), 20)." ". + "Z:".&ui_textbox("spawnz", int($z), 20)." ". &ui_submit($text{'conn_spawnb'}, 'spawn')); # Teleport to location print &ui_table_row($text{'conn_tp'}, - "X:".&ui_textbox("tpx", $x, 20)." ". - "Y:".&ui_textbox("tpy", $y, 20)." ". - "Z:".&ui_textbox("tpz", $z, 20)." ". + "X:".&ui_textbox("tpx", int($x), 20)." ". + "Y:".&ui_textbox("tpy", int($y), 20)." ". + "Z:".&ui_textbox("tpz", int($z), 20)." ". &ui_submit($text{'conn_tpb'}, 'tp')); # Teleport to player if (@conns) { print &ui_table_row($text{'conn_tpp'}, - &ui_select("player", \@conns, undef)." ". + &ui_select("player", undef, \@conns)." ". &ui_submit($text{'conn_tpb'}, 'tpp')); } @@ -100,7 +104,9 @@ if ($c || 1) { else { print &ui_table_row($text{'conn_banlist'}, $text{'conn_pardoned'}." ". - &ui_submit($text{'conn_banb'}, 'ban')); + &ui_submit($text{'conn_banb'}, 'ban')." ". + $text{'conn_reason'}." ". + &ui_textbox("reason", undef, 30)); } # Op or de-op player