Skip to content

Commit 112ddd9

Browse files
committed
gaming
1 parent 2b558cd commit 112ddd9

File tree

1 file changed

+58
-55
lines changed

1 file changed

+58
-55
lines changed

server

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env bash
22

3+
cd "${0%/*}"
4+
35
set noglob
46

57
ORM_DB=db.sqlite
68
VERSION=0.0.1
7-
IRC_SERVER_HOST=localhost
9+
IRC_SERVER_HOST=127.0.0.1
810
PORT=${PORT:-6667}
911

1012
declare -A SUBS
@@ -46,64 +48,58 @@ function listen() {
4648
debug "ATTEMPTED TO LISTEN ON EMPTY TOPIC"
4749
return
4850
fi
51+
local -a arr
4952
while true; do
50-
[[ -z "${SUBS[$TOPIC]}" ]] && break
51-
cat "$TOPIC"
53+
IFS= read -r line < "$TOPIC"
54+
split "${line//$'\r'}" ' '
55+
if [[ "${arr[0]}" =~ :(.+) ]]; then
56+
PREFIX="${BASH_REMATCH[1]}"
57+
arr=("${arr[@]:1}")
58+
fi
59+
COMMAND="${arr[0]}"
60+
debug "LISTENED COMMAND: '$COMMAND'"
61+
arr=("${arr[@]:1}")
62+
63+
case "$COMMAND" in
64+
PRIVMSG)
65+
[[ "${PREFIX%%@*}" == "$NICK" ]] && continue
66+
;;
67+
JOIN)
68+
;;
69+
*)
70+
continue
71+
;;
72+
esac
73+
# forward the message
74+
printf "%s\n" "$line"
75+
debug "SENT: '${line//$'\r'/\\r}'"
5276
done
5377
}
5478

5579
function subscribe() {
56-
local TOPIC
57-
TOPIC="$1"
58-
if [[ -z "$TOPIC" ]]; then
59-
debug "ATTEMPTED TO SUBSCRIBE ON EMPTY TOPIC"
60-
return
61-
fi
62-
mkdir -p pubsub/"${TOPIC}"
63-
local tmppipe=pubsub/"${TOPIC}/${NICK}"
80+
mkdir -p pubsub/irc
81+
local tmppipe=$(mktemp -up pubsub/irc)
6482
mkfifo -m 600 "$tmppipe"
6583
SUBSCRIPTION="$tmppipe"
84+
debug "PIPE CREATED: $tmppipe"
6685
}
6786

6887
function unsubscribe() {
69-
local TOPIC
70-
TOPIC="$1"
71-
if [[ "$TOPIC" != "pubsub/"* ]]; then
72-
debug "FAILED TO UNSUBSCRIBE"
73-
return
74-
fi
75-
rm -f "$TOPIC"
76-
unset SUBS["$TOPIC"]
88+
debug "PIPE CLOSING: $SUBSCRIPTION"
89+
rm -f "$SUBSCRIPTION"
90+
debug "PIPE CLOSED: $SUBSCRIPTION"
7791
}
7892

7993
function broadcast() {
80-
local TOPIC
81-
local IGNORE
82-
local line
83-
TOPIC="$1"
84-
IGNORE="$2"
85-
if [[ -z "$TOPIC" ]]; then
86-
return
87-
fi
88-
if [[ ! -d "pubsub/${TOPIC}" ]]; then
89-
return
90-
fi
91-
if [[ -z "$IGNORE" ]]; then
92-
TEE_ARGS=$(find pubsub/"${TOPIC}" -type p)
93-
else
94-
TEE_ARGS=$(find pubsub/"${TOPIC}" -type p ! -iname "$IGNORE")
95-
fi
96-
if [[ -z "$TEE_ARGS" ]]; then
97-
return
98-
fi
94+
[[ ! -d "pubsub/irc" ]] && return
95+
TEE_ARGS=$(find pubsub/irc -type p)
96+
[[ -z "$TEE_ARGS" ]] && return
9997
tee $TEE_ARGS > /dev/null
10098
}
10199

