#!/usr/bin/perl
# This command sets up .ssh/authorized_keys for an avatar, allowing them
# passwordless access to the moo.
#use Mooix::Thing;
#use Mooix::Root;
run sub {
	my $this=shift;

	if (! @_) {
		# Unsetting key.
		if (ref $this->_ssh) {
			unlink "sshkey" || fail "Permission denied.";
			if ($this->_ssh->setkey) {
				return; # success
			}
			fail "You can't do that.";
		}
		else {
			return; # success, since there is none
		}
	}
	
	if (! ref $this->_ssh) {
		# Set up the object. Note that the owner is not set to this
		# avatar, because I don't want builders mucking around in
		# the .ssh directory and getting shells.
		$this->create(id => '.ssh', parent => $Mooix::Root->abstract->ssh);
	}

	# The key is not passed, but has to be set, and then the ssh
	# object's setkey method called.
	my @oldkey=$this->sshkey;
	$this->sshkey(@_);
	if ($this->_ssh->setkey) {
		return @_; # success
	}
	else {
		$this->sshkey(@oldkey ? @oldkey : "");
		fail "Sorry, the key was rejected. Check the format and try again.";
	}
}
