]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - modulemanager
Update copyright headers.
[user/henk/code/inspircd.git] / modulemanager
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2012-2014, 2017-2020 Sadie Powell <sadie@witchery.services>
6 #   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 #   Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
9 #
10 # This file is part of InspIRCd.  InspIRCd is free software: you can
11 # redistribute it and/or modify it under the terms of the GNU General Public
12 # License as published by the Free Software Foundation, version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but WITHOUT
15 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23
24 BEGIN {
25         require 5.10.0;
26         unless (eval "use LWP::Simple; 1") {
27                 die "Your system is missing the LWP::Simple Perl module!";
28         }
29         unless (eval "use Crypt::SSLeay; 1" || eval "use IO::Socket::SSL; 1") {
30                 die "Your system is missing the Crypt::SSLeay or IO::Socket::SSL Perl modules!";
31         }
32 }
33
34 use feature ':5.10';
35 use strict;
36 use warnings FATAL => qw(all);
37
38 use File::Basename qw(basename);
39 use FindBin        qw($RealDir);
40
41 use lib $RealDir;
42 use make::common;
43 use make::console;
44
45 my %installed;
46 # $installed{name} = $version
47
48 my %modules;
49 # $modules{$name}{$version} = {
50 #       url => URL of this version
51 #       depends => [ 'm_foo 1.2.0-1.3.0', ... ]
52 #       conflicts => [ ]
53 #       from => URL of source document
54 #       mask => Reason for not installing (INSECURE/DEPRECATED)
55 #       description => some string
56 # }
57
58 my %url_seen;
59
60 sub parse_url;
61
62 # retrieve and parse entries from sources.list
63 sub parse_url {
64         chomp(my $src = shift);
65         return if $url_seen{$src};
66         $url_seen{$src}++;
67
68         my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
69         my $response = $ua->get($src);
70
71         unless ($response->is_success) {
72                 my $err = $response->message;
73                 die "Could not retrieve $src: $err";
74         }
75
76         my $mod;
77         for (split /\n+/, $response->content) {
78                 s/^\s+//; # ignore whitespace at start
79                 next if /^#/;
80                 if (/^module (\S+) (\S+) (\S+)/) {
81                         my($name, $ver, $url) = ($1,$2,$3);
82                         if ($modules{$name}{$ver}) {
83                                 my $origsrc = $modules{$name}{$ver}{from};
84                                 warn "Overriding module $name $ver defined from $origsrc with one from $src";
85                         }
86                         $mod = {
87                                 from => $src,
88                                 url => $url,
89                                 depends => [],
90                                 conflicts => [],
91                         };
92                         $modules{$name}{$ver} = $mod;
93                 } elsif (/^depends (.*)/) {
94                         push @{$mod->{depends}}, $1;
95                 } elsif (/^conflicts (.*)/) {
96                         push @{$mod->{conflicts}}, $1;
97                 } elsif (/^description (.*)/) {
98                         $mod->{description} = $1;
99                 } elsif (/^mask (.*)/) {
100                         $mod->{mask} = $1;
101                 } elsif (/^source (\S+)/) {
102                         parse_url $1;
103                 } else {
104                         print "Unknown line in $src: $_\n";
105                 }
106         }
107 }
108
109 # hash of installed module versions from our mini-database, key (m_foobar) to version (00abacca..).
110 my %mod_versions = read_config_file '.modulemanager';
111
112 # useless helper stub
113 sub getmodversion {
114         my ($file) = @_;
115         return $mod_versions{$file};
116 }
117
118 # read in external URL sources
119 open SRC, 'sources.list' or die "Could not open sources.list: $!";
120 while (<SRC>) {
121         next if /^\s*#/;
122         parse_url($_);
123 }
124 close SRC;
125
126 # determine core version
127 my %version = get_version();
128 $installed{core} = "$version{MAJOR}.$version{MINOR}.$version{PATCH}";
129 for my $mod (keys %modules) {
130         MODVER: for my $mver (keys %{$modules{$mod}}) {
131                 for my $dep (@{$modules{$mod}{$mver}{depends}}) {
132                         next unless $dep =~ /^core (.*)/;
133                         if (!ver_in_range($installed{core}, $1)) {
134                                 delete $modules{$mod}{$mver};
135                                 next MODVER;
136                         }
137                 }
138         }
139         delete $modules{$mod} unless %{$modules{$mod}};
140 }
141 $modules{core}{$installed{core}} = {
142         url => 'NONE',
143         depends => [],
144         conflicts => [],
145         from => 'local file',
146 };
147
148 # set up core module list
149 for my $modname (<$RealDir/src/modules/m_*.cpp>) {
150         my $mod = basename($modname, '.cpp');
151         my $ver = getmodversion($mod) || '0.0';
152         $ver =~ s/\$Rev: (.*) \$/$1/; # for storing revision in SVN
153         $installed{$mod} = $ver;
154         next if $modules{$mod}{$ver};
155         $modules{$mod}{$ver} = {
156                 url => 'NONE',
157                 depends => [],
158                 conflicts => [],
159                 from => 'local file',
160         };
161 }
162
163 my %todo = %installed;
164
165 sub ver_cmp {
166         ($a,$b) = @_ if @_;
167
168         if ($a !~ /^[0-9.]+$/ or $b !~ /^[0-9.]+$/)
169         {
170                 # not a valid version number, don't try to sort
171                 return $a ne $b;
172         }
173
174         # else it's probably a numerical type version.. i.e. 1.0
175         my @a = split /\./, $a;
176         my @b = split /\./, $b;
177         push @a, 0 while $#a < $#b;
178         push @b, ($_[2] || 0) while $#b < $#a;
179         for my $i (0..$#a) {
180                 my $d = $a[$i] <=> $b[$i];
181                 return $d if $d;
182         }
183         return 0;
184 }
185
186 sub ver_in_range {
187         my($ver, $range) = @_;
188         return 1 unless defined $range;
189         my($l,$h) = ($range, $range);
190         if ($range =~ /(.*)-(.*)/) {
191                 ($l,$h) = ($1,$2);
192         }
193         return 0 if $l && ver_cmp($ver, $l) < 0;
194         return 0 if $h && ver_cmp($ver, $h, 9999) > 0;
195         return 1;
196 }
197
198 sub find_mod_in_range {
199         my($mod, $vers, $force) = @_;
200         my @versions = keys %{$modules{$mod}};
201         @versions = sort { -ver_cmp() } @versions;
202         for my $ver (@versions) {
203                 next if $modules{$mod}{$ver}{mask} && !$force;
204                 return $ver if ver_in_range($ver, $vers);
205         }
206         return undef;
207 }
208
209 sub resolve_deps {
210         my($trial) = @_;
211         my $tries = 100;
212         my $changes = 'INIT';
213         my $fail = undef;
214         while ($changes && $tries) {
215                 $tries--;
216                 $changes = '';
217                 $fail = undef;
218                 my @modsnow = sort keys %todo;
219                 for my $mod (@modsnow) {
220                         my $ver = $todo{$mod};
221                         my $info = $modules{$mod}{$ver} or die "no dependency information on $mod $ver";
222                         for my $dep (@{$info->{depends}}) {
223                                 $dep =~ /^(\S+)(?: (\S+))?/ or die "Bad dependency $dep from $info->{from}";
224                                 my($depmod, $depvers) = ($1,$2);
225                                 next if $todo{$depmod} && ver_in_range($todo{$depmod}, $depvers);
226                                 # need to install a dependency
227                                 my $depver = find_mod_in_range($depmod, $depvers);
228                                 if (defined $depver) {
229                                         $todo{$depmod} = $depver;
230                                         $changes .= " $mod-$ver->$depmod-$depver";
231                                 } else {
232                                         $fail ||= "Could not find module $depmod $depvers required by $mod $ver";
233                                 }
234                         }
235                         for my $dep (@{$info->{conflicts}}) {
236                                 $dep =~ /^(\S+)(?: (\S+))?/ or die "Bad dependency $dep from $info->{from}";
237                                 my($depmod, $depvers) = ($1,$2);
238                                 next unless $todo{$depmod} && ver_in_range($todo{$depmod}, $depvers);
239                                 # if there are changes this round, maybe the conflict won't come up after they are resolved.
240                                 $fail ||= "Cannot install: module $mod ($ver) conflicts with $depmod version $todo{$depmod}";
241                         }
242                 }
243         }
244         if ($trial) {
245                 return !($changes || $fail);
246         }
247         if ($changes) {
248                 print "Infinite dependency loop:$changes\n";
249                 exit 1;
250         }
251         if ($fail) {
252                 print "$fail\n";
253                 exit 1;
254         }
255 }
256
257 command 'install', 'Install a third-party module', sub {
258         for my $mod (@_) {
259                 my $vers = $mod =~ s/=([-0-9.]+)// ? $1 : undef;
260                 $mod = lc $mod;
261                 unless ($modules{$mod}) {
262                         print "Cannot find module $mod\n";
263                         exit 1;
264                 }
265                 my $ver = find_mod_in_range($mod, $vers, $vers ? 1 : 0);
266                 unless ($ver) {
267                         print "Cannot find suitable version of $mod\n";
268                         exit 1;
269                 }
270                 $todo{$mod} = $ver;
271         }
272 };
273
274 command 'upgrade', 'Upgrade a third-party module', sub {
275         my @installed = sort keys %installed;
276         for my $mod (@installed) {
277                 next unless $mod =~ /^m_/;
278                 my %saved = %todo;
279                 $todo{$mod} = find_mod_in_range($mod);
280                 if (!resolve_deps(1)) {
281                         %todo = %saved;
282                 }
283         }
284 };
285
286 command 'list', 'List available third-party modules', sub {
287         my @all = sort keys %modules;
288         for my $mod (@all) {
289                 my @vers = sort { ver_cmp() } keys %{$modules{$mod}};
290                 my $desc = '';
291                 for my $ver (@vers) {
292                         # latest defined description wins
293                         $desc = $modules{$mod}{$ver}{description} || $desc;
294                 }
295                 next if @vers == 1 && $modules{$mod}{$vers[0]}{url} eq 'NONE';
296                 my $instver = $installed{$mod} || '';
297                 my $vers = join ' ', map { $_ eq $instver ? "\e[1m$_\e[m" : $_ } @vers;
298                 print "$mod ($vers) - $desc\n";
299         }
300         exit 0;
301 };
302
303 execute_command @ARGV;
304
305 resolve_deps(0);
306
307 $| = 1; # immediate print of lines without \n
308
309 print "Processing changes...\n";
310 for my $mod (keys %installed) {
311         next if $todo{$mod};
312         print "Uninstalling $mod $installed{$mod}\n";
313         unlink "src/modules/$mod.cpp";
314 }
315
316 my $count = scalar keys %todo;
317 print "Checking $count items...\n";
318 for my $mod (sort keys %todo) {
319         my $ver = $todo{$mod};
320         my $oldver = $installed{$mod};
321         if ($modules{$mod}{$ver}{mask}) {
322                 print "Module $mod $ver is masked: $modules{$mod}{$ver}{mask}\n";
323         }
324         next if $oldver && $oldver eq $ver;
325         my $url = $modules{$mod}{$ver}{url};
326         if ($oldver) {
327                 print "Upgrading $mod from $oldver to $ver using $url"
328         } else {
329                 print "Installing $mod $ver from $url";
330         }
331         $mod_versions{$mod} = $ver;
332
333         my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
334         my $response = $ua->get($url);
335
336         if ($response->is_success) {
337                 open(MF, ">src/modules/$mod.cpp") or die "\nFilesystem not writable: $!";
338                 print MF $response->content;
339                 close(MF);
340                 print " - done\n";
341         } else {
342                 printf "\nHTTP %s: %s\n", $response->code, $response->message;
343         }
344 }
345
346 # write database of installed versions
347 write_config_file '.modulemanager', %mod_versions;
348
349 print "Finished!\n";