Skip to content

Commit 7ac4f22

Browse files
committed
i think it will work
1 parent 3009765 commit 7ac4f22

File tree

1 file changed

+81
-3
lines changed

1 file changed

+81
-3
lines changed

server

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ORM_DB=db.sqlite
66
VERSION=0.0.1
77
IRC_SERVER_HOST=localhost
88

9+
declare -A SUBS
910
# self explanatory
1011
function query() {
1112
function _query() {
@@ -37,6 +38,47 @@ split() {
3738
IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
3839
}
3940

41+
function subscribe() {
42+
local TOPIC
43+
TOPIC="$1"
44+
if [[ -z "$TOPIC" ]]; then
45+
debug "ATTEMPTED TO SUBSCRIBE ON EMPTY TOPIC"
46+
return
47+
fi
48+
mkdir -p pubsub/"${TOPIC}"
49+
local tmppipe=pubsub/"${TOPIC}/${NICK}"
50+
mkfifo -m 600 "$tmppipe"
51+
SUBSCRIPTION="$tmppipe"
52+
}
53+
54+
function unsubscribe() {
55+
local TOPIC
56+
TOPIC="$1"
57+
if [[ "$TOPIC" != "pubsub/"* ]]; then
58+
debug "FAILED TO UNSUBSCRIBE"
59+
return
60+
fi
61+
rm -f "$TOPIC"
62+
unset SUBS["$TOPIC"]
63+
}
64+
65+
function broadcast() {
66+
local TOPIC
67+
local line
68+
TOPIC="$1"
69+
if [[ -z "$TOPIC" ]]; then
70+
return
71+
fi
72+
if [[ ! -d "pubsub/${TOPIC}" ]]; then
73+
return
74+
fi
75+
TEE_ARGS=$(find pubsub/"${TOPIC}" -type p)
76+
if [[ -z "$TEE_ARGS" ]]; then
77+
return
78+
fi
79+
tee $TEE_ARGS > /dev/null
80+
}
81+
4082
reply() {
4183
local CODE="$1"
4284
shift
@@ -69,7 +111,7 @@ if [[ -z "$IRC_CONN_HANDLER" ]]; then
69111
else
70112
echo "connection opened with $TCPREMOTEIP" 1>&2
71113
while IFS= read -r line; do
72-
split "$line" ' '
114+
split "${line//$'\r'}" ' '
73115
if [[ "${arr[0]}" =~ :(.+) ]]; then
74116
PREFIX="${BASH_REMATCH[1]}"
75117
debug "PREFIX: '$PREFIX'"
@@ -80,22 +122,58 @@ else
80122
arr=("${arr[@]:1}")
81123
case "${COMMAND^^}" in
82124
NICK)
83-
NICK="${arr[0]//$'\r'}"
125+
NICK="${arr[0]}"
84126
;;
85127
USER)
86128
USER="${arr[0]}"
87129
MODE="${arr[1]}"
88-
REALNAME="${arr[3]//$'\r'}"
130+
REALNAME="${arr[3]}"
89131
reply 001 ":Welcome, $NICK!$USER"
90132
reply 002 ":Your host is $IRC_SERVER_HOST, running version $VERSION"
91133
reply 003 ":This server was created $IRC_SERVER_CREATED"
92134
reply 004 "$IRC_SERVER_HOST bash-$VERSION"
93135
;;
136+
PRIVMSG)
137+
TARGET="${arr[0]}"
138+
arr=("${arr[@]:1}")
139+
printf "%s\r\n" ":$NICK PRIVMSG $TARGET ${arr[@]}" | broadcast "$TARGET"
140+
debug "FORWARDING MESSAGE TO $TARGET"
141+
142+
;;
143+
JOIN)
144+
if [[ "${arr[@]}" == "0" ]]; then
145+
debug "YOU LEFT, IDK WHAT TO DO"
146+
else
147+
CHANNELS="${arr[0]}"
148+
KEYS="${arr[1]}"
149+
split "$CHANNELS" ','
150+
CHANNELS=("${arr[@]}")
151+
split "$KEYS" ','
152+
KEYS=("${arr[@]}")
153+
debug "CHANNELS: ${CHANNELS[@]}"
154+
debug "KEYS: ${KEYS[@]}"
155+
for CHANNEL in ${CHANNELS[@]}; do
156+
if [[ "$CHANNEL" != "#test" ]]; then
157+
continue
158+
fi
159+
subscribe "$CHANNEL"
160+
cat "$SUBSCRIPTION" &
161+
SUBS["$SUBSCRIPTION"]=true
162+
printf "%s\r\n" ":$NICK JOIN $CHANNEL" | broadcast "$CHANNEL"
163+
done
164+
fi
165+
;;
166+
PING)
167+
reply PONG "${arr[0]}"
168+
;;
94169
*)
95170
debug "UNRECOGNIZED COMMAND"
96171
debug "${arr[@]}"
97172
;;
98173
esac
99174
done
100175
echo "connection closed with $TCPREMOTEIP" 1>&2
176+
for SUB in "${!SUBS[@]}"; do
177+
unsubscribe "$SUB"
178+
done
101179
fi

0 commit comments

Comments
 (0)