no warnings qw(redefine);

sub LookupExternalUserInfo {
# Taken from an unknoen source
# If the original author sees this please email
# stewart.james@vu.edu.au so I can place your credit here.
# I only modified this to work with my LDAP host and to make it function
# within RT

	my %UserInf = {};
	$UserInfo{'EmailAddress'} = shift;
	$UserInfo{'RealName'} = shift;
	$UserInfo{'RealName'} =~ s/\"//g;
	my ($FoundUser);

	# {{{ load up ldap modules

	use Net::LDAP;
	use Net::LDAP::Constant qw(LDAP_SUCCESS);

	# }}}

	# {{{ defined constants we're going to need

	use constant LDAP      => q(your.directory.server);
	use constant LDAP_PORT => q(389);
	use constant LDAP_BASE => q(dc=your,dc=base);
	# If you're using a server that doesn't require you to
	# bind with a password, set LDAP_BIND and LDAP_BINDPASS to q();

	use constant LDAP_BIND => q();

	use constant LDAP_BINDPASS => q();



	# }}}

	# {{{ connect to the ldap server
	my $ldap = new Net::LDAP(LDAP, port => LDAP_PORT); # ||
	#   $RT::Logger->critical("GetExternalUserWithLDAP: ".
	#                        "Cannot connect to LDAP'\n"),
	#      return (0, %UserInfo);

	# If we're running against a server that
	#  if (LDAP_BIND) {
	#        my $mesg = $ldap->bind(LDAP_BIND, password => LDAP_BINDPASS );
	#        if ($mesg->code != LDAP_SUCCESS) {
	#            $RT::Logger->critical("GetExternalUserWithLDAP: Cannot bind to LDAP:",
	#                                  $mesg->code, "\n");
	#            return (0, %UserInfo);
	#        }
	#}
	# }}}

	# {{{ search for this user by email address

	my $filter = "mail=".$UserInfo{'EmailAddress'};
	$RT::Logger->debug("GetExternalUserWithLDAP: First search filter '$filter'\n");
	$mesg = $ldap->search(base   => LDAP_BASE,
		filter => $filter,
		attrs  => ['mail', 'cn', 'ou', 'uid', 'telephonenumber', 'l', 'roomnumber', 'mobile']);
	if ($mesg->code != LDAP_SUCCESS)  {
		$RT::Logger->critical("GetExternalUserWithLDAP: Could not search for $filter: ",
			$mesg->code, "\n");

		return (0, %UserInfo);
	}

	$RT::Logger->debug("GetExternalUserWithLDAP: First search produced ",
		$mesg->count, " results\n");


	# }}}

	# {{{ if the E-mail search failed, try searching by cn
	unless ($mesg->count == 1) {
		$filter = "cn=".$UserInfo{'RealName'};
		$RT::Logger->debug("GetExternalUserWithLDAP: Second search filter '$filter'\n"); 
		$mesg = $ldap->search(base   => LDAP_BASE, 
			filter => $filter,
			attrs  => ['mail', 'cn', 'ou', 'uid']);
		if ($mesg->code != LDAP_SUCCESS)  {
			$RT::Logger->critical("GetExternalUserWithLDAP: Could not search for $filter: ", 
				$mesg->code, "\n");

			return (0, %UserInfo);
		}                          
	}            

	$RT::Logger->debug("GetExternalUserWithLDAP: Second search produced ", $mesg->count, 
		" results with filter $filter\n");
	# }}} 

	# One of the two searches succeeded with just one match
	if ($mesg->count == 1) {
		$UserInfo{'EmailAddress'} = ($mesg->first_entry->get_value('mail'))[0];
		$UserInfo{'RealName'} = ($mesg->first_entry->get_value('cn'))[0];
		$UserInfo{'Name'} = ($mesg->first_entry->get_value('uid'))[0];
		$UserInfo{'Address1'} = ($mesg->first_entry->get_value('ou'))[0];
		$UserInfo{'Address2'} = ($mesg->first_entry->get_value('departmentNumber'))[0];
		$UserInfo{'City'} = ($mesg->first_entry->get_value('l'))[0];
		$UserInfo{'Country'} = ($mesg->first_entry->get_value('roomnumber'))[0];
		$UserInfo{'WorkPhone'} = ($mesg->first_entry->get_value('extension'))[0]; 
		$UserInfo{'MobilePhone'} = ($mesg->first_entry->get_value('mobile'))[0]; 
		$FoundUser = 1;
	}

	# {{{ close down the ldap connection
	$mesg = $ldap->unbind();
	if ($mesg->code != LDAP_SUCCESS) {
		$RT::Logger->critical("GetExternalUserWithLDAP: Could not unbind from LDAP: ", 
			$mesg->code, "\n");
	}
	# }}}

	return ($FoundUser, %UserInfo);

}   


1;
