Advertisement
DanielLaby99

Skype

Mar 17th, 2013
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.77 KB | None | 0 0
  1. local nMinNameLen = 1 -- minimum user name lenght
  2. local nMaxNameLen = 20 -- maximum user name lenght
  3. local nMaxHistory = 100 -- maximum chat history size, set to nil to disable
  4.  
  5. -- System vars
  6. local nScreenW, nScreenH = term.getSize()
  7. local tUsers = {}
  8. -- User message vars
  9. local sUsrName = ""
  10. local sUsrMsg = ""
  11. local nPos = 0
  12. -- Received messages vars
  13. local tMessages = {}
  14. local nMsg = 1
  15. -- Menu vars
  16. local bMenu = false
  17. local nSelected = 1
  18. local tMenuFunctions = {}
  19. local tMenuOptions = {}
  20. -- Menu functions vars
  21. -- Add any variable you need here
  22. local bExit = false
  23.  
  24. local function Clear()
  25.         term.clear()
  26.         term.setCursorPos(1, 1)
  27. end
  28.  
  29. local function OpenAll()
  30.         for _,side in ipairs(rs.getSides()) do
  31.                 rednet.open(side)
  32.         end
  33. end
  34.  
  35. local function AddMessage(sMsg, sName)
  36.         if #sMsg > 0 then
  37.                 local msg
  38.                 if sName then
  39.                         msg = sName..": "..sMsg
  40.                 else
  41.                         msg = "<"..sMsg..">"
  42.                 end
  43.                 table.insert(tMessages, msg)
  44.                 if #tMessages - nMsg >= nScreenH - 1 then
  45.                         nMsg = nMsg + 1
  46.                 end
  47.                 if nMaxHistory ~= nil and #tMessages > nMaxHistory then
  48.                         table.remove(tMessages, 1)
  49.                         if nMsg > 1 then
  50.                                 nMsg = nMsg - 1
  51.                         end
  52.                 end
  53.         end
  54. end
  55.  
  56. local function AddUser(nID, sName)
  57.         tUsers[nID] = sName
  58.         AddMessage(""..sName.." ist online!")
  59. end
  60.  
  61. local function RemoveUser(nID)
  62.         AddMessage(""..tUsers[nID].." ist offline gegangen.")
  63.         tUsers[nID] = nil
  64. end
  65.  
  66. local function WriteMsg(sText)
  67.         local x, y = term.getCursorPos()
  68.         local function newLine()
  69.                 x = 1
  70.                 y = y + 1
  71.                 term.setCursorPos(x, y)
  72.                 if y < nScreenH - 2 then
  73.                         return true
  74.                 end
  75.                 return false
  76.         end
  77.         while #sText > 0 do
  78.                 local whitespace = string.match(sText, "^[ \t]+")
  79.                 if whitespace then
  80.                         term.write(whitespace)
  81.                         x, y = term.getCursorPos()
  82.                         sText = string.sub(sText, #whitespace + 1)
  83.                 end
  84.                 local newline = string.match(sText, "^\n")
  85.                 if newline then
  86.                         if not newLine() then
  87.                                 return true
  88.                         end
  89.                         sText = string.sub(sText, 2)
  90.                 end
  91.                 local text = string.match(sText, "^[^ \t\n]+")
  92.                 if text then
  93.                         sText = string.sub(sText, #text + 1)
  94.                         if #text > nScreenW then
  95.                                 while #text > 0 do
  96.                                         if x > nScreenW then
  97.                                                 if not newLine() then
  98.                                                         return true
  99.                                                 end
  100.                                         end
  101.                                         term.write(text)
  102.                                         text = string.sub(text, (nScreenW - x) + 2)
  103.                                         x, y = term.getCursorPos()
  104.                                 end
  105.                         else
  106.                                 if x + #text > nScreenW then
  107.                                         if not newLine() then
  108.                                                 return true
  109.                                         end
  110.                                 end
  111.                                 term.write(text)
  112.                                 x, y = term.getCursorPos()
  113.                         end
  114.                 end
  115.         end
  116.         return false
  117. end
  118.  
  119. local function WriteMessages()
  120.         local i = 0
  121.         while nMsg + i <= #tMessages and i < nScreenH - 1 do
  122.                 if WriteMsg(tMessages[nMsg + i]) then
  123.                         break
  124.                 end
  125.                 local x, y = term.getCursorPos()
  126.                 term.setCursorPos(1, y + 1)
  127.                 i = i + 1
  128.         end
  129. end
  130.  
  131. local function RedrawUserMsg()
  132.         local nScroll = 0
  133.         if nPos + 3 >= nScreenW then
  134.                 nScroll = nPos + 3 - nScreenW
  135.         end
  136.         term.setCursorPos(1, nScreenH)
  137.         write("> ")
  138.         write(string.sub(sUsrMsg, nScroll + 1))
  139.         term.setCursorPos(nPos + 3 - nScroll, nScreenH)
  140.  
  141. end
  142.  
  143. local function RedrawMenu()
  144.         for i, s in ipairs(tMenuOptions) do
  145.                 if i == nSelected then
  146.                         term.write("["..s.."]")
  147.                 else
  148.                         term.write(s)
  149.                 end
  150.                 term.setTextColor(colors.white)
  151.                 shell.run("back")
  152.         end
  153. end
  154.  
  155. local function Redraw()
  156.         Clear()
  157.         WriteMessages()
  158.         if bMenu then
  159.                 RedrawMenu()
  160.         else
  161.                 RedrawUserMsg()
  162.         end
  163. end
  164.  
  165. local function ParseMsg(nID, sMsg)
  166.         local sAction, sArgs = string.match(sMsg, "(%a+): (.+)")
  167.         if sAction == "MSG" then
  168.                 AddMessage(sArgs, tUsers[nID])
  169.         elseif sAction == "PONG" then
  170.                 AddUser(nID, sArgs)
  171.         elseif sAction == "PING" then
  172.                 AddUser(nID, sArgs)
  173.                 rednet.send(nID, "PONG: "..sUsrName)
  174.         elseif sAction == "QUIT" then
  175.                 RemoveUser(nID)
  176.         end
  177. end
  178.  
  179. local function Ping()
  180.         rednet.broadcast("PING: "..sUsrName)
  181. end
  182.  
  183. local function Disconnect()
  184.         for id,_ in pairs(tUsers) do
  185.                 rednet.send(id, "QUIT: "..sUsrName)
  186.         end
  187. end
  188.  
  189. local function SendMsg()
  190.         AddMessage(sUsrMsg, sUsrName)
  191.         for id,_ in pairs(tUsers) do
  192.                 rednet.send(id, "MSG: "..sUsrMsg)
  193.         end
  194.         sUsrMsg = ""
  195.         nPos = 0
  196. end
  197.  
  198. local function DoMenuItem()
  199.         tMenuFunctions[nSelected]()
  200.         nSelected = 1
  201. end
  202.  
  203. function KeyPress(key)
  204.         if key == 28 then -- Enter
  205.                 if bMenu then
  206.                         DoMenuItem()
  207.                 else
  208.                         SendMsg()
  209.                 end
  210.         elseif key == 203 then -- Left
  211.                 if bMenu then
  212.                         if nSelected > 1 then
  213.                                 nSelected = nSelected - 1
  214.                         end
  215.                 else
  216.                         if nPos > 0 then
  217.                                 nPos = nPos - 1
  218.                         end
  219.                 end
  220.         elseif key == 205 then -- Right
  221.                 if bMenu then
  222.                         if nSelected < #tMenuOptions then
  223.                                 nSelected = nSelected + 1
  224.                         end
  225.                 else
  226.                         if nPos < #sUsrMsg then
  227.                                 nPos = nPos + 1
  228.                         end
  229.                 end
  230.         elseif key == 14 then -- Backspace
  231.                 if not bMenu then
  232.                         if nPos > 0 then
  233.                                 sUsrMsg = string.sub(sUsrMsg, 1, nPos - 1)..string.sub(sUsrMsg, nPos + 1)
  234.                                 nPos = nPos - 1
  235.                         end
  236.                 end
  237.         elseif key == 211 then -- Delete
  238.                 if not bMenu then
  239.                         sUsrMsg = string.sub(sUsrMsg, 1, nPos)..string.sub(sUsrMsg, nPos + 2)
  240.                 end
  241.         elseif key == 199 then -- Start
  242.                 if bMenu then
  243.                         nSelected = 1
  244.                 else
  245.                         nPos = 0
  246.                 end
  247.         elseif key == 207 then -- End
  248.                 if bMenu then
  249.                         nSelected = #tMenuOptions
  250.                 else
  251.                         nPos = #sUsrMsg
  252.                 end
  253.         elseif key == 200 then -- Up
  254.                 if nMsg > 1 then
  255.                         nMsg = nMsg - 1
  256.                 end
  257.         elseif key == 208 then -- Down
  258.                 if nMsg < #tMessages then
  259.                         nMsg = nMsg + 1
  260.                 end
  261.         elseif key == 29 then -- Ctrl
  262.                 bMenu = not bMenu
  263.                 term.setCursorBlink(not bMenu)
  264.         end
  265. end
  266.  
  267. local function AddChar(sChar)
  268.         sUsrMsg = string.sub(sUsrMsg, 1, nPos)..sChar..string.sub(sUsrMsg, nPos + 1)
  269.         nPos = nPos + 1
  270. end
  271.  
  272. local function CheckUserName()
  273.         if #sUsrName < nMinNameLen then
  274.                 return false, "Dein Name ist zu kurz."
  275.         elseif #sUsrName > nMaxNameLen then
  276.                 return false, "Dein Name ist zu lange."
  277.         end
  278.         return true
  279. end
  280.  
  281. -- Menu
  282.  
  283. -- Add Menu functions here --
  284. --[[
  285. tMenuOptions[n] = "your option name"
  286. tMenuFunctions[n] = function()
  287.         -- Your code here
  288. end
  289. --]]
  290.  
  291. table.insert(tMenuOptions, "Offline gehen")
  292. table.insert(tMenuFunctions, function()
  293.         bExit = true
  294. end )
  295.  
  296. -- Main loop
  297.  
  298. repeat
  299.         Clear()
  300.         print("Willkommen bei Skype!")
  301.         print("Strg um zum Desktop zu kommen")
  302.         write("Dein Username: ")
  303.  
  304.  
  305.         sUsrName = read()
  306.         local bValid, err = CheckUserName()
  307.         if not bValid then
  308.                 print(err)
  309.                 sleep(2)
  310.         end
  311. until bValid
  312. OpenAll()
  313. Ping()
  314. term.setCursorBlink(true)
  315.  
  316. while not bExit do
  317.         Redraw()
  318.         local evt, arg1, arg2 = os.pullEvent()
  319.         if evt == "key" then
  320.                 KeyPress(arg1)
  321.         elseif evt == "char" then
  322.                 AddChar(arg1)
  323.         elseif evt == "rednet_message" then
  324.                 ParseMsg(arg1, arg2)
  325.         end
  326. end
  327. Disconnect()
  328. Clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement