This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
74 lines (68 loc) · 2.03 KB
/
app.rb
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
require 'sinatra'
class ZNCLogViewerAPI < Sinatra::Base
get '/' do
[].to_json
end
get '/users/?' do
userdir = Dir.open(File.expand_path("~/.znc/users"))
users = userdir.entries
users.delete '..'
users.delete '.'
ret = []
users.each do |u|
netdir = Dir.open(File.expand_path("~/.znc/users/#{u}/networks"))
networks = netdir.entries
networks.delete '..'
networks.delete '.'
ret << {name: u, networks: networks}
end
ret.to_json
end
get '/users/:user/networks/?' do
netdir = Dir.open(File.expand_path("~/.znc/users/#{params[:user]}/networks"))
networks = netdir.entries
networks.delete '..'
networks.delete '.'
networks.to_json
end
get '/users/:user/networks/:network/logs/?' do
logdir = Dir.open(File.expand_path("~/.znc/users/#{params[:user]}/networks/#{params[:network]}/moddata/log"))
files = logdir.entries
files.delete '..'
files.delete '.'
files.map do |f|
{
name: f.sub(/^\#/, '')
}
end.to_json
end
get '/users/:user/networks/:network/logs/:file?' do
logpath = File.expand_path("~/.znc/users/#{params[:user]}/networks/#{params[:network]}/moddata/log/")
if File.exists?(File.join(logpath, params[:file]))
f = File.open(File.join(logpath, params[:file]), "r:UTF-8")
elsif File.exists?(File.join(logpath, "##{params[:file]}"))
f = File.open(File.join(logpath, "##{params[:file]}"), "r:UTF-8")
else
status 404
return { status: 404 }.to_json
end
{
name: params[:file],
lines: f.readlines.map do |line|
u16= line.encode!('UTF-16', :undef => :replace, :invalid => :replace, :replace => "")
line.encode!("UTF-8")
matchdata = /\[(?<time>.+)\] \<(?<user>[^>]+)\> (?<message>.+)/.match(line)
p matchdata
if matchdata
{
time: matchdata[:time],
user: matchdata[:user],
message: matchdata[:message]
}
else
nil
end
end.compact
}.to_json
end
end