]> Pileus Git - ~andy/rhawk/blob - irc.awk
Add debug command and update test cases
[~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         if (!DEBUG)
25                 system("sleep 1")
26         fflush()
27 }
28
29 // {
30         #print ""         > "/dev/stderr"
31         print "  < " $0  > "/dev/stderr"
32 }
33
34 function debug(msg) {
35         print "  # " msg > "/dev/stderr"
36         fflush()
37 }
38
39 function set() {
40         debug("CMD:  [" CMD  "]")
41         debug("SRC:  [" SRC  "]")
42         debug("DST:  [" DST  "]")
43         debug("FROM: [" FROM "]")
44         debug("TO:   [" TO   "]")
45         debug("ARG:  [" ARG  "]")
46         debug("MSG:  [" MSG  "]")
47         debug("$0:   [" $0   "]")
48 }
49
50 # Functions
51 function connect(server, nick, channel) {
52         SERVER  = server
53         NICK    = nick
54         CHANNEL = channel
55         if (FIRST) {
56                 "whoami"   | getline _name
57                 "hostname" | getline _host
58                 send("USER " _name " " _host " " server " :" nick)
59                 send("NICK " nick)
60         }
61 }
62
63 function say(to, msg) {
64         if (msg == "") {
65                 msg = to
66                 if (DST ~ "^#")
67                         to = DST
68                 else if (DST == NICK && FROM)
69                         to = FROM
70                 else
71                         to = CHANNEL
72         }
73         send("PRIVMSG " to " :" msg)
74 }
75
76 function action(to, msg)
77 {
78         if (msg)
79                 say(to, "\001ACTION " msg "\001")
80         else
81                 say("\001ACTION " to "\001")
82 }
83
84 function reply(msg) {
85         say(FROM ": " msg)
86 }
87
88 function join(chan) {
89         send("JOIN " chan)
90         send("TOPIC " chan)
91 }
92
93 function part(chan) {
94         send("PART " chan)
95 }
96
97 function topic(chan, msg) {
98         send("TOPIC " chan " :" msg)
99 }
100
101 # Reloading
102 BEGIN {
103         if (CHILD == "") {
104                 debug("Starting server");
105                 cmd = "awk -f rhawk" \
106                       " -v CHILD=1" \
107                       " -v START=" systime() \
108                       " -v DEBUG=" !!DEBUG
109                 status = system(cmd " -v FIRST=1");
110                 while (status)
111                         status = system(cmd);
112                 exit(0);
113         } else {
114                 debug("Starting child:" \
115                       " DEBUG=" DEBUG   \
116                       " CHILD=" CHILD   \
117                       " START=" START   \
118                       " FIRST=" FIRST);
119         }
120 }
121
122 function quit() {
123         exit(0)
124 }
125
126 function reload() {
127         exit(1)
128 }
129
130 # Input parsing
131 // {
132         gsub(/\s+/,     " ")
133         gsub(/^ | $/,    "")
134         gsub(/\3[0-9]*/, "")
135         match($0, /(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))/, arr);
136         SRC = arr[2]
137         CMD = arr[4]
138         DST = arr[6]
139         ARG = arr[8]
140         MSG = arr[10]
141
142         match(SRC, /([^! ]+)!/, arr);
143         FROM = arr[1]
144
145         match(MSG, /(([^ :,]*)[:,] *)?(.*)/, arr);
146         TO  = arr[2]
147         $0  = TO ? arr[3] : MSG
148
149         if (CMD == "PRIVMSG" && DST == NICK && FROM && !TO)
150                 TO = DST
151
152         #set()
153 }
154
155 # IRC client
156 CMD == "001" && MSG ~ /Welcome/ {
157         join(CHANNEL)
158 }
159
160 CMD == "PING" {
161         send("PING " MSG)
162 }
163
164 CMD == "332" {
165         CMD = "TOPIC"
166         DST = ARG
167 }
168
169 CMD == "TOPIC" {
170         topics[DST] = MSG
171 }