html/map.pl


   1 # Map - Generates simple tree of IRC network based on the output of the LINKS
   2 # command.
   3 #
   4 # $Id: map.pl,v 1.2 2002/02/01 22:21:20 pasky Exp pasky $
   5 
   6 
   7 use strict;
   8 
   9 use vars qw ($VERSION %IRSSI $rcsid);
  10 
  11 $rcsid = '$Id: map.pl,v 1.2 2002/02/01 22:21:20 pasky Exp pasky $';
  12 ($VERSION) = '$Revision: 1.2 $' =~ / (\d+\.\d+) /;
  13 %IRSSI = (
  14           name        => 'map',
  15           authors     => 'Petr Baudis',
  16           contact     => 'pasky@ji.cz',
  17           url         => 'http://pasky.ji.cz/~pasky/dev/irssi/',
  18           license     => 'GPLv2, not later',
  19           description => 'Generates simple tree of IRC network based on the output of the LINKS command.'
  20          );
  21 
  22 
  23 my $root;  # The root lc(server)
  24 my %tree;  # Key is lc(server), value is lc(array of downlinks)
  25 my %rcase; # Key is lc(server), value is server
  26 my %sname; # Key is lc(server), value is server's name
  27 my @branches; # Index is level, value is (should_print_'|')
  28 
  29 
  30 use Irssi 20011112;
  31 use Irssi::Irc;
  32 
  33 
  34 sub cmd_map {
  35   my ($data, $server, $channel) = @_;
  36 
  37   # ugly, but no easy way how to distinguish between two mixes links output :/
  38   $server->redirect_event('command map', 0, '',
  39       (split(/\s+/, $data) > 1), undef,
  40       {
  41 	"event 364", "redir links_line",
  42 	"event 365", "redir links_done",
  43       } );
  44 
  45   $server->send_raw("LINKS $data");
  46 
  47   Irssi::signal_stop();
  48 }
  49 
  50 
  51 sub event_links_line {
  52   my ($server, $data, $nick, $address) = @_;
  53   my ($target, $to, $from, $hops, $name) = $data =~ /^(\S*)\s+(\S*)\s+(\S*)\s+:(\d+)\s+(.*)$/;
  54   
  55   $rcase{lc($from)} = $from;
  56   $rcase{lc($to)} = $to;
  57   $sname{lc($to)} = $name;
  58 
  59   if ($hops == 0) {
  60     $root = lc($from);
  61   } else {
  62     push(@{$tree{lc($from)}}, lc($to));
  63   }
  64 
  65   Irssi::signal_stop();
  66 }
  67 
  68 sub event_links_done {
  69   my ($server, $data, $nick, $address) = @_;
  70   
  71   @branches = (' ');
  72 
  73   print_server($root, 0) if ($root);
  74 
  75   $root = undef;
  76 }
  77 
  78 sub print_server {
  79   my ($parent, $level, $last) = @_;
  80   my ($i, $str);
  81 
  82   for ($i = 0; $i < $level; $i++) {
  83     $str .= "   " . $branches[$i];
  84   }
  85 
  86   $str .= ($level ? "-" : " ") . " ";
  87   $str .= $rcase{$parent};
  88   $str = sprintf('%-50s %s', $str, $sname{$parent})
  89     if Irssi::settings_get_bool("show_server_names");
  90 
  91   Irssi::print $str;
  92 
  93   return unless ($tree{$parent});
  94 
  95   $branches[$level - 1] = ' '
  96     if ($level and $branches[$level - 1] eq '`');
  97 
  98   $branches[$level] = '|';
  99 
 100   while (@{$tree{$parent}}) {
 101     my ($server) = shift @{$tree{$parent}};
 102     
 103     $last = not scalar @{$tree{$parent}}; # sounds funny, eh? :^)
 104     $branches[$level] = '`' if ($last);
 105     
 106     print_server($server, $level + 1, $last);
 107   } 
 108 }
 109 
 110 
 111 Irssi::command_bind("map", "cmd_map");
 112 Irssi::signal_add("redir links_line", "event_links_line");
 113 Irssi::signal_add("redir links_done", "event_links_done");
 114 Irssi::settings_add_bool("lookandfeel", "show_server_names", 1);
 115 
 116 Irssi::Irc::Server::redirect_register("command map", 0, 0,
 117     {
 118       "event 364" => 1, # link line (wait...)
 119     },
 120     {
 121       "event 402" => 1,  # not found
 122       "event 263" => 1,  # try again
 123       "event 365" => 1,  # end of links
 124     },
 125     undef,
 126     );
 127 
 128 
 129 Irssi::print("Map $VERSION loaded...");