html/hitcount.pl


   1 # $Id: hitcount.pl,v 1.3.2.2 2002/03/05 18:19:28 shrike Exp shrike $
   2 
   3 use vars qw($VERSION %IRSSI);
   4 my @rev = split(/ /, "$Revision: 1.3.2.2 $n");
   5 $VERSION = "1.3";
   6 %IRSSI = (
   7 	    authors     => 'Riku "Shrike" Lindblad',
   8 	    contact     => 'shrike\@addiktit.net, Shrike on IRCNet/QNet/EFNet/DALNet',
   9 	    name        => 'hitcount',
  10 	    description => 'Add a apache page hitcounter to statusbar',
  11 	    license     => 'Free',
  12 	    changed     => '$Date: 2002/03/05 18:19:28 $ ',
  13 	  );
  14 
  15 # Changelog:
  16 #
  17 # $Log: hitcount.pl,v $
  18 # Revision 1.3.2.2  2002/03/05 18:19:28  shrike
  19 # Added use vars qw($VERSION %IRSSI);
  20 #
  21 # Revision 1.3.2.1  2002/03/05 18:04:01  shrike
  22 # Damnit, left default refresh to a debug value...
  23 #
  24 # Revision 1.3.1.1  2002/03/05 17:59:47  shrike
  25 # Forgot to turn off debugging...
  26 #
  27 # Revision 1.3  2002/03/05 17:57:04  shrike
  28 # Added use strict
  29 # .. which finally cleared up last of the bugs from the code (hopefully)
  30 # Next on TODO: get the item colors from theme and more configuration options.
  31 #
  32 # Revision 1.2  2002/03/05 17:27:15  shrike
  33 # Added standard script headers (http://juerd.nl/irssi/headers.html)
  34 # Removed call to Irssi::statusbars_recreate_items();
  35 # And a bit of polishing here and there..
  36 # The first two updates bug, need to fix that.
  37 #
  38 
  39 #  To install, you also need to put 
  40 #  hitcount = { };
  41 #  into your statusbar code in irssi config
  42 #
  43 # sets:
  44 #  /SET hitcount_regexp - A regexp that identifies your homepage
  45 #  /SET hitcount_refresh - Refresh rate
  46 #  /SET hitcount_access_log - webserver access log
  47 
  48 # TODO:
  49 # Add ignore regexp, to prevent f.ex. css-files from increasing counter
  50 
  51 use Irssi::TextUI;
  52 use strict;
  53 
  54 # Debug level - higher levels print out more crap
  55 my $debug_level = 0;
  56 # current hitcount
  57 my ($total_hitcount, $my_hitcount) = (0);
  58 # change prefixes
  59 my ($my_prefix, $total_prefix) = ("");
  60 # change from last update
  61 my ($my_change, $total_change) = (0);
  62 # hitcount on last update
  63 my ($last_total_hitcount, $last_my_hitcount, $last_refresh) = (0);
  64 # set default variables
  65 my ($filename, $regexp, $refresh) = ("/var/log/apache/access.log", "/", 60);
  66 
  67 sub get_hitcount {
  68     my $filename = Irssi::settings_get_str('hitcount_access_log');
  69     my $regexp = Irssi::settings_get_str('hitcount_regexp');
  70     
  71     Irssi::print("Finding match for \"".my $regexp."\"", MSGLEVEL_CLIENTERROR) if($debug_level > 2);
  72     
  73     ($total_hitcount, $my_hitcount) = (0);
  74     
  75     # Go through the access log and count matches to the given regexp
  76     if(open STUFF, "$filename")
  77     {
  78    	while (<STUFF>) 
  79 	{
  80 	    $total_hitcount++;
  81 	    if(/$regexp/ois)
  82 	    {
  83 		# DEBUG
  84 		Irssi::print("Matched $_", MSGLEVEL_CLIENTERROR) if($debug_level > 3);
  85 		$my_hitcount++;
  86 	    }
  87 	}
  88 	close STUFF;
  89     }
  90     else
  91     {
  92 	Irssi::print("Failed to open <$filename: $!", MSGLEVEL_CLIENTERROR);
  93     }
  94     return($my_hitcount,$total_hitcount);
  95 }
  96 
  97 sub hitcount {
  98     my ($item, $get_size_only) = @_;
  99     
 100     # DEBUG
 101     Irssi::print("$get_size_only | $last_my_hitcount/$last_total_hitcount | $my_hitcount/$total_hitcount | $my_prefix$my_change $total_prefix$total_change", MSGLEVEL_CLIENTERROR) if($debug_level > 0);
 102     
 103     my ($my_hitcount, $my_total_hitcount) = get_hitcount();
 104     
 105     if($my_hitcount eq '') { $my_hitcount = 0; }
 106     
 107     # Calculate change since last update
 108     $my_change = $my_hitcount - $last_my_hitcount;
 109     $total_change = $total_hitcount - $last_total_hitcount;
 110     
 111     # Get correct prefix for change
 112     $my_prefix = "+" if($my_change > 0);
 113     $my_prefix = "-" if($my_change < 0);
 114     $my_prefix = ""  if($my_change == 0);
 115     $total_prefix = "+" if($total_change > 0);
 116     $total_prefix = "-" if($total_change < 0);
 117     $total_prefix = "" if($total_change == 0);
 118     
 119     $item->default_handler($get_size_only, undef, "$last_my_hitcount $last_total_hitcount $my_prefix$my_change $total_prefix$total_change", 1);
 120     
 121     # last hitcount = current hitcount
 122     $last_my_hitcount = $my_hitcount;
 123     $last_total_hitcount = $total_hitcount;
 124     
 125     # reset hitcounts
 126     $my_hitcount = 0;
 127     $total_hitcount = 0;
 128     $my_total_hitcount = 0;
 129 }
 130 
 131 sub refresh_hitcount {
 132     get_hitcount();
 133     Irssi::statusbar_items_redraw('hitcount');
 134 }
 135 
 136 sub read_settings {
 137     my $time = Irssi::settings_get_int('hitcount_refresh');
 138     return if ($time == $last_refresh);
 139 
 140     $last_refresh = $time;
 141     Irssi::timeout_remove(my $refresh_tag) if (my $refresh_tag);
 142     $refresh_tag = Irssi::timeout_add($time*1000, 'refresh_hitcount', undef);
 143 }
 144 
 145 # default values
 146 Irssi::settings_add_str('misc', 'hitcount_regexp', $regexp);
 147 Irssi::settings_add_int('misc', 'hitcount_refresh', $refresh);
 148 Irssi::settings_add_str('misc', 'hitcount_access_log', $filename);
 149 # sub to call, string on statusbar, func on statusbar
 150 Irssi::statusbar_item_register('hitcount', '{sb Hits: $0%K/%N$1 $2%K/%N$3}', 'hitcount');
 151 
 152 read_settings();
 153 Irssi::signal_add('setup changed', 'read_settings');
 154 
 155 Irssi::print("Hitcounter version ".$rev[1]." loaded");