#!/usr/bin/perl -w ### Modules use strict; use Mail::IMAPClient; use Mail::SpamAssassin; use Proc::Daemon; use Term::ReadKey; use Getopt::Long; ### Variables my ($spamtest, $imap, $status); my ($flag, $password, $msg, $msgtxt); my ($opt_user, $opt_host, $opt_help, $opt_interval); my @messages; ### Initial values my $inbox = "INBOX"; # Name of mailbox to check for SPAM in my $junk = "INBOX/SPAM"; # Name of mailbox to move SPAM to ### Main program print <<_EoT_; IMAP interface to SpamAssassin V1.0 (c)2003 Alexander J. Trentini (thanks to anithri of perlmonks.org) _EoT_ # Check commandline parameters GetOptions("user=s" => \$opt_user, # IMAP user name "host=s" => \$opt_host, # IMAP host name or IP addres "interval=s" => \$opt_interval, # Check interval "help" => \$opt_help); # Usage text # Help text? if (defined($opt_help)) { print <<_EoT_; Usage: $0 [--user --host --interval] [--help] If no parameters are given on the commandline, the script enters interactive mode. Parameters: --help Prints this usage text --user IMAP user name to use --host IMAP host name or IP address to use --interval Check interval Note: The password will ALWAYS be read via interactive mode, due to security reasons! _EoT_ exit(1); } # Interactive mode? unless ($opt_host and $opt_user and $opt_interval) { print("Interactive mode.\n\n"); print("Check interval (seconds): "); $opt_interval = <>; chomp($opt_interval); print("IMAP host name or IP address: "); $opt_host = <>; chomp($opt_host); print("IMAP user name: "); $opt_user = <>; chomp($opt_user); } # Get password (always interactive) print "Password for $opt_user: "; ReadMode(2); $password = <>; chomp($password); ReadMode(0); print("\n"); # Interval ok? #TODO if ($opt_interval < 60) { die("Are you crazy? I'm just a little CELERON (60s interval min.)!\n"); } # Get rid of zombie problems $SIG{CHLD} = 'IGNORE'; # Run as daemon Proc::Daemon::Init; # Initialize SpamAssassin $spamtest = Mail::SpamAssassin->new(); # Loop endlessly while (1) { # Connect to IMAP server $imap = Mail::IMAPClient->new(User => $opt_user, Password => $password, Server => $opt_host, UID => 1, Debug => 0, Peek => 1) or die("Couldn't connect" . " to $opt_host using $opt_user (Incorrect password?).\n"); $imap->select($inbox); # Create $junk folder if not existant unless ($imap->exists($junk)) { $imap->create($junk); } # Omit already checked messages and messages within # the IXOS mail archive (compressed, would result in # garbage output) #TODO @messages = $imap->search('NOT HEADER X-Spam-Checker-Version "SpamAssassin" NOT SUBJECT "IXOS"'); # Traverse messages foreach $msg (@messages) { # Only check if mail is smaller than 150KB (performance) next if ($imap->size($msg) > 153600); # Get message and pass to SpamAssassin $msgtxt = $imap->message_string($msg); $status = $spamtest->check_message_text($msgtxt); $status->rewrite_mail(); $msgtxt = $status->get_full_message_as_text(); if ($status->is_spam()) { $flag = $imap->append_string($junk, $msgtxt); } else { $flag = $imap->append_string($inbox, $msgtxt); } # If successfully created, delete original from inbox $imap->delete_message($msg) if ($flag); } # Close IMAP session $imap->close(); sleep $opt_interval; }