osmarks

Lightweight Messaging System

Aug 1st, 2019
228
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function update()
  2.     local h = http.get "https://pastebin.com/raw/L0ZKLBRG"
  3.     local f = fs.open(shell.getRunningProgram(), "w")
  4.     f.write(h.readAll())
  5.     f.close()
  6.     h.close()
  7. end
  8.  
  9. if ... == "update" then update() end
  10.  
  11. local m = peripheral.find "modem"
  12. local s = peripheral.find "speaker"
  13.  
  14. local chan = 3636
  15. print "Welcome to the Lightweight Messaging System (developed by GTech Potatronics)"
  16.  
  17. m.open(chan)
  18.  
  19. local username = settings.get "lms.username"
  20. if username == nil then
  21.     write "Username: "
  22.     username = read()
  23. end
  24.  
  25. local w, h = term.getSize()
  26. local send_window = window.create(term.current(), 1, h, w, 1)
  27. local message_window = window.create(term.current(), 1, 1, w, h - 1)
  28.  
  29. local function notification_sound()
  30.     if s then
  31.         for i = 4, 12, 4 do
  32.             s.playNote("flute", 3, i)
  33.             sleep(0.2)
  34.         end
  35.     end
  36. end
  37.  
  38. local function exec_in_window(w, f)
  39.     local x, y = term.getCursorPos()
  40.     local last = term.redirect(w)
  41.     f()
  42.     term.redirect(last)
  43.     w.redraw()
  44.     term.setCursorPos(x, y)
  45. end
  46.  
  47. local function print_message(txt)
  48.     exec_in_window(message_window, function()
  49.         print(txt)
  50.     end)
  51. end
  52.  
  53. local function trim(s)
  54.    return s:match( "^%s*(.-)%s*$" )
  55. end
  56.  
  57. local banned_text = {
  58.     "yeet",
  59.     "ecs dee",
  60.     "dab",
  61. }
  62.  
  63. if debug and debug.getmetatable then
  64.     _G.getmetatable = debug.getmetatable
  65. end
  66.  
  67. local function to_case_insensitive(text)
  68.     return text:gsub("[a-zA-Z]", function(char) return ("[%s%s]"):format(char:lower(), char:upper()) end)
  69. end
  70.  
  71. local function filter(text)
  72.     local out = text
  73.     for _, b in pairs(banned_text) do
  74.         out = out:gsub(to_case_insensitive(b), "")
  75.     end
  76.     return out
  77. end
  78.  
  79. local function strip_extraneous_spacing(text)
  80.     return text:gsub("%s+", " ")
  81. end
  82.  
  83. local function collapse_e_sequences(text)
  84.     return text:gsub("ee+", "ee")
  85. end
  86.  
  87. local function preproc(text)
  88.     return trim(filter(strip_extraneous_spacing(collapse_e_sequences(text:sub(1, 128)))))
  89. end
  90.  
  91. local function add_message(msg, usr)
  92.     local msg, usr = preproc(msg), preproc(usr)
  93.     if msg == "" or usr == "" then return end
  94.     print_message(usr .. ": " .. msg)
  95. end
  96.  
  97. local function send()
  98.     term.redirect(send_window)
  99.     term.setBackgroundColor(colors.white)
  100.     term.setTextColor(colors.black)
  101.     term.clear()
  102.     local hist = {}
  103.     while true do
  104.         local msg = read(nil, hist)
  105.         if msg == "!!exit" then return
  106.         elseif msg == "!!update" then update() print_message "Updated. Please restart the program."
  107.         else
  108.             table.insert(hist, msg)
  109.             if preproc(msg) == "" then
  110.                 print_message "Your message is considered spam."
  111.             else
  112.                 add_message(msg, username)
  113.                 m.transmit(chan, chan, { message = msg, username = username })
  114.             end
  115.         end
  116.     end
  117. end
  118.  
  119. local function recv()
  120.     while true do
  121.         local _, _, channel, _, message = os.pullEvent "modem_message"
  122.         if channel == chan and type(message) == "table" and message.message and message.username then
  123.             notification_sound()
  124.             add_message(message.message, message.username)
  125.         end
  126.     end
  127. end
  128.  
  129. m.transmit(chan, chan, { username = username, message = "Connected." })
  130. parallel.waitForAny(send, recv)
RAW Paste Data Copied