#!/usr/bin/perl -w
# Converts an attached vCard to LDIF format and submits it to the LDAP server
# Hugh Caley, 2000-01-03

#The email with the attached vcard is forwarded to the sendmail server 
#using the users uid (as defined in the LDAP database) plus "-contacts"
#Sendmail receives the email, then sends it to the script
#using an alias.  Examples follow:
#fred-contacts: "| /usr/local/bin/mailvcard.pl fred"
#jmerry-contacts: "| /usr/local/bin/mailvcard.pl jmerry"

# The owner's uid, if any, is passed as $ARGV[0]
# Mail should be forwarded to contacts-{uid}@yourorg.com
# Requires the perl module Mail::Sendmail for emailed responses

$yourorg = "yourorg";
$passwd = "passwd";
$writeout = 0;
$fromcount = 0;

while (<STDIN>)

{
	# Get the from address

	if( $_ =~ "From:" && $fromcount == 0 )
	{
		$fromline = $_;
		@sender_address = split(/: /, $fromline );
		$fromcount++;
	};

	# Look for the begin:vcard line

	if( $_ =~ "[Bb][Ee][Gg][Ii][Nn]:[Vv]" && $writeout == 0 ) 
	{
		$writeout = 1;
	};

	# Look for the end:vcard line

	if ( $_ =~ "[Ee][Nn][Dd]:[Vv]" )
	{
		$writeout = 3;
		last;
	};
	
	# write the line to the data string
	
	if ( $writeout == 1 )
	{
		$vcardtest =  "$vcardtest"."$_";
	};
};

# look at the data string line by line

@vcard = split( /\n/, $vcardtest );

foreach $item(@vcard)
{
	# Work Address

	if( $item =~ "^[Aa][Dd][Rr];" )
	{
		if( $item =~ "=0D=0A" )
		{
			$item =~ s/=0D=0A/ /g;
		};
		@address1 = split( /:/, $item );
		@address = split( /;/, $address1[1]);
		if( $address1[0] =~ "[Ww][Oo][Rr][Kk]" )
		{
			$ldif = "$ldif"."postalAddress: "."$address[0] "."$address[2] "."$address[1]"."\n";
			$ldif = "$ldif"."l: "."$address[3]"."\n";
			$ldif = "$ldif"."st: "."$address[4]"."\n";
			$ldif = "$ldif"."postalCode: "."$address[5]"."\n";
			$ldif = "$ldif"."c: "."$address[6]"."\n";
		};
		if ( $address1[0] =~ "[Hh][Oo][Mm][Ee]" )
		{
			$ldif = "$ldif"."homePostalAddress: "."$address[1]"."$address[0] "."$address[2] "."$address[3] "."$address[4] "."$address[5] "."$address[6]"."\n";
		};
	};

	# Organization

	if( $item =~ "[Oo][Rr][Gg]:" )
	{
		@org1 = split( /:/, $item );
		@org = split( /;/, $org1[1] );
		$ldif = "$ldif"."o: "."$org[0]"."\n";
	};

	# Telephone Numbers

	if( $item =~ "[Tt][Ee][Ll];" )
	{
		@tel1 = split( /:/, $item );
		@tel = split(/;/, $tel1[1] );

		# Home phone voice

		if( $tel1[0] =~ "[Hh][Oo][Mm][Ee];[Vv][Oo]" )
		{
			$ldif = "$ldif"."homePhone: "."$tel[0]"."\n";
		};

		# Work Phone voice

		if ( $tel1[0] =~ "[Ww][Oo][Rr][Kk];[Vv][Oo]" )
		{
			$ldif = "$ldif"."telephoneNumber: "."$tel[0]"."\n";
        };
		
		# Fax Numbers

		if( $tel1[0] =~ "[Ff][Aa][Xx]" )
        {
            $ldif = "$ldif"."facsimileTelephoneNumber: "."$tel[0]"."\n";
        };

		# Cell

		if( $tel1[0] =~ "[Cc][Ee][Ll]" )
        {
            $ldif = "$ldif"."mobile: "."$tel[0]"."\n";
        };
		# Pager 

        if( $tel1[0] =~ "[Pp][Aa][Gg]" )
        {
            $ldif = "$ldif"."pager: "."$tel[0]"."\n";
        };



	};

	# Name

	if( $item =~ "^[Nn]:" )
    {
        @name1 = split( /:/, $item );
        @name = split( /;/, $name1[1] );
        $ldif = "dn: "."cn="."$name[1] "."$name[0]".", ou=$ARGV[0]-contacts, ou=contacts"."\n"."$ldif";
		$ldif = "$ldif"."cn: "."$name[1] "."$name[0]"."\n";
		$ldif = "$ldif"."sn: "."$name[0]"."\n";
		$ldif = "$ldif"."givenname: "."$name[1]"."\n";
    };

	# Title

	if( $item =~ "^[Tt][Ii][Tt][Ll][Ee]" )
	{
		@title1 = split( /:/, $item);
		$ldif = "$ldif"."title: "."$title1[1]"."\n";
	};

	# Description

	if( $item =~ "^[Nn][Oo][Tt][Ee]" )
    {
        @note1 = split( /:/, $item);
        $ldif = "$ldif"."description: "."$note1[1]"."\n";
    };

	# Email

	if( $item =~ "^[Ee][Mm][Aa][Ii][Ll]" )
    {
        @mail1 = split( /:/, $item);
        $ldif = "$ldif"."mail: "."$mail1[1]"."\n";
    };

	# URL 

    if( $item =~ "^[Uu][Rr][Ll]" )
    {
        @url1 = split( /:/, $item);
        $ldif = "$ldif"."labeledURI: "."$url1[1]"."$url1[2]"."\n";
    };



};
	$ldif = "$ldif"."owner: "."$ARGV[0]"."\n";
	# Add the objectclass definitions
	$ldif = "$ldif"."objectclass: top\nobjectclass: person\nobjectclass: organizationalPerson\nobjectclass: inetOrgPerson\nobjectclass: organization";

$output = `echo "$ldif" | ldapadd -D "cn=root, dc=$yourorg, dc=com" -w "$passwd" -h systems 2<&1`;

use Mail::Sendmail;
                %mail = ( To => "$sender_address[1]",
#               CC => "$name\@$yourorg.com",
                Subject => "vCard Submission",
                From => "Postmaster <postmaster\@$yourorg.com>",
                Message => "The following vcard was received:\n$ldif\nThe response from the LDAP server was \"$output\"\n\nVcard submitted by $sender_address[1]");
sendmail(%mail) or die $Mail::Sendmail::error;

