1 # ho_manage.pl
  2 #
  3 # $Id: ho_manage.pl,v 1.3 2004/10/24 12:10:08 jvunder REL_0_3 $
  4 #
  5 # Part of the Hybrid Oper Script Collection.
  6 #
  7 # Manages all HOSC scripts.
  8 
  9 use strict;
 10 use vars qw($VERSION %IRSSI $SCRIPT_NAME);
 11 
 12 use Irssi;
 13 use HOSC::again;
 14 use HOSC::again 'HOSC::Base';
 15 use HOSC::again 'HOSC::Tools';
 16 
 17 eval {
 18     require_again("LWP::UserAgent");
 19 };
 20 if ($@) {
 21     Irssi::print("You need LWP::UserAgent for this script. Please install ".
 22         "it.");
 23     return 0;
 24 }
 25 
 26 # This is the file that contains the most recent CVS versions of each
 27 # script and each module.
 28 my $VERSION_FILE_URI = 'http://garion.org/hosc/data/hosc_versions.txt';
 29 
 30 # This file contains the most recent stable release.
 31 my $RELEASE_FILE_URI = 'http://garion.org/hosc/data/RELEASE';
 32 
 33 # The local file which tells us which release we're using.
 34 my $CURRENT_VERSION_FILE = Irssi::get_irssi_dir() . '/scripts/HOSC/VERSION';
 35 
 36 # ---------------------------------------------------------------------
 37 
 38 ($VERSION) = '$Revision: 1.3 $' =~ / (\d+\.\d+) /;
 39 %IRSSI = (
 40     authors     => 'Garion',
 41     contact     => 'garion@efnet.nl',
 42     name        => 'ho_manage',
 43     description => 'Manages all HOSC scripts.',
 44     license     => 'Public Domain',
 45     url         => 'http://garion.org/hosc/',
 46     changed     => '19 October 2004 22:27:30',
 47 );
 48 $SCRIPT_NAME = 'Manage';
 49 
 50 # ---------------------------------------------------------------------
 51 
 52 sub cmd_manage {
 53     my ($data, $server, $item) = @_;
 54     if ($data =~ m/^[(help)|(check)]/i ) {
 55         Irssi::command_runsub ('manage', $data, $server, $item);
 56         return;
 57     }
 58 
 59     ho_print("Use /MANAGE HELP for help.");
 60 }
 61 
 62 # ---------------------------------------------------------------------
 63 
 64 sub cmd_manage_help {
 65     print_help();
 66 }
 67 
 68 # ---------------------------------------------------------------------
 69 
 70 sub cmd_manage_check {
 71     my ($data, $server, $item) = @_;
 72 
 73     ho_print("Checking versions of loaded scripts.");
 74 
 75     my $version = get_current_version();
 76 
 77     if ($version eq "CVS") {
 78         check_cvs();
 79     } else {
 80         check_stable($version);
 81     }
 82 }
 83 
 84 sub get_current_version {
 85     # If there is no version file, assume we're running CVS.
 86     if (!-f $CURRENT_VERSION_FILE) {
 87         ho_print_warning("No version file $CURRENT_VERSION_FILE found. " .
 88             "Assuming that we're running CVS.");
 89         return "CVS";
 90     }
 91     
 92     open(F, $CURRENT_VERSION_FILE) or
 93         ho_print_error("Could not open current version file: $!"),
 94         return "CVS";
 95     
 96     my $version = <F>;
 97     chomp $version;
 98     close F or
 99         ho_print_error("Could not open current version file: $!"),
100         return "CVS";
101 
102     return $version;
103 }
104 
105 sub check_stable {
106     my ($current_version) = @_;
107 
108     my $ua = LWP::UserAgent->new;
109     my $response = $ua->get($RELEASE_FILE_URI);
110 
111     if (!$response->is_success) {
112         ho_print_error("Could not retrieve $RELEASE_FILE_URI: " .
113             $response->status_line);
114         return;
115     }
116 
117     my $remote_version;
118 
119     if ($response->content =~ /^(\d+\.\d+)/) {
120         $remote_version = $1;
121     } else {
122         ho_print_error("Got unexpected data in release uri; expected ".
123             "<number>.<number>, but got " . $response->content);
124         return;
125     }
126         
127     if ($remote_version > $current_version) {
128         ho_print("You are running stable release $current_version, but ".
129             "$remote_version is the latest stable release.");
130     } else {
131         ho_print("You are running stable release $current_version, which ".
132             "is the latest.");
133     }
134 }
135 
136 sub check_cvs() {
137     # Get the version file first.
138     my $ua = LWP::UserAgent->new;
139     my $response = $ua->get($VERSION_FILE_URI);
140 
141     if (!$response->is_success) {
142         ho_print_error("Could not retrieve $VERSION_FILE_URI: " .
143             $response->status_line);
144         return;
145     }
146   
147     # Obtain currently loaded ho_ scripts.
148     my %hosc_scripts;
149     my %scripts = %Irssi::Script::;
150     my $num_out_of_date = 0;
151     no strict 'refs';
152     for my $name (sort keys %scripts) {
153         next unless $name =~ /ho_/;
154         $name =~ s/:://;
155         $hosc_scripts{"$name.pl"}->{version} = 
156             ${ "Irssi::Script::${name}::VERSION" };
157     }
158     
159     # Do a version check on all of them.
160     for my $line (split /\n/, $response->content) {
161         if ($line =~ /^(ho_.*.pl)\s+(\d+\.\d+)$/) {
162             my ($script, $version) = ($1, $2);
163             if ($hosc_scripts{$script}) {
164                 if ($hosc_scripts{$script}->{version} < $version) {
165                     $num_out_of_date++;
166                     ho_print("$script " . (' ' x (20 - length $script)) .
167                         "loaded " . 
168                         $hosc_scripts{$script}->{version} .
169                         " - available " . $version) . ".";
170                 }
171             }
172         }
173     }
174    
175     if ($num_out_of_date == 0) {
176         ho_print("All scripts up to date.");
177     } elsif ($num_out_of_date == 1) {
178         ho_print("One script out of date.");
179     } else {
180         ho_print("$num_out_of_date scripts out of date.");
181     }
182 }
183 
184 # ---------------------------------------------------------------------
185 
186 ho_print_init_begin();
187 
188 Irssi::command_bind('manage',       'cmd_manage');
189 Irssi::command_bind('manage help',  'cmd_manage_help');
190 Irssi::command_bind('manage check', 'cmd_manage_check');
191 
192 ho_print_init_end();
193 ho_print("Use /MANAGE HELP for help.");
194 
195 # ---------------------------------------------------------------------
196 
197 sub print_help {
198     ho_print_help('head', $SCRIPT_NAME);
199 
200     ho_print_help('section', 'Description');
201     ho_print_help("This script can be used to keep your HOSC collection ".
202         "up to date. For the moment it can only check which versions you ".
203         "have loaded, and which are available on the website.");
204     ho_print_help("To check how up to date your scripts are, use ".
205         "/MANAGE CHECK");
206     ho_print_help("No downloads yet. Just checking. That'll be in the next ".
207         "version!");
208 
209     ho_print_help('section', 'Syntax');
210     ho_print_help('syntax', 'MANAGE HELP');
211     ho_print_help('syntax', 'MANAGE CHECK');
212 }
213 
214 # ---------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1