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