-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathel_robot.sh
executable file
·88 lines (71 loc) · 2.16 KB
/
el_robot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
# Adapted from https://github.com/joshcartwright/bashbot
# Setting timezone to GST
export TZ='GST-0'
# Helper functions for colorized messages
function error { printf >&2 '[1;31mERROR: %s[0m\n' "$*" ;}
function success { printf >&2 '[1;32mSUCCESS: %s[0m\n' "$*" ;}
function warning { printf >&2 '[1;33mWARNING: %s[0m\n' "$*" ;}
function prompt { printf >&2 '[1;36m%s[0m\n' "$*" ;}
# Helper functions for commmunicating with the IRC server
function recv {
if [[ "$@" =~ "PRIVMSG $nick :" ]] ; then
echo "[35m← $(date +%FT%TZ$TZ): $@[0m" >&2
elif [[ "$@" =~ "PRIVMSG #" ]] ; then
echo "[32m← $(date +%FT%TZ$TZ): $@[0m" >&2
elif [[ "$@" =~ [Nn]ick[Ss]erv.*NOTICE\ $nick\ :This\ nickname\ is\ registered.* ]] ; then
echo "[33m← $(date +%FT%TZ$TZ): $@[0m" >&2
identify_with_nickserv
else
echo "[34m← $(date +%FT%TZ$TZ): $@[0m" >&2
fi
}
function send {
echo "[1;34m→ $(date +%FT%TZ$TZ): $@[0m" >&2
printf '%s\r\n' "$@" >&3
}
export -f send
function identify_with_nickserv {
send "PRIVMSG nickserv :identify $secret"
}
# Remaining function declarations
function quit { prompt "Exiting..." ; exit 0 ;}
# Load configuration
if [ -f ./el_robot.rc ] ; then
. ./el_robot.rc
else
error "Could not load configuration from ./el_robot.rc"
quit
fi
# IRC Commands
ircNICK="NICK $nick"
ircUSER="USER $nick $hostName 8 :$realName"
ircQUIT='QUIT'
# Open connection to IRC network
exec 3<> /dev/tcp/${server}/${port} || { error "Could not connect to ${server}:${port}" ; quit ;}
# Login to IRC network
send "$ircUSER"
send "$ircNICK"
# Login to initial channels
for channel in $channels ; do
send "JOIN :$channel"
done
# Continuously listen to server
while read -r response ; do
response="${response%%$'\r'}" # Strip trailing carriage return
recv $response
# This converts the response into positional parameters
set -- $response
case "$@" in
# This keeps the connect to the IRC server alive
"PING "*)
send "PONG $2"
continue ;;
"PRIVMSG $nick :"*)
# private message to bot
channel=$name
prefix="PRIVMSG $name "
set -- "${3#:}" "${@:4}"
;;
esac
done <&3