diff --git a/minecraft/index.cgi b/minecraft/index.cgi
index 3a79c3aac..da42fdd48 100755
--- a/minecraft/index.cgi
+++ b/minecraft/index.cgi
@@ -57,17 +57,17 @@ my @links = ( "edit_conf.cgi", "edit_users.cgi",
"view_logs.cgi", "list_conns.cgi",
"list_worlds.cgi", "edit_cmds.cgi",
"console.cgi", "edit_backup.cgi",
- "edit_manual.cgi" );
+ "list_playtime.cgi", "edit_manual.cgi" );
my @titles = ( $text{'conf_title'}, $text{'users_title'},
$text{'logs_title'}, $text{'conns_title'},
$text{'worlds_title'}, $text{'cmds_title'},
$text{'console_title'}, $text{'backup_title'},
- $text{'manual_title'} );
+ $text{'playtime_title'}, $text{'manual_title'} );
my @icons = ( "images/conf.gif", "images/users.gif",
"images/logs.gif", "images/conns.gif",
"images/worlds.gif", "images/cmds.gif",
"images/console.gif", "images/backup.gif",
- "images/manual.gif" );
+ "images/playtime.gif", "images/manual.gif" );
&icons_table(\@links, \@titles, \@icons, 5);
print &ui_hr();
diff --git a/minecraft/lang/en b/minecraft/lang/en
index 38622eb67..d34581030 100644
--- a/minecraft/lang/en
+++ b/minecraft/lang/en
@@ -306,10 +306,11 @@ monitor_checklog=Check server response to commands?
playtime_title=Daily Play Time
playtime_user=Username
playtime_time=Time online
+playtime_ltime=Enforced time
playtime_now=Status
playtime_none=No players were connected for this day.
playtime_on=Online
-playtime_off=Off
+playtime_off=Offline
playtime_header=Daily play limit settings
playtime_enabled=Limits enabled?
playtime_max=Maximum time per day
diff --git a/minecraft/list_playtime.cgi b/minecraft/list_playtime.cgi
index ead98f04f..2e059ac29 100644
--- a/minecraft/list_playtime.cgi
+++ b/minecraft/list_playtime.cgi
@@ -8,12 +8,13 @@ our (%in, %text, %config);
&ui_print_header(undef, $text{'playtime_title'}, "");
-my $playtime = &get_current_day_usage();
+my ($playtime, $limit_playtime) = &get_current_day_usage();
my @conns = &list_connected_players();
if (keys %$playtime) {
print &ui_columns_start([ $text{'playtime_user'},
$text{'playtime_time'},
+ $text{'playtime_ltime'},
$text{'playtime_now'} ]);
foreach my $u (sort { $playtime->{$b} <=> $playtime->{$a} }
keys %$playtime) {
@@ -21,9 +22,10 @@ if (keys %$playtime) {
"".
&html_escape($u)."",
&nice_seconds($playtime->{$u}),
+ &nice_seconds($limit_playtime->{$u} || 0),
&indexof($u, @conns) >= 0 ?
"$text{'playtime_on'}" :
- $text{'playtime_off'},
+ "$text{'playtime_off'}",
]);
}
print &ui_columns_end();
@@ -64,6 +66,7 @@ print &ui_table_row($text{'playtime_ips'},
&ui_opt_textbox("ips", $config{'playtime_ips'}, 40,
$text{'playtime_all2'}, $text{'playtime_sel2'}));
+print &ui_table_end();
print &ui_form_end([ [ undef, $text{'save'} ] ]);
&ui_print_footer("", $text{'index_return'});
diff --git a/minecraft/minecraft-lib.pl b/minecraft/minecraft-lib.pl
index 703c60f07..ef2b4e66a 100644
--- a/minecraft/minecraft-lib.pl
+++ b/minecraft/minecraft-lib.pl
@@ -13,6 +13,8 @@ our ($module_root_directory, %text, %gconfig, $root_directory, %config,
our $history_file = "$module_config_directory/history.txt";
our $server_jar_url = "https://s3.amazonaws.com/MinecraftDownload/launcher/minecraft_server.jar";
+&foreign_require("webmin");
+
# check_minecraft_server()
# Returns an error message if the Minecraft server is not installed
sub check_minecraft_server
@@ -756,7 +758,8 @@ if (time() - $config{'last_check'} > 6*60*60) {
}
# get_current_day_usage()
-# Returns a hash ref from usernames to usage over the last day
+# Returns a hash ref from usernames to total usage over the last day, and
+# usage that counts towards any limits
sub get_current_day_usage
{
my $logfile = $config{'minecraft_dir'}."/server.log";
@@ -783,15 +786,20 @@ while(1) {
}
# Read forwards, looking for logins and logouts for today
-my %rv;
-my %lastlogin;
+my (%rv, %limit_rv);
+my (%lastlogin, %limit_lastlogin);
while(my $line = ) {
$line =~ /^((\d+)\-(\d+)\-(\d+))\s+(\d+):(\d+):(\d+)/ || next;
- $1 eq $wantday || next;
+ my $day = $1;
+ $day eq $wantday || next;
my $secs = $5*60*60 + $6*60 + $7;
- if ($line =~ /\s(\S+)\s*\[[^\]]+\]\s+logged\s+in\s/) {
+ if ($line =~ /\s(\S+)\[.*\/([0-9\.]+):(\d+)\]\s+logged\s+in\s/) {
# Login by a user
- $lastlogin{$1} = $secs;
+ my ($u, $ip) = ($1, $2);
+ $lastlogin{$u} = $secs;
+ if (&limit_user($ip, $u, $day)) {
+ $limit_lastlogin{$u} = $secs;
+ }
}
elsif ($line =~ /\s(\S+)(\s*\[[^\]]+\])?\s+lost\s+connection/) {
# Logout .. count time
@@ -800,6 +808,11 @@ while(my $line = ) {
$rv{$1} += $secs - $lastlogin{$1};
delete($lastlogin{$1});
}
+ if (defined($limit_lastlogin{$1})) {
+ # Also for login that counts towards limits
+ $limit_rv{$1} += $secs - $limit_lastlogin{$1};
+ delete($limit_lastlogin{$1});
+ }
}
}
close(LOGFILE);
@@ -809,8 +822,11 @@ my $now = $tm[2]*60*60 + $tm[1]*60 + $tm[0];
foreach my $u (keys %lastlogin) {
$rv{$u} += $now - $lastlogin{$u};
}
+foreach my $u (keys %limit_lastlogin) {
+ $limit_rv{$u} += $now - $limit_lastlogin{$u};
+ }
-return \%rv;
+return (\%rv, \%limit_rv);
}
# nice_seconds(secs)
@@ -833,4 +849,28 @@ else {
}
}
+# limit_user(ip, user, date)
+# Returns 1 if some usage should be counted for limiting purposes
+sub limit_user
+{
+my ($ip, $user, $date) = @_;
+my @users = split(/\s+/, $config{'playtime_users'});
+if (@users && &indexoflc($user, @users) < 0) {
+ return 0;
+ }
+my @ips = split(/\s+/, $config{'playtime_ips'});
+if (@ips && !&webmin::ip_match($ip, @ips)) {
+ return 0;
+ }
+my @days = split(/\s+/, $config{'playtime_days'});
+if (@days > 0 && @days < 7) {
+ my ($y, $m, $d) = split(/\-/, $date);
+ my @tm = localtime(timelocal(0, 0, 0, $d, $m-1, $y-1900));
+ if (@tm && &indexof($tm[6], @days) < 0) {
+ return 0;
+ }
+ }
+return 1;
+}
+
1;