LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2003, 09:29 PM   #1
ctbeal
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: 0
Automated IMAP Expunge


I am sysadmin of a small mail server with around 15 users, about half of which maintain Imap mailboxes (vice pop). I am running RH8, with the imapd right out of the box. My users are uneducated/uninterested in expunging their email instead of just deleting it, and a lot of server space is taken up with deleted email that is never expunged. I would like to execute a script as a cron task to find all IMAP mail boxes and expunge them. I have written the two scripts below. The first is a script to generate the list of mailboxes and then call the second. The second is a perl script that actually expunges the boxes.
This solution does not work because I cannot log into the Imap server as root, and I don't know how to extract each user's password to log on as each user.
I need to know how to run these scripts so I don't need to log into the server, or how to extract users' passwords to log into the server, or how some other way to have root (as a cron task) be able to expunge users' mailboxes.
Thanks.
Travis Beal (ashcan90@hotmail.com)
========================================
SCRIPT 1:
#!/bin/bash
#broadcast stop
echo 'Starting purge of folders.'
date

#stop internet services
/etc/init.d/httpd stop
/etc/init.d/sendmail stop
/etc/init.d/xinetd stop

#generate list of IMAP mailboxes
ls /home/*/.mailboxlist > /root/listofmailboxlists

#Perl purge script
/root/purge_folders_perl

#start services
/etc/init.d/xinetd start
/etc/init.d/sendmail start
/etc/init.d/httpd start

#remove the list
rm -rf /root/listofmailboxlists

#broadcast
echo 'Purge of folders complete.'
date


========================================
SCRIPT 2:
#!/usr/bin/perl

#get the IMAP tools
use Mail::IMAPClient;
print "Entering Perl section.....\n";

#read only open this list of mailbox lists
open (LISTOFMAILBOXLISTS, "<listofmailboxlists");

#go through file
#while(<LISTOFMAILBOXLISTS>){
#load next line in file
chop;
#parse out next data file
($mailboxlisttoscan) = split (/\n/, $_);
#debug print "Next is $mailboxlisttoscan.\n";
#get absolute address prefix
($file_prefix,$junk) = split (/\/.mailbox/,$mailboxlisttoscan);
#parse out user_name
($trash, $user_name) = split (/ome\//,$file_prefix);

# (returns a new, authenticated Mail::IMAPClient object)
$host = "127.0.0.1";
$id = "$user_name";
$pass = "?????????";
$imap = Mail::IMAPClient->new(
Server => $host,
User => $id,
Password=> $pass,
) or die "Cannot connect to $host as $id: $@";

#read only open this mailbox list
open (MAILBOXLIST, "<$mailboxlisttoscan");
#go through this list
while(<MAILBOXLIST>){
#load next line in file
chop;
#parse out based on CSV
($mailboxtopurge) = split (/\n/, $_);
#debug print "Purging $mailboxtopurge for user $user_name.\n";
$imap->expunge($file_prefix/$mailboxtopurge) or die "Could not expunge: $@\n";
} #end of mailboxlists while
close (MAILBOXLIST);
} #end of listofmailboxlists while
close (LISTOFMAILBOXLISTS);

print "Exiting Perl section.\n";

Last edited by ctbeal; 09-18-2003 at 05:42 AM.
 
Old 09-26-2003, 10:25 AM   #2
ctbeal
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Original Poster
Rep: Reputation: 0
I solved this problem this way.
1. Create user postmaster.
2. Create group mailadm. Anyone in this group can work with user's IMAP folders.
3. Add postmaster to group mailadm.
4. Run script 1 as cron job. Script 1 shuts down mail and web services, generates a list of mailbox lists to process, calls the processing script, deletes the list of mailbox lists, and then restarts all services. Script 2 expunges all messages marked for deletion from IMAP folders and the regular /var/spool/mail inbox. It also moves messages in the Sent folder more than N days old to the trash.

As you may expect, there is no guarantee that this will work for anyone else. It works on my system, but it should be used on other systems at one's own peril. It may cause data loss, mail corruption, and a plague of locusts o'er the land. I accept no responsibility for this script other than use on my own system.

SCRIPT 1
--------------
#!/bin/bash
#This script, run as a cron job, will expurge messages marked for deletion from IMAP mailboxes.
#The Perl script below does the actual expurging.
#Version 1.0 26 Sept 03
#broadcast stop
echo 'Starting purge of folders.'
date

#stop internet services
/etc/init.d/httpd stop
/etc/init.d/sendmail stop

#generate list of IMAP mailboxes
ls /home/*/.mailboxlist > /root/listofmailboxlists

#Perl purge
/root/purge_folders_perl

