# cups-pdf-dispatch.conf
# Configuration file for cups-pdf-dispatch.
# This file is interpreted by perl.


# $GET_USER_MAILADDR_SUB
# Reference to a function which converts username to e-mail address.
# Arguments: username
# Returns: e-mail address
#

my $_user_realname = undef;

use locale;

$GET_USER_MAILADDR_SUB = 
sub {
	my $username = shift;
	my $filename = "/var/spool/pdf/${username}/mailto";
	my $mailto = undef;

	if (-r $filename) {
		if (open(MAILTO,"< $filename")) {
			while (<MAILTO>) {
				chomp;
				next if /^#/;
				if (not $mailto) {
					$mailto = $_;
					last;
				}
			}
			close(MAILTO);
		}
		else {
			warn "could not open ${filename}: $!";
		}
	}
	else {
		$mailto = sprintf("%s@%s", $username, hostname());	
	}

	if ($mailto =~ m{^([^<]+)\s<([^>]+)>}) {
		$_user_realname = $1;
		$mailto = $2;
	}
		
	return $mailto;
};

# $GET_USER_REALNAME_SUB
# Reference to a function which converts username to user's realname (used
# when constructing To: header).
# Arguments: username
# Returns: user's real name
#

$GET_USER_REALNAME_SUB =
sub {
	my $username = shift;

	if (defined $_user_realname) {
		return $_user_realname;
	}
	else {
		# (i.e. user's real name from gecos)
		return (split( /,/, (getpwnam($username))[6], 2 ))[0];
	}
};

