@@ -6,6 +6,7 @@ ORM_DB=db.sqlite
6
6
VERSION=0.0.1
7
7
IRC_SERVER_HOST=localhost
8
8
9
+ declare -A SUBS
9
10
# self explanatory
10
11
function query() {
11
12
function _query() {
@@ -37,6 +38,47 @@ split() {
37
38
IFS=$' \n ' read -d " " -ra arr <<< " ${1//$2/$'\n'}"
38
39
}
39
40
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
+
40
82
reply () {
41
83
local CODE=" $1 "
42
84
shift
@@ -69,7 +111,7 @@ if [[ -z "$IRC_CONN_HANDLER" ]]; then
69
111
else
70
112
echo " connection opened with $TCPREMOTEIP " 1>&2
71
113
while IFS= read -r line; do
72
- split " $line " ' '
114
+ split " ${ line// $' \r ' } " ' '
73
115
if [[ " ${arr[0]} " =~ :(.+) ]]; then
74
116
PREFIX=" ${BASH_REMATCH[1]} "
75
117
debug " PREFIX: '$PREFIX '"
@@ -80,22 +122,58 @@ else
80
122
arr=(" ${arr[@]: 1} " )
81
123
case " ${COMMAND^^} " in
82
124
NICK)
83
- NICK=" ${arr[0]// $' \r ' } "
125
+ NICK=" ${arr[0]} "
84
126
;;
85
127
USER)
86
128
USER=" ${arr[0]} "
87
129
MODE=" ${arr[1]} "
88
- REALNAME=" ${arr[3]// $' \r ' } "
130
+ REALNAME=" ${arr[3]} "
89
131
reply 001 " :Welcome, $NICK !$USER "
90
132
reply 002 " :Your host is $IRC_SERVER_HOST , running version $VERSION "
91
133
reply 003 " :This server was created $IRC_SERVER_CREATED "
92
134
reply 004 " $IRC_SERVER_HOST bash-$VERSION "
93
135
;;
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
+ ;;
94
169
* )
95
170
debug " UNRECOGNIZED COMMAND"
96
171
debug " ${arr[@]} "
97
172
;;
98
173
esac
99
174
done
100
175
echo " connection closed with $TCPREMOTEIP " 1>&2
176
+ for SUB in " ${! SUBS[@]} " ; do
177
+ unsubscribe " $SUB "
178
+ done
101
179
fi
0 commit comments