-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitter-notifier.sh
executable file
·96 lines (84 loc) · 2.15 KB
/
gitter-notifier.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env bash
#
# gitter-notifier -- pop up notification when you get new post in gitter
#
# Copyright 2019 (c) Cj-bc
# This software is released under MIT License
#
# @(#) version 0.1.0
gitter_notifier_fifo_name=".gitter_notifier_$(date +%Y%m%d%H%M).fifo"
# _version: echo version {{{2
function _version()
{
local self="${BASH_SOURCE[0]}"
versionline="$(grep '# @(#) version 0.1.0' "$self" | head -n 1)"
echo "${versionline/\# @(\#)/gitter-notifier:}"
}
# }}}
# _help: echo help messages {{{2
function _help()
{
local regex_usage_region='/# usage.*/, /#.*/'
local regex_tag='#.*'
local regex_notext='^$'
local README="README.md"
local usage_with_padding
usage_with_padding="$(sed -n -e "$regex_usage_region p" "$README" |grep -v "$regex_tag" |grep -v "$regex_notext")"
echo "$(echo "$usage_with_padding")"
}
# }}}
# _daemon(): pull new gitter post {{{2
# @param <string room_name>
# @return <string daemon_PID>
_daemon() {
local room="$1"
echo -n "Daemon: making fifo..."
mkfifo "$gitter_notifier_fifo_name"
echo " ok"
echo -n "starting gitter-cli..."
gitter-cli s --room "$room" > $gitter_notifier_fifo_name &
daemon_pid="$!"
echo " ok"
}
# }}}
# _cleanup: clean up after execution.called from trap {{{2
# @param <string daemon_pid>
function _cleanup()
{
echo "Detect Quit code."
echo -n "kill daemon..."
kill $1
echo " ok"
echo -n "Remove fifo..."
rm "$gitter_notifier_fifo_name"
echo " ok"
echo -n "Untrap..."
trap SIGINT
trap SIGTERM
echo " ok"
echo "Done."
}
# }}}
# main(): main loop to pop up notifications {{{2
main() {
# TODO: check whether room is available
echo -n "starting daemon..."
_daemon "$1"
echo "daemon PID: $daemon_pid"
echo -n "setting traps..."
trap "_cleanup $daemon_pid" SIGINT
trap "_cleanup $daemon_pid" SIGTERM
echo " ok"
echo "Start main loop... go ahead"
local date time user line
while read date time user line; do
user="${user//[():]/}";
terminal-notifier -message "$line" -title "GITTER" -subtitle "$user";
done < "$gitter_notifier_fifo_name"
}
# }}}
case "$1" in
"-h"|"help"|"--help") _help;;
"-v"|"version"|"--version") _version;;
*) main $@;;
esac