Advertisement
Guest User

PIAF Phonebook updates from vcf file. For use with DavCard

a guest
Mar 10th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.17 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use DBI;
  3.  
  4. #This script updates the FOP2 and Asteridex phone books in PIAF from a vcf file.
  5. #It clears out the phonebooks before recreating them, so this is one way sync only. All changes will be lost.
  6. #Records with more then one phone number have an entry created for each number.
  7. #10 or 7 digits US phone numbers only.
  8. #3/10/2014
  9.  
  10. $dbh = DBI->connect ( "dbi:mysql:host=localhost;database=asteridex","root","********"); #Asterdex phonebook
  11. $dbh2 = DBI->connect ( "dbi:mysql:host=localhost;database=asterisk","root","********"); #FOP2 phone book.
  12.  
  13. #clear out both phone books before adding
  14. $dbh->do("DELETE FROM user1;");
  15. $dbh2->do("DELETE FROM visual_phonebook;");
  16.  
  17.  
  18. open(FILE1, "contacts.vcf");
  19.  
  20. while (<FILE1>) {
  21.     $line=$_;
  22.     if ($line =~ /FN:/) { $FN=$line };
  23.     if ($line =~ /TEL/) { ParseData($FN,$line); };
  24.     }
  25.    
  26.  
  27.    
  28. sub ParseData {
  29.  
  30.     my $fn = $_[0];
  31.     my $tel = $_[1];
  32.     chomp $fn;
  33.     chomp $tel;
  34.     $fn =~ s/FN://;
  35.  
  36.     #strip out all the extra stuff in the phone numbers. 7 or 10 digits only
  37.     $tel =~ s/-//g;
  38.     $tel =~ s/ //g;
  39.     $tel =~ s/\(//g;
  40.     $tel =~ s/\)//g;
  41.     $tel =~ s/\+//g;
  42.     my @tel2 = $tel =~ /(\d{7}$|\d{10}$)/; #get the phone number off the back.
  43.    
  44.     if (! $tel2[0]) {return;} #skip if no phone number
  45.     if (! $fn) {return;} #skip if no name
  46.    
  47.  
  48.     my @fn2 = $fn =~ /^(\w*)\s(.*)$/; #split out the first name (first word) and use whatever is left for the last name.
  49.     if (! $fn2[0] && $fn) {$fn2[0] = $fn;} #if we don't get anything out of the above regex, then it's first name only, use that.
  50.    
  51.     my $fnfirst = $dbh2->quote($fn2[0]);
  52.     my $fnlast = $dbh2->quote($fn2[1]);
  53.    
  54.     my $fnsafe = $dbh->quote($fn);
  55.     my $tel2safe = $dbh->quote($tel2[0]);
  56.    
  57.    
  58.     #insert into visual_phonebook, need to change user1 to the user.
  59.     $dbh->do("INSERT user1 SET
  60.    name=$fnsafe,
  61.    `out`=$tel2safe");
  62.  
  63.     #insert into fop2 database, need to change owner to the extension.
  64.     $dbh2->do("INSERT visual_phonebook SET
  65.    firstname=$fnfirst,
  66.    lastname=$fnlast,
  67.    phone1=$tel2safe,
  68.    owner=200");
  69.  
  70.  
  71.     #print $fn."-----".$tel2[0]."\n";
  72.  
  73. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement