pub / pp-notifier

Instant Messenger Notification daemon for the PinePhone
git clone https://https://src.jayvii.de/pub/pp-notifier.git
Home | Log | Files | Exports | Refs | README | RSS

im-notifier (3322B)


      1 #!/usr/bin/env bash
      2 # SPDX-FileCopyrightText: 2021 JayVii
      3 # SPDX-License-Identifier: GPL-3.0-or-later
      4 
      5 # Settings --------------------------------------------------------------------
      6 
      7 # Load Config file if present
      8 if [[ -f "$HOME/.config/im-notifier/config" ]]; then
      9     source "$HOME/.config/im-notifier/config"
     10 fi
     11 
     12 # Set Defaults
     13 if [[ -z $POLLTIME ]]; then
     14     POLLTIME=30
     15 fi
     16 if [[ -z $TIMEOUT ]]; then
     17     TIMEOUT=25
     18 fi
     19 if [[ -z $TELEGRAM ]] && [[ -z $(command -v telegram-cli) ]]; then
     20     TELEGRAM=1
     21     echo "Enabling Telegram!"
     22 fi
     23 if [[ -z $TG_ICON ]]; then
     24     TG_ICON="document-send"
     25 fi
     26 if [[ -z $TG_ACTION ]]; then
     27     TG_ACTION="telegram-desktop"
     28 fi
     29 
     30 # Set Initial Notification IDs ------------------------------------------------
     31 
     32 TG_NOTIFICATION_ID=0
     33 
     34 # Modules ---------------------------------------------------------------------
     35 
     36 ## Common
     37 function im_notify {
     38     gdbus call --session \
     39         --dest=org.freedesktop.Notifications \
     40         --object-path=/org/freedesktop/Notifications \
     41         --method=org.freedesktop.Notifications.Notify \
     42         "$1" $2 "$3" "$1" "$4" "$5" \
     43         '{"category": <"im.received">}' 3000
     44 }
     45 function im_unnotify {
     46         gdbus call --session \
     47             --dest=org.freedesktop.Notifications \
     48             --object-path=/org/freedesktop/Notifications \
     49             --method=org.freedesktop.Notifications.CloseNotification \
     50             $1
     51 }
     52 function im_signal_listen {
     53     dbus-monitor "type='signal',sender='org.freedesktop.Notifications',interface='org.freedesktop.Notifications',member='ActionInvoked'" | \
     54         while read -r line; do
     55             if [[ $(printf "$line" | grep "$1") != "" ]]; then
     56                 $SHELL -c "$2"
     57             fi
     58         done
     59 }
     60 
     61 ## Telegram
     62 function tg_check {
     63     telegram-cli --disable-colors \
     64      --exec dialog_list | \
     65         grep -E "[1-9][0-9]* unread"
     66 }
     67 function tg_kill {
     68     sleep $1
     69     if [[ ! -z $(pidof telegram-cli) ]]; then
     70         pkill telegram-cli
     71     fi
     72 }
     73 function tg_notify {
     74     im_notify "Telegram" $2 "$TG_ICON" "$1" "['im-notify-tg', 'Open Message']"
     75 }
     76 
     77 # Run Daemon ------------------------------------------------------------------
     78 
     79 # Notify user about running state of the daemon
     80 INITMSG="Enabled Services:"
     81 if [[ $TELEGRAM != 0 ]]; then
     82     INITMSG="$INITMSG Telegram"
     83 fi
     84 im_notify "IM-Notifier" 0 "gnome-settings" "$INITMSG" "[]"
     85 
     86 # Run Signal Listeners (executes actions)
     87 if [[ $TELEGRAM != 0 ]]; then
     88     im_signal_listen "im-notify-tg" "${TG_ACTION}" &
     89 fi
     90 
     91 # Run daemon in endless while-loop
     92 while :; do
     93 
     94     # Telegram
     95     if [[ $TELEGRAM != 0 ]]; then
     96 
     97         # kill after some time
     98         tg_kill $TIMEOUT &
     99 
    100         # Check Telegram Messages
    101         TG_UNREAD=$(tg_check)
    102 
    103         if [[ ! -z "$TG_UNREAD" ]]; then
    104             # Notify Telegram Messages
    105             TG_NOTIFICATION_ID=$(tg_notify \
    106                 "$TG_UNREAD" $TG_NOTIFICATION_ID | \
    107                 sed -e 's/^.*\s//' -e 's/[^0-9]//g')
    108         elif [[ $TG_NOTIFICATION_ID != 0 ]]; then
    109             # Close Notification
    110             im_unnotify $TG_NOTIFICATION_ID
    111             TG_NOTIFICATION_ID=0
    112         fi
    113     fi
    114 
    115     # Sleep until next round...
    116     sleep $POLLTIME
    117 
    118 done
    119 
    120 # Notify user about running state of the daemon
    121 im_notify "IM-Notifier" 0 "gnome-settings" "IM-Notifier stopped..." "[]"
    122 
    123 # EOF im-notifier.sh