#!/usr/bin/perl # # (c) 2010 Alessandro De Zorzi - www.rhx.it # # LICENSE GPL2 # # Require config file # require "variable.pl"; use Text::CSV; use DBI; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $mon = sprintf("%02d", $mon ); $mday = sprintf("%02d", $mday ); $hour = sprintf("%02d", $hour ); $min = sprintf("%02d", $min ); $sec = sprintf("%02d", $sec ); $changed = ($year+1900).'-'.$mon.'-'.$mday.' '.$hour.':'.$min.':'.$sec; my $csv = Text::CSV_XS->new({ sep_char => "|" }); open(CSV, $ARGV[0]) or die "open: $ARGV[0]: $!\n"; # Create MySQL connection $dbh = DBI->connect('DBI:mysql:'.$db_name.';host='.$db_host, $db_user, $db_pass, { RaiseError => 1 } ); $username = substr($ARGV[0],0,-8); # Try to find user code in roundcube $sth = $dbh->prepare("SELECT user_id FROM users WHERE username='".$username."'"); $sth->execute(); $result = $sth->fetchrow_hashref(); $user_id = $result->{user_id}; if (!$user_id) { print "User ".$username." not found... (never logged in RC?)"."\n"; print "-------------------------------------"."\n"; } else { $lines=0; while (defined ($_ = )) { s/"//g; s/è/e/g; s/è/e/g; s/é/e/g; s/ì/i/g; s/ò/o/g; s/ù/u/g; s/\'/\_/g; s/\| /\|/g; $csv->parse($_) or warn("invalid CSV line: ", $csv->error_input(), "\n"), next; my @fields = $csv->fields(); $firstname = $fields[1]; $surname = $fields[2]; $email = $fields[3]; if ($fields[0]) { $name = $fields[0]; } elsif ($fields[4]) { $name = $fields[4]; } else { $name = $firstname.' '.$surname; } $query = "INSERT INTO contacts (`contact_id`,`changed`,`del`,`name`,`email`,`firstname`,`surname`,`vcard`,`user_id`) VALUES (NULL,'$changed',0,'$name','$email','$firstname','$surname',NULL,$user_id) "; # Check if countact already exists $sth = $dbh->prepare("SELECT contacts.* FROM contacts WHERE user_id='".$user_id."' AND email='".$email."'"); $sth->execute(); $result = $sth->fetchrow_hashref(); if (!$result->{email}) { # Insert the contact $dbh->do($query, undef, 'Hello'); } $lines = ($lines + 1); } $sth = $dbh->prepare("SELECT COUNT(contact_id) AS nr FROM contacts WHERE user_id='".$user_id."'"); $sth->execute(); $result = $sth->fetchrow_hashref(); print "User: ".$username."\n"; print "SM contacts: ".$lines."\n"; print "Import contacts: ".$result->{nr}."\n"; print "-------------------------------------"."\n"; } close(CSV) or die "close: $ARGV[0]: $!\n"; # Close DB MySQL #$dbh->disconnect();