Henrybk

Plugin again

Feb 12th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.73 KB | None | 0 0
  1. package dcstop;
  2.  
  3. use strict;
  4. use Log qw(error warning);
  5. use Globals;
  6. use Misc;
  7.  
  8. my ($start,$now,$work,$dcPerHour);
  9.  
  10. my @dcs;
  11.  
  12. my $hooks = Plugins::addHooks(
  13.     ['disconnected', \&dc],
  14.     ['initialized', \&conf],
  15. );
  16.  
  17. my $command = Commands::register(
  18.     ['dcstop', 'dcstop command', \&cmddcstop],
  19. );
  20.  
  21. Plugins::register("dcstop", "disconnect if dc/hour > x", \&unload);
  22.  
  23. sub conf {
  24.     if (!$config{dcstop_dcPerHour}) {
  25.         Misc::configModify('dcstop_dcPerHour', '15');
  26.     }
  27.     if (!$config{dcstop_hours}) {
  28.         Misc::configModify('dcstop_hours', '1');
  29.     }
  30. }
  31.  
  32. sub unload {
  33.     Plugins::delHooks($hooks);
  34.     Commands::unregister($command);
  35. }
  36.  
  37. sub cmddcstop {
  38.     my (undef, $args) = @_;
  39.  
  40.     if ($args eq "reset") {
  41.         Log::warning("Resetting dc counter\n");
  42.         ($start,$now,$work,$dcPerHour) = 0;
  43.                undef @dcs;
  44.         $start = time;
  45.     } elsif (!$args or $args eq "info") {
  46.               my $dc = scalar @dcs;
  47.         Log::warning("dc Counter: ".$dc."\ndcPerHour Counter: ".$dcPerHour."\n");
  48.     } else {
  49.         Log::error("Unknown argument: ".$args."\n");
  50.     }
  51. }
  52.  
  53. sub dc {
  54.         push(@dcs, time);
  55.         $now = time;
  56.         my $timelimit = ($now -($config{dcstop_hours} * 3600));
  57.         while ($dcs[0] < $timelimit) {
  58.             shift(@dcs);
  59.         }
  60.         my $dc = scalar @dcs;
  61.     $start = $startTime_EXP;
  62.     $work = (int($now - $start))/3600;
  63.     $dcPerHour = int($dc/$work);
  64.  
  65.     if ($config{dcstop_dcPerHour} <= $dcPerHour) {
  66.         Log::error("Disconnect per hour greater than ".$config{dcstop_dcPerHour}." : ".$dcPerHour."\n");
  67.         Misc::offlineMode();
  68.         ($dc,$start,$now,$work,$dcPerHour) = 0;
  69.         $start = time;
  70.     }
  71. }
  72. 1;
Advertisement
Add Comment
Please, Sign In to add comment