Skip to content

Instantly share code, notes, and snippets.

@hahanein
Created December 20, 2025 11:35
Show Gist options
  • Select an option

  • Save hahanein/f5bdc7c2f61d15320249c72c75ae0884 to your computer and use it in GitHub Desktop.

Select an option

Save hahanein/f5bdc7c2f61d15320249c72c75ae0884 to your computer and use it in GitHub Desktop.
Poll POP mail via fdm and notify via notify-send
#!/usr/bin/env bash
set -euo pipefail
STATE_DIR="$XDG_RUNTIME_DIR/fdm-poll"
STATE_FILE="$STATE_DIR/lastcount"
mkdir -p "$STATE_DIR"
out="$(fdm poll 2>/dev/null || true)"
# Extract the first integer after ":" on each line and sum it.
count="$(
printf '%s\n' "$out" |
sed -n 's/^[^:]*:[[:space:]]*\([0-9]\+\).*/\1/p' |
awk '{s+=$1} END{print s+0}'
)"
last=0
[[ -f "$STATE_FILE" ]] && last="$(cat "$STATE_FILE" 2>/dev/null || echo 0)"
if ((count > last)); then
delta=$((count - last))
# x-canonical-private-synchronous makes repeated notifications replace each other on many setups :contentReference[oaicite:2]{index=2}
notify-send -a mail \
-h string:x-canonical-private-synchronous:fdm-poll \
-h int:transient:1 \
"New mail" "$delta new message(s) on POP server"
fi
printf '%s\n' "$count" >"$STATE_FILE"
[Unit]
Description=Poll POP mail via fdm and notify via notify-send
[Service]
Type=oneshot
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=%t/bus
ExecStart=%h/.local/bin/fdm-poll-notify
[Unit]
Description=Periodic fdm poll
[Timer]
OnBootSec=30s
OnUnitActiveSec=2min
AccuracySec=15s
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment