Skip to content

Commit

Permalink
[notmuch] initial tagging (mostly via afew)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakelley committed Mar 27, 2023
1 parent 1f30da8 commit 23cdc93
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions .config/afew/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[HeyFilter]
67 changes: 67 additions & 0 deletions .config/afew/hey-filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

from pathlib import Path
import re

from afew.filters.BaseFilter import Filter
from afew.FilterRegistry import register_filter
from afew.utils import get_sender
from afew.NotmuchSettings import get_notmuch_new_tags, get_notmuch_new_query

import orgparse
import pandas

ORG_CONTACTS_PATH = Path("~/.local/share/notes/contacts.org").expanduser()
# from this SE answer: https://stackoverflow.com/a/201378/5054505
EMAIL_REGEX = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"
NAMED_EMAIL_REGEX = f"(?P<address>{EMAIL_REGEX})"


# TODO make method that generates a sieve filter for this logic
@register_filter
class HeyFilter(Filter):
message = 'Tagging all mail with HEY categories'

def __init__(self, database):
super().__init__(database)

with open(str(ORG_CONTACTS_PATH), "r") as f:
contacts = orgparse.load(f)

self.contact_group = {node.properties.get("EMAIL"): node.properties.get("EMAIL-GROUP", "screener")
for node in contacts.children
if node.properties.get("EMAIL")}
self.addresses = [node.properties.get("EMAIL")
for node in contacts.children
if node.properties.get("EMAIL")]

@property
def query(self):
'''
Need to read the notmuch settings first. Using a property here
so that the setting is looked up on demand.
'''
return get_notmuch_new_query()

# @staticmethod
def get_address(self, message):
reply_to = message.get_header("Reply-To").strip()
# gotta be a better syntax, but haven't bothered with it yet
match = re.match(f".*<{NAMED_EMAIL_REGEX}>.*", reply_to) or re.match(NAMED_EMAIL_REGEX, reply_to)

if match:
sender = match.group("address")
return sender
else:
# TODO see what's going on with these
self.add_tags(message, "not-parsed")
# print(f">>>>>ERROR>>>>>>: {reply_to}")

def handle_message(self, message):
# sender = get_sender(message)
sender = self.get_address(message)
if sender:
print(f"Sender: {sender}")
group = self.contact_group.get(sender, "screener")
self.add_tags(message, group)
self.remove_tags(message, *get_notmuch_new_tags())
4 changes: 4 additions & 0 deletions .config/notmuch/default/hooks/post-new
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
notmuch tag +protonmail -- is:new path:account.protonmail/**
notmuch tag +kelleys -- is:new path:account.kelleys-gmail/**
/Users/pakelley/.pyenv/shims/afew --tag --new
1 change: 1 addition & 0 deletions .mackup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ directory = ...
chemacs2
karabiner-elements
mackup
my-afew
my-homebrew
my-doom-emacs
my-notmuch
Expand Down
5 changes: 5 additions & 0 deletions .mackup/my-afew.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[application]
name = afew

[xdg_configuration_files]
afew

0 comments on commit 23cdc93

Please sign in to comment.