X-Status processing for reconstruct

Rayed Alrashed rayed at riy-01-s5.saudi.net.sa
Tue Oct 14 06:25:33 EDT 2003


Hi,

This tool will scan IMAP folders for Status, and X-Status for status 
information and apply them on IMAP using store function.

Usage: fix_status.py username password

It took 1.5 second for ~1000 emails, hope it will be usefull.

- Rayed




#!/usr/local/bin/python -u

"""
fix_status.py
This tool will scan IMAP folders for Status, and X-Status
for status information and apply them on IMAP using store function

Rayed Alrashed  13 Oct 2003
rayed at saudi.net.sa

"""



import imaplib
import sys
from string import *




#------------------------------------------------------------
def fix_folder(con,folder):
    i =0
    list_r = []
    list_o = []
    list_d = []
    list_f = []
    list_a = []
    list_t = []

    type,data = con.select(folder)
    if type!='OK': return

    nmsg = atoi(data[0])
    type,data = con.fetch( '1:%s' % nmsg, '(BODY[HEADER.FIELDS (status 
x-status)])' )

    # scan all the meseages in folder
    print "Scanning..."
    for msg in data:
        try:
           s = msg[1]
           i +=1
           if find(str,'R')>-1: list_r.append(str(i))
           if find(str,'O')>-1: list_o.append(str(i))
           if find(str,'D')>-1: list_d.append(str(i))
           if find(str,'F')>-1: list_f.append(str(i))
           if find(str,'A')>-1: list_a.append(str(i))
           if find(str,'T')>-1: list_t.append(str(i))
        except: pass


    # apply changes using IMAP store
    print "Applying..."

    if len(list_r)>0: con.store( join(list_r,','), '+FLAGS', '\Seen')
    if len(list_o)>0: con.store( join(list_o,','), '+FLAGS', '\Seen')
    if len(list_d)>0: con.store( join(list_d,','), '+FLAGS', '\Deleted')
    if len(list_f)>0: con.store( join(list_f,','), '+FLAGS', '\Flagged')
    if len(list_a)>0: con.store( join(list_a,','), '+FLAGS', '\Answered')
    if len(list_t)>0: con.store( join(list_t,','), '+FLAGS', '\Draft')

    con.close()






#------------------------------------------------------------
try:
    username = sys.argv[1]
    password = sys.argv[2]
except:
    print "Usage: %s username password" % (sys.argv[0])
    sys.exit(1)




con = imaplib.IMAP4()
con.login(username, password)


for i in con.lsub()[1]:
    fix_folder(con, i[8:-1])


con.logout()






More information about the Info-cyrus mailing list