html/ircgallery.pl


   1 # Show IRC gallery (http://irc-galleria.net, finnish only) information
   2 # on /WHOIS or /GALLERY
   3 
   4 # version 1.13
   5 # for irssi 0.8.0 by Timo Sirainen
   6 use Symbol;
   7 use vars qw($VERSION %IRSSI); 
   8 $VERSION = "1.13";
   9 %IRSSI = (
  10     authors	=> "Timo \'cras\' Sirainen",
  11     contact	=> "tss\@iki.fi", 
  12     name	=> "ircgallery",
  13     description	=> "Show IRC gallery (http://irc-galleria.net, finnish only) information on /WHOIS or /GALLERY",
  14     license	=> "Public Domain",
  15     url		=> "http://irssi.org/",
  16     changed	=> "2002-03-04T22:47+0100"
  17 );
  18 
  19 
  20 Irssi::theme_register([
  21   'whois_gallery', '{whois gallery $1}',
  22   'gallery_header', '{hilight $0} - IRC gallery information',
  23   'gallery_line', ' $[8]0 : $1',
  24   'gallery_footer', 'End of info',
  25   'gallery_notfound', '$0 is not in IRC gallery',
  26   'gallery_nolist', 'Nick list of IRC gallery not downloaded yet - please wait'
  27 ]);
  28 
  29 
  30 my $cache_path = glob "~/.irssi/ircgallery";
  31 my @print_queue;
  32 
  33 my $nicklist_path = "$cache_path/nicks.list";
  34 my $gallery_nicks_time = 0;
  35 my %gallery_nicks = {};
  36 
  37 my $last_whois_nick;
  38 
  39 sub get_view_url {
  40   return 'http://irc-galleria.net/view.php?nick='.$_[0];
  41 }
  42 
  43 # print the gallery information - assumes the file is in cache directory
  44 sub print_gallery {
  45   my $nick = shift;
  46 
  47   my $found = 0;
  48   my $next_channels = 0;
  49   my $channels;
  50 
  51   $. = "\n";
  52   my $f = gensym;
  53   if (!open($f, "$cache_path/$nick")) {
  54     Irssi::print("Couldn't open file $cache_path/$nick: $!", MSGLEVEL_CLIENTERROR);
  55     return;
  56   }
  57   while (<$f>) {
  58     last if (/\<title\>.*Etsi nick/); # unknown nick
  59 
  60     if ($next_channels) {
  61       if (m,\<a .*\>(#.*)\</a>,) {
  62         $channels .= "$1 ";
  63         next;
  64       } else {
  65         $next_channels = 0;
  66         if ($channels) {
  67           Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
  68 		"channels", $channels);
  69 	  $channels = "";
  70 	}
  71       }
  72     }
  73 
  74     if (/\<h1\>[^\(]*\(([^\)]*)/) {
  75       my $realname = $1;
  76       Irssi::printformat(MSGLEVEL_CRAP, 'gallery_header', $nick);
  77       Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
  78                 "ircname", $realname);
  79       Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
  80 		"url", get_view_url($nick));
  81       $found = 1;
  82       next;
  83     }
  84 
  85     if (/\<img.*src="([^"]*)".*alt="$nick"/) {
  86       Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line',
  87 		"image", "http://irc-galleria.net/$1");
  88       next;
  89     }
  90 
  91     my ($title, $value) = $_ =~ m,\<span class="otsikko"\>([^:]+):\</span\> (.*)\<br /\>,;
  92     if ($value =~ m,\<a .*\>(.*)\</a\>,) {
  93       $value = $1;
  94     }
  95     $next_channels = 1 if (m,\<span class="otsikko"\>Kanavat,);
  96     
  97     if ($title && $value) {
  98       if ($title eq "Maili") {
  99         $title = "e-mail";
 100       } elsif ($title =~ /Kaupunki/) {
 101         $title = "city";
 102       } elsif ($title eq "Syntynyt") {
 103         $title = "birthday";
 104       } elsif ($title eq "Muutettu") {
 105         $title = "last modified";
 106       }
 107       Irssi::printformat(MSGLEVEL_CRAP, 'gallery_line', $title, $value);
 108     }
 109   }
 110   close($f);
 111 
 112   if ($found) {
 113     Irssi::printformat(MSGLEVEL_CRAP, 'gallery_footer', $nick);
 114   } elsif ($print_notfound{$nick}) {
 115     Irssi::printformat(MSGLEVEL_CRAP, 'gallery_notfound', $nick);
 116   }
 117   
 118   delete $print_notfound{$nick};
 119 }
 120 
 121 # download the info from gallery to cache dir,
 122 # if the files aren't there already.
 123 sub download_nicks_info {
 124   foreach my $nick (@_) {
 125     my $filename = "$cache_path/$nick";
 126     if (! -f $filename) {
 127       # FIXME: we could do this ourself with sockets...
 128       Irssi::command("exec - wget -O$filename.tmp -q -UMozilla ".get_view_url($nick)."; mv $filename.tmp $filename");
 129     }
 130   }
 131 }
 132 
 133 # print info from all given nicks that have file in cache dir
 134 sub gallery_show {
 135   foreach my $nick (@_) {
 136     if (-f "$cache_path/$nick") {
 137       print_gallery($nick);
 138     } else {
 139       push @print_queue, $nick;
 140     }
 141   }
 142 }
 143 
 144 sub print_whois_gallery {
 145   my ($server, $nick) = @_;
 146 
 147   if ($gallery_nicks{lc $nick}) {
 148     $server->printformat($nick, MSGLEVEL_CRAP, 'whois_gallery',
 149 			 $nick, get_view_url($nick));
 150   }
 151 }
 152 
 153 # /WHOIS - print the gallery URL after realname
 154 sub event_whois {
 155   my ($server, $data) = @_;
 156   my ($temp, $nick) = split(" ", $data);
 157 
 158   print_whois_gallery($server, $last_whois_nick) if ($last_whois_nick);
 159   $last_whois_nick = $nick;
 160 }
 161 
 162 sub event_end_of_whois {
 163   my ($server) = @_;
 164 
 165   if ($last_whois_nick) {
 166     print_whois_gallery($server, $last_whois_nick);
 167     $last_whois_nick = undef;
 168   }
 169 }
 170 
 171 # /GALLERY <nicks>
 172 sub cmd_gallery {
 173   my @nicks = split(/[, ]/, $_[0]);
 174   
 175   if (!$gallery_nicks_time) {
 176     Irssi::printformat(MSGLEVEL_CLIENTERROR, 'gallery_nolist');
 177     return;
 178   }
 179 
 180   my @new_list;
 181   foreach my $nick (@nicks) {
 182     my $gallery_nick = $gallery_nicks{lc $nick};
 183     if (!$gallery_nick) {
 184       Irssi::printformat(MSGLEVEL_CRAP, 'gallery_notfound', $nick);
 185     } else {
 186       push @new_list, $gallery_nick;
 187     }
 188   }
 189   
 190   download_nicks_info(@new_list);
 191   gallery_show(@new_list);
 192 
 193   if ($gallery_nicks_time < time()-(3600*8)) {
 194     # nicklist hasn't been updated for a while, refresh it
 195     download_nicklist();
 196   }
 197 }
 198 
 199 # parse all known nicks from nick index file 
 200 sub parse_nicks {
 201   my $filename = shift;
 202 
 203   %gallery_nicks = {};
 204   $gallery_nicks_time = time();
 205 
 206   my $f = gensym;
 207   if (!open($f, $filename)) {
 208     Irssi::print("Couldn't open file $filename: $!", MSGLEVEL_CLIENTERROR);
 209     return;
 210   }
 211   while (<$f>) {
 212     if (m,\<a href="view.php.*\>(.*)\</a\>,) {
 213       $gallery_nicks{lc $1} = $1;
 214     }
 215   }
 216   close($f);
 217 }
 218 
 219 # /EXEC finished - maybe there's new files downloaded in cache dir?
 220 sub sig_exec_remove {
 221   my @new_queue;
 222 
 223   if (-f $nicklist_path) {
 224     parse_nicks($nicklist_path);
 225     unlink($nicklist_path);
 226   }
 227 
 228   foreach my $nick (@print_queue) {
 229     if (-f "$cache_path/$nick") {
 230       print_gallery($nick);
 231     } else {
 232       push @new_queue, $nick;
 233     }
 234   }
 235   @print_queue = @new_queue;
 236 }
 237 
 238 sub download_nicklist {
 239   Irssi::command("exec - wget -O$nicklist_path -q -UMozilla http://irc-galleria.net/list.php?letter=_");
 240 }
 241 
 242 # clear cache dir
 243 if (-d $cache_path) {
 244   unlink(<$cache_path/*>);
 245 } else {
 246   mkdir($cache_path, 0700) || die "Can't create cache directory $cache_dir";
 247 }
 248 
 249 # we need the nick list, get it once per hour
 250 download_nicklist();
 251 
 252 Irssi::signal_add_first('event 311', 'event_whois');
 253 Irssi::signal_add_first('event 318', 'event_end_of_whois');
 254 Irssi::signal_add('exec remove', 'sig_exec_remove');
 255 Irssi::command_bind('gallery', 'cmd_gallery');