Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Created March 3, 2013 18:54
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save maxandersen/5077622 to your computer and use it in GitHub Desktop.
somewhat complete archiving mails per quarter
archiveblocks = {
Q1 = { '01-Jan', '31-Mar' },
Q2 = { '01-Apr', '30-Jun' },
Q3 = { '01-Jul', '30-Sep' },
Q4 = { '01-Oct', '31-Dec' }
}
function archive(startyear, endyear, mailbox, account)
if (mailbox=='INBOX/_inbox') then return end
print('Archiving ' .. mailbox .. ' from ' .. startyear .. ' ' .. endyear)
continue = true
for year=endyear, startyear, -1 do
if (continue) then
matches = 0
for block, range in pairs(archiveblocks) do
if(not (year==2012 and (block == 'Q3' or block == 'Q4'))) then
since = range[1] .. '-'..year
before = range[2]..'-'..year
results = account[mailbox]:send_query('SINCE ' .. since .. ' BEFORE ' .. before) + account[mailbox]:send_query('ON ' .. before)
matches = matches + # results
target = 'archive/' .. mailbox .. '/' .. year .. '/' .. block
print(block .. " Found " .. # results .. ' move to ' .. target)
results:move_messages(account[target])
else
print("Skipping move - too recent")
end
end
if(matches==0) then
continue = false
end
end
end
end
function archiveNonEmptyMailboxes()
boxes = redhat:list_all('INBOX','*')
for _,box in pairs(boxes) do
print (box)
exist, unread, unseen, uidnext = redhat[box]:check_status()
if(exist~=0) then
archive(2006,2012,box,redhat)
end
end
end
archiveNonEmptyMailboxes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment