Koozali.org: home of the SME Server

SME / IMAP Integration with M$ Outlook (deletions) - quick n dirty solution

Offline tjbirkett

  • 3
  • +0/-0
Ok, I got fed up of our clients who would not move to any other mail client from MS Outlook (even though their servers / infrastructure were been migrated to SME Server and other Open Source alternatives) moaning because of the way that MS Outlook handles deleted IMAP messages and also getting annoyed / worried (we back everything up!) that they've lost there items once the inbox is purged.

I created this bash script that I run in a screen to move the messages marked for deletion to the users "Trash" folder and unmark the messages (remove the "T" from the filename). I Know it's a bit of a dirty way to do it (nobody likes infinite loops). I have it printing how many messages are moved each time it moves one (or over 15000 the first time I ran it at a clients after migrating from Exchange).

Code: [Select]
#!/bin/bash
INTERVAL=1
while true
do
find /home/e-smith/files/users/*/Maildir/ -type f -name "*T*" -exec echo {} >> /tmp/deleted.imap \;
        exec 3<&0
        exec 0</tmp/deleted.imap
        i=0
                while read line
                do
                        filepath=${line%/Maildir/*}
                        filename=${line##*/}; newfilename=${filename//T/}
                        moveto="$filepath/Maildir/.Trash/cur/$newfilename"
                        mv "$line" "$moveto"
                        i=$(($i+1))
                done
        exec 0<&3
        if [ $i -gt 0 ]
        then
        echo "$i messages moved"
        fi
        rm -f /tmp/deleted.imap
        sleep $INTERVAL
done


Just thought others may find it useful for there M$ loving clients...
« Last Edit: August 03, 2010, 04:03:56 PM by tjbirkett »

Offline larieu

  • *****
  • 214
  • +0/-0
I think with minor modifications this should also very helpfully for deleting "old" trash messages

I find very often trashes with over 75% quota of user sapce
especially users which access email only by mobile phone (they delete the message but it will not care about trash)

it will be another "dirty" way to keep clean your server

for the moment I use xpunge plugin on thunderbird clients with some settings on several (trash, junkmail, ...) folders as:
- delete mesages older than xx days,

but this mean
1. user use thunderbird (mobile users - not so often)
2. you need access to user account on his computer (not so easy in some cases)
3. users be quite "kind" to don't override your settings in their account
if everybody's life around you is better, probably yours will be better
just try to improve their life

Offline tjbirkett

  • 3
  • +0/-0
I thought about inserting this line above the first find command to remove items deleted in trash otherwise they just re-appear
Code: [Select]
find /home/e-smith/files/users/*/Maildir/.Trash/ -type f -name "*T*" -exec rm -f {} \;

I think instead I will move it to some sort of archives folder along with emails older than 3 months and tar / clean those folders on a weekly basis.

SODS law that I insert to the line to rm them and the client suddenly needs the emails for a court case or complaint...


 
« Last Edit: August 06, 2010, 04:50:47 PM by tjbirkett »

Offline tjbirkett

  • 3
  • +0/-0
Updated Script.... When users drag and drop to another folder sometimes the script worked too quick and moved it to the trash before outlook finished moving it to the folder??? also using arrays instead of writing to a file (save disk I/O) had to "massage" the find / array output to replace spaces in folder names with _ and then back again otherwise the file path is split into 2.

Code: [Select]
#!/bin/bash
INTERVAL=3
oldIFS=$IFS
while true
do
        #Create one list of files replacing spaces with _
        Array1=($(find /home/e-smith/files/users/*/Maildir/ -type f -name "*T*" | tr ' ' '_' | tr '\n' ' '))
        sleep 3
        #Create another list after 3 seconds
        Array2=($(find /home/e-smith/files/users/*/Maildir/ -type f -name "*T*" | tr ' ' '_' | tr '\n' ' '))
        oldIFS=$IFS
        IFS=$'\n\t'
        #Compare the list and discard anything unique to the first array (comm)
        Array3=($(comm -1 <(echo ${Array1[*]}) <(echo ${Array2[*]})))
        IFS=$oldIFS
        i=0
        for MSG in ${Array3[@]}
        do
                filepath=${MSG%/Maildir/*}
                filename=${MSG##*/}
                newfilename=${filename//T/}
                moveto="$filepath/Maildir/.Trash/cur/$newfilename"
                MSG=$(echo $MSG | sed 's/_/\ /g')
                mv "$MSG" "$moveto"
                i=$(($i+1))
        done
        if [ $i -gt 0 ]
                then
                echo "$i messages moved to trash" >> /var/log/imaptrasher
        fi
        Array1=()
        Array2=()
        Array3=()
done