Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- From TekMail Client
- local serverID = 0
- local routerID = 0
- local clientID = os.getComputerID()
- local reqCounter = 0
- local loggedInUser, loggedInPass
- local function discoverNearestRouter(timeout)
- timeout = timeout or 15
- local knownRouters = {}
- local start = os.clock()
- while os.clock() - start < timeout do
- local remaining = timeout - (os.clock() - start)
- local id, msg = rednet.receive(remaining)
- if id then
- if type(msg) == "table" and msg.type == "router_broadcast" then
- local ridStr = msg.id
- local ridNum = tonumber((ridStr or ""):match("^(%d+)%[")) or id
- if ridNum then
- knownRouters[ridNum] = msg.coords or {}
- end
- end
- end
- end
- if next(knownRouters) == nil then return nil end
- local gx, gy, gz = nil, nil, nil
- if gps and gps.locate then
- pcall(function()
- gx, gy, gz = gps.locate(3)
- end)
- end
- local selectedID = nil
- if gx and gy and gz then
- local minDist = math.huge
- for rid, pos in pairs(knownRouters) do
- if pos.x and pos.y and pos.z then
- local dx, dy, dz = pos.x - gx, pos.y - gy, pos.z - gz
- local dist = math.sqrt(dx*dx + dy*dy + dz*dz)
- if dist < minDist then
- minDist = dist
- selectedID = rid
- end
- end
- end
- end
- if not selectedID then
- for rid in pairs(knownRouters) do
- selectedID = rid
- break
- end
- end
- return selectedID
- end
- routerID = discoverNearestRouter(15)
- local function initializeModem()
- local sides = {"left","right","top","bottom","front","back"}
- for _, side in ipairs(sides) do
- local pType = peripheral.getType(side)
- if pType == "wireless_modem" or pType == "modem" then
- rednet.open(side)
- print("[Modem] Modem opened on side: " .. side)
- return true
- end
- end
- print("[Error] No modem found. Exiting.")
- return false
- end
- local function attemptLogin(username, password)
- rednet.send(routerID, {
- type = "client_request",
- fromClient = clientID,
- payload = { type = "login", username = username, password = password }
- })
- local _, response = rednet.receive(5)
- if response and response.status == "ok" then
- loggedInUser = username
- loggedInPass = password
- okMsg = "Logged in."
- errMsg = nil
- drawFields()
- sleep(1)
- term.setCursorBlink(false)
- return true
- end
- error("Invalid login information.")
- end
- local function requestReply(payload, timeout)
- timeout = timeout or 5
- reqCounter = reqCounter + 1
- payload.reqID = tostring(clientID) .. "-" .. tostring(reqCounter) .. "-" .. tostring(math.random(1, 1000000))
- rednet.send(routerID, {
- type = "client_request",
- fromClient = clientID,
- payload = payload
- })
- local timer = os.startTimer(timeout)
- while true do
- local event, p1, p2 = os.pullEvent()
- if event == "rednet_message" then
- local msg = p2
- if type(msg) == "table" and msg.type == "server_response" and type(msg.payload) == "table" then
- msg = msg.payload
- end
- if type(msg) == "table" and msg.reqID == payload.reqID and msg.status ~= nil then
- return msg
- end
- elseif event == "timer" and p1 == timer then
- return nil
- end
- end
- end
- -- end copied code
- initializeModem()
- local function sendNewMail(recipient, messageContent)
- print("User to impersonate:")
- loggedInUser = read()
- -- loggedInPass = read("*")
- local payload = {
- type = "send",
- from = loggedInUser,
- to = recipient,
- content = messageContent
- }
- local response = requestReply(payload)
- -- Check if the server acknowledged the message
- if response and response.status == "ok" then
- print("Mail sent successfully!")
- return true
- else
- print("Failed to send mail or timed out.")
- return false
- end
- end
- sendNewMail("Admin", "Hi there ;)")
Advertisement
Add Comment
Please, Sign In to add comment