1 # ho_stats_y.pl
  2 #
  3 # $Id: ho_stats_l.pl,v 1.3 2004/08/26 16:57:46 jvunder REL_0_3 $
  4 #
  5 # Part of the Hybrid Oper Script Collection
  6 #
  7 # Reformats /stats y output.
  8 #
  9 
 10 use strict;
 11 use vars qw($VERSION %IRSSI $SCRIPT_NAME);
 12 
 13 use Irssi;
 14 use Irssi::Irc;
 15 use HOSC::again;
 16 use HOSC::again 'HOSC::Base';
 17 use HOSC::again 'HOSC::Tools';
 18 
 19 # ---------------------------------------------------------------------
 20 
 21 $SCRIPT_NAME = 'Stats L reformatting';
 22 ($VERSION) = '$Revision: 1.3 $' =~ / (\d+\.\d+) /;
 23 %IRSSI = (
 24     authors     => 'JamesOff',
 25     contact     => 'james@jamesoff.net',
 26     name        => 'stats l',
 27     description => 'Reformats stats l and stats ?',
 28     license     => 'GPL v2',
 29     url         => 'http://www.jamesoff.net',
 30 );
 31 
 32 # Temporary variables to be able to print the output of two lines in a
 33 # single line. A rather ugly hack, but, shrug.
 34 # Sent total :   11.72 Megabytes
 35 # Recv total :   13.37 Megabytes
 36 # $sent_server is used to store "11.72 Megabytes" which is used when the
 37 # Recv total line is processed.
 38 my ($sent_server, $sent_total, $sent_total_speed);
 39 
 40 # ---------------------------------------------------------------------
 41 
 42 sub event_stats_l_line {
 43     my ($server, $data, $nick, $address) = @_;
 44 
 45     my ($user, $sendq, $sentMsgs, $sentK, $recvMsgs, $recvK, $time_on, $time_idle, $features) = 
 46         $data =~ /\w+ (\S+) (\d+) (\d+) (\d+) (\d+) (\d+) :(\d+) (\d+)( .+)?/;
 47 
 48     my ($username, $hostname) = ('unknown', 'unknown');
 49     if ($user =~ /^([^[]+)\[(.+)@(.+)\]$/) {
 50         ($user, $username, $hostname) = ($1, $2, $3);
 51     }
 52     Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_header', 
 53         $user, $username, $hostname);
 54 
 55     my $sendq_format = 'ho_stats_l_sendq_zero';
 56     my $tick_size = Irssi::settings_get_int('ho_stats_l_sendq_tick');
 57     $sendq_format = 'ho_stats_l_sendq_low'    if $sendq > 4 * $tick_size;
 58     $sendq_format = 'ho_stats_l_sendq_medium' if $sendq > 8 * $tick_size;
 59     $sendq_format = 'ho_stats_l_sendq_high'   if $sendq > 16 * $tick_size;
 60 
 61     my $ticks  = '*' x ($sendq / $tick_size);
 62     my $spaces = '.' x 
 63         (Irssi::settings_get_int('ho_stats_l_sendq_width') - (length $ticks));
 64 
 65     Irssi::printformat(MSGLEVEL_CRAP, $sendq_format, $sendq, 
 66         $ticks, $spaces, ' ' x (8 - (length $sendq)));
 67 
 68     Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_sent', $sentMsgs, $sentK)
 69         unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
 70     Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_recv', $recvMsgs, $recvK)
 71         unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
 72 
 73     my $days = int($time_on / 86400);  
 74     my $hours = int(($time_on - $days * 86400) / 3600);
 75     $time_on -= ($days * 86400);
 76     my $mins = int(($time_on - $hours * 3600) / 60);
 77     my $secs = $time_on - $hours * 3600 - $mins * 60;
 78 
 79     $time_on = sprintf("%03d+%02d:%02d:%02d", $days, $hours, $mins, $secs);
 80 
 81     $days = int($time_idle / 86400);  
 82     $hours = int(($time_idle - $days * 86400) / 3600 );
 83     $time_idle -= ($days * 86400);
 84     $mins = int(($time_idle - $hours * 3600) / 60);
 85     $secs = $time_idle - $hours * 3600 - $mins * 60;
 86 
 87     $time_idle = sprintf("%03d+%02d:%02d:%02d", $days, $hours, $mins, $secs);
 88 
 89     Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_connidle', 
 90         $time_on, $time_idle)
 91         unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
 92 
 93     if ($features ne " -") {
 94         Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_supports', $features)
 95             unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
 96     }
 97 }
 98 
 99 # ---------------------------------------------------------------------
