#!/usr/bin/perl # --------------------------------------------------------------------- # Convert sqwebmail addressbooks to squirrelmail database format. # This will output the SQL you need. Run it, and inspect the sql # before you insert it into the table. # - rudy at my domain monkeybrains net # Mon Jan 3 17:35:00 PST 2005 # --------------------------------------------------------------------- $MAILDIR_BASE = "/home"; # where are the address-books ??? $DB_NAME = "squirrelmail"; # only used when printing the first line $debug = undef; # =1 prints out degugging info # --------------------------------------------------------------------- print "use $DB_NAME;\n"; chdir "$MAILDIR_BASE" or die "You need to set your maildir!\n"; open LS, " ls */Maildir/sqwebmail-addressbook |" or die; while ($a = ) { chomp($a); $a =~ m!^([^/]+)/Maildir/sqwebmail-addressbook$! or die; my $user = $1; $count = 0; print "$user ---> $a\n" if $debug; open A, "<$a" or die; while () { chomp(); $count++; # convert multi lines to single lines while (/,$/) { # print "+"; $_ .= ; chomp(); } # sqwebmail format - danx: "Dan Xxxx" m/^(.*?): ("[^"]+" )?(.*)$/ or die "DIE: $_\n"; my $nick = $1; my $name = $2; my $email = $3; $name =~ s/^"(.*)" $/$1/; $name =~ s/^'(.*)'$/$1/; # change the name into First and Last names... my ($first, $last); # Last, First if ($name =~ m/^(\S+), (\S+)$/) { $first = $2; $last= $1; # First Last } elsif ($name =~ m/^(\S+) (\S+)$/) { $first = $1; $last= $2; # First.Last@domain.com } elsif ($name =~ m/^(\S+)\.(\S+)\@/) { $first = $1; $last= $2; # First Middle Last } elsif ($name =~ m/^(\S+ \S+) (\S+)$/) { $first = $1; $last= $2; # Can't figure it out... } else { $first = $name; } # if the address had multiple email addresses, concat them with a # comma my $e = ''; while ($email =~ s/<([^>]+?)>/_/ ) { $e .= "$1," } chop($e); $email = $e; # escape single quotes and trim whitespace $first= &trim($first); $last = &trim($last); $nick = &trim($nick); $email = &trim($email); # make sure this item is unique $id = "$user - $nick"; $id =~ tr/A-Z/a-z/; if ($unique{$id} == 1) { $nick = "$nick$count"; $id = "$user - $nick"; print "DUP... $user ---> $nick\n" if $debug; } $unique{$id} == 1 and next; # give up $unique{$id} = 1; # print out the sql line! print " INSERT INTO address VALUES ('$user', '$nick', '$first', '$last', '$email', NULL);\n"; } print " Addresses: $count \n" if $debug; close A; } close LS; sub trim { my $t = shift; $t =~ s/\s+/ /; $t =~ s/^ //; $t =~ s/ $//; $t =~ s/'/\\'/g; return $t; }