# Vend::Subs - Run Interchange Subs as object methods
# 
# $Id: Subs.pm,v 2.1 2005/10/15 01:25:23 jon Exp $
#
# Copyright (C) 2005 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., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  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;
