-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprx.lua
118 lines (118 loc) · 2.92 KB
/
prx.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
118
local lanes=require("posix")
local func=function(svip,svname,lport,nick)
local socket=require("socket")
local sv
print("trying "..lport)
local scl=assert(socket.bind("*",lport))
print("listening on "..lport)
local logfile={}
local function log(chan,txt)
if chan then
local fn="logs/"..svname.."/"..chan..".txt"
if not logfile[fn] then
logfile[fn]=io.open(fn,"a")
end
logfile[fn]:write("["..(math.floor(socket.gettime()-1394214999)).."] "..txt.."\n")
logfile[fn]:flush()
else
for k,v in pairs(logfile) do
v:write("["..(math.floor(socket.gettime()-1394214999)).."] "..txt.."\n")
v:flush()
end
end
end
local function connect()
socket.sleep(5)
sv=socket.connect(svip,6667)
while not sv do
print("failed")
socket.sleep(10)
print("retrying")
sv=socket.connect(svip,6667)
end
assert(sv:send("NICK "..nick.."\n"))
assert(sv:send("USER ping ~ ~ :ping's bot\n"))
assert(sv:settimeout(0))
print("got server")
end
local function scheck(s)
local s,e=s:receive(0)
return e and e~="timeout"
end
local cl=scl:accept()
cl:settimeout(0)
print("got client")
connect()
local svbuff=""
while true do
if scheck(cl) then
print("lost client")
cl=scl:accept()
cl:settimeout(0)
print("got client")
end
if scheck(sv) then
cl:send("QUIT\n")
print("lost server")
connect()
end
local s,e=cl:receive()
if s then
local chan,txt=s:match("^PRIVMSG (%S+) :(.*)$")
if txt then
log(chan,"<^v> "..txt)
end
print("<"..s)
assert(sv:send(s.."\n"))
end
local s,e,r=sv:receive("*a")
if e=="timeout" then
svbuff=svbuff..r
while svbuff:match("[\r\n]") do
local s=svbuff:match("^[^\r\n]*")
local nick,chan,txt=s:match("^:([^!]+)![^@]+@%S+ PRIVMSG (%S+) :(.*)$")
if nick then
local action=txt:match("^\1ACTION (.-)\1?$")
if action then
log(chan,"* "..nick.." "..action)
else
log(chan,"<"..nick.."> "..txt)
end
end
local nick,ident,chan=s:match("^:([^!]+)!([^@]+@%S+) JOIN (%S+)$")
if nick then
log(chan,nick.." ("..ident..") has joined")
end
local nick,chan,reason=s:match("^:([^!]+)![^@]+@%S+ PART (%S+) ?:?(.*)$")
if nick then
log(chan,nick.." has left ("..reason..")")
end
local nick,reason=s:match("^:([^!]+)![^@]+@%S+ QUIT :(.*)$")
if nick then
log(nil,nick.." has quit ("..reason..")")
end
local nick,tonick=s:match("^:([^!]+)![^@]+@%S+ NICK :(.*)$")
if nick then
log(nil,nick.." is now known as "..tonick)
end
print(">"..s)
cl:settimeout(5)
cl:send(s.."\n")
cl:settimeout(0)
local pong=s:match("^PING (.+)$")
if pong then
assert(sv:send("PONG "..pong.."\n"))
print("<PONG "..pong)
end
svbuff=svbuff:gsub("^[^\r\n]*[\r\n]+","")
end
end
socket.select({sv,cl},nil,1)
end
end
-- fork because lazy
if posix.fork()==0 then
func("185.30.166.35","freenode",1335,"^0")
else
func("ipv4.irc.esper.net","esper",1337,"^v")
end