102100
reply() {
103-
local CODE="$1"
104-
shift
105101
local RESPONSE
106-
printf -v "RESPONSE" ":%s %s %s %s\r\n" "$IRC_SERVER_HOST" "$CODE" "$NICK" "$@"
102+
printf -v "RESPONSE" ":%s %s\r\n" "$IRC_SERVER_HOST" "$*"
107103
RESPONSE="${RESPONSE:0:512}"
108104
printf "%s" "$RESPONSE"
109105
debug "REPLYING: $RESPONSE"
@@ -115,6 +111,10 @@ debug() {
115111

116112
if [[ -z "$IRC_CONN_HANDLER" ]]; then
117113

114+
rm -rf pubsub
115+
mkdir pubsub
116+
117+
118118
query 'CREATE TABLE IF NOT EXISTS channels
119119
(
120120
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -129,6 +129,7 @@ if [[ -z "$IRC_CONN_HANDLER" ]]; then
129129
tcpserver -1 -o -l 0 -H -R -c 1000 0 $PORT $0
130130
else
131131
# echo "connection opened with $TCPREMOTEIP" 1>&2
132+
subscribe
132133
while IFS= read -r line; do
133134
split "${line//$'\r'}" ' '
134135
if [[ "${arr[0]}" =~ :(.+) ]]; then
@@ -140,22 +141,27 @@ else
140141
debug "COMMAND: '$COMMAND'"
141142
arr=("${arr[@]:1}")
142143
case "${COMMAND^^}" in
144+
# CAP)
145+
# CAP="${arr[0]}"
146+
# reply CAP "* NAK :"
147+
# ;;
143148
NICK)
144149
NICK="${arr[0]}"
145150
;;
146151
USER)
147152
USER="${arr[0]}"
148153
MODE="${arr[1]}"
149154
REALNAME="${arr[3]}"
150-
reply 001 ":Welcome, $NICK!$USER"
151-
reply 002 ":Your host is $IRC_SERVER_HOST, running version $VERSION"
152-
reply 003 ":This server was created $IRC_SERVER_CREATED"
153-
reply 004 "$IRC_SERVER_HOST bash-$VERSION"
155+
reply 001 $NICK ":Welcome, $NICK!$USER@$IRC_SERVER_HOST"
156+
reply 002 $NICK ":Your host is $IRC_SERVER_HOST, running version $VERSION"
157+
reply 003 $NICK ":This server was created $IRC_SERVER_CREATED"
158+
reply 004 $NICK "$IRC_SERVER_HOST bash-$VERSION"
159+
reply 376 $NICK ":End of the /MOTD command."
154160
;;
155161
PRIVMSG)
156162
TARGET="${arr[0]}"
157163
arr=("${arr[@]:1}")
158-
printf "%s\r\n" ":$NICK PRIVMSG $TARGET ${arr[*]}" | broadcast "$TARGET" "$NICK"
164+
printf "%s\r\n" ":$NICK PRIVMSG $TARGET ${arr[*]}" | broadcast
159165
debug "FORWARDING MESSAGE TO $TARGET"
160166

161167
;;
@@ -169,21 +175,20 @@ else
169175
CHANNELS=("${arr[@]}")
170176
split "$KEYS" ','
171177
KEYS=("${arr[@]}")
172-
debug "CHANNELS: ${CHANNELS[@]}"
173-
debug "KEYS: ${KEYS[@]}"
174178
for CHANNEL in ${CHANNELS[@]}; do
175179
if [[ "$CHANNEL" != "#test" ]]; then
176180
continue
177181
fi
178-
subscribe "$CHANNEL"
179-
SUBS["$SUBSCRIPTION"]=true
182+
debug "$NICK joined $CHANNEL"
183+
SUBS["$CHANNEL"]=true
180184
listen "$SUBSCRIPTION" &
181-
printf "%s\r\n" ":$NICK JOIN $CHANNEL" | broadcast "$CHANNEL"
185+
printf "%s\r\n" ":$NICK!$USER@$IRC_SERVER_HOST JOIN :$CHANNEL" | broadcast
186+
reply 366 $NICK $CHANNEL :
182187
done
183188
fi
184189
;;
185190
PING)
186-
reply PONG "${arr[0]}"
191+
reply PONG "$NICK" "${arr[0]}"
187192
;;
188193
*)
189194
debug "UNRECOGNIZED COMMAND"
@@ -192,7 +197,5 @@ else
192197
esac
193198
done
194199
# echo "connection closed with $TCPREMOTEIP" 1>&2
195-
for SUB in "${!SUBS[@]}"; do
196-
unsubscribe "$SUB"
197-
done
200+
unsubscribe
198201
fi

0 commit comments

Comments
 (0)