check-mail.sh 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/bash
  2. # hooks/check-mail.sh
  3. # PreToolUse hook - checks for unread inter-session mail
  4. # Runs on every tool call. Silent when inbox is empty.
  5. # Matcher: * (all tools)
  6. #
  7. # Configuration in .claude/settings.json or .claude/settings.local.json:
  8. # {
  9. # "hooks": {
  10. # "PreToolUse": [{
  11. # "matcher": "*",
  12. # "hooks": ["bash hooks/check-mail.sh"]
  13. # }]
  14. # }
  15. # }
  16. MAIL_DB="$HOME/.claude/mail.db"
  17. # Skip if no database exists yet
  18. [ -f "$MAIL_DB" ] || exit 0
  19. PROJECT=$(basename "$PWD" | sed "s/'/''/g")
  20. # Single fast query - count unread
  21. UNREAD=$(sqlite3 "$MAIL_DB" "SELECT COUNT(*) FROM messages WHERE to_project='${PROJECT}' AND read=0;" 2>/dev/null)
  22. # Silent exit if no mail
  23. [ "${UNREAD:-0}" -eq 0 ] && exit 0
  24. # Show notification with preview of first 3 messages
  25. echo ""
  26. echo "=== MAIL: ${UNREAD} unread message(s) ==="
  27. sqlite3 -separator ' ' "$MAIL_DB" \
  28. "SELECT ' From: ' || from_project || ' | ' || subject FROM messages WHERE to_project='${PROJECT}' AND read=0 ORDER BY timestamp DESC LIMIT 3;" 2>/dev/null
  29. if [ "$UNREAD" -gt 3 ]; then
  30. echo " ... and $((UNREAD - 3)) more"
  31. fi
  32. echo "Use /mail to read messages."
  33. echo "==="