Code cleanup

This commit is contained in:
Shy 2024-08-27 14:43:43 +02:00
parent dae2a45710
commit 590ccdec1f

22
c4mate
View file

@ -9,14 +9,14 @@
USER = nil USER = nil
HOST = "http://mate.labor.koeln.ccc.de" HOST = "http://mate.labor.koeln.ccc.de"
function utf8_decode(str) local function utf8_decode(str)
-- Replace JSON Unicode escape sequences with chars. -- Replace JSON Unicode escape sequences with chars.
if str == nil then return "" end if str == nil then return "" end
return string.gsub(str, "\\u(%x%x%x%x)", return string.gsub(str, "\\u(%x%x%x%x)",
function (s) return utf8.char(tonumber(s, 16)) end) function (s) return utf8.char(tonumber(s, 16)) end)
end end
function url_encode(str) local function url_encode(str)
-- URL-encode string. Used only for usernames. -- URL-encode string. Used only for usernames.
-- Todo: make this more sophisticated. -- Todo: make this more sophisticated.
return string.gsub(str, " ", "%%20") return string.gsub(str, " ", "%%20")
@ -25,10 +25,10 @@ end
MatePad = { MatePad = {
CURL_FLAGS = "--silent --show-error --fail --user-agent 'c4mate' --max-redirs 0 --max-filesize 65536", CURL_FLAGS = "--silent --show-error --fail --user-agent 'c4mate' --max-redirs 0 --max-filesize 65536",
} }
function MatePad:new(HOST, USER) function MatePad:new(host, user)
local o = { local o = {
HOST = HOST, HOST = host,
USER = USER, USER = user,
COOKIE_JAR, COOKIE_JAR,
balance balance
} }
@ -199,13 +199,13 @@ function MatePad:print_log(log)
)) ))
elseif event.method == "transferTo" then elseif event.method == "transferTo" then
print(string.format( print(string.format(
"[%s] Transferred %.2f € to #%d (%s)", "[%s] Transferred %.2f € to user #%d (%s)",
event.time, (event.oldbalance - event.newbalance)/100, event.time, (event.oldbalance - event.newbalance)/100,
event.parameter, event.reason event.parameter, event.reason
)) ))
elseif event.method == "transferFrom" then elseif event.method == "transferFrom" then
print(string.format( print(string.format(
"[%s] Received %.2f € from #%d (%s)", "[%s] Received %.2f € from user #%d (%s)",
event.time, (event.newbalance - event.oldbalance)/100, event.time, (event.newbalance - event.oldbalance)/100,
event.parameter, event.reason event.parameter, event.reason
)) ))
@ -306,7 +306,7 @@ Buy by item id (non-interactive):
Show log: Show log:
c4mate -l|--log c4mate -l|--log
Add or subtract credits: Add or subtract credits:
c4mate <difference> c4mate +/-<amount>
Give credits: Give credits:
c4mate -g <amount> <user> [<reason>]]=]) c4mate -g <amount> <user> [<reason>]]=])
os.exit(0) os.exit(0)
@ -327,7 +327,7 @@ if USER == nil then
os.exit(1) os.exit(1)
end end
end end
pad = MatePad:new(HOST, USER) local pad = MatePad:new(HOST, USER)
if arg[1] == nil then if arg[1] == nil then
-- Print balance. -- Print balance.
@ -387,7 +387,7 @@ elseif tonumber(arg[1]) ~= nil then
-- Add to (or subtract from) balance. -- Add to (or subtract from) balance.
pad:init() pad:init()
if pad:update_balance() then if pad:update_balance() then
new = math.tointeger(pad.balance + tonumber(arg[1])*100) local new = math.tointeger(pad.balance + tonumber(arg[1])*100)
if pad:set_balance(new) == "OK" then if pad:set_balance(new) == "OK" then
pad:update_balance() pad:update_balance()
print(string.format( print(string.format(
@ -420,7 +420,7 @@ else
table.sort(menu, function (e1, e2) return e1.id < e2.id end) table.sort(menu, function (e1, e2) return e1.id < e2.id end)
-- Check if we can invoke stty. -- Check if we can invoke stty.
is_tty = os.execute("tty -s") == true local is_tty = os.execute("tty -s") == true
for i, item in ipairs(menu) do for i, item in ipairs(menu) do
io.stdout:write(string.format("[%d/%d] Buy %s (#%d) for %.2f €? ", io.stdout:write(string.format("[%d/%d] Buy %s (#%d) for %.2f €? ",