]> Pileus Git - ~andy/rhawk/blob - irc.awk
better
[~andy/rhawk] / irc.awk
1 # Connect with:
2 #   socat 'TCP:localhost:12345' 'EXEC:awk -f $0'
3 #
4 # Pre-defined variables:
5 #
6 # When connected:
7 #   SERVER:  IRC Server that is connected to
8 #   NICK:    Nickname given by the bot
9 #   CHANNEL: Channel the bot is currently in
10 #
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
17 #   MSG:     Message sent
18 #   $0:      Message sans TO
19
20 # Debugging
21 function send(msg) {
22         print "  > " msg > "/dev/stderr"
23         print msg
24         system("sleep 1")
25         fflush()
26 }
27
28 // {
29         #print ""         > "/dev/stderr"
30         print "  < " $0  > "/dev/stderr"
31 }
32
33 function debug(msg) {
34         print "  # " msg > "/dev/stderr"
35         fflush()
36 }
37
38 function set() {
39         debug("CMD:  " CMD)
40         debug("SRC:  " SRC)
41         debug("DST:  " DST)
42         debug("FROM: " FROM)
43         debug("TO:   " TO)
44         debug("MSG:  " MSG)
45 }
46
47 # Functions
48 function connect(server, nick, channel) {
49         SERVER  = server
50         NICK    = nick
51         CHANNEL = channel
52         if (FIRST) {
53                 "whoami"   | getline _name
54                 "hostname" | getline _host
55                 send("USER " _name " " _host " " server " :" nick)
56                 send("NICK " nick)
57         }
58 }
59 function privmsg(to, msg) {
60         send("PRIVMSG " to " :" msg)
61 }
62 function say(msg) {
63         if (DST ~ "^#")
64                 privmsg(DST, msg)
65         else if (DST == NICK && FROM)
66                 privmsg(FROM, msg)
67         else
68                 privmsg(CHANNEL, msg)
69 }
70
71 function reply(msg) {
72         say(FROM ": " msg)
73 }
74
75 function join(chan) {
76         send("JOIN " chan)
77         send("TOPIC " chan)
78 }
79
80 function part(chan) {
81         send("PART " chan)
82 }
83
84 function topic(chan, msg) {
85         send("TOPIC " chan " :" msg)
86 }
87
88 # Reloading
89 BEGIN {
90         if (CHILD == "") {
91                 debug("Starting server");
92                 status = system("awk -f rhawk -v CHILD=1 -v FIRST=1");
93                 while (status)
94                         status = system("awk -f rhawk -v CHILD=1");
95                 exit(0);
96         } else {
97                 debug("Starting child: CHILD=" CHILD " FIRST=" FIRST);
98         }
99 }
100
101 function quit() {
102         exit(0)
103 }
104
105 function reload() {
106         exit(1)
107 }
108
109 # Input parsing
110 // {
111         match($0, /(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?([^:]*:(.*))/, arr);
112         gsub(/\s+/,     " ", arr[8])
113         gsub(/^ | $/,    "", arr[8])
114         gsub(/\3[0-9]*/, "", arr[8])
115         SRC = arr[2]
116         CMD = arr[4]
117         DST = arr[6]
118         MSG = arr[8]
119
120         match(SRC, /([^! ]+)!/, arr);
121         FROM = arr[1]
122
123         match(MSG, /(([^ :,]*)[:,] *)?(.*)/, arr);
124         TO  = arr[2]
125         $0  = TO == NICK ? arr[3] : MSG
126
127         if (CMD == "PRIVMSG" && DST == NICK && FROM)
128                 TO = DST
129 }
130
131 # IRC client
132 CMD == "001" && MSG ~ /Welcome/ {
133         join(CHANNEL)
134 }
135
136 CMD == "PING" {
137         send("PING " MSG)
138 }
139
140 CMD == "332" ||
141 CMD == "TOPIC" {
142         topics[DST] = MSG
143 }