forked from mirrexagon/lua-irc-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.lua
117 lines (87 loc) · 2.65 KB
/
example.lua
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
--[[
Example for Lua IRC Engine (https://github.com/mirrexagon/lua-irc-engine)
Uses LuaSocket for network communication.
]]
--- Require ---
local IRCe = require("irce")
print(IRCe._VERSION .. " running on " .. _VERSION)
local socket = require("socket")
--- ==== ---
--- Constants ---
local SERVER = arg[1] or "irc.example.com"
local NICK = "IRCe"
local USERNAME = "ircengine"
local REALNAME = "IRC Engine"
local CHANNEL = "#example"
--- ==== ---
--- IRC object initialisation ---
local irc = IRCe.new()
-- Path may change depending on your directory structure.
-- These should work for LuaRocks installations.
assert(irc:load_module(require("irce.modules.base")))
assert(irc:load_module(require("irce.modules.message")))
assert(irc:load_module(require("irce.modules.channel")))
--- ==== ---
--- Raw send function ---
local client = socket.tcp()
irc:set_send_func(function(self, message)
return client:send(message)
end)
client:settimeout(1)
--- ==== ---
--- Callbacks ---
irc:set_callback(IRCe.RAW, function(self, send, message)
print(("%s %s"):format(send and ">>>" or "<<<", message))
end)
irc:set_callback("CTCP", function(self, sender, origin, command, params, pm)
if command == "VERSION" then
assert(self:CTCP_REPLY(origin, "VERSION", "Lua IRC Engine - Test"))
end
end)
irc:set_callback("001", function(self, ...)
assert(irc:JOIN(CHANNEL))
end)
irc:set_callback("PRIVMSG", function(self, sender, origin, message, pm)
if message == "?quit" then
assert(self:QUIT("And away we go!"))
end
end)
irc:set_callback("NAMES", function(self, sender, channel, list, kind, message)
print("---")
if not list then
print("No channel called " .. channel)
else
print(("Channel %s (%s):"):format(channel, kind))
print("-")
for _, nick in ipairs(list) do
print(nick)
end
end
print("---")
end)
irc:set_callback("USERMODE", function(self, sender, operation, mode)
print(("User mode: %s%s"):format(operation, mode))
end)
irc:set_callback("CHANNELMODE", function(self, sender, operation, mode, param)
print(("Channel mode: %s%s %s"):format(operation, mode, param))
end)
--- ==== ---
--- Running ---
do
local line, err = client:connect(SERVER, 6667)
irc:NICK(NICK)
irc:USER(USERNAME, REALNAME)
local ctimeout, timeout, maxtimeout = 0, 5, 200
client:settimeout(timeout)
while
line or -- server message received
err=="timeout" -- the only error allowed is timeout
and
ctimeout<maxtimeout -- maxtimeout hasn't been reached
do
line,err = client:receive() -- if line is set, err is nil, if line is not set, err can be anything
irc:process(line)
ctimeout = line and 0 or ctimeout+timeout
end
if err~="closed" then print(err) client:close() end
end