100 
101 sub event_stats_l_line_traffic {
102     my ($server, $data, $nick, $address) = @_;
103 
104     if ($data =~ /:(\d) total server/) {
105         Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_total_servers', $1)
106             unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
107     } elsif ($data =~ /:Sent total\s*:\s*(\d+\.\d+\s+\w+)\s*$/) {
108         $sent_server = $1;
109     } elsif ($data =~ /:Recv total\s*:\s*(\d+\.\d+\s+\w+)\s*$/) {
110         Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_traffic_servers', 
111                 $sent_server, $1)
112             unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
113     } elsif ($data =~ /:Server send:\s*(\d+\.\d+\s+\w+)\s+\(\s*([^)]+)\)/) {
114         ($sent_total, $sent_total_speed) = ($1, $2);
115     } elsif ($data =~ /:Server recv:\s*(\d+\.\d+\s+\w+)\s+\(\s*([^)]+)\)/) {
116         Irssi::printformat(MSGLEVEL_CRAP, 'ho_stats_l_traffic_total', 
117                 $sent_total, $sent_total_speed, $1, $2)
118             unless Irssi::settings_get_bool('ho_stats_l_print_sendq_only');
119     } else {
120         # Let the signal continue, it's not for us.
121         return;
122     }
123 
124     Irssi::signal_stop();
125 }
126 
127 # ---------------------------------------------------------------------
128 
129 sub reemit_stats_l_line {
130     my ($server, $data, $servername) = @_;
131 
132     # We need to re-emit this 249 numeric in case it contains data which
133     # has nothing to do with stats l or ?. Unfortunately, STATS p
134     # also uses numeric 249, and we don't want to lose this data.
135 
136     # For some reason I do not comprehend, Irssi does not display
137     # the first word of $args when re-emitting this signal. Hence
138     # the 'dummy_data' addition.
139     # Perhaps the number of the numeric should be here.
140     # Perhaps there is a rational explanation.
141     # I do not know, but this seems to work properly.
142     Irssi::signal_emit("default event numeric",
143         $server, "dummy_data " . $data, $servername);
144     Irssi::signal_stop();
145 }
146 
147 # ---------------------------------------------------------------------
148 
149 sub event_stats_end {
150     my ($server, $data, $servername) = @_;
151 
152     return unless $data =~ /[lL?] :End of \/STATS report/;
153 
154     Irssi::signal_stop();
155 }
156 
157 # ---------------------------------------------------------------------
158 
159 ho_print_init_begin();
160 
161 Irssi::theme_register([
162     'ho_stats_l_header',
163     '%_$0%_ ($1@$2)',
164 
165     'ho_stats_l_sendq_zero',
166     '  SendQ %G$0%n bytes $3[%G$1%n$2]',
167     
168     'ho_stats_l_sendq_low',
169     '  SendQ %Y$0%n bytes $3[%Y$1%n$2]',
170     
171     'ho_stats_l_sendq_medium',
172     '  SendQ %r$0%n bytes $3[%r$1%n$2]',
173     
174     'ho_stats_l_sendq_high',
175     '  SendQ %R$0%n bytes $3[%R$1%n$2]',
176     
177     'ho_stats_l_sent',
178     '  Sent %_$[-10]0%_ msgs in %_$[-10]1%_kB',
179 
180     'ho_stats_l_recv',
181     '  Recv %_$[-10]0%_ msgs in %_$[-10]1%_kB',
182 
183     'ho_stats_l_connidle',
184     '  Conn %_$0%_  Idle %_$1%_',
185 
186     'ho_stats_l_supports',
187     '  Supports %_$0%_',
188 
189     'ho_stats_l_total_servers',
190     'Linked servers: %G$0%n',
191 
192     'ho_stats_l_traffic_servers',
193     'Sent %Y$0%n  Recv %G$1%n  [to/from other servers]',
194 
195     'ho_stats_l_traffic_total',
196     'Sent %Y$0%n ($1) Recv %G$2%n ($3) [total]',
197 
198 ]);
199 
200 Irssi::settings_add_bool('ho', 'ho_stats_l_print_sendq_only', 0);
201 Irssi::settings_add_int('ho', 'ho_stats_l_sendq_tick', 262144);
202 Irssi::settings_add_int('ho', 'ho_stats_l_sendq_width', 40);
203 
204 Irssi::signal_add_first("event 211", "event_stats_l_line");
205 Irssi::signal_add_first("event 249", "event_stats_l_line_traffic");
206 Irssi::signal_add_last ("event 249", "reemit_stats_l_line");
207 Irssi::signal_add_first("event 219", "event_stats_end");
208 
209 ho_print("STATS L and STATS ? output is now being reformatted.");
210 ho_print("Enable the 'ho_stats_l_print_sendq_only' setting if you ".
211     "only want to see the sendqs if you use STATS ?.");
212 
213 ho_print_init_end();
214 
215 # ---------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1