feat: implement comprehensive wildcard folder selection and keyword filtering

## Wildcard Folder Selection
- Add support for wildcard patterns (`*`, `?`, `[abc]`) using filepath.Match
- Implement special case: `"*"` selects ALL available folders
- Support for complex include/exclude pattern combinations
- Maintain backwards compatibility with exact string matching
- Enable subfolder pattern matching (e.g., `Work/*`, `*/Drafts`)

## Keyword Filtering
- Add SubjectKeywords, SenderKeywords, RecipientKeywords to MessageFilter config
- Implement case-insensitive keyword matching across message fields
- Support multiple keywords per filter type with inclusive OR logic
- Add ShouldProcessMessage method for message-level filtering

## Enhanced Test Environment
- Create comprehensive wildcard pattern test scenarios
- Add 12 test folders covering various pattern types: Work/*, Important/*, Archive/*, exact matches
- Implement dedicated wildcard test script (test-wildcard-patterns.sh)
- Update test configurations to demonstrate real-world wildcard usage patterns
- Enhance test data generation with folder-specific messages for validation

## Documentation
- Create FOLDER_PATTERNS.md with comprehensive wildcard examples and use cases
- Update CLAUDE.md to reflect all implemented features and current status
- Enhance test README with detailed wildcard pattern explanations
- Provide configuration examples for common email organization scenarios

## Message Origin Tracking
- Verify all messages in CouchDB properly tagged with origin folder in `mailbox` field
- Maintain per-account database isolation for better organization
- Document ID format: `{folder}_{uid}` ensures uniqueness across folders

Key patterns supported:
- `["*"]` - All folders (with excludes)
- `["Work*", "Important*"]` - Prefix matching
- `["Work/*", "Archive/*"]` - Subfolder patterns
- `["INBOX", "Sent"]` - Exact matches
- Complex include/exclude combinations

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Ole-Morten Duesund 2025-08-01 17:24:02 +02:00
commit 357cd06264
10 changed files with 602 additions and 84 deletions

View file

@ -9,19 +9,21 @@ from email import encoders
import time
import sys
def create_simple_message(subject, body, from_addr="test-sender@example.com"):
def create_simple_message(subject, body, from_addr="test-sender@example.com", to_addr="user@example.com"):
"""Create a simple text message"""
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = email.utils.formatdate(localtime=True)
return msg.as_string()
def create_message_with_attachment(subject, body, attachment_content, from_addr="test-sender@example.com"):
def create_message_with_attachment(subject, body, attachment_content, from_addr="test-sender@example.com", to_addr="user@example.com"):
"""Create a message with an attachment"""
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = email.utils.formatdate(localtime=True)
# Add body
@ -44,45 +46,93 @@ def populate_user_mailbox(username, password, host='localhost', port=3143):
# Connect to IMAP server
imap = imaplib.IMAP4(host, port)
imap.login(username, password)
imap.select('INBOX')
print(f"Creating messages for {username}...")
# Create 10 regular messages
for i in range(1, 11):
if i % 3 == 0:
# Every 3rd message has attachment
msg = create_message_with_attachment(
f"Test Message {i} with Attachment",
f"This is test message {i} for {username} with an attachment.",
f"Sample attachment content for message {i}"
)
print(f" Created message {i} (with attachment)")
else:
# Simple message
msg = create_simple_message(
f"Test Message {i}",
f"This is test message {i} for {username}."
)
print(f" Created message {i}")
# Append message to INBOX
imap.append('INBOX', None, None, msg.encode('utf-8'))
time.sleep(0.1) # Small delay to avoid overwhelming
# Create additional test messages
special_messages = [
("Important Message", "This is an important message for testing sync/archive modes."),
("Message with Special Characters", "This message contains special characters: äöü ñ 中文 🚀")
# Create additional folders for testing wildcards
# These folders are designed to test various wildcard patterns
test_folders = [
'Sent', # Exact match
'Work/Projects', # Work/* pattern
'Work/Archive', # Work/* pattern
'Work/Temp', # Work/* but excluded by *Temp*
'Personal', # Exact match
'Important/Urgent', # Important/* pattern
'Important/Meetings', # Important/* pattern
'Archive/2024', # Archive/* pattern
'Archive/Projects', # Archive/* pattern
'Archive/Drafts', # Archive/* but excluded by */Drafts
'Drafts', # Should be excluded
'Trash' # Should be excluded
]
created_folders = ['INBOX'] # INBOX always exists
for subject, body in special_messages:
msg = create_simple_message(subject, body)
imap.append('INBOX', None, None, msg.encode('utf-8'))
print(f" Created special message: {subject}")
for folder in test_folders:
try:
imap.create(folder)
created_folders.append(folder)
print(f" Created folder: {folder}")
except Exception as e:
print(f" Folder {folder} may already exist or creation failed: {e}")
# Try to select it to see if it exists
try:
imap.select(folder)
created_folders.append(folder)
print(f" Folder {folder} already exists")
except:
pass
print(f"Available folders for {username}: {created_folders}")
# Populate each folder with messages
for folder in created_folders[:3]: # Limit to first 3 folders to avoid too many messages
imap.select(folder)
print(f"Creating messages in {folder} for {username}...")
# Create fewer messages per folder for testing
message_count = 3 if folder != 'INBOX' else 10
for i in range(1, message_count + 1):
if i % 3 == 0 and folder == 'INBOX':
# Every 3rd message has attachment (only in INBOX)
msg = create_message_with_attachment(
f"[{folder}] Test Message {i} with Attachment",
f"This is test message {i} in {folder} for {username} with an attachment.",
f"Sample attachment content for message {i} in {folder}",
"test-sender@example.com",
f"{username}@example.com"
)
print(f" Created message {i} (with attachment)")
else:
# Simple message
msg = create_simple_message(
f"[{folder}] Test Message {i}",
f"This is test message {i} in {folder} for {username}.",
"test-sender@example.com",
f"{username}@example.com"
)
print(f" Created message {i}")
# Append message to current folder
imap.append(folder, None, None, msg.encode('utf-8'))
time.sleep(0.1) # Small delay to avoid overwhelming
# Add special messages only to INBOX for keyword filtering tests
if folder == 'INBOX':
special_messages = [
("Important Meeting Reminder", "This is an important meeting message for testing keyword filters.", "manager@company.com", "team@company.com"),
("Urgent: System Maintenance", "Important notification about system maintenance.", "admin@company.com", f"{username}@example.com"),
("Regular Newsletter", "This is a regular newsletter message.", "newsletter@external.com", f"{username}@example.com"),
("Team Meeting Notes", "Meeting notes from the team.", "secretary@company.com", "support@company.com"),
("Message with Special Characters", "This message contains special characters: äöü ñ 中文 🚀", "test-sender@example.com", f"{username}@example.com")
]
for subject, body, sender, recipient in special_messages:
msg = create_simple_message(subject, body, sender, recipient)
imap.append(folder, None, None, msg.encode('utf-8'))
print(f" Created special message: {subject} from {sender} to {recipient}")
time.sleep(0.1)
imap.logout()
print(f"✅ Successfully created 12 messages for {username}")
print(f"✅ Successfully created messages across {len(created_folders[:3])} folders for {username}")
return True
except Exception as e: