-
Notifications
You must be signed in to change notification settings - Fork 1
/
bashbot2
executable file
·138 lines (106 loc) · 3.54 KB
/
bashbot2
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# Default values for things
nick=IrcRoboto22
name=kebab
channel=ircRobotoTestChannel
server=irc.freenode.net
port=6667
fileToTail=''
readonly modulePath="${0%/*}/modules"
# there seems to be a bug(?) in bash which makes it impossible to declare a
# global array in an included script. This is needed in the IRC module
declare -A privMsg
import() {
# @desc import a module. Which should be another bash script.
# @param $1 is the script to import
local module="$1"
if [[ -f "$modulePath/$module" ]]; then
. "$modulePath/$module"
else
printf '%s\n' "Module $modulePath/$module does not exist" >&2
exit 1;
fi
}
import 'string'
import 'irc'
function usage() {
read -r -d '' usageText << EOF
Usage:
${0##*/} [OPTIONS...]
Options:
-s, --server $(string.underline SERVER)
IP or domain name of the IRC server
-p, --port $(string.underline PORT)
Which port to connect to
-c, --channel $(string.underline CHANNEL)
Name of the channel this bot should join
-n, --nick $(string.underline NICK)
Nickname this bot sould use
-r, --real-name $(string.underline REALNAME)
Real name this bot sould use
-t, --tail-file $(string.underline FILE)
Tail $(string.underline FILE) and output the conetent to the channel
-d, --debug
Print debugging messages
-h, --help
Print this message
EOF
printf '%s\n\n' "$usageText"
exit 0
}>&2
TEMP=$(getopt -o s:p:n:r:c:t:hd --long server:,port:,nick:,real-name:,\
channel:,tail-file:,help,debug -n 'bashbot2.sh' -- "$@")
if [[ $? != 0 ]]; then
printf '%s\n' "Terminating..." >&2 ; exit 1 ;
fi
eval set -- "$TEMP"
while true ; do
case "$1" in
-s|--server) server=$2; shift 2 ;;
-p|--port) port=$2; shift 2;;
-n|--nick) nick=$2; shift 2;;
-r|--real-name) name=$2; shift 2;;
-c|--channel) channel=$2; shift 2;;
-t|--tail-file) fileToTail=$2; shouldTailFile=1; shift 2;;
-d|--debug) debug=1; shift;;
-h|--help) usage; shift;;
--) shift; break;;
*) printf '%s\n' "Parsing error" >&2 ; exit 1;;
esac
done
debugMsg() {
# @desc Print a noticable debug message to stdout
# @param Takes all parameters and as message
# if debug is not set, just abort this function
[[ ! $debug ]] && return
local debugMessage="$@"
printf '%s\n' "$(string.cyan 'DEBUG:') $debugMessage"
}
onExit() {
# @desc Will send QUIT to IRC server, remove temporary tail file if there is
# @desc any, kill forks and close the socket.
kill 0
irc.sendToServer 'QUIT :Shutting down'
[[ $shouldTailFile ]] && rm -f ./$fileToTail
exec 3<&-
exec 3>&-
debugMsg "Exit caught. Cleaning up and closing connection."
}
# run onExit when exiting/stopping the script
trap onExit EXIT
irc.connectToServer "$server" "$port"
irc.identify "$nick" "$name" "$channel"
# if -t is set, listen the file
[[ $shouldTailFile ]] && irc.listenToFile
# read every response from the server and handle it line by line
while read -r line; do
# Line needs to be trimmed of spaces or it will be impossible to parse.
line=$(string.trim "$line")
msgType=$(irc.getMsgType "$line")
printf '%s\n' "$(string.green '>') $line"
case "$msgType" in
PING) irc.handlePing "$line";;
PRIVMSG) irc.handlePrivMsg "$line";;
MODE) irc.handleMode "$line";;
esac
done <&3