# fields -- lintian check script (rewrite)
#
# Copyright (C) 2004 Marc Brockschmidt
#
# Parts of the code were taken from the old check script, which
# was Copyright (C) 1998 Richard Braakman (also licensed under the
# GPL 2 or higher)
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

package Lintian::fields;
use strict;
use lib "$ENV{'LINTIAN_ROOT'}/checks/";
use common_data;
use Tags;
use Util;

sub run {

my $pkg = shift;
my $type = shift;
my $version;

local $/ = undef; #Read everything in one go

unless (-d "fields") {
	fail("directory in lintian laboratory for $type package $pkg missing: fields");
}

#---- Package

if ($type eq "binary"){
	if (not open (FH, "fields/package")) {
		tag "no-package-name", "";
	} else {
		my $name = <FH>;
		close FH;

		unfold("package", \$name);
	
		tag "bad-package-name", "" unless $name =~ /^[A-Z0-9][-+\.A-Z0-9]+$/i;
		tag "package-not-lowercase", "" if ($name =~ /[A-Z]/)
	}
}

#---- Version

if (not open (FH, "fields/version")) {
	tag "no-version-field", "";
} else {
	$version = <FH>;
	close FH;

	unfold("version", \$version);

	if (@_ = _valid_version($version)) {
		my ($epoch, $upstream, $debian) = @_;
		if ($upstream !~ /^\d/i) {
			tag "upstream-version-not-numeric", "$version";
		}
		if (defined $debian) {
			tag "debian-revision-should-not-be-zero", "$version"
				if $debian eq '0';
			$debian =~ /^-([^.]+)?(?:\.[^.]+)?(?:\.[^.]+)?(\..*)?$/;
			if (not defined $1 or defined $2) {
				tag "debian-revision-not-well-formed", "$version";
			}
			if ($debian =~ /^-[^.]+\.[^.]+\./ && ($type eq "source")) {
				tag "binary-nmu-debian-revision-in-source", "$version";
			}
		}
	} else {
		tag "bad-version-number", "$version";
	}
}

#---- Architecture

if (not open (FH, "fields/architecture")) {
	tag "no-architecture-field", "";
} else {
	my $archs = <FH>;
	close FH;

	unfold("architecture", \$archs);

	my @archs = split / /, $archs;

	if (@archs > 1 && grep { $_ eq "any" || $_ eq "all" } @archs) {
		tag "magic-arch-in-arch-list", "";
	}

	for my $arch (@archs) {
		unless ($known_archs{$arch}) {
			tag "unknown-architecture", "";
		}
	}

	if ($type eq "binary") {
		tag "too-many-architectures", "" if (@archs > 1);
		tag "arch-any-in-binary-pkg", "" if (grep { $_ eq "any" } @archs);
	}
}

#---- Subarchitecture (udeb)

if (open(FH, "fields/subarchitecture")) {
	my $subarch = <FH>;
	close(FH);

	unfold("subarchitecture", \$subarch);
}

#---- Maintainer
#---- Uploaders

for my $f (qw(maintainer uploaders)) {
	if (not open (FH, "fields/$f")) {
		tag "no-maintainer-field", "" if $f eq "maintainer";
	} else {
		my $maintainer = <FH>;
		close FH;

		unfold($f, \$maintainer);

		$maintainer =~ s/^\s*(.+?)\s*$/$1/; #Remove leading and trailing whitespace

		if ($f eq "uploaders") {
			check_maint($_, "uploader") for (split /\s*,\s*/, $maintainer);
		} else {
			check_maint($maintainer, $f);
		}
	}
}

#---- Source

if ($type eq "source") {
	if (not open (FH, "fields/source")) {
		tag "no-source-field", "";
	} else {
		my $source = <FH>;
		close FH;
	
		unfold("source", \$source);
	
		if ($type eq 'source') {
			if ($source ne $pkg) {
				tag "source-field-does-not-match-pkg-name", "$_";
			}
		} else {
			if ($source !~ /[A-Z0-9][-+\.A-Z0-9]+                      #Package name
			                \s*
			                (?:\((?:\d+:)?(?:[-\.+:A-Z0-9]+?)(?:-[\.+A-Z0-9]+)?\))?\s*$/ix) { #Version
				tag "source-field-malformed", "$source";
			}
		}	
	}
}

#---- Essential

if (open (FH, "fields/essential")) {
	my $essential = <FH>;
	close FH;

	unfold("essential", \$essential);

	tag "essential-in-source-package", "" if ($type eq "source");
	tag "essential-no-not-needed", "" if ($essential eq "no");
	tag "unknown-essential-value", "" if ($essential ne "no" and $essential ne "yes");
	tag "new-essential-package", "" if ($essential eq "yes" and ! $known_essential{$pkg});
}

#---- Section

if (not open (FH, "fields/section")) {
	tag "no-section-field", "" if $type eq "binary";
} else {
	my $section = <FH>;
	close FH;

	unfold("section", \$section);

	if ($type eq 'udeb') {
	    unless ($section eq 'debian-installer') {
		tag "wrong-section-for-udeb", "$section";
	    }
	} else {

	    my @parts = split /\//, $section, 2;
	    
	    if ($parts[0] =~ /non-US/i) {
		tag "non-us-spelling", "" if ($parts[0] ne "non-US");
		if ($parts[1] and not $known_non_us_parts{$parts[1]}) {
		    tag "unknown-section", "$section";
		}
	    } elsif (scalar @parts > 1) {
		tag "unknown-section", "$section" unless $known_archive_parts{$parts[0]};
		tag "unknown-section", "$section" unless $known_sections{$parts[1]};
	    } else {
		tag "unknown-section", "$section" unless $known_sections{$parts[0]};
	    }
	}
}

#---- Priority

if (not open (FH, "fields/priority")) {
	tag "no-priority-field", "" if $type eq "binary";
} else {
	my $priority = <FH>;
	close FH;

	unfold("priority", \$priority);

	tag "unknown-priority", "$priority" if (! $known_prios{$priority});
}

#---- Standards-Version
# handled in checks/standards-version

#---- Description
# handled in checks/description

#---- Installer-Menu-Item (udeb)

if (open(FH, "fields/installer-menu-item")) {
	my $menu_item = <FH>;
	close(FH);

	unfold('installer-menu-item', \$menu_item);

	$menu_item =~ /^\d+$/ or tag "bad-menu-item", "$menu_item";
}


#---- Package relations (binary package)

if (($type eq "binary") || ($type eq 'udeb')) {
	my %deps;
	for my $field (qw(depends pre-depends recommends suggests conflicts provides replaces)) {
		if (open(FH, "fields/$field")) {
			#Get data and clean it
			my $data = <FH>;
			unfold($field, \$data);
			$data =~ s/^\s*(.+?)\s*$/$1/;

			my (@seen_libstdcs, @seen_tcls, @seen_tclxs, @seen_tks, @seen_tkxs, @seen_libpngs);

			my $is_dep_field = sub { grep { $_ eq $_[0] } qw(depends pre-depends recommends suggests) };

			tag "alternates-not-allowed", "$field"
			    if ($data =~ /\|/ && ! &$is_dep_field($field));

			for my $dep (split /\s*,\s*/, $data) {
				my @alternatives;
				push @alternatives, [_split_dep($_), $_] for (split /\s*\|\s*/, $dep);

				push @seen_libstdcs, $alternatives[0]->[0] if defined $known_libstdcs{$alternatives[0]->[0]};
				push @seen_tcls, $alternatives[0]->[0] if defined $known_tcls{$alternatives[0]->[0]};
				push @seen_tclxs, $alternatives[0]->[0] if defined $known_tclxs{$alternatives[0]->[0]};
				push @seen_tks, $alternatives[0]->[0] if defined $known_tks{$alternatives[0]->[0]};
				push @seen_tkxs, $alternatives[0]->[0] if defined $known_tkxs{$alternatives[0]->[0]};
				push @seen_libpngs, $alternatives[0]->[0] if defined $known_libpngs{$alternatives[0]->[0]};

				tag "virtual-package-depends-without-real-package-depends", "$field: $alternatives[0]->[0]"
				    if ($known_virtual_packages{$alternatives[0]->[0]} && &$is_dep_field($field)
				        && $field ne "suggests"); #We don't wanna issue this for suggests, only for depends and recommends

				for my $part_d (@alternatives) {
					my ($d_pkg, $d_version, $d_arch, $rest, $part_d_orig) = @$part_d;

					#Save the type of relationship (<<, <=, ...) and the field name:
					if (&$is_dep_field($field) && scalar @alternatives == 1) {
						$deps{$d_pkg} = [] if ! $deps{$d_pkg};
						push @{$deps{$d_pkg}}, [$field, $d_version];
					}

					tag "versioned-provides", "$part_d_orig"
					    if ($field eq "provides" && $d_version->[0]);

					tag "obsolete-relation-form", "$field: $part_d_orig"
					    if ($d_version && grep { $d_version->[0] eq $_ } ("<", ">"));

					tag "bad-version-in-relation", "$field: $part_d_orig"
					    if ($d_version->[0] && ! (_valid_version($d_version->[1]))[1]);
					
					tag "package-relation-with-self", "$field: $part_d_orig"
					    if ($pkg eq $d_pkg) && ($field ne 'conflicts');

					tag "bad-relation", "$field: $part_d_orig"
					    if $rest;

					tag "depends-on-obsolete-package", "$field: $part_d_orig"
					    if ($known_obsolete_packages{$d_pkg} && &$is_dep_field($field));

					tag "depends-on-essential-package-without-using-version", "$field: $part_d_orig"
					    if ($d_pkg ne "coreutils" && $known_essential{$d_pkg} && ! $d_version->[0] && &$is_dep_field($field));

					tag "package-depends-on-an-x-font-package", "$field: $part_d_orig"
					    if ($field =~ /^(pre-)?depends$/ && $d_pkg =~ /^xfont.*/);

					tag "needlessly-depends-on-awk", "$field"
					    if ($d_pkg eq "awk" && ! $d_version->[0] && &$is_dep_field($field));

					tag "depends-on-libdb1-compat", "$field"
					    if ($d_pkg eq "libdb1-compat" && $pkg !~ /^libc(6|6.1|0.3)/ && $field =~ /^(pre-)depends$/);

					tag "doc-package-depends-on-main-package", "$field"
					    if ("$d_pkg-doc" eq $pkg && $field =~ /^(pre-)depends$/);
				}
			}
			tag "package-depends-on-multiple-libstdc-versions", @seen_libstdcs
			    if (scalar @seen_libstdcs > 1);
			tag "package-depends-on-multiple-tcl-versions", @seen_tcls
			    if (scalar @seen_tcls > 1);
			tag "package-depends-on-multiple-tclx-versions", @seen_tclxs
			    if (scalar @seen_tclxs > 1);
			tag "package-depends-on-multiple-tk-versions", @seen_tks
			    if (scalar @seen_tks > 1);
			tag "package-depends-on-multiple-tkx-versions", @seen_tkxs
			    if (scalar @seen_tkxs > 1);
			tag "package-depends-on-multiple-libpng-versions", @seen_libpngs
			    if (scalar @seen_libpngs > 1);
		}
	}

	for my $d_pkg_name (keys %deps) {
		my $d_pkg = $deps{$d_pkg_name};
		if (scalar @$d_pkg > 1) {
			#Allow things like Depends: package1 (>= 1.3), package1 (<= 5.2)
			unless ((scalar @$d_pkg == 2) && 
			        (($d_pkg->[0]->[1]->[0] =~ />=|>>|>/ && $d_pkg->[1]->[1]->[0] =~ /<=|<<|</) or
			         ($d_pkg->[0]->[1]->[0] =~ /<=|<<|</ && $d_pkg->[1]->[1]->[0] =~ />=|>>|>/))) {
				my @relations;
				my $extra = '';
			 	if ($d_pkg->[0][0] eq $d_pkg->[1][0]) {
					$extra .= "$d_pkg->[0][0]: ";
					for (@$d_pkg) {
						if ($_->[1][0]) {
							push @relations, "$d_pkg_name (".$_->[1][0]." ".$_->[1][1].")";
						} else {
							push @relations, "$d_pkg_name";
						}
					}
				} else {
					for (@$d_pkg) {
						if ($_->[1][0]) {
							push @relations, "$_->[0]: $d_pkg_name (".$_->[1][0]." ".$_->[1][1].")";
						} else {
							push @relations, "$_->[0]: $d_pkg_name";
						}
					}
				}
				$extra .= join( ", ", @relations );
				tag "package-has-a-duplicate-relation", $extra;
			}
		}
	}
}

#---- Package relations (source package)

if ($type eq "source") {
	
	#Get number of arch-indep packages:
	my $arch_indep_packages = 0;
	my $arch_dep_packages = 0;
	if (not open(CONTROL, "debfiles/control")) {
		fail("Can't open debfiles/control: $!");
	} else {
		local $/ = "\n"; #Read this linewise
		while (<CONTROL>) {	
			if (/^Architecture: all/) {			
				$arch_indep_packages++;
			} elsif (/^Architecture:/) {		
				$arch_dep_packages++;
			}
		}
	} 

	if (-e "fields/build-depends" && $arch_dep_packages == 0) {
		if (not open(BD, "fields/build-depends")) {
			fail("Can't open fields/build-depends");
		} else {
			my $build_depends = <BD>;
			close BD;

			my $uses_dh = 0;
			if (not open (RULES, "debfiles/rules")) {
				fail("cannot read debfiles/rules: $!");
			} else {
				my $target = "none";
				local $/ = "\n"; #Read this linewise				
				while (<RULES>) {
					$target = $1 if (/^(\S+):/);
					if (/^\s+dh_.+/ && grep ($_ eq $target, qw(clean binary-arch build-arch)) or
							m#^include\s+/usr/share/cdbs/1/rules/debhelper.mk#) {
						$uses_dh = "yes";
						last
					}
				}
				close RULES;
			}
			unless ($build_depends =~ /^\s*debhelper(?:\s+\((.+?)\))?(?:\s+(\[.+?\]))?\s*$/ && $uses_dh){
				tag "build-depends-without-arch-dep", ""
			}
		}
	}

	tag "build-depends-indep-without-arch-indep", ""
		if (-e "fields/build-depends-indep" && $arch_indep_packages == 0);

	my $is_dep_field = sub { grep { $_ eq $_[0] } qw(build-depends build-depends-indep) };
	
	for my $field (qw(build-depends build-depends-indep build-conflicts build-conflicts-indep)) {
		if (open(FH, "fields/$field")) {
			#Get data and clean it
			my $data = <FH>;
			unfold($field, \$data);
			$data =~ s/^\s*(.+?)\s*$/$1/;

			for my $dep (split /\s*,\s*/, $data) {
				my @alternatives;
				push @alternatives, [_split_dep($_), $_] for (split /\s*\|\s*/, $dep);

				tag "virtual-package-depends-without-real-package-depends", "$field: $alternatives[0]->[0]"
				    if ($known_virtual_packages{$alternatives[0]->[0]} && &$is_dep_field($field));

				for my $part_d (@alternatives) {
					my ($d_pkg, $d_version, $d_arch, $rest, $part_d_orig) = @$part_d;

					for my $arch (@{$d_arch->[0]}) {
						tag "invalid-arch-string-in-source-relation", "$arch [$field: $part_d_orig]"
						    unless ($known_archs{$arch} || $arch eq "any" || $arch eq "all");
					}

					tag "depends-on-build-essential-package-without-using-version", "$d_pkg [$field: $part_d_orig]"
					    if ($known_build_essential{$d_pkg} && ! $d_version->[1]);

					tag "build-depends-on-essential-package-without-using-version", "$field: $part_d_orig"
					    if ($d_pkg ne "coreutils" && $known_essential{$d_pkg} && ! $d_version->[0]);

					tag "bad-relation", "$field: $part_d_orig"
					    if $rest;
				}
			}
		}
	}
}

#----- Origin

if (open(FH, "fields/origin")) {
	my $origin = <FH>;
	close(FH);

	unfold('origin', \$origin);

	tag "redundant-origin-field", "" if $origin =~ /^\s*debian\s*$/i;
}

#----- Bugs

if (open(FH, "fields/bugs")) {
	my $bugs = <FH>;
	close FH;

	unfold('bugs', \$bugs);

	tag "redundant-bugs-field", "" if $bugs =~ m{^\s*debbugs://bugs.debian.org/?\s*$}i;
}

#----- Field checks (without checking the value)

for my $field (glob("fields/*")) {
	$field =~ s!^fields/!!;

	tag "obsolete-field", "$field"
	    if $known_obsolete_fields{$field};

	tag "unknown-field-in-dsc", "$field"
	    if ($type eq "source" && ! $known_source_fields{$field} && ! $known_obsolete_fields{$field});

	tag "unknown-field-in-control", "$field"
	    if ($type eq "binary" && ! $known_binary_fields{$field} && ! $known_obsolete_fields{$field});

	tag "unknown-field-in-control", "$field"
	    if ($type eq "udeb" && ! $known_udeb_fields{$field} && ! $known_obsolete_fields{$field});
}

}

# splits "foo (>= 1.2.3) [!i386 ia64]" into
# ( "foo", [ ">=", "1.2.3" ], [ [ "i386", "ia64" ], 1 ], "" )
#                                                  ^^^   ^^
#                                 true, if ! was given   ||
#           rest (should always be "" for valid dependencies)
sub _split_dep {
	my $dep = shift;
	my ($pkg, $version, $darch) = ("", ["",""], [[],""]);

	$pkg = $1 if $dep =~ s/^\s*([^\s\[\(]+)\s*//;

	if (length $dep) {
		if ($dep =~ s/\s* \( \s* (<<|<=|<|=|>=|>>|>) \s* ([^\s(]+) \s* \) \s*//x) {
			@$version = ($1, $2);
		}
		if ($dep && $dep =~ s/\s*\[([^\]]+)\]\s*//) {
			my $t = $1;
			$darch->[1] = 1 if ($t =~ s/!//g);
			$darch->[0] = [ split /\s+/, $t ];
		}
	}

	return ($pkg, $version, $darch, $dep);
}

sub _valid_version {
	my $ver = shift;

	# epoch check means nothing here... This check is only useful to detect
	# weird characters in version (and to get the debian revision)
	if ($ver =~ m/^(\d+:)?([-\.+:A-Z0-9]+?)(-[\.+A-Z0-9]+)?$/i) {
		return ($1, $2, $3);
	} else {
		return ();
	}
}

sub unfold {
	my $field = shift;
	my $line = shift;

	$$line =~ s/\n$//;

	if ($$line =~ s/\n//g) {
		tag "multiline-field", "$field";
	}
}

sub check_maint {
	my ($maintainer, $f) = @_;
	$maintainer =~ /^([^<\s]*(?:\s+[^<\s]+)*)?(\s*)(?:<(.+)>)?(.*)$/, 
	my ($name, $del, $mail, $crap) = ($1, $2, $3, $4);

	if (!$mail && $name =~ m/@/) { # name probably missing and address has no <>
		$mail = $name;
		$name = undef;
	}

	tag "$f-address-malformed", "$maintainer" if $crap;
	tag "$f-address-looks-weird", "$maintainer" if ! $del && $name && $mail;

	if (! $name) {
		tag "$f-name-missing", "$maintainer";
	} elsif ($name !~ /^\S+\s+\S+/) {
		tag "$f-not-full-name", "$name";
	}
			
	#This should be done with Email::Valid:
	if (!$mail) {
		tag "$f-address-missing", "$maintainer";
	} else {
		tag "$f-address-malformed", "$maintainer" 
		    unless ($mail =~ /^[^()<>@,;:\\"[\]]+@(\S+\.)+\S+/); #"

		tag "$f-address-is-on-localhost", "$maintainer"
		    if ($mail =~ /(?:localhost|\.localdomain|\.localnet)$/);

		tag "wrong-debian-qa-address-set-as-maintainer", "$maintainer"
		    if ($f eq "maintainer" && $mail eq 'debian-qa@lists.debian.org');

		tag "wrong-debian-qa-group-name", "$maintainer"
		    if ($f eq "maintainer" && $mail eq 'packages@qa.debian.org' &&
				$name ne 'Debian QA Group');
	}
}

1;

# vim: syntax=perl sw=4 ts=4 noet shiftround
