# # This is an example of using the Text::ParseWords module to convert # a comma-delimited file to LDIF. Requires Perl 5.005 or higher. Just # pipe the CSV file to this script and redirect the output to a file: # # cat FILENAME.csv | csv2ldif.pl > FILENAME.ldif # # Anthony Greene # # This version modified for the fields used by MS Outlook 98 by # Buchan Milne 20011002 # # Load the required modules use Text::ParseWords; use MIME::Base64; # Only configuation required, customize $suffix to your taste $suffix = 'ou=AddressBook,dc=YourDomain,dc=com'; sub printAttribute { my( $attr, $val ) = @_; # remove all trailing white space $val =~ s/\s+$//; # if $val is empty return doing nothing if ( $val eq "" ) { return; } # encode $val if it contains newline characters if ( $val =~ /\n/ ) { # while its possible to encode and create a dn with # newline characters, its obnoxious for a variety of # reasons and therefore best not permitted - remove # this if statment if you disagree... if ( $attr eq "dn" ) { warn "Impromperly Formatted DN (Contains Newline " . "Characters):\n$val\nDiscarding...\n\n"; return; } $encoded = encode_base64($val, ""); print "${attr}:: ${encoded}\n"; } else { print "${attr}: ${val}\n"; } } sub buildEvolutionAddress { my( $ladd1, $ladd2, $ladd3, $lcity, $lstate, $lzip, $lc ) = @_; local( $a1, $a2, $address ); if ($ladd1 ne "" || $ladd2 ne "" || $ladd3 ne "") { $a1 = $ladd1 . " " . $ladd2 . " " . $ladd3; # outlook separates address1, 2 and 3 in the street address details # field by newlines, get rid of them so evolution can manage the # address info $a1 =~ s/\n/ /g; } if ($lcity ne "" || $lstate ne "" || $lzip ne "") { $a2 = $lcity . ", " . $lstate . " " . $lzip; #if ($hc ne "") { $a2 .= "\n" .$hc; } } $address = $a1 . "\n" . $a2; return $address; } # line counter $lineCounter = 0; # Read lines from STDIN. while ( $line = <> ) { # increment line counter $lineCounter++; # remove those pesky carriage return characters $line =~ s/\r//g; # save the current line to a buffer $buf .= $line; # if the last character in a line isn't a quote continue reading # appending to $buf - comment: this still fails if field opening # quote ends a line and closing quote appears on another line next if ( !($line =~ /\"$/) ); #{ # next; # } # parse fields from the $buf @fields = "ewords(',',0,$buf); # clear $buf so future lines start fresh $buf = ""; # Set variable values based on the array values. # Edit these to match the format of your CSV file. $title = $fields[0]; $fname = $fields[1]; $mname = $fields[2]; $lname = $fields[3]; $suff = $fileds[4]; $o = $fields[5]; $ou = $fields[6]; $job = $fields[7]; $add1 = $fields[8]; $add2 = $fields[9]; $add3 = $fields[10]; $city = $fields[11]; $state = $fields[12]; $zip = $fields[13]; $c = $fields[14]; $hadd1 = $fields[15]; $hadd2 = $fields[16]; $hadd3 = $fields[17]; $hcity = $fields[18]; $hstate = $fields[19]; $hzip = $fields[20]; $hc = $fields[21]; $oadd1 = $fields[22]; $oadd2 = $fields[23]; $oadd3 = $fields[24]; $ocity = $fields[25]; $ostate = $fields[26]; $ozip = $fields[27]; $oc = $fields[28]; $aphone = $fields[29]; $fax = $fields[30]; $phone = $fields[31]; $phone2 = $fields[32]; $callbck= $fields[33]; $carphn = $fields[34]; $o_phone= $fields[35]; $hfax = $fields[36]; $hphon = $fields[37]; $hphon2 = $fields[38]; $isdn = $fields[39]; $mobile = $fields[40]; $ofax = $fields[41]; $ophone = $fields[42]; $pager = $fields[43]; $pphone = $fields[44]; $rphone = $fields[45]; $ttyph = $fields[46]; $telex = $fields[47]; $acc = $fields[48]; $ann = $fields[49]; $ass = $fields[50]; $bill = $fields[51]; $bday = $fields[52]; $pobox = $fields[53]; $cat = $fields[54]; $child = $fields[55]; $ds = $fields[56]; $mail = $fields[57]; $mtype = $fields[58]; $mdisp = $fields[59]; $mail2 = $fields[60]; $mtype2 = $fields[61]; $mdisp2 = $fields[62]; $mail3 = $fields[63]; $mtype3 = $fields[64]; $mdisp3 = $fields[65]; $gender = $fields[66]; $idnum = $fields[67]; $hobby = $fields[68]; $hpobox = $fields[69]; $initial= $fields[70]; $inetfb = $fields[71]; $keywds = $fields[72]; $lang = $fields[73]; $locat = $fields[74]; $manager= $fields[75]; $mileage= $fields[76]; $notes = $fields[77]; $office = $fields[78]; $o_id = $fields[79]; $opobox = $fields[80]; $prioty = $fields[81]; $prvt = $fields[82]; $proff = $fields[83]; $refer = $fields[84]; $sens = $fields[85]; $spouse = $fields[86]; $user1 = $fields[87]; $user2 = $fields[88]; $user3 = $fields[89]; $user4 = $fields[90]; $web = $fields[91]; # need a first name, last name or company to create a dn if ($fname eq "" && $lname eq "" && $o eq "") { warn "Line [$lineCounter] missing required fields, " . "discarding: \n\n@fields\n\n"; next; } # for dn, replace any "," in fname, lname with "\," ( $fname2 = $fname ) =~ s/,/\\,/g; ( $lname2 = $lname ) =~ s/,/\\,/g; ( $o2 = $o ) =~ s/,/\\,/g; $dn = ""; $cn = ""; if ( $lname2 ne "" && $fname2 ne "" ) { $dn = "cn=$lname2\\, $fname2"; $cn = "$lname, $fname"; } elsif ( $lname2 ne "" ) { $dn = "cn=$lname2"; $cn = $lname; } elsif ( $fname2 ne "" ) { $dn = "cn=$fname2"; $cn = $fname; # object class 'evolutionPerson' requires attribute 'sn' $lname = $fname; } else { $dn = "cn=$o2"; $cn = $o; $lname = $o; } $dn .= ",$suffix"; printAttribute("dn", $dn); printAttribute("objectclass", "top"); printAttribute("objectclass", "person"); printAttribute("objectclass", "inetOrgPerson"); printAttribute("objectclass", "evolutionPerson"); printAttribute("objectclass", "organizationalPerson"); printAttribute("cn", $cn); printAttribute("sn", $lname); printAttribute("givenname", $fname); printAttribute("title", $job); printAttribute("o", $o); printAttribute("ou", $ou); printAttribute("mail", $mail); printAttribute("mail", $mail2); printAttribute("mail", $mail3); printAttribute("telephoneNumber", $phone); printAttribute("facsimileTelephoneNumber", $fax); printAttribute("otherFacsimileTelephoneNumber", $ofax); printAttribute("mobile", $mobile); printAttribute("carPhone", $carphn); printAttribute("l", $city); printAttribute("st", $state); printAttribute("postalCode", $zip); #printAttribute("c", $c); printAttribute("postOfficeBox", $pobox); $address = buildEvolutionAddress($add1,$add2,$add3,$city,$state,$zip,$c); printAttribute("postalAddress", $address); $address = buildEvolutionAddress($hadd1,$hadd2,$hadd3,$hcity, $hstate,$hzip,$hc); printAttribute("homePostalAddress", $address); # TODO: find a friendly place to put "other" addresses, hmmm... printAttribute("initials", $initial); printAttribute("homePhone", $hphon); printAttribute("pager", $pager); printAttribute("note", $notes); printAttribute("physicalDeliveryOfficeName", $office); #printAttribute("manager", $manager); printAttribute("birthDate", $bday); printAttribute("assistantName", $ass); printAttribute("assistantPhone", $aphone); printAttribute("anniversary", $ann); printAttribute("spouseName", $spouse); printAttribute("categories", $cat); print "\n"; } exit;