[imp] Migrating addressbook

Micah Anderson micah@riseup.net
Tue, 7 Aug 2001 21:44:17 -0700


Well, I didn't get a response right away, and I'm impatient, so I went
ahead and wrote this perl script to translate an older IMP addressbook
to the newer turba format. Just had to generate some random unique
keys and strip out the domain part of the user whose address entry it
was. 

Of course I saw the script that someone else posted after. :)

You can get the script here: http://riseup.net/~micah/imp2turba.pl 

#!/usr/bin/perl
#
#    File:      imp2turba.pl
#    Author:    Micah Anderson - micah@riseup.net
#    Version:   1.0
#
#    Date:      8/7/01
#    Purpose:   This script converts an old imp addressbook database contents
#               to the newer turba addressbook format, it was written for
#               mysql, but could be easily ported to other databases
#
#    Usage:     Modify the variables at the beginning of the script
#               so that your database user/password, location and imp
#               database and turba database are correct. Then simply run
#               ./convert.pl, if you have made the script executable, or
#               perl ./convert.pl
#

use DBI;

our $location       = 'localhost';
our $port_num       = '3306';
our $username       = 'xxxx';
our $password       = 'xxxxxx';

our $IMP_DATABASE   = 'imp';
our $IMP_TABLE      = 'imp_addr';
our $TURBA_DATABASE = 'horde2';
our $TURBA_TABLE    = 'Turba_Objects';

our $dbi_options = {RaiseError => 1, ChopBlanks => 1, AutoCommit => 1};

 $db_imphandle = DBI->connect
                      ("DBI:mysql:$IMP_DATABASE:$location:port_num", 
                        $username, $password, $dbi_options )
                        || die ("Connection error: $DBI::errstr");

 $db_turbahandle = DBI->connect
                        ("DBI:mysql:$TURBA_DATABASE", 
	                  $username, $password, $dbi_options )
                          || die ("Connection error: $DBI::errstr");

 $imp_statement = $db_imphandle->prepare("SELECT * FROM $IMP_TABLE");
 $imp_statement->execute();

 while (my ($owner, $address, $nickname, $fullname) = 
 	$imp_statement->fetchrow_array())

 {

   @chars     = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) );
   $unique_key = join("", @chars[ map { rand @chars } ( 1 .. 31 ) ]);

# Remove the @domain.com part from the $owner, doesn't work in Turba
   $owner =~ s/\@.*$//;

# Quote the strings appropriately for the database
   my $quoted_key = $db_imphandle->quote($unique_key);
   my $quoted_owner = $db_imphandle->quote($owner);
   my $quoted_fullname = $db_imphandle->quote($fullname);
   my $quoted_address = $db_imphandle->quote($address);

   $turba_statement = "INSERT INTO $TURBA_TABLE VALUES 
                       ($quoted_key, $quoted_owner, $quoted_fullname, 
		       $quoted_address, '','','','','','','','','')";

   $turba_statement = $db_turbahandle->prepare($turba_statement)
      || die "prepare: $$stmt: $DBI::errstr";
   $turba_statement->execute || die "execute: $$stmt: $DBI::errstr";
   $turba_statement->finish();

 }

 $imp_statement->finish();
 $turba_statement->finish();
 $db_imphandle->disconnect;
 $db_turbahandle->disconnect;