Last active
January 7, 2017 05:59
-
-
Save kaeza/10584337 to your computer and use it in GitHub Desktop.
Search entries in databases for XBan2 mod for Minetest (https://github.com/minetest-mods/xban2).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env lua5.1 | |
local l_os, l_io, l_loadfile = | |
os, io, loadfile | |
os, io, debug, loadfile, dofile, require = nil | |
local function load_db(filename) | |
local ok, r = pcall(l_loadfile, filename) | |
if not ok then error(r) end | |
ok, r = pcall(r) | |
if not ok then error("error loading db: "..r) end | |
return r | |
end | |
local function print_table(t, level) | |
local indent = (" "):rep(level or 0) | |
for k, v in pairs(t) do | |
if type(v) == "table" then | |
print(("%s%q = {"):format(indent, k)) | |
print_table(v, (level or 0) + 1) | |
print(("%s}"):format(indent)) | |
else | |
print(("%s%q = %q,"):format(indent, k, tostring(v))) | |
end | |
end | |
end | |
local function usage() | |
print([[Usage: xban-grep.lua [OPTIONS] PATTERN | |
Search for PATTERN in the xban database and show information on the entry. | |
Available options: | |
-h, --help Show this help text and exit. | |
-f, --db-file FILE Set database file (default: `./players.iplist.v2'). | |
]]) | |
end | |
local function print_entry(entry) | |
local names = { } | |
for name in pairs(entry.names) do | |
table.insert(names, name) | |
end | |
print(("Known names/IPs: %s"):format(table.concat(names, ", "))) | |
if entry.record then | |
print("Ban record:") | |
for _, e in ipairs(entry.record) do | |
local time, reason = e.time, e.reason or "No reason given." | |
time = time and ("Banned for %d seconds"):format(time) or "Permanent ban." | |
print(("%s %s (%s)"):format(l_os.date("%c", e.date), reason, time)) | |
end | |
end | |
end | |
local function main(argv) | |
local i = 1 | |
local db_filename = "players.iplist.v2" | |
local pattern | |
while i <= #argv do | |
local a = argv[i] | |
if (a == "-f") or (a == "--db-file") then | |
i = i + 1 | |
if not argv[i] then | |
error(("missing filename after `%s'"):format(a)) | |
end | |
db_filename = argv[i] | |
elseif (a == "-h") or (a == "--help") then | |
usage() | |
return | |
else | |
pattern = a | |
end | |
i = i + 1 | |
end | |
if not pattern then | |
error("no pattern specified. Try `--help'") | |
end | |
local db, e = load_db(db_filename) | |
if not db then | |
error(e) | |
end | |
local entries = { } | |
for _, entry in ipairs(db) do | |
if entry.names then | |
for name in pairs(entry.names) do | |
if name:match(pattern) then | |
table.insert(entries, {name, entry}) | |
end | |
end | |
end | |
end | |
if #entries == 0 then | |
error("No entries match the given pattern.") | |
end | |
for _, entry in ipairs(entries) do | |
print(("Entry for `%s':"):format(entry[1])) | |
print_entry(entry[2]) | |
end | |
end | |
local ok, err = pcall(main, arg) | |
if not ok then | |
print(("xban-grep: %s"):format(err)) | |
l_os.exit(1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment