]> git.lizzy.rs Git - luairc.git/blob - test/test.lua
Port to Lua 5.3
[luairc.git] / test / test.lua
1 #!/usr/bin/lua
2
3 local irc = require "irc"
4 local dcc = require "irc.dcc"
5
6 irc.DEBUG = true
7
8 local ip_prog = io.popen("get_ip")
9 local ip = ip_prog:read()
10 ip_prog:close()
11 irc.set_ip(ip)
12
13 local function print_state()
14     for chan in irc.channels() do
15         print(chan..": Channel ops: "..table.concat(chan:ops(), " "))
16         print(chan..": Channel voices: "..table.concat(chan:voices(), " "))
17         print(chan..": Channel normal users: "..table.concat(chan:users(), " "))
18         print(chan..": All channel members: "..table.concat(chan:members(), " "))
19     end
20 end
21
22 local function on_connect()
23     print("Joining channel #doytest...")
24     irc.join("#doytest")
25     print("Joining channel #doytest2...")
26     irc.join("#doytest2")
27 end
28 irc.register_callback("connect", on_connect)
29
30 local function on_me_join(chan)
31     print("Join to " .. chan .. " complete.")
32     print(chan .. ": Channel type: " .. chan.chanmode)
33     if chan.topic.text and chan.topic.text ~= "" then
34         print(chan .. ": Channel topic: " .. chan.topic.text)
35         print("  Set by " .. chan.topic.user ..
36               " at " .. os.date("%c", chan.topic.time))
37     end
38     irc.act(chan.name, "is here")
39     print_state()
40 end
41 irc.register_callback("me_join", on_me_join)
42
43 local function on_join(chan, user)
44     print("I saw a join to " .. chan)
45     if tostring(user) ~= "doylua" then
46         irc.say(tostring(chan), "Hi, " .. user)
47     end
48     print_state()
49 end
50 irc.register_callback("join", on_join)
51
52 local function on_part(chan, user, part_msg)
53     print("I saw a part from " .. chan .. " saying " .. part_msg)
54     print_state()
55 end
56 irc.register_callback("part", on_part)
57
58 local function on_nick_change(new_nick, old_nick)
59     print("I saw a nick change: "  ..  old_nick .. " -> " .. new_nick)
60     print_state()
61 end
62 irc.register_callback("nick_change", on_nick_change)
63
64 local function on_kick(chan, user)
65     print("I saw a kick in " .. chan)
66     print_state()
67 end
68 irc.register_callback("kick", on_kick)
69
70 local function on_quit(chan, user)
71     print("I saw a quit from " .. chan)
72     print_state()
73 end
74 irc.register_callback("quit", on_quit)
75
76 local function whois_cb(cb_data)
77     print("WHOIS data for " .. cb_data.nick)
78     if cb_data.user then print("Username: " .. cb_data.user) end
79     if cb_data.host then print("Host: " .. cb_data.host) end
80     if cb_data.realname then print("Realname: " .. cb_data.realname) end
81     if cb_data.server then print("Server: " .. cb_data.server) end
82     if cb_data.serverinfo then print("Serverinfo: " .. cb_data.serverinfo) end
83     if cb_data.away_msg then print("Awaymsg: " .. cb_data.away_msg) end
84     if cb_data.is_oper then print(nick .. "is an IRCop") end
85     if cb_data.idle_time then print("Idletime: " .. cb_data.idle_time) end
86     if cb_data.channels then
87         print("Channel list for " .. cb_data.nick .. ":")
88         for _, channel in ipairs(cb_data.channels) do print(channel) end
89     end
90 end
91
92 local function serverversion_cb(cb_data)
93     print("VERSION data for " .. cb_data.server)
94     print("Version: " .. cb_data.version)
95     print("Comments: " .. cb_data.comments)
96 end
97
98 local function ping_cb(cb_data)
99     print("CTCP PING for " .. cb_data.nick)
100     print("Roundtrip time: " .. cb_data.time .. "s")
101 end
102
103 local function time_cb(cb_data)
104     print("CTCP TIME for " .. cb_data.nick)
105     print("Localtime: " .. cb_data.time)
106 end
107
108 local function version_cb(cb_data)
109     print("CTCP VERSION for " .. cb_data.nick)
110     print("Version: " .. cb_data.version)
111 end
112
113 local function stime_cb(cb_data)
114     print("TIME for " .. cb_data.server)
115     print("Server time: " .. cb_data.time)
116 end
117
118 local function on_channel_msg(chan, from, msg)
119     if from == "doy" then
120         if msg == "leave" then
121             irc.part(chan.name)
122             return
123         elseif msg:sub(1, 3) == "op " then
124             chan:op(msg:sub(4))
125             return
126         elseif msg:sub(1, 5) == "deop " then
127             chan:deop(msg:sub(6))
128             return
129         elseif msg:sub(1, 6) == "voice " then
130             chan:voice(msg:sub(7))
131             return
132         elseif msg:sub(1, 8) == "devoice " then
133             chan:devoice(msg:sub(9))
134             return
135         elseif msg:sub(1, 5) == "kick " then
136             chan:kick(msg:sub(6))
137             return
138         elseif msg:sub(1, 5) == "send " then
139             dcc.send(from, msg:sub(6))
140             return
141         elseif msg:sub(1, 6) == "whois " then
142             irc.whois(whois_cb, msg:sub(7))
143             return
144         elseif msg:sub(1, 8) == "sversion" then
145             irc.server_version(serverversion_cb)
146             return
147         elseif msg:sub(1, 5) == "ping " then
148             irc.ctcp_ping(ping_cb, msg:sub(6))
149             return
150         elseif msg:sub(1, 5) == "time " then
151             irc.ctcp_time(time_cb, msg:sub(6))
152             return
153         elseif msg:sub(1, 8) == "version " then
154             irc.ctcp_version(version_cb, msg:sub(9))
155             return
156         elseif msg:sub(1, 5) == "stime" then
157             irc.server_time(stime_cb)
158             return
159         elseif msg:sub(1, 6) == "trace " then
160             irc.trace(trace_cb, msg:sub(7))
161             return
162         elseif msg:sub(1, 5) == "trace" then
163             irc.trace(trace_cb)
164             return
165         end
166     end
167     if from ~= "doylua" then
168         irc.say(chan.name, from .. ": " .. msg)
169     end
170 end
171 irc.register_callback("channel_msg", on_channel_msg)
172
173 local function on_private_msg(from, msg)
174     if from == "doy" then
175         if msg == "leave" then
176             irc.quit("gone")
177             return
178         elseif msg:sub(1, 5) == "send " then
179             dcc.send(from, msg:sub(6))
180             return
181         end
182     end
183     if from ~= "doylua" then
184         irc.say(from, msg)
185     end
186 end
187 irc.register_callback("private_msg", on_private_msg)
188
189 local function on_channel_act(chan, from, msg)
190     irc.act(chan.name, "jumps on " .. from)
191 end
192 irc.register_callback("channel_act", on_channel_act)
193
194 local function on_private_act(from, msg)
195     irc.act(from, "jumps on you")
196 end
197 irc.register_callback("private_act", on_private_act)
198
199 local function on_op(chan, from, nick)
200     print(nick .. " was opped in " .. chan .. " by " .. from)
201     print_state()
202 end
203 irc.register_callback("op", on_op)
204
205 local function on_deop(chan, from, nick)
206     print(nick .. " was deopped in " .. chan .. " by " .. from)
207     print_state()
208 end
209 irc.register_callback("deop", on_deop)
210
211 local function on_voice(chan, from, nick)
212     print(nick .. " was voiced in " .. chan .. " by " .. from)
213     print_state()
214 end
215 irc.register_callback("voice", on_voice)
216
217 local function on_devoice(chan, from, nick)
218     print(nick .. " was devoiced in " .. chan .. " by " .. from)
219     print_state()
220 end
221 irc.register_callback("devoice", on_devoice)
222
223 local function on_dcc_send()
224     return true
225 end
226 irc.register_callback("dcc_send", on_dcc_send)
227
228 irc.connect{network = "irc.libera.chat", nick = "doylua"}