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