]> Pileus Git - ~andy/rhawk/blob - irc.awk
Misc updates
[~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
62 function say(to, msg) {
63         if (msg == "") {
64                 msg = to
65                 if (DST ~ "^#")
66                         to = DST
67                 else if (DST == NICK && FROM)
68                         to = FROM
69                 else
70                         to = CHANNEL
71         }
72         send("PRIVMSG " to " :" msg)
73 }
74
75 function action(to, msg)
76 {
77         if (msg)
78                 say(to, "\001ACTION " msg "\001")
79         else
80                 say("\001ACTION " to "\001")
81 }
82
83 function reply(msg) {
84         say(FROM ": " msg)
85 }
86
87 function join(chan) {
88         send("JOIN " chan)
89         send("TOPIC " chan)
90 }
91
92 function part(chan) {
93         send("PART " chan)
94 }
95
96 function topic(chan, msg) {
97         send("TOPIC " chan " :" msg)
98 }
99
100 # Reloading
101 BEGIN {
102         if (CHILD == "") {
103                 debug("Starting server");
104                 cmd = "awk -f rhawk -v CHILD=1 -v START=" systime();
105                 status = system(cmd " -v FIRST=1");
106                 while (status)
107                         status = system(cmd);
108                 exit(0);
109         } else {
110                 debug("Starting child:" \
111                       " CHILD=" CHILD   \
112                       " START=" START   \
113                       " FIRST=" FIRST);
114         }
115 }
116
117 function quit() {
118         exit(0)
119 }
120
121 function reload() {
122         exit(1)
123 }
124
125 # Input parsing
126 // {
127         gsub(/\s+/,     " ")
128         gsub(/^ | $/,    "")
129         gsub(/\3[0-9]*/, "")
130         match($0, /(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))/, arr);
131         SRC = arr[2]
132         CMD = arr[4]
133         DST = arr[6]
134         ARG = arr[8]
135         MSG = arr[10]
136
137         match(SRC, /([^! ]+)!/, arr);
138         FROM = arr[1]
139
140         match(MSG, /(([^ :,]*)[:,] *)?(.*)/, arr);
141         TO  = arr[2]
142         $0  = TO ? arr[3] : MSG
143
144         if (CMD == "PRIVMSG" && DST == NICK && FROM && !TO)
145                 TO = DST
146
147         #set()
148 }
149
150 # IRC client
151 CMD == "001" && MSG ~ /Welcome/ {
152         join(CHANNEL)
153 }
154
155 CMD == "PING" {
156         send("PING " MSG)
157 }
158
159 CMD == "332" {
160         CMD = "TOPIC"
161         DST = ARG
162 }
163
164 CMD == "TOPIC" {
165         topics[DST] = MSG
166 }