2 # socat 'TCP:localhost:12345' 'EXEC:awk -f $0'
4 # Pre-defined variables:
7 # SERVER: IRC Server that is connected to
8 # NICK: Nickname given by the bot
9 # CHANNEL: Channel the bot is currently in
11 # When a messages is recieved:
12 # CMD: Message, e.g. PRIVMSG
13 # SRC: Source of the message
14 # DST: Destination of the message, e.g. the channel
15 # TO: Nickname the message was addressed to
16 # FROM: Nickname of the user who sent the message
22 print " > " msg > "/dev/stderr"
28 #print "" > "/dev/stderr"
29 print " < " $0 > "/dev/stderr"
33 print " # " msg > "/dev/stderr"
38 debug("CMD: [" CMD "]")
39 debug("SRC: [" SRC "]")
40 debug("DST: [" DST "]")
41 debug("FROM: [" FROM "]")
43 debug("MSG: [" MSG "]")
46 debug("ARG"i": [" ARG[i] "]")
50 function connect(server, nick, channel, auth, pass) {
55 "whoami" | getline _name
56 "hostname" | getline _host
57 send("USER " _name " " _host " " server " :" nick)
59 send("CAP REQ :account-notify")
60 send("CAP REQ :extended-join")
62 say("NickServ", "IDENTIFY " pass)
68 function say(to, msg) {
73 else if (DST == NICK && FROM)
78 send("PRIVMSG " to " :" msg)
83 function action(to, msg)
86 say(to, "\001ACTION " msg "\001")
88 say("\001ACTION " to "\001")
98 send("WHO " chan " %uhnar")
101 function part(chan) {
105 function topic(chan, msg) {
106 send("TOPIC " chan " :" msg)
112 debug("Starting server");
113 cmd = "awk -f rhawk" \
115 " -v START=" systime() \
117 status = system(cmd " -v FIRST=1");
119 status = system(cmd);
122 debug("Starting child:" \
143 match($0, /(:([^ ]+) )?([A-Z0-9]+)(( [^:][^ ]*)*)( :(.*))?/, arr);
144 sub(/^ /, "", arr[4])
152 match(SRC, /([^! ]+)!([^@ ]+)@([^ ]+\/[^ ]+)?/, arr);
157 match(MSG, /(([^ :,]*)[:,] *)?(.*)/, arr);
159 $0 = TO ? arr[3] : MSG
161 if (CMD == "PRIVMSG" && DST == NICK && FROM && !TO)
165 AUTH = USERS[FROM]["auth"]
173 CMD == "001" && MSG ~ /Welcome/ {
193 for (i=1; i<=NF; i++)
194 send("WHO " $i " %uhnar")
198 _auth = ARG[1] == "*" ? 0 : ARG[1]
199 USERS[FROM]["auth"] = _auth
203 _auth = ARG[5] == "*" ? 0 : ARG[5]
204 USERS[ARG[4]]["user"] = ARG[2]
205 USERS[ARG[4]]["host"] = ARG[3]
206 USERS[ARG[4]]["nick"] = ARG[4]
207 USERS[ARG[4]]["auth"] = _auth
208 USERS[ARG[4]]["real"] = MSG
212 _auth = ARG[2] == "*" ? 0 : ARG[2]
213 USERS[FROM]["user"] = USER
214 USERS[FROM]["host"] = HOST
215 USERS[FROM]["nick"] = FROM
216 USERS[FROM]["auth"] = _auth
217 USERS[FROM]["real"] = MSG