| 123456789101112131415161718192021222324252627282930313233343536373839 |
- #!/bin/bash
- # hooks/check-mail.sh
- # PreToolUse hook - checks for unread inter-session mail
- # Runs on every tool call. Silent when inbox is empty.
- # Matcher: * (all tools)
- #
- # Configuration in .claude/settings.json or .claude/settings.local.json:
- # {
- # "hooks": {
- # "PreToolUse": [{
- # "matcher": "*",
- # "hooks": ["bash hooks/check-mail.sh"]
- # }]
- # }
- # }
- MAIL_DB="$HOME/.claude/mail.db"
- # Skip if no database exists yet
- [ -f "$MAIL_DB" ] || exit 0
- PROJECT=$(basename "$PWD" | sed "s/'/''/g")
- # Single fast query - count unread
- UNREAD=$(sqlite3 "$MAIL_DB" "SELECT COUNT(*) FROM messages WHERE to_project='${PROJECT}' AND read=0;" 2>/dev/null)
- # Silent exit if no mail
- [ "${UNREAD:-0}" -eq 0 ] && exit 0
- # Show notification with preview of first 3 messages
- echo ""
- echo "=== MAIL: ${UNREAD} unread message(s) ==="
- sqlite3 -separator ' ' "$MAIL_DB" \
- "SELECT ' From: ' || from_project || ' | ' || subject FROM messages WHERE to_project='${PROJECT}' AND read=0 ORDER BY timestamp DESC LIMIT 3;" 2>/dev/null
- if [ "$UNREAD" -gt 3 ]; then
- echo " ... and $((UNREAD - 3)) more"
- fi
- echo "Use /mail to read messages."
- echo "==="
|