-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc.lua
100 lines (83 loc) · 2.43 KB
/
irc.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
--global posix = require("posix")
socket = require("socket")
-- Set buffering mode to 'no' for non-blocking input
io.stdin:setvbuf("no")
local ev = require "ev"
function nonBlockingInput(callback)
local stdin_watcher = ev.IO.new(function()
local input = io.stdin:read("*l")
callback(input)
end, 0, ev.READ)
stdin_watcher:start(ev.Loop.default)
end
-- Example usage
nonBlockingInput(function(userInput)
if userInput then
print("User input:", userInput)
else
print("No input within the timeout.")
end
ev.Loop.default:unloop()
end)
ev.Loop.default:loop()
client = socket.tcp()
client:connect("irc.libera.chat", 6667)
client:settimeout(0)
user_input = ""
print("waiting 15 seconds for connect...\r\n server: irc.libera.chat channel: ##anime")
socket.sleep(15) -- wait enough till logon
line = "nick shortestLuaIRC" .. math.random(1, 100) .. "\r\nuser a a a a\r\n"
print(line .. " nick and user ***")
client:send(line)
socket.sleep(2)
line = "join ##anime\r\n"
print(line .. " joining channel ***")
client:send(line)
socket.sleep(2)
-- ... (previous code)
buff = ""
while true do
buff, err = client:receive(4096)
if buff then
io.write(buff)
io.flush()
end
if buff and buff:find("^PING") then
local s = buff:sub(5)
line = "PONG\r\n"
print("pong reached", line)
client:send(line)
socket.sleep(2)
end
if not buff and err == "timeout" then
-- No data available from the socket, handle input
socket.sleep(0.05) -- 50ms delay (20 checks per second)
local key = any_key()
if key then
-- Handle user input here
if key == '\x7f' then
-- Backspace pressed
user_input = user_input:sub(1, -2)
print("")
print(user_input)
elseif key == '\n' then
-- Enter pressed
line = "privmsg ##anime : " .. user_input .. "\r\n"
client:send(line)
print("sending:", line)
socket.sleep(2)
user_input = ""
print("")
else
-- Alphanumeric key pressed
user_input = user_input .. key
io.write(key)
io.flush()
end
end
elseif not buff then
-- A "real" error occurred
print(err)
os.exit(1)
end
end