-
Notifications
You must be signed in to change notification settings - Fork 2
/
jji
executable file
·77 lines (64 loc) · 1.57 KB
/
jji
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
#!/bin/sh -e
#
# Send stdin to a channel.
#
# Usage: jji <channel> [<channel>]...
#
# Commands can be executed by prefixing them with a '/'. Typing two
# slashes allows sending of messages with a slash in the beginning.
# The channel can be changed using `/channel <channel>`. Omitting the
# channel argument, switches to the previously set channel. The shorter
# `/chan`or `/c` can also be used. `/<number>` selects channels from the
# argument list.
arg0=${0##*/}
fifo=${IRC_DIR:-$HOME/irc}/${IRC_HOST:-irc.libera.chat}/in
! [ "$1" ] && {
printf '%s: missing channel argument\n' "$arg0" >&2
exit 1
}
if ! [ -p "$fifo" ] || ! [ -w "$fifo" ]; then
printf '%s: not a named pipe or not writeable: %s\n' \
"$arg0" "$fifo" >&2
exit 1
fi
chan_by_number() {
[ "$1" -ge $# ] &&
return
shift "$1"
last_chan=$chan
chan=$1
}
chan=$1
last_chan=${2:-$chan}
prompt() { [ -t 0 ] && printf '\033[37m%s\033[0m> ' "$chan" >&2 || :; }
while prompt "$chan" && IFS= read -r line || [ -n "$line" ]; do
case $line in
/[0-9]|/[0-9][0-9]|/[0-9]\ *|/[0-9][0-9]\ *)
num=${line#?}
num=${num%% *}
chan_by_number "$num" "$@"
continue
;;
/channel\ *[!\ ]*|/chan\ *[!\ ]*|/c\ *[!\ ]*)
# Strip trailing whitespace.
line=${line%%${line##*[! ]}}
[ "${line##* }" != "$chan" ] && {
last_chan=$chan
chan=${line##* }
}
continue
;;
/channel|/chan|/c|/channel\ *|/chan\ *|/c\ *|/)
tmp=$chan
chan=$last_chan
last_chan=$tmp
continue
;;
"")
continue
;;
/[!/]*) line=${line#?} ;;
*) line="msg $chan ${line#/}" ;;
esac
printf %s\\n "$line"
done >"$fifo"