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
30 print " # " msg > "/dev/stderr"
45 "whoami" | getline _name
46 "hostname" | getline _host
47 send("USER " _name " " _host " " server " :" nick)
51 function connect(server, port, nick, channel) {
58 child = "awk -f rhawk -v CHILD=1 -v FIRST=1"
59 socket = "/inet/tcp/0/" SERVER "/" PORT
61 debug("Starting server");
67 if (!(fd = select("from", child, socket))) {
71 if (!(st = fd |& getline line)) {
86 debug("Starting child: CHILD=" CHILD " FIRST=" FIRST);
100 function privmsg(to, msg) {
101 send("PRIVMSG " to " :" msg)
107 else if (DST == NICK && FROM)
110 privmsg(CHANNEL, msg)
113 function reply(msg) {
117 function join(chan) {
122 function part(chan) {
126 function topic(chan, msg) {
127 send("TOPIC " chan " :" msg)
132 match($0, /(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))/, arr);
133 gsub(/\s+/, " ", arr[8])
134 gsub(/^ | $/, "", arr[8])
135 gsub(/\3[0-9]*/, "", arr[8])
142 match(SRC, /([^! ]+)!/, arr);
145 match(MSG, /(([^ :,]*)[:,] *)?(.*)/, arr);
147 $0 = TO == NICK ? arr[3] : MSG
149 if (CMD == "PRIVMSG" && DST == NICK && FROM)
154 CMD == "001" && MSG ~ /Welcome/ {