# Vend::Subs - Run Interchange Subs as object methods
#
# Copyright (C) 2005-2007 Interchange Development Group
#
# 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, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA  02110-1301  USA.

package Vend::Subs;

sub new {
	return bless {}, shift;
}

sub DESTROY {
	1;
}

sub AUTOLOAD {
	our $AUTOLOAD;
	shift;
	my $sub_name = $AUTOLOAD;
	$sub_name =~ s/.*:://;

	if (
		! $Global::AllowGlobal->{$Vend::Cat}
		and defined $Vend::Cfg->{AdminSub}{$sub_name}
	) {
		if ($Vend::restricted) {
			die ::errmsg(
					"Sub '%s' in execution-restricted area: %s",
					$sub_name,
					$Vend::restricted,
				);
		}
		elsif (! $Vend::admin) {
			die ::errmsg("Unauthorized for admin sub '%s'", $sub_name);
		}
	}

	my $sub_ref = $Vend::Cfg->{Sub}{$sub_name} || $Global::GlobalSub->{$sub_name};
	unless ($sub_ref) {
		::logError("Sub '%s' not defined.", $sub_name);
		return;
	}

	return $sub_ref->(@_);
}

1;
