tb T2: attach checker
Browse files
tier2_thunderbird/_attach/check_attachments.py
CHANGED
|
@@ -1,32 +1,59 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""Report whether a Thunderbird draft (matched by subject) carries a named
|
| 3 |
attachment. Usage: check_attachments.py <subject-substring> <attachment-filename>.
|
| 4 |
Prints "Attachment added!" if found, else "Attachment not found!".
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import sys, os, glob, mailbox
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def main():
|
| 9 |
if len(sys.argv) < 3:
|
| 10 |
print("Attachment not found!"); return
|
| 11 |
subject, fname = sys.argv[1], sys.argv[2]
|
| 12 |
home = os.path.expanduser("~")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
for path in
|
| 16 |
-
if not os.path.isfile(path):
|
| 17 |
-
continue
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
except Exception:
|
| 21 |
continue
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
if
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
print("Attachment not found!")
|
| 31 |
|
| 32 |
main()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Report whether a saved Thunderbird draft (matched by subject) carries a named
|
| 3 |
attachment. Usage: check_attachments.py <subject-substring> <attachment-filename>.
|
| 4 |
Prints "Attachment added!" if found, else "Attachment not found!".
|
| 5 |
+
|
| 6 |
+
Robust: scans EVERY mbox file under the profile's Mail/ and ImapMail/ trees
|
| 7 |
+
(not just a "Drafts" folder, since the draft may land in Local Folders/Drafts or
|
| 8 |
+
the account Drafts depending on the identity's draft_folder). Also matches the
|
| 9 |
+
attachment by a raw filename scan of the message body as a fallback (Thunderbird
|
| 10 |
+
writes `filename="q3-report.pdf"` and/or `name="q3-report.pdf"` in the MIME part).
|
| 11 |
+
Emits diagnostics so a run can be debugged from the captured stdout."""
|
| 12 |
import sys, os, glob, mailbox
|
| 13 |
|
| 14 |
+
def mboxes(home):
|
| 15 |
+
roots = glob.glob(os.path.join(home, ".thunderbird", "*", "Mail")) + \
|
| 16 |
+
glob.glob(os.path.join(home, ".thunderbird", "*", "ImapMail"))
|
| 17 |
+
out = []
|
| 18 |
+
for r in roots:
|
| 19 |
+
for dp, _dirs, files in os.walk(r):
|
| 20 |
+
for f in files:
|
| 21 |
+
if f.endswith(".msf") or f.endswith(".dat"):
|
| 22 |
+
continue
|
| 23 |
+
out.append(os.path.join(dp, f))
|
| 24 |
+
return out
|
| 25 |
+
|
| 26 |
def main():
|
| 27 |
if len(sys.argv) < 3:
|
| 28 |
print("Attachment not found!"); return
|
| 29 |
subject, fname = sys.argv[1], sys.argv[2]
|
| 30 |
home = os.path.expanduser("~")
|
| 31 |
+
files = mboxes(home)
|
| 32 |
+
print("DIAG scanned {} mbox files".format(len(files)))
|
| 33 |
+
for path in files:
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
raw = open(path, "r", encoding="utf-8", errors="replace").read()
|
| 36 |
except Exception:
|
| 37 |
continue
|
| 38 |
+
if subject.lower() not in raw.lower():
|
| 39 |
+
continue
|
| 40 |
+
print("DIAG subject found in", os.path.basename(path))
|
| 41 |
+
# structured check
|
| 42 |
+
try:
|
| 43 |
+
for msg in mailbox.mbox(path):
|
| 44 |
+
if subject.lower() not in (msg.get("Subject", "") or "").lower():
|
| 45 |
+
continue
|
| 46 |
+
for part in msg.walk():
|
| 47 |
+
disp = str(part.get("Content-Disposition") or "")
|
| 48 |
+
pf = part.get_filename() or ""
|
| 49 |
+
if "attachment" in disp.lower() and fname.lower() in pf.lower():
|
| 50 |
+
print("Attachment added!"); return
|
| 51 |
+
except Exception:
|
| 52 |
+
pass
|
| 53 |
+
# raw fallback: the MIME part header names the file
|
| 54 |
+
if ('filename="%s"' % fname).lower() in raw.lower() or \
|
| 55 |
+
('name="%s"' % fname).lower() in raw.lower():
|
| 56 |
+
print("Attachment added!"); return
|
| 57 |
print("Attachment not found!")
|
| 58 |
|
| 59 |
main()
|