Tomodachi94

Untitled

Jul 21st, 2026 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. -- From TekMail Client
  2.  
  3. local serverID = 0
  4. local routerID = 0
  5. local clientID = os.getComputerID()
  6. local reqCounter = 0
  7.  
  8. local loggedInUser, loggedInPass
  9.  
  10. local function discoverNearestRouter(timeout)
  11. timeout = timeout or 15
  12. local knownRouters = {}
  13. local start = os.clock()
  14. while os.clock() - start < timeout do
  15. local remaining = timeout - (os.clock() - start)
  16.  
  17. local id, msg = rednet.receive(remaining)
  18. if id then
  19. if type(msg) == "table" and msg.type == "router_broadcast" then
  20. local ridStr = msg.id
  21. local ridNum = tonumber((ridStr or ""):match("^(%d+)%[")) or id
  22. if ridNum then
  23. knownRouters[ridNum] = msg.coords or {}
  24. end
  25. end
  26. end
  27. end
  28. if next(knownRouters) == nil then return nil end
  29.  
  30. local gx, gy, gz = nil, nil, nil
  31. if gps and gps.locate then
  32. pcall(function()
  33. gx, gy, gz = gps.locate(3)
  34. end)
  35. end
  36. local selectedID = nil
  37. if gx and gy and gz then
  38. local minDist = math.huge
  39. for rid, pos in pairs(knownRouters) do
  40. if pos.x and pos.y and pos.z then
  41. local dx, dy, dz = pos.x - gx, pos.y - gy, pos.z - gz
  42. local dist = math.sqrt(dx*dx + dy*dy + dz*dz)
  43. if dist < minDist then
  44. minDist = dist
  45. selectedID = rid
  46. end
  47. end
  48. end
  49. end
  50. if not selectedID then
  51. for rid in pairs(knownRouters) do
  52. selectedID = rid
  53. break
  54. end
  55. end
  56. return selectedID
  57. end
  58.  
  59. routerID = discoverNearestRouter(15)
  60.  
  61. local function initializeModem()
  62. local sides = {"left","right","top","bottom","front","back"}
  63. for _, side in ipairs(sides) do
  64. local pType = peripheral.getType(side)
  65. if pType == "wireless_modem" or pType == "modem" then
  66. rednet.open(side)
  67. print("[Modem] Modem opened on side: " .. side)
  68. return true
  69. end
  70. end
  71. print("[Error] No modem found. Exiting.")
  72. return false
  73. end
  74.  
  75. local function attemptLogin(username, password)
  76. rednet.send(routerID, {
  77. type = "client_request",
  78. fromClient = clientID,
  79. payload = { type = "login", username = username, password = password }
  80. })
  81. local _, response = rednet.receive(5)
  82.  
  83. if response and response.status == "ok" then
  84. loggedInUser = username
  85. loggedInPass = password
  86. okMsg = "Logged in."
  87. errMsg = nil
  88. drawFields()
  89. sleep(1)
  90. term.setCursorBlink(false)
  91. return true
  92. end
  93.  
  94. error("Invalid login information.")
  95. end
  96.  
  97. local function requestReply(payload, timeout)
  98. timeout = timeout or 5
  99. reqCounter = reqCounter + 1
  100. payload.reqID = tostring(clientID) .. "-" .. tostring(reqCounter) .. "-" .. tostring(math.random(1, 1000000))
  101. rednet.send(routerID, {
  102. type = "client_request",
  103. fromClient = clientID,
  104. payload = payload
  105. })
  106. local timer = os.startTimer(timeout)
  107. while true do
  108. local event, p1, p2 = os.pullEvent()
  109. if event == "rednet_message" then
  110. local msg = p2
  111. if type(msg) == "table" and msg.type == "server_response" and type(msg.payload) == "table" then
  112. msg = msg.payload
  113. end
  114. if type(msg) == "table" and msg.reqID == payload.reqID and msg.status ~= nil then
  115. return msg
  116. end
  117. elseif event == "timer" and p1 == timer then
  118. return nil
  119. end
  120. end
  121. end
  122.  
  123. -- end copied code
  124.  
  125. initializeModem()
  126.  
  127. local function sendNewMail(recipient, messageContent)
  128. print("User to impersonate:")
  129. loggedInUser = read()
  130. -- loggedInPass = read("*")
  131. local payload = {
  132. type = "send",
  133. from = loggedInUser,
  134. to = recipient,
  135. content = messageContent
  136. }
  137.  
  138. local response = requestReply(payload)
  139.  
  140. -- Check if the server acknowledged the message
  141. if response and response.status == "ok" then
  142. print("Mail sent successfully!")
  143. return true
  144. else
  145. print("Failed to send mail or timed out.")
  146. return false
  147. end
  148. end
  149.  
  150. sendNewMail("Admin", "Hi there ;)")
Advertisement
Add Comment
Please, Sign In to add comment