#! /usr/bin/perl -w # ################################################## # Keep your Evolution Email List up to date with # an external source, file or web site. # The source must be a list of email addresses # one per line that will replace the ENTIRE # list that is in your Evolution Addressbook # ################################################## # ################################################## # Copyright (C) 2005 Dennis Kaptain # http://www.kaptain.us # Released under GPL License http://www.gnu.org/copyleft/gpl.html # August 2005 # Wrtten on Fedora Core 4 # Kernel version 2.6.12-1.1398_FC4 # Perl version v5.8.6 # Evolution version 2.2.3 # Caviat: It worked for me. # ################################################## # 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., # 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. # ################################################## # this program requires the Berkeley DB library # db-4.3.28.tar.gz # from http://www.sleepycat.com # and # Perl BerkeleyDB module downloaded from cpan.org # BerkeleyDB-0.26.tar.gz use strict; use BerkeleyDB; # ################################################## # Evolution List name and addressbook # ################################################## # name of evolution email list my $list = "My-List"; # location of evolution addressbook file my $abook = "/home/user/.evolution/addressbook/local/system/addressbook.db"; # ################################################## # Data source for new list # ################################################## # Source of new email list - can be "file" or "web" my $source = "web"; # name of file containing new email addresses for list my $inputFile = "emails.txt"; # name of web page source my $web = "http://www.myDomain.com/getEmailList.php"; my $username = "user"; my $password = "password"; # where to keep wget log for web based source my $wgetlog = "wget.log"; # ################################################## # after running evolution is dead 1=restart 0=don't restart # ################################################## my $startEvoAgain = 1; # ################################################## # stuff you don't need to worry about # ################################################## my @newEmails; my $hashKey; my $db; my $somedata; my $var=""; my %h; my $k; my $v; my @listItems; my $lineBreak = chr(hex("0D")).chr(hex("0A")); my $end; my $str1 = "EMAIL;X-EVOLUTION-DEST-EMAIL="; my $str2 = ";X-EVOLUTION-DEST-HTML-MAIL=FALSE:"; my $numNew = 0; print "\n"; print "***************************************************\n"; print "***************************************************\n"; print "*** ***\n"; print "*** WARNING!!! This program will ***\n"; print "*** replace the ENTIRE list ***\n"; print "*** $list with new Data!!\n"; print "*** press 'y' to continue. ***\n"; print "*** ***\n"; print "***************************************************\n"; print "***************************************************\n"; print "\ny or n : "; my $resp = <>; print "\n"; chomp $resp; if ($resp ne 'y') { print "bye.\n"; exit (0); } # ##################################### # Kill all evolution processes # ##################################### # Note: evolution-data-server process has to be # stopped or none of this will work. my $pid; my @ps; my @tmp; open(PS, "ps aux | grep evolution |"); @ps = ; close (PS); for (my $n=0;$n<=$#ps;$n++) { if ($ps[$n] !~ m/grep/) { chomp $ps[$n]; @tmp = split " ",$ps[$n]; $pid = $tmp[1]; system ("kill $pid"); } } # all evolution processes are now dead ## ##################################### ## Read in the list of new email addreses ## ##################################### if ($source eq "file") { # # Read in the list of new email addreses from file # open (FILE, "<$inputFile"); while () { $newEmails[$numNew] = $_; chomp $newEmails[$numNew]; $numNew++; } print "Adding $numNew addresses.\n"; } elsif ($source eq "web") { # # Read in the list of new email addreses from the web # print "Getting addresses from the server...\n"; $numNew = 0; my $readadrs = 0; my @temp; system("wget -o $wgetlog --http-user=$username --http-passwd=$password $web"); open(FILE,") { print "."; if (length($_) < 3) { $readadrs = 0; } if ($readadrs == 1 && length($_) > 7) { @temp = split ("<",$_); $newEmails[$numNew] = $temp[0]; $numNew++; } if ($_ =~ m/INCLUDE/) { $readadrs = 1; } } print "\n"; close FILE; print "Got $numNew new addresses.\n"; } else { print "No Email sources defined.\n"; exit (1); } # ##################################### # Loop through addressbook.db and search for the right key # ##################################### print "searching for $list\n"; tie %h, "BerkeleyDB::Hash",-Filename=>"$abook",-Flags=>DB_CREATE or die "Tryig"; while (($k,$v)= each %h) { if ($v =~ m/$list/ ) { print "Found $list in $k\n"; $hashKey = $k; } } untie %h; # ##################################### # Replace the record with the new list # ##################################### # get the record print "Opening Record $hashKey\n"; $db = new BerkeleyDB::Hash(-Filename=>"$abook", -Flags=>DB_CREATE ) or die "Can't open file: $!"; $db->db_get($hashKey, $var); # put it in an array @listItems = split "\n", $var; # this is all fluff for printing stats my $numOld = 0; for (my $n=0;$n<$#listItems;$n++) { if ($listItems[$n] =~ m/$str1/) { $numOld++; } } print "Deleting $numOld addresses.\n"; #clear out the variable $var = ""; # Add in the first 7 fields. for (my $n=0; $n <= 7; $n++) { chop $listItems[$n]; $var .= $listItems[$n].$lineBreak; } # Save the last entry for later $end = $listItems[$#listItems]; # Add in the new addresses with the correct length lines in the DB format # format is $str1."\"$email\"".$str2.$str3.$email."\n" # 75 chars then a line break then the rest print "Adding new addresses "; for (my $n=0; $n<$numNew; $n++) { print "."; my $tmpStr1 = $str1."\"".$newEmails[$n]."\"".$str2.$newEmails[$n]; my $tmpStr2 = substr($tmpStr1,0,75); my $tmpStr3 = substr($tmpStr1,75); $var .= $tmpStr2.$lineBreak." ".$tmpStr3.$lineBreak; } # Add on the ending $var .= $end; # Write the new data $db->db_put($hashKey, $var); # Close the DB file $db->db_close(); print "\n"; if ($startEvoAgain == 1) { print "\nStarting evolution\n"; system("evolution &"); } # clean up or wget will make lots of files and I won't get the # right one to play with!! if ($source eq "web") { my @webFile = split /\//, $web; system ("rm $webFile[$#webFile]"); } exit(0);