Skip to content

Commit 3009765

Browse files
committed
humble beginnings
0 parents  commit 3009765

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sqlite

server

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env bash
2+
3+
set noglob
4+
5+
ORM_DB=db.sqlite
6+
VERSION=0.0.1
7+
IRC_SERVER_HOST=localhost
8+
9+
# self explanatory
10+
function query() {
11+
function _query() {
12+
local QUERY
13+
QUERY="$1"
14+
shift
15+
printf ".parameter init\n"
16+
INDEX=1
17+
for arg in "$@"; do
18+
: "${arg//\\/\\\\\\\\}"
19+
: "${_//$'\t'/ }"
20+
: "${_//\"/\\\"}"
21+
: "${_//\'/\\\'}"
22+
: "${_//$'\n'/\\\\n}"
23+
if [[ "$_" =~ "'" ]]; then
24+
printf ".parameter set ?%d \"%s\"\n" $INDEX "$_"
25+
else
26+
printf ".parameter set ?%d \"'%s'\"\n" $INDEX "$_"
27+
fi
28+
((INDEX++))
29+
done
30+
printf "%s\n" "$QUERY";
31+
}
32+
sqlite3 -separator $'\t' "$ORM_DB" < <(_query "$@")
33+
}
34+
35+
split() {
36+
# Usage: split "string" "delimiter"
37+
IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
38+
}
39+
40+
reply() {
41+
local CODE="$1"
42+
shift
43+
local RESPONSE
44+
printf -v "RESPONSE" ":%s %s %s %s\r\n" "$IRC_SERVER_HOST" "$CODE" "$NICK" "$@"
45+
RESPONSE="${RESPONSE:0:512}"
46+
printf "%s" "$RESPONSE"
47+
debug "REPLYING: $RESPONSE"
48+
}
49+
50+
debug() {
51+
printf "%s\n" "$@" 1>&2
52+
}
53+
54+
if [[ -z "$IRC_CONN_HANDLER" ]]; then
55+
56+
query 'CREATE TABLE IF NOT EXISTS channels
57+
(
58+
id INTEGER PRIMARY KEY AUTOINCREMENT,
59+
name varchar(201)
60+
);'
61+
62+
echo "i am the server" 1>&2
63+
PORT=6667
64+
echo -n "Starting server on port "
65+
66+
export IRC_CONN_HANDLER=true
67+
export IRC_SERVER_CREATED="$(date)"
68+
tcpserver -1 -o -l 0 -H -R -c 1000 0 $PORT $0
69+
else
70+
echo "connection opened with $TCPREMOTEIP" 1>&2
71+
while IFS= read -r line; do
72+
split "$line" ' '
73+
if [[ "${arr[0]}" =~ :(.+) ]]; then
74+
PREFIX="${BASH_REMATCH[1]}"
75+
debug "PREFIX: '$PREFIX'"
76+
arr=("${arr[@]:1}")
77+
fi
78+
COMMAND="${arr[0]}"
79+
debug "COMMAND: '$COMMAND'"
80+
arr=("${arr[@]:1}")
81+
case "${COMMAND^^}" in
82+
NICK)
83+
NICK="${arr[0]//$'\r'}"
84+
;;
85+
USER)
86+
USER="${arr[0]}"
87+
MODE="${arr[1]}"
88+
REALNAME="${arr[3]//$'\r'}"
89+
reply 001 ":Welcome, $NICK!$USER"
90+
reply 002 ":Your host is $IRC_SERVER_HOST, running version $VERSION"
91+
reply 003 ":This server was created $IRC_SERVER_CREATED"
92+
reply 004 "$IRC_SERVER_HOST bash-$VERSION"
93+
;;
94+
*)
95+
debug "UNRECOGNIZED COMMAND"
96+
debug "${arr[@]}"
97+
;;
98+
esac
99+
done
100+
echo "connection closed with $TCPREMOTEIP" 1>&2
101+
fi

0 commit comments

Comments
 (0)