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