Add addr/name-only variants of 'To' and 'From'

This commit is contained in:
Jaidyn Ann 2023-01-24 19:26:13 -06:00
parent 8829a1e65b
commit 289de6576e

View File

@ -16,6 +16,7 @@
# #
import os import os
import re
from sys import argv from sys import argv
import urllib import urllib
import email.parser import email.parser
@ -30,20 +31,41 @@ class Maildir(GObject.GObject,
Caja.InfoProvider): Caja.InfoProvider):
# Pass our columns to Caja
def get_columns(self): def get_columns(self):
return ( return (
Caja.Column(
name="Maildir::from_column",
attribute="from",
label="From",
description=""
),
Caja.Column( Caja.Column(
name="Maildir::subject_column", name="Maildir::subject_column",
attribute="subject", attribute="subject",
label="Subject", label="Subject",
description="" description=""
), ),
Caja.Column(
name="Maildir::sent_column",
attribute="sent",
label="Sent",
description=""
),
Caja.Column(
name="Maildir::from_column",
attribute="from",
label="From",
description=""
),
Caja.Column(
name="Maildir::from_addr_column",
attribute="from_addr",
label="From (Addr)",
description=""
),
Caja.Column(
name="Maildir::from_name_column",
attribute="from_name",
label="From (Name)",
description=""
),
Caja.Column( Caja.Column(
name="Maildir::to_column", name="Maildir::to_column",
attribute="to", attribute="to",
@ -51,14 +73,21 @@ class Maildir(GObject.GObject,
description="" description=""
), ),
Caja.Column( Caja.Column(
name="Maildir::sent_column", name="Maildir::to_addr_column",
attribute="sent", attribute="to_addr",
label="Sent", label="To (Addr)",
description=""
),
Caja.Column(
name="Maildir::to_name_column",
attribute="to_name",
label="To (Name)",
description="" description=""
) )
) )
# Implants e-mail-related attributes into a file's columns
def update_file_info(self, file): def update_file_info(self, file):
if not file.is_mime_type("message/rfc822"): if not file.is_mime_type("message/rfc822"):
return return
@ -66,10 +95,41 @@ class Maildir(GObject.GObject,
filename = file.get_uri()[7:] filename = file.get_uri()[7:]
message = email.message_from_file(open(filename)) message = email.message_from_file(open(filename))
file.add_string_attribute('to', message.get("To")) sender = message.get("From")
file.add_string_attribute('from', message.get("From")) to = message.get("To")
file.add_string_attribute('subject', message.get("Subject")) subject = message.get("Subject")
date = parser.parse(message.get("Date")) if to:
if date: file.add_string_attribute('to', to)
file.add_string_attribute('sent', date.strftime("%Y-%m-%d %H:%M:%S %z")) file.add_string_attribute('to_addr', self.from_header_addr(to))
file.add_string_attribute('to_name', self.from_header_name(to))
if sender:
file.add_string_attribute('from', sender)
file.add_string_attribute('from_addr', self.from_header_addr(sender))
file.add_string_attribute('from_name', self.from_header_name(sender))
if subject:
file.add_string_attribute('subject', message.get("Subject"))
try:
date = parser.parse(message.get("Date"))
if date:
file.add_string_attribute('sent', date.strftime("%Y-%m-%d %H:%M:%S %z"))
except:
print("The date couldn't be parsed — that's alright, we didn't need it anyway.")
# Helper function, for parsing e-mail addresses of to/from headers
def from_header_addr(self, str):
if re.search(r'<', str):
return re.sub('[<|>]', '',
re.search(r'<.*@.*>', str).group(0))
return str
# Helper function, for parsing names of to/from headers
def from_header_name(self, str):
if re.search(r'<', str):
return re.sub('<.*', '', str)
return ''