Advertisement
nonogamer9

CC-IRC (A IRC client for ComputerCraft) V 1.0

Mar 11th, 2025
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | Software | 0 0
  1. -- MAKE SURE TO DOWNLOAD AND RUN THIS: https://pastebin.com/tHAMJgrz OTHERWIZE IT WONT WORK
  2. local function logo()
  3.     print([[
  4.  
  5.    ____    ____                  ____       ____  
  6. U /"___|U /"___|        ___   U |  _"\ u U /"___|
  7. \| | u  \| | u  U  u   |_"_|   \| |_) |/ \| | u  
  8. | |/__  | |/__ /___\   | |     |  _ <    | |/__  
  9.  \____|  \____|__"__|U/| |\u   |_| \_\    \____|
  10.  _// \\  _// \\    .-,_|___|_,-.//   \\_  _// \\  
  11. (__)(__)(__)(__)    \_)-' '-(_/(__)  (__)(__)(__)
  12.  
  13. ]])
  14. end
  15.  
  16. local function getUserInput(prompt)
  17.     write(prompt .. ": ")
  18.     return read()
  19. end
  20.  
  21. local function configureServer()
  22.     local url = "http://localhost:8000/config"
  23.     local server = getUserInput("Enter IRC server (e.g., irc.libera.chat)")
  24.     local port = getUserInput("Enter IRC port (e.g., 6667)")
  25.     local nick = getUserInput("Enter your nickname")
  26.     local channel = getUserInput("Enter channel name (e.g., #testchannel)")
  27.    
  28.     local payload = textutils.serializeJSON({
  29.         server = server,
  30.         port = tonumber(port),
  31.         nick = nick,
  32.         channel = channel,
  33.     })
  34.    
  35.     local headers = {["Content-Type"] = "application/json"}
  36.    
  37.     local response = http.post(url, payload, headers)
  38.    
  39.     if response then
  40.         local responseBody = response.readAll()
  41.         response.close()
  42.        
  43.         local data = textutils.unserializeJSON(responseBody)
  44.         if data and data.status then
  45.             print(data.status)
  46.             return true
  47.         else
  48.             print(data.error or "Failed to configure server")
  49.             return false
  50.         end
  51.     else
  52.         print("Failed to connect to server")
  53.         return false
  54.     end
  55. end
  56.  
  57. local function sendMessage(message)
  58.     local url = "http://localhost:8000/send"
  59.    
  60.     local payload = textutils.serializeJSON({message = message})
  61.     local headers = {["Content-Type"] = "application/json"}
  62.    
  63.     local response = http.post(url, payload, headers)
  64.    
  65.     if response then
  66.         local responseBody = response.readAll()
  67.         response.close()
  68.        
  69.         local data = textutils.unserializeJSON(responseBody)
  70.         if data and data.status then
  71.             print(data.status)
  72.         else
  73.             print(data.error or "Failed to send message")
  74.         end
  75.     else
  76.         print("Failed to connect to server")
  77.     end
  78. end
  79.  
  80. local function fetchMessages()
  81.     local url = "http://localhost:8000/messages"
  82.    
  83.     local response = http.get(url)
  84.    
  85.     if response then
  86.         local responseBody = response.readAll()
  87.         response.close()
  88.        
  89.         local data = textutils.unserializeJSON(responseBody)
  90.        
  91.         if data and data.messages then
  92.             for _, message in ipairs(data.messages) do
  93.                 print(message)
  94.             end
  95.         end
  96.     else
  97.         print("Failed to connect to server")
  98.     end
  99. end
  100.  
  101. local function checkForNewMessages()
  102.     while true do
  103.         fetchMessages()
  104.         sleep(0.5)
  105.     end
  106. end
  107.  
  108. local function chatClient()
  109.     while true do
  110.         write("> ")
  111.         local input = read()
  112.        
  113.         if input == "/quit" then break end
  114.        
  115.         sendMessage(input)
  116.        
  117.         sleep(0.1)
  118.     end
  119. end
  120.  
  121. logo()
  122.  
  123. if configureServer() then
  124.    parallel.waitForAny(chatClient, checkForNewMessages)
  125. end
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement