1
1
#! /usr/bin/env bash
2
2
3
+ cd " ${0%/* } "
4
+
3
5
set noglob
4
6
5
7
ORM_DB=db.sqlite
6
8
VERSION=0.0.1
7
- IRC_SERVER_HOST=localhost
9
+ IRC_SERVER_HOST=127.0.0.1
8
10
PORT=${PORT:- 6667}
9
11
10
12
declare -A SUBS
@@ -46,64 +48,58 @@ function listen() {
46
48
debug " ATTEMPTED TO LISTEN ON EMPTY TOPIC"
47
49
return
48
50
fi
51
+ local -a arr
49
52
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} '"
52
76
done
53
77
}
54
78
55
79
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)
64
82
mkfifo -m 600 " $tmppipe "
65
83
SUBSCRIPTION=" $tmppipe "
84
+ debug " PIPE CREATED: $tmppipe "
66
85
}
67
86
68
87
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 "
77
91
}
78
92
79
93
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
99
97
tee $TEE_ARGS > /dev/null
100
98
}
101
99
102
100
reply () {
103
- local CODE=" $1 "
104
- shift
105
101
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 " " $* "
107
103
RESPONSE=" ${RESPONSE: 0: 512} "
108
104
printf " %s" " $RESPONSE "
109
105
debug " REPLYING: $RESPONSE "
@@ -115,6 +111,10 @@ debug() {
115
111
116
112
if [[ -z " $IRC_CONN_HANDLER " ]]; then
117
113
114
+ rm -rf pubsub
115
+ mkdir pubsub
116
+
117
+
118
118
query ' CREATE TABLE IF NOT EXISTS channels
119
119
(
120
120
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -129,6 +129,7 @@ if [[ -z "$IRC_CONN_HANDLER" ]]; then
129
129
tcpserver -1 -o -l 0 -H -R -c 1000 0 $PORT $0
130
130
else
131
131
# echo "connection opened with $TCPREMOTEIP" 1>&2
132
+ subscribe
132
133
while IFS= read -r line; do
133
134
split " ${line// $' \r ' } " ' '
134
135
if [[ " ${arr[0]} " =~ :(.+) ]]; then
@@ -140,22 +141,27 @@ else
140
141
debug " COMMAND: '$COMMAND '"
141
142
arr=(" ${arr[@]: 1} " )
142
143
case " ${COMMAND^^} " in
144
+ # CAP)
145
+ # CAP="${arr[0]}"
146
+ # reply CAP "* NAK :"
147
+ # ;;
143
148
NICK)
144
149
NICK=" ${arr[0]} "
145
150
;;
146
151
USER)
147
152
USER=" ${arr[0]} "
148
153
MODE=" ${arr[1]} "
149
154
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."
154
160
;;
155
161
PRIVMSG)
156
162
TARGET=" ${arr[0]} "
157
163
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
159
165
debug " FORWARDING MESSAGE TO $TARGET "
160
166
161
167
;;
@@ -169,21 +175,20 @@ else
169
175
CHANNELS=(" ${arr[@]} " )
170
176
split " $KEYS " ' ,'
171
177
KEYS=(" ${arr[@]} " )
172
- debug " CHANNELS: ${CHANNELS[@]} "
173
- debug " KEYS: ${KEYS[@]} "
174
178
for CHANNEL in ${CHANNELS[@]} ; do
175
179
if [[ " $CHANNEL " != " #test" ]]; then
176
180
continue
177
181
fi
178
- subscribe " $CHANNEL "
179
- SUBS[" $SUBSCRIPTION " ]=true
182
+ debug " $NICK joined $CHANNEL "
183
+ SUBS[" $CHANNEL " ]=true
180
184
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 :
182
187
done
183
188
fi
184
189
;;
185
190
PING)
186
- reply PONG " ${arr[0]} "
191
+ reply PONG " $NICK " " $ {arr[0]}"
187
192
;;
188
193
* )
189
194
debug " UNRECOGNIZED COMMAND"
192
197
esac
193
198
done
194
199
# echo "connection closed with $TCPREMOTEIP" 1>&2
195
- for SUB in " ${! SUBS[@]} " ; do
196
- unsubscribe " $SUB "
197
- done
200
+ unsubscribe
198
201
fi
0 commit comments