-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.cgi
executable file
·39 lines (34 loc) · 1 KB
/
chat.cgi
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
#!/usr/bin/env ruby
require 'erb'
require 'cgi'
CHAT_HTML = File.read("template.erb")
def esc(str)
str && str.gsub("&", "&").gsub("<", "<").gsub(">", ">").gsub('"', """)
end
File.open("sessionlock.lck", "w") do |f|
f.flock(File::LOCK_EX)
chat = nil
begin
begin
chat = Marshal.load File.open("chat.log")
rescue
chat = []
end
cgi = CGI.new
now = Time.now
break if !cgi["name"] || cgi["name"].empty? || !cgi["chat"] || cgi["chat"].empty?
chat.unshift({name: esc(cgi["name"]), timestamp: now.strftime("%y-%m-%d %T"), chat: esc(cgi["chat"])&.[](0, 1024)})
chat = chat[0,30]
if chat.length > 500
require 'json'
File.open(now.strftime('archives/chat-%y%m%d%H%M%S.json'), "w") {|f| JSON.dump(chat, f) }
chat = chat[0, 30]
end
File.open("chat.log", "w") {|f| Marshal.dump chat, f }
File.open("chat.html", "w") {|f| f.puts ERB.new(CHAT_HTML).result(binding)}
ensure
f.flock(File::LOCK_UN)
end
end
puts "Status: 204"
puts