Implement mail search using Himalaya CLI with auto-select first result

This commit is contained in:
Bendt
2025-12-19 14:18:40 -05:00
parent d3468f7395
commit 0cd7cf6984
2 changed files with 109 additions and 28 deletions

View File

@@ -312,6 +312,57 @@ async def mark_as_read(
return str(e), False
async def search_envelopes(
query: str,
folder: Optional[str] = None,
account: Optional[str] = None,
limit: int = 100,
) -> Tuple[List[Dict[str, Any]], bool]:
"""
Search for envelopes matching a query using Himalaya CLI.
The query is searched across from, to, subject, and body fields.
Args:
query: The search term to look for
folder: The folder to search in (defaults to INBOX)
account: The account to use (defaults to default account)
limit: Maximum number of results to return
Returns:
Tuple containing:
- List of matching envelope dictionaries
- Success status (True if operation was successful)
"""
try:
# Build a compound query to search from, to, subject, and body
# Himalaya query syntax: from <pattern> or to <pattern> or subject <pattern> or body <pattern>
search_query = f"from {query} or to {query} or subject {query} or body {query}"
cmd = f"himalaya envelope list -o json -s {limit} {search_query}"
if folder:
cmd += f" -f '{folder}'"
if account:
cmd += f" -a '{account}'"
process = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
if process.returncode == 0:
envelopes = json.loads(stdout.decode())
return envelopes, True
else:
logging.error(f"Error searching envelopes: {stderr.decode()}")
return [], False
except Exception as e:
logging.error(f"Exception during envelope search: {e}")
return [], False
def sync_himalaya():
"""This command does not exist. Halucinated by AI."""
try: