本篇全文出自:鳥毅的Blog


最近由於某些新進同仁都使用WebMail,但又不砍信,造成Mail Server繁忙,只好請他們自行砍信/移信。有些皮比較癢公務繁忙的同仁沒空處理,只好抽空寫支小程式處理。

由 於Openwebmail的限制,不能用maildir,只能維持使用mailbox,這是系統loading高的主因。由於許多原因以及沒有功能更齊全 的webmail前提下,只能暫時如此處理。本來要用比較熟悉的Java寫,但是搜尋之下並沒有功能完整的library,而Python內建就支援 mailbox處理,在缺乏其他支援/資源的情況下也只能硬著頭皮寫。因為我比較懶,裡頭有用到一個date parser,使用前請先安裝。

這裡的環境針對FreeBSD + Postfix+Openwebmail,sendmail/qmail應該也適用,我在Ubuntu開發,所以在Linux"應該"也能正常執行。

完整程式如下:

#!/usr/bin/env python                                             
# coding: utf-8                                                   
                                                                  
import datetime                                                   
import mailbox                                                    
import email.Errors                                               
import os                                                         
from stat import ST_SIZE                                          
#由於python沒有內建的Date Parser,所以找了一個dateutil parser, http
#FreeBSD ports裡在 /usr/ports/devel/py-dateutil                   
from dateutil.parser import parse                                 
                                                                  
def mailbox_apart(inputfile, outputfile, splitdate):              
    inbox = mailbox.mbox(inputfile);                              
    mboxNew = mailbox.mbox(outputfile);                           
                                                                  
    for key, message in inbox.iteritems():                        
        timex = parse(message['date']);                           
        timex = datetime.date(timex.year, timex.month, timex.day);
        if cmp(timex, splitdate) < 0:                             
            mboxNew.lock();                                       
            mboxNew.add(message);                                 
            mboxNew.flush();                                      
            mboxNew.unlock();                                     
            # Remove original message                             
            inbox.lock();                                         
            inbox.discard(key);                                   
            inbox.flush();                                        
            inbox.unlock();                                       
    inbox.close();                                                
    mboxNew.close();                                              
    return;                                                       
                                                                  
def read_passwd_and_do_it(passwordfile, daysago):                 
    f=open(passwordfile, 'r');                                    
    for line in f:                                                
        line=line.lstrip();                                       
        if not line.startswith('#'):                              
            x=line.split(':');                                    
            userid=x[0];                                          
            homedir = x[5];                                       
            if homedir.startswith('/home'):                       
                inxfile = '/var/mail/'+userid;                    
                outxfile = homedir +'/mail/saved-messages'        
                try:                                              
                    size = os.stat(inxfile)[ST_SIZE];             
                    if size > critial:                            
                        mailbox_apart(inxfile, outxfile, daysago);
                except (IOError, OSError), e:                     
                    pass;                                         
                except mailbox.ExternalClashError, e:             
                    pass;                                         
                except Exception, e:                              
                    print e;                                      
    f.close();                                                    
    return;                                                       
                                                                  
#主程式                                                           
#超過多大的檔案才處理,此處是500M                                 
critial = 500000000;                                              
#超過幾天的mail才移                                               
removedays = 30;                                                  
#帳號檔                                                           
passwdfile = '/etc/passwd';                                       
today = datetime.date.today();                                    
theDate = datetime.date(today.year, today.month, today.day);      
aMonthAgo = theDate - datetime.timedelta(days=removedays);        
read_passwd_and_do_it(passwdfile, aMonthAg                        

創作者介紹

胖虎的祕密基地

idobest 發表在 痞客邦 PIXNET 留言(0) 人氣()