# FILE: filter31.pl
# DESCRIPTION: Read in data from Discus 3.1X for upgrade purposes
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### v31_user_line_as_hash
###
### Converts a line of raw input to a useful, easy manipulated hash
###

sub v31_user_line_as_hash {
	my @r = ();
	my @s = ('user', 'pass', 'email', 'fullname', 'edit', 'notify');
	while (my $x = shift @_) {
		my $i = {};
		chomp $x;
		my @x = split(/:/, $x);
		foreach my $s (@s) {
			$i->{$s} = shift @x;
		}
		($i->{stime}, $i->{atime}) = split(/\//, shift @x);
		$i->{ctime} = 0;
		$i->{groups} = shift @x;
		push (@r, $i);
	}
	return $r[0] if scalar(@r) == 1;
	return \@r;
}

###
### queue_transform
###
### Messages in the queue
###

sub queue_transform {
	my ($self) = @_;
	my $n = {};
	$self->{PAGE} =~ m|^(\d+)/(\d+)|; $n->{topic_number} = $1; $n->{me_number} = $2;
	$n->{text} = $self->{TEXT};
	$n->{author} = $self->{POSTBY};
	$n->{real_author} = join(":", $self->{USERNAME}, $self->{GROUP});
	$n->{username} = $self->{USERNAME};
	$n->{author_status} = (
		$self->{USERNAME} eq $DCONF->{superuser} && $self->{GROUP} eq "MODERATOR" ? 10 : (
		$self->{GROUP} eq "MODERATOR" ? 9 : (
		$self->{USERNAME} eq "PUBLIC" ? 2 : 3)));
	$n->{'time'} = $self->{TIME};
	if ($self->{PROFILE} =~ m|<A HREF="(.*?)">|) {
		$n->{email} = $1;
	} elsif ($self->{EMAIL}) {
		$n->{email} = "mailto:$self->{EMAIL}";
	}
	$n->{subject} = $self->{SUBJECT} if $self->{SUBJECT} ne "";
	$n->{source} = $self->{SOURCE};
	$n->{remote_addr} = $self->{RA};
	$n->{remote_host} = $self->{RH};
	return $n;
}

###
### enhanced_user_profile_3031
###
### Reads an enhanced user profile from 3.0 or 3.1 format
###

sub enhanced_user_profile_3031 {
	my ($filedata, $username, $oldconf) = @_;
	dreq("webtags");
	my @p = grep(/\S/, @{ $filedata }); @p = grep(!/^#/, @p);
	my $flag = 0;
	my $f = {};
	my @in = ();
	my $ctr = 0;
	foreach my $line (@p) {
		chomp $line;
		if ($line =~ m|<INFO>|) {
			$flag = 1;
		} elsif ($line =~ m|</INFO>|) {
			$flag = 0;
		} elsif ($flag == 1 && $line =~ m|^(\w+)=(.*)|) {
			my ($var, $val) = ($1, $2);
			if ($var =~ m|(\w+)_hide|) {
				$f->{pershide}->{$1} = $val;
			} else {
				next if $val eq "";
				my $vl = unescape($val);
				my ($lint, $message) = webtags($vl, 1, 1);
				$message = "" if $lint eq "!Error";
				if (defined $oldconf->{$var} && $oldconf->{$var} < 20) {
					$in[$oldconf->{$var}] = { var => $var, val => $message };
				} else {
					$f->{cust}->{$var}->{val} = $message;
					$ctr++;
					$f->{cust}->{$var}->{order} = $ctr;
				}
			}
		} elsif ($line =~ m|<PICTURE>|) {
			$flag = 2;
		} elsif ($line =~ m|</PICTURE>|) {
			$flag = 0;
		} elsif ($flag == 2 && $line =~ m|^(\w+)=(.*)|) {
			$f->{pict} = unescape($2);
		} elsif ($line =~ m|<CUSTOM>|) {
			$flag = 3;
		} elsif ($line =~ m|</CUSTOM>|) {
			$flag = 0;
		} elsif ($flag == 3 && $line =~ m|^(\w+)=(.*)|) {
			my ($var, $val) = ($1, $2);
			next if $val eq "";
			my $vl = unescape($val);
			my ($lint, $message) = webtags($vl, 1, 1);
			$message = "" if $lint eq "!Error";
			$f->{cust}->{$var}->{var} = $message;
		} elsif ($line =~ m|<PREFS>|) {
			$flag = 4;
		} elsif ($line =~ m|</PREFS>|) {
			$flag = 0;
		} elsif ($flag == 4 && $line =~ m|^(\w+)=(.*)|) {
			$f->{pref}->{$1} = unescape($2);
		}
	}
	my $conv = {cookie => 'a',cookie_temp => 'b',noemail => 'c', skip_preview => 'd',show_all_nm => 'e',nm_view => 'f',nm_newwindow => 'g',last_favs => 'h',spellcheck => 'i'};
	my %h = map { $conv->{$_}, 1 } grep($f->{pref}->{$_} > 0 && $_ ne "favorites", keys %{$f->{pref}});
	my %fav = map { $_, 1 } split(/,/, $f->{pref}->{favorites});
	my @inf = ();
	my @cus = ();
	foreach my $c (@in) {
		if (! defined $f->{cust}->{$c->{var}}) {
			push @inf, { hide => ($f->{pershide}->{$c->{var}} ? 1 : 0), val => unescape($c->{val}) };
		}
	}
	foreach my $k (sort { $f->{cust}->{$a}->{order} <=> $f->{cust}->{$b}->{order} } keys %{ $f->{cust} }) {
		push @cus, { var => $f->{cust}->{$k}->{var}, val => $f->{cust}->{$k}->{val} };		
	}
	my $s = {};
	$s->{username} = $username;
	$s->{picture} = $f->{pict} =~ m|^(\d+)\.(\w+)$| ? $f->{pict} : "";
	$s->{pref} = \%h;
	$s->{favorite} = \%fav;
	$s->{custom} = \@cus;
	$s->{personal} = \@inf;
	return $s;
}

###
### import_posting_reading_privileges
###
### Posting and reading privileges (posting.txt, security.txt, postoptions.txt)
###

sub import_posting_reading_privileges {
	my $p = { p => 'posting.txt', r => 'security.txt' };
	my @u = ('p'); push (@u, 'r') if $DCONF->{pro};
	dreq("fcn-grp");
	my $G = read_group_file();
	my @actions = ();
	foreach my $i (@u) {
		if (-e "$DCONF->{admin_dir}/$p->{$i}") {
			my $file = readfile("$DCONF->{admin_dir}/$p->{$i}", "import_posting_reading_privileges(3.1)", { zero_ok => 1 });
			foreach my $line (@{ $file }) {
				chomp $line;
				$line =~ s/~/\*/g;
				my ($topic, $ip_based, $user_based, $moderator_based, $special) = split(/:/, $line);
				undef my $u;
				$u->{action} = "set_equal";
				$u->{topic} = $topic;
				$u->{type} = $i;
				$u->{ip} = $ip_based;
				$u->{user} = $user_based;
				if ($moderator_based ne "" && $moderator_based ne "*") {
					my $mm = {};
					foreach my $grp (split(/,/, $moderator_based)) {
						my %h = map { $_, 1 } keys %{$G->{by_grp}->{$grp}};
						hash_merge($mm, \%h, 1);
					}
					$moderator_based = join(",", keys %{$mm});
				}
				$u->{moderator} = $moderator_based;
				$u->{special} = $special;
				push (@actions, $u);
			}
		}
	}
	if (-e "$DCONF->{admin_dir}/postoptions.txt") {
		my $file = readfile("$DCONF->{admin_dir}/postoptions.txt", "import_posting_reading_privileges(3.1)", { zero_ok => 1 });
		foreach my $line (@{ $file }) {
			chomp $line;
			my ($topic, $anon, $fullname, $email) = split(/:/, $line);
			undef my $u;
			$u->{topic} = $topic;
			$u->{type} = "o";
			$u->{anon} = 0 + $anon;
			$u->{fullname} = 0 + $fullname;
			$u->{email} = 0 + $email;
			$u->{profile} = 0 + $email;
			push (@actions, $u);
		}
	}
	if ($DCONF->{pro}) {
		if (-e "$DCONF->{admin_dir}/queue.txt") {
			my $file = readfile("$DCONF->{admin_dir}/queue.txt", "import_posting_reading_privileges(3.1)", { zero_ok => 1 });
			foreach my $line (@{ $file }) {
				chomp $line;
				my ($topic, $users, $moderator, $special, $public) = split(/:/, $line);
				next if $topic !~ m|\d|;
				undef my $u;
				$u->{topic} = $topic;
				$u->{type} = "q";
				$u->{users} = 0 + $users;
				$u->{moderators} = 0 + $moderator;
				$u->{special} = 0 + $special;
				$u->{public} = 0 + $public;
				push (@actions, $u);
			}
		}
	}
	return \@actions;
}

1;
