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