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