e-smith-turba/root/etc/e-smith/events/actions/migrate-imp-to-turba

112 lines
4.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl -wT
#----------------------------------------------------------------------
# copyright (C) 2002 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Technical support for this program is available from Mitel Networks
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------
# Derived from imp2turba.pl-txt contrib script from horde.org
# Modified for SME Server 5.5 by Mark Knox <markk@e-smith.com>
use strict;
use esmith::ConfigDB;
use DBI;
# Exit early if there is nothing to do
exit 0 unless (-f '/var/lib/mysql/horde/imp_addr.frm');
die("horde db must exist") unless ( -d "/var/lib/mysql/horde/");
die("turba db must exist") unless ( -f "/var/lib/mysql/horde/turba_objects.frm");
my $conf = esmith::ConfigDB->open() || die 'Unable to open config db';
# FIXME - what are these for?
$ENV{'PATH'} = '';
$ENV{'SHELL'} = '/bin/bash';
delete $ENV{'ENV'};
delete $ENV{'BASH_ENV'};
our $location = 'localhost';
our $port_num = '3306';
our $username = 'horde';
our $password = $conf->get_prop('horde', 'DbPassword') || 'horde';
our $IMP_DATABASE = 'horde';
our $IMP_TABLE = 'imp_addr';
our $TURBA_DATABASE = 'horde';
our $TURBA_TABLE = 'turba_objects';
our $domain = $conf->get('DomainName') || 'localhost';
unless ($domain eq "localhost")
{
$domain = $domain->value || "localhost";
}
our $dbi_options = {RaiseError => 1, ChopBlanks => 1, AutoCommit => 1};
my $db_imphandle = DBI->connect
("DBI:mysql:$IMP_DATABASE:$location:$port_num",
$username, $password, $dbi_options )
|| die ("Connection error: $DBI::errstr");
my $db_turbahandle = DBI->connect
("DBI:mysql:$TURBA_DATABASE",
$username, $password, $dbi_options )
|| die ("Connection error: $DBI::errstr");
my $imp_statement = $db_imphandle->prepare("SELECT * FROM $IMP_TABLE");
$imp_statement->execute();
my $turba_statement;
while (my ($owner, $address, $nickname, $fullname) =
$imp_statement->fetchrow_array())
{
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) );
my $unique_key = join("", @chars[ map { rand @chars } ( 1 .. 31 ) ]);
# Remove the @domain.com part from the $owner, doesn't work in Turba
$owner =~ s/\@.*$//;
$owner .= "\@$domain";
# 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);
my $quoted_nickname = $db_imphandle->quote($nickname);
$turba_statement = "INSERT INTO $TURBA_TABLE VALUES
($quoted_key, $quoted_owner, $quoted_fullname,
$quoted_nickname,$quoted_address, '','','','','','','','','','','')";
$turba_statement = $db_turbahandle->prepare($turba_statement)
|| die "prepare: $$turba_statement: $DBI::errstr";
$turba_statement->execute || die "execute: $$turba_statement: $DBI::errstr";
}
$imp_statement = "DROP TABLE $IMP_TABLE";
$imp_statement = $db_imphandle->prepare($imp_statement)
|| die "prepare: $$imp_statement: $DBI::errstr";
$imp_statement->execute || die "execute: $$imp_statement: $DBI::errstr";
$imp_statement->finish();
# turba_statement will be undef if the imp_addr table is empty, so we check
$turba_statement->finish() if $turba_statement;
$db_imphandle->disconnect;
$db_turbahandle->disconnect;