]> Pileus Git - ~andy/rhawk/blob - irc.awk
Convert privmsg to say
[~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                 status = system("awk -f rhawk -v CHILD=1 -v FIRST=1");
105                 while (status)
106                         status = system("awk -f rhawk -v CHILD=1");
107                 exit(0);
108         } else {
109                 debug("Starting child: CHILD=" CHILD " FIRST=" FIRST);
110         }
111 }
112
113 function quit() {
114         exit(0)
115 }
116
117 function reload() {
118         exit(1)
119 }
120
121 # Input parsing
122 // {
123         gsub(/\s+/,     " ")
124         gsub(/^ | $/,    "")
125         gsub(/\3[0-9]*/, "")
126         match($0, /(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))/, arr);
127         SRC = arr[2]
128         CMD = arr[4]
129         DST = arr[6]
130         ARG = arr[8]
131         MSG = arr[10]
132
133         match(SRC, /([^! ]+)!/, arr);
134         FROM = arr[1]
135
136         match(MSG, /(([^ :,]*)[:,] *)?(.*)/, arr);
137         TO  = arr[2]
138         $0  = TO == NICK ? arr[3] : MSG
139
140         if (CMD == "PRIVMSG" && DST == NICK && FROM)
141                 TO = DST
142
143         #set()
144 }
145
146 # IRC client
147 CMD == "001" && MSG ~ /Welcome/ {
148         join(CHANNEL)
149 }
150
151 CMD == "PING" {
152         send("PING " MSG)
153 }
154
155 CMD == "332" {
156         CMD = "TOPIC"
157         DST = ARG
158 }
159
160 CMD == "TOPIC" {
161         topics[DST] = MSG
162 }