local modem = peripheral.find("modem") local PORT = 1 local function start() if modem then modem.open(PORT) modem.transmit(PORT, PORT, "User connected") end print("Chat program started.") end local function stop() if modem then modem.transmit(PORT, PORT, "User disconnected") modem.close(PORT) end print("Chat program stopped.") end local function restart() stop() start() end local function receiveLoop() while true do local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") if channel == PORT then local x, y = term.getCursorPos() term.setCursorPos(1, y) term.clearLine() print("Received: " .. tostring(message)) write("> ") end end end local function sendLoop() while true do write("> ") local input = read() if modem then modem.transmit(PORT, PORT, input) end end end local function main() if not modem then print("No modem attached") while true do os.pullEvent() end end parallel.waitForAny(receiveLoop, sendLoop) end return { start = start, stop = stop, restart = restart, main = main }