aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAutumn <git@autumnfo.rest>2026-05-17 17:22:58 +0100
committerAutumn <git@autumnfo.rest>2026-05-17 17:22:58 +0100
commite7563787be5b3a9c1de66a7f4eb16ed09d9d4a21 (patch)
tree2956aed96a4d1873ac997bc17ad224815ba4bbb5
parent2bad2793f33c3c0879d3a8a36de327a6626c32ed (diff)
[main] added ability to fetch service & tidied up functions
-rw-r--r--README.md9
-rw-r--r--fetch.lua88
-rw-r--r--src/actions.lua25
-rw-r--r--src/pawsd/metadata.lua21
-rw-r--r--src/pawsd/record.lua6
-rw-r--r--src/pawsd/send.lua2
-rw-r--r--src/pawsd/service.lua7
-rw-r--r--src/pawsd/tag.lua14
-rw-r--r--src/utils.lua24
9 files changed, 80 insertions, 116 deletions
diff --git a/README.md b/README.md
index 7b91de8..8c4999e 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,12 @@ SelenePaw is a [PawSD](https://pawpub.entities.org.uk/pawsd/start) fetching util
## Usage
-To fetch a service (e.g. winter's first service):
+### Dependencies
-`lua fetch.lua winternet 0000`
+* `>=lua-5.3`
+* `luasocket`
+
+### Running
+
+To view all available commands, run `lua selenepaw.lua`.
diff --git a/fetch.lua b/fetch.lua
deleted file mode 100644
index 72b579a..0000000
--- a/fetch.lua
+++ /dev/null
@@ -1,88 +0,0 @@
---
--- ~~~ fetch a pawsd service
---
-
--- require a name & service index to fetch
-if not arg[1] or not arg[2] then
- error("server name & index required")
-end
-
--- imports
-local PawSD = require("src/pawsd/variables")
-local Servers = require("src/pawsd/servers")
-require("src/utils")
-
-local pawsd = {}
-pawsd.response = require("src/pawsd/metadata")
-pawsd.signature = require("src/pawsd/signature")
-pawsd.service = require("src/pawsd/service")
-
-local socket = require("socket")
-local client = assert(socket.tcp())
-
--- get arguments & check validity
-local server = Servers[arg[1]]
-local index = arg[2]
-
-if not server then
- error("server not found")
-end
-
--- connect to client
-client:connect(server.host, server.port)
-
--- send request
-local request = string.fromhex(PawSD.Magic.REQUEST .. PawSD.Version .. PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. index)
-client:send(request)
-
--- print response
-local response = {}
-
-while true do
- local packet, status = client:receive(1)
-
- -- if packet then print(response) end
- table.insert(response, packet)
- if status == "closed" then break end
-end
-
--- check that the response is correct
-local responseMagic = pawsd.response.type(response)
-if not responseMagic == PawSD.Magic.RESPONSE then
- error("invalid response")
-end
-
-print("R> Response Magic: " .. responseMagic)
-
--- check that the status is okay
-local responseStatus = pawsd.response.status(response)
-if not responseStatus == PawSD.Responses.OK then
- error("unsuccessful request")
-end
-
-print("R> Response Status: " .. responseStatus)
-
--- get signature information
-local responseSignature = pawsd.signature.get(response)
-local responseSignatureValid = pawsd.signature.validate(responseSignature, server.key)
-
-print("R> Signature: " .. responseSignature)
-
-if responseSignatureValid then
- print("R> Signature Validity: VALID")
-else
- print("R> Signature Validity: INVALID")
-end
-
--- get service info
-local responseIndex = pawsd.service.index(response)
-local responseFlags = pawsd.service.flags(response)
-
-print("\nR> Index: " .. responseIndex)
-print("R> Flags: " .. responseFlags)
-
--- get number of records
-local responseRecords = pawsd.service.records.all(response)
-
--- close connection
-client:close()
diff --git a/src/actions.lua b/src/actions.lua
index 8f9bf29..007d978 100644
--- a/src/actions.lua
+++ b/src/actions.lua
@@ -11,6 +11,8 @@ local help = require("src/help")
local Servers = require("src/pawsd/servers")
local pawsd = {}
+pawsd.response = require("src/pawsd/metadata")
+pawsd.service = require("src/pawsd/service")
local PawSD = require("src/pawsd/variables")
local send = require("src/pawsd/send")
@@ -73,8 +75,27 @@ function fetchservice ()
local request = PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. arg[3]
local response = send(server, request)
- -- log response
- print(response)
+ -- check response
+ local valid = pawsd.response.validate(response)
+
+ if not valid then
+
+ print("\nE> Error: invalid request.\n")
+ return
+
+ end
+
+ -- todo: validate signature
+
+ -- get records
+ local records = pawsd.service.records.all(response)
+
+ -- log records
+ for recordindex = 1, #records, 1 do
+ for recordtagindex = 1, #records[recordindex], 1 do
+ print(table.dump(records[recordindex][recordtagindex]))
+ end
+ end
end
diff --git a/src/pawsd/metadata.lua b/src/pawsd/metadata.lua
index a8dd98d..4a8532e 100644
--- a/src/pawsd/metadata.lua
+++ b/src/pawsd/metadata.lua
@@ -4,6 +4,9 @@
require("src/utils")
+-- load pawsd
+local PawSD = require("src/pawsd/variables")
+
-- get response type
function getType (response)
@@ -18,8 +21,24 @@ function getStatus (response)
end
+-- validate response status
+function validateStatus (response)
+
+ -- check type
+ local type = getType(response)
+ if not (type == PawSD.Magic.RESPONSE) then return false end
+
+ -- check status
+ local status = getStatus(response)
+ if not (status == PawSD.Responses.OK) then return false end
+
+ return true
+
+end
+
-- return
return {
type = getType,
- status = getStatus
+ status = getStatus,
+ validate = validateStatus
}
diff --git a/src/pawsd/record.lua b/src/pawsd/record.lua
index ae6d145..869796f 100644
--- a/src/pawsd/record.lua
+++ b/src/pawsd/record.lua
@@ -20,18 +20,14 @@ function getrecord(response, offset)
local tagcount = getrecordtagnumber(response, offset)
- print("R>------ Number Of Tags: " .. tagcount)
-
local tagoffset = offset + 2
local tags = {}
for tagindex = 1, tagcount, 1 do
- print("\nT>------ Getting Tag " .. tagindex .. "...")
-
local tag = pawsd.tag.get(response, tagoffset)
- tags[tagindex] = tag.value
+ tags[tagindex] = tag.tag
tagoffset = tag.endoffset
end
diff --git a/src/pawsd/send.lua b/src/pawsd/send.lua
index 46ff2b5..156664d 100644
--- a/src/pawsd/send.lua
+++ b/src/pawsd/send.lua
@@ -36,7 +36,7 @@ function send (server, data)
end
-- return response
- return table.concat(response)
+ return response
end
diff --git a/src/pawsd/service.lua b/src/pawsd/service.lua
index 8cbce77..c176faf 100644
--- a/src/pawsd/service.lua
+++ b/src/pawsd/service.lua
@@ -37,19 +37,12 @@ end
-- get all records
function getallrecords (response)
- print("\nR>--- Getting Records...")
-
local records = {}
local recordoffset = 23 + pawsd.signature.length(response)
local recordnumber = getrecordnumber(response)
- print("\nR>--- Record Offset: " .. recordoffset)
- print("R>--- Number Of Records: " .. recordnumber)
-
for recordindex = 1, recordnumber , 1 do
- print("\nR>--- Getting Record " .. recordindex .. "...")
-
local record = pawsd.record.get(response, recordoffset)
records[recordindex] = record.tags
diff --git a/src/pawsd/tag.lua b/src/pawsd/tag.lua
index f4031f4..b5997b0 100644
--- a/src/pawsd/tag.lua
+++ b/src/pawsd/tag.lua
@@ -22,28 +22,22 @@ function gettagvalue (response, offset)
local valuetypes = gettagtypes(response, offset)
- print("T>--------- Tag (High): " .. valuetypes.high)
- print("T>--------- Tag (Low): " .. valuetypes.low)
-
local valuelength = tonumber(table.extracthex(response, offset + 16, offset + 19), 16)
local valueoffset = offset + 20
local value = ""
- print("\nT>--------- Length: " .. valuelength)
- print("T>--------- Value Offset: " .. valueoffset)
-
if valuelength > 0 then
value = table.extracthex(response, valueoffset, valueoffset + valuelength)
valueoffset = valueoffset + valuelength
- print("\nT>--------- Value (Hex): " .. value)
- print("T>--------- Value (Binary): " .. string.fromhex(value))
-
end
return {
- value = value,
+ tag = {
+ types = valuetypes,
+ value = value
+ },
endoffset = valueoffset
}
diff --git a/src/utils.lua b/src/utils.lua
index 83e1d46..5851e1c 100644
--- a/src/utils.lua
+++ b/src/utils.lua
@@ -27,3 +27,27 @@ function table.extracthex(response, start, last)
return result
end
+
+-- table -> pretty print
+function table.dump(response)
+
+ if type(response) == "table" then
+
+ local start = "{ "
+
+ for key, value in pairs(response) do
+
+ if type(key) ~= "number" then key = '"' .. key .. '"' end
+
+ start = start .. "[" .. key .. "] = " .. table.dump(value) .. ","
+
+ end
+
+ return start .. "} "
+
+ else
+
+ return tostring(response)
+
+ end
+end