81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
import mailbox
|
|
import sys
|
|
from imap_tools import A, MailBox
|
|
from datetime import date
|
|
import os
|
|
|
|
MDloc = "/home/mysaa/Globox"
|
|
MD = mailbox.Maildir(MDloc)
|
|
# firstDate = '"1-Sept-2025"'
|
|
firstDate = date(day=1, month=9, year=2025)
|
|
|
|
|
|
def parseFolder(folderString):
|
|
if folderString[0] == 34: # Double quote
|
|
folderString = folderString[1:-1]
|
|
folderz = (
|
|
folderString.replace(b"&", b"+")
|
|
.replace(b",", b"/")
|
|
.decode("utf-7")
|
|
.split(".")
|
|
)
|
|
return folderz
|
|
|
|
|
|
def sbdify(name):
|
|
if name == "INBOX":
|
|
return "Inbox.sbd" # I have no idea why thunderbird uncapitalizes this
|
|
return name + ".sbd"
|
|
|
|
|
|
password = sys.argv[1].strip()
|
|
with MailBox("imap.hadoly.fr", 993).login("mysaa@hadoly.fr", password) as M:
|
|
|
|
folders = []
|
|
folderzz = []
|
|
folderToFolderz = dict()
|
|
for f in M.folder.list():
|
|
folders.append(f)
|
|
print(f)
|
|
|
|
mdfolders = {"": MD}
|
|
folders = sorted(folders, key=lambda f: f.name.count(f.delim))
|
|
for f in folders:
|
|
print("Checking folder", f)
|
|
ff = f.name.split(f.delim)
|
|
folderPath = os.path.join(MDloc, *[sbdify(s) for s in ff[:-1]], ff[-1])
|
|
if os.path.exists(folderPath):
|
|
print("Already exists")
|
|
else:
|
|
print("Creating the folder", folderPath)
|
|
os.makedirs(folderPath)
|
|
os.mkdir(os.path.join(folderPath, "tmp"))
|
|
os.mkdir(os.path.join(folderPath, "cur"))
|
|
mdf = mailbox.Maildir(folderPath, create=True)
|
|
mdfolders[f.name] = mdf
|
|
|
|
print("Now filling the emails")
|
|
for f in folders:
|
|
mdfolder = mdfolders[f.name]
|
|
M.folder.set(f.name)
|
|
uids = M.uids(
|
|
A(date_lt=firstDate, seen=True, flagged=False, draft=False)
|
|
)
|
|
print(
|
|
"Moving",
|
|
len(uids),
|
|
"from folder",
|
|
f.name,
|
|
"out of",
|
|
len(M.uids(A(all=True))),
|
|
)
|
|
for u in uids:
|
|
msg = next(M.fetch(A(uid=u)))
|
|
msg = mailbox.MaildirMessage(msg.obj.as_bytes())
|
|
msg.set_subdir("cur")
|
|
msg.set_flags("S")
|
|
mdfolder.add(msg)
|
|
|
|
# Then remove the message
|
|
M.delete(u)
|