#start services
/etc/init.d/sendmail start
/etc/init.d/httpd start

#remove the list
rm -rf /root/listofmailboxlists

#broadcast
echo 'Purge of folders complete.'
date


************************
SCRIPT 2
------------------
#!/usr/bin/perl
#get the Perl tools
use Mail::IMAPClient;
use Date::Manip;

#this is the perl script that purges the folders. Works with purge_imap_folders
print "Entering Perl section.....\n";

#variables
$AGE = 60; #how many days a message may stay in Sent

#read only open this list of mailbox lists
open (LISTOFMAILBOXLISTS, "<listofmailboxlists");

#go through file
while(<LISTOFMAILBOXLISTS>){
#load next line in file
chop;
#parse out next data file
($mailboxlisttoscan) = $_;
($file_prefix,$junk) = split (/\/.mailbox/,$mailboxlisttoscan);
($trash, $user_name) = split (/ome\//,$file_prefix);
# (returns a new, authenticated Mail::IMAPClient object)
$host = "localhost";
$id = "$user_name*postmaster"; #logging in with postmaster for admin
$pass = "XXXXXXXXXXXXXXX"; #postmasters password
$imap = Mail::IMAPClient->new(
Server => $host,
User => $id,
Password=> $pass,
) or die "Cannot connect to $host as $id: $@";

#expunge the normal mail spool
$imap->expunge("/var/spool/mail/$user_name") or die "Could not expunge: $@\n";

#read only open this mailbox lists
open (MAILBOXLIST, "<$mailboxlisttoscan");
#go through this list
while(<MAILBOXLIST>){
#load next line in file
chop;
$mailboxtopurge = $_;
$folder_to_expunge = "$file_prefix/$mailboxtopurge";
#check to see if this is Sent
$sentcheck = index($mailboxtopurge,"Sent");
$truthcheck = ($sentcheck != -1); #kluge, but it works
if ($truthcheck != 1) {
#if not Sent folder, expurge normally
#determine if the file exists
if (-e "$folder_to_expunge"){
#if it exists, expunge it
$imap->expunge($folder_to_expunge)
} #end of exists check if
else {print "$folder_to_expunge does not exist.\n";}
} #end of exists check else
else {
#if the folder is Sent, then clean it out
#determine where the trash is
#chop Sent off folder location
($trash_location,$junk) = split(/\/Sent/,$folder_to_expunge);
$trash_location = "$trash_location/Trash";

#return message ID numbers
$imap->Uid(1);
#select this folder
$imap->select("$folder_to_expunge") or die "cannot select the Sent folder for $user_name: $@\n";
#calculate cutoff date
#figure out today in seconds
$right_now = time;
#subtract the age difference in seconds
$cutoff_date = $right_now - ($AGE * 24 * 3600);
#fetch all messages before that time
$Rfc2060_date = $imap->Rfc2060_date($cutoff_date); #convert to RFC2060 format
@message_list = $imap->search("before",$Rfc2060_date) or warn "No messages found before $Rfc2060_date.\n";
#how many messages are in the list
$number_of_messages = @message_list;
#pack this list to the trash
for($counter = 0; $counter < $number_of_messages; $counter++)
{
$msg_id = @message_list[$counter];
my $yes = $imap->move($trash_location,$msg_id);
#see if the move was successful
if ($yes) {
#If we successfully moved it, then delete it from Sent
$imap->delete_message($msg);
} #end of if to see if we moved
} #end of for loop
} #end of else
} #end of mailboxlists while
close (MAILBOXLIST);
#close the connection
$imap->disconnect;
} #end of listofmailboxlists while
close (LISTOFMAILBOXLISTS);

print "Exiting Perl section.\n";
 
Old 01-05-2004, 08:28 AM   #3
carstenson
Member
 
Registered: Oct 2003
Location: Austin, TX
Distribution: RH 7.3, RH 9.0, FC4, Arch
Posts: 36

Rep: Reputation: 15
Thanks a lot, ct. I had this exact same issue when I switched to an imap server. I appreciate you sharing the time that you spent on this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
qmail-ldap + courier-imap woes (imap capability) syahid Linux - General 1 10-12-2005 12:03 AM
IMAP and Cyrus-IMAP on Slackware 10 cyberjames Slackware 2 01-10-2005 01:07 AM
IMAP works but IMAP TLS doesn't under PHP (Horde IMP) theparadigm Linux - Software 0 11-17-2003 12:35 AM
UW IMAP problem downloading IMAP folders nuzzy Linux - Software 1 04-17-2003 04:25 AM
Evolution wont expunge! graystarr Linux - Software 2 02-22-2003 07:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration