aboutsummaryrefslogtreecommitdiffstats
path: root/test/test.lua
blob: 02ed4dac8460700ceb629f0082a03b8004b44a7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/lua

local irc = require "irc"
local dcc = require "irc.dcc"

irc.DEBUG = true

local ip_prog = io.popen("get_ip")
local ip = ip_prog:read()
ip_prog:close()
irc.set_ip(ip)

local function print_state()
    for chan in irc.channels() do
        print(chan..": Channel ops: "..table.concat(chan:ops(), " "))
        print(chan..": Channel voices: "..table.concat(chan:voices(), " "))
        print(chan..": Channel normal users: "..table.concat(chan:users(), " "))
        print(chan..": All channel members: "..table.concat(chan:members(), " "))
    end
end

local function on_connect()
    print("Joining channel #doytest...")
    irc.join("#doytest")
    print("Joining channel #doytest2...")
    irc.join("#doytest2")
end
irc.register_callback("connect", on_connect)

local function on_me_join(chan)
    print("Join to " .. chan .. " complete.")
    print(chan .. ": Channel type: " .. chan.chanmode)
    if chan.topic.text and chan.topic.text ~= "" then
        print(chan .. ": Channel topic: " .. chan.topic.text)
        print("  Set by " .. chan.topic.user ..
              " at " .. os.date("%c", chan.topic.time))
    end
    irc.act(chan.name, "is here")
    print_state()
end
irc.register_callback("me_join", on_me_join)

local function on_join(chan, user)
    print("I saw a join to " .. chan)
    if tostring(user) ~= "doylua" then
        irc.say(tostring(chan), "Hi, " .. user)
    end
    print_state()
end
irc.register_callback("join", on_join)

local function on_part(chan, user, part_msg)
    print("I saw a part from " .. chan .. " saying " .. part_msg)
    print_state()
end
irc.register_callback("part", on_part)

local function on_nick_change(new_nick, old_nick)
    print("I saw a nick change: "  ..  old_nick .. " -> " .. new_nick)
    print_state()
end
irc.register_callback("nick_change", on_nick_change)

local function on_kick(chan, user)
    print("I saw a kick in " .. chan)
    print_state()
end
irc.register_callback("kick", on_kick)

local function on_quit(chan, user)
    print("I saw a quit from " .. chan)
    print_state()
end
irc.register_callback("quit", on_quit)

local function whois_cb(cb_data)
    print("WHOIS data for " .. cb_data.nick)
    if cb_data.user then print("Username: " .. cb_data.user) end
    if cb_data.host then print("Host: " .. cb_data.host) end
    if cb_data.realname then print("Realname: " .. cb_data.realname) end
    if cb_data.server then print("Server: " .. cb_data.server) end
    if cb_data.serverinfo then print("Serverinfo: " .. cb_data.serverinfo) end
    if cb_data.away_msg then print("Awaymsg: " .. cb_data.away_msg) end
    if cb_data.is_oper then print(nick .. "is an IRCop") end
    if cb_data.idle_time then print("Idletime: " .. cb_data.idle_time) end
    if cb_data.channels then
        print("Channel list for " .. cb_data.nick .. ":")
        for _, channel in ipairs(cb_data.channels) do print(channel) end
    end
end

local function serverversion_cb(cb_data)
    print("VERSION data for " .. cb_data.server)
    print("Version: " .. cb_data.version)
    print("Comments: " .. cb_data.comments)
end

local function ping_cb(cb_data)
    print("CTCP PING for " .. cb_data.nick)
    print("Roundtrip time: " .. cb_data.time .. "s")
end

local function time_cb(cb_data)
    print("CTCP TIME for " .. cb_data.nick)
    print("Localtime: " .. cb_data.time)
end

local function version_cb(cb_data)
    print("CTCP VERSION for " .. cb_data.nick)
    print("Version: " .. cb_data.version)
end

local function stime_cb(cb_data)
    print("TIME for " .. cb_data.server)
    print("Server time: " .. cb_data.time)
end

local function on_channel_msg(chan, from, msg)
    if from == "doy" then
        if msg == "leave" then
            irc.part(chan.name)
            return
        elseif msg:sub(1, 3) == "op " then
            chan:op(msg:sub(4))
            return
        elseif msg:sub(1, 5) == "deop " then
            chan:deop(msg:sub(6))
            return
        elseif msg:sub(1, 6) == "voice " then
            chan:voice(msg:sub(7))
            return
        elseif msg:sub(1, 8) == "devoice " then
            chan:devoice(msg:sub(9))
            return
        elseif msg:sub(1, 5) == "kick " then
            chan:kick(msg:sub(6))
            return
        elseif msg:sub(1, 5) == "send " then
            dcc.send(from, msg:sub(6))
            return
        elseif msg:sub(1, 6) == "whois " then
            irc.whois(whois_cb, msg:sub(7))
            return
        elseif msg:sub(1, 8) == "sversion" then
            irc.server_version(serverversion_cb)
            return
        elseif msg:sub(1, 5) == "ping " then
            irc.ctcp_ping(ping_cb, msg:sub(6))
            return
        elseif msg:sub(1, 5) == "time " then
            irc.ctcp_time(time_cb, msg:sub(6))
            return
        elseif msg:sub(1, 8) == "version " then
            irc.ctcp_version(version_cb, msg:sub(9))
            return
        elseif msg:sub(1, 5) == "stime" then
            irc.server_time(stime_cb)
            return
        elseif msg:sub(1, 6) == "trace " then
            irc.trace(trace_cb, msg:sub(7))
            return
        elseif msg:sub(1, 5) == "trace" then
            irc.trace(trace_cb)
            return
        end
    end
    if from ~= "doylua" then
        irc.say(chan.name, from .. ": " .. msg)
    end
end
irc.register_callback("channel_msg", on_channel_msg)

local function on_private_msg(from, msg)
    if from == "doy" then
        if msg == "leave" then
            irc.quit("gone")
            return
        elseif msg:sub(1, 5) == "send " then
            dcc.send(from, msg:sub(6))
            return
        end
    end
    if from ~= "doylua" then
        irc.say(from, msg)
    end
end
irc.register_callback("private_msg", on_private_msg)

local function on_channel_act(chan, from, msg)
    irc.act(chan.name, "jumps on " .. from)
end
irc.register_callback("channel_act", on_channel_act)

local function on_private_act(from, msg)
    irc.act(from, "jumps on you")
end
irc.register_callback("private_act", on_private_act)

local function on_op(chan, from, nick)
    print(nick .. " was opped in " .. chan .. " by " .. from)
    print_state()
end
irc.register_callback("op", on_op)

local function on_deop(chan, from, nick)
    print(nick .. " was deopped in " .. chan .. " by " .. from)
    print_state()
end
irc.register_callback("deop", on_deop)

local function on_voice(chan, from, nick)
    print(nick .. " was voiced in " .. chan .. " by " .. from)
    print_state()
end
irc.register_callback("voice", on_voice)

local function on_devoice(chan, from, nick)
    print(nick .. " was devoiced in " .. chan .. " by " .. from)
    print_state()
end
irc.register_callback("devoice", on_devoice)

local function on_dcc_send()
    return true
end
irc.register_callback("dcc_send", on_dcc_send)

irc.connect{network = "irc.freenode.net", nick = "doylua", pass = "doylua"}