Advertisement
Tag365

Social Net Client

Apr 8th, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.47 KB | None | 0 0
  1. local username, password
  2.  
  3. local width, height = term.getSize()
  4. term.setCursorBlink(false)
  5. term.setBgColor = term.setBackgroundColor
  6. local oldPullEvent = os.pullEventRaw
  7. function os.pullEvent(filter)
  8.     local event, p1, p2, p3, p4, p5 = oldPullEvent(filter)
  9.     if event == "terminate" then
  10.         term.setCursorPos(1, 1)
  11.         term.setTextColor(colors.white)
  12.         term.setBackgroundColor(colors.black)
  13.         term.clear()
  14.         os.pullEvent = oldPullEvent
  15.         error("Terminated", 0)
  16.     end
  17.     return event, p1, p2, p3, p4, p5
  18. end
  19.  
  20. function message(str, sleept)
  21.     sleept = sleept or 3
  22.     term.clear()
  23.     term.setCursorPos(math.ceil((width - #str)*.5), math.floor(height*.5))
  24.     print(str)
  25.     if sleept > 0 and sleept < 10 then
  26.         sleep(sleept)
  27.     elseif sleept == -1 then
  28.         local contStr = "Press any key to continue..."
  29.         term.setCursorPos(math.ceil((width - #str)*.5), math.floor(height - 1))
  30.         print(contStr)
  31.         os.pullEvent("key")
  32.         term.clear()
  33.     end
  34.     if sleept >= 3 then
  35.         term.clear()
  36.     end
  37. end
  38.  
  39. -- Load the Social Net API
  40. function loadapi()
  41.     message("Loading Social Net API...", 0)
  42.     if fs.exists("snet") then
  43.         shell.run("snet")
  44.         return true
  45.     end
  46.     aa = aa or {}
  47.     local a2 = http.get("https://vault.dannysmc.com/lua/api/socialnetapi.lua")
  48.     a = tostring(a2.readAll())
  49.     a2.close()
  50.     if a == "nil" then
  51.         error("There was a problem when retrieving the Social Net API.", 0)
  52.     end
  53.     local env = {}
  54.     a = loadstring(a)
  55.     if not a then error() end
  56.     local env = getfenv()
  57.     setfenv(a,env)
  58.     local status, err = pcall(a, unpack(aa))
  59.     if (not status) and err then
  60.         printError("Error loading api")
  61.         return false
  62.     end
  63.     local returned = err
  64.     env = env
  65.     _G["snet"] = env
  66. end
  67. loadapi()
  68.  
  69. messages = {}
  70. local optionsel = 1
  71. local headercolor = colors.white
  72. local headertextcolor = colors.black
  73. local imagebgcolor = colors.lightGray
  74. local bgcolor = colors.gray
  75. local highlightcolor = colors.blue
  76. local hitextcolor = colors.white
  77. local textcolor = colors.white
  78. if not term.isColor() then
  79.     if not _CC_VERSION then -- Old version, use black and white ONLY
  80.         imagebgcolor = colors.black
  81.         bgcolor = colors.black
  82.         highlightcolor = colors.black
  83.         hitextcolor = colors.white
  84.     else -- New version, use black and white along with lightGray and darkGray.
  85.         hitextcolor = colors.white
  86.         highlightcolor = colors.black
  87.     end
  88. end
  89.  
  90. message("Please Wait...", 0)
  91.  
  92. function LoadMessages()
  93.     showingUserMessages = false
  94.     local rawmessages = social.post.getall(username, password)
  95.     messages = {}
  96.     for k, v in ipairs(rawmessages) do
  97.         table.insert(messages, 1, v)
  98.     end
  99.     messages[#messages + 1] = "Exit"
  100. end
  101.  
  102. function LoadFriends()
  103.     showingThisUsersFriends = true
  104.     friends = social.friends.get(username, password, username)
  105.     friends[#friends + 1] = "Add New"
  106.     friends[#friends + 1] = "Exit"
  107. end
  108.  
  109. function LoadUsers()
  110.     users = user.list(username, password)
  111.     users[#users + 1] = "Exit"
  112. end
  113.  
  114. -- My Friends Menu --
  115. function drawFriendsList(optionsel)
  116.     term.clear()
  117.     local options = friends
  118.     if type(options) ~= "table" then
  119.         LoadFriends()
  120.     end
  121.     term.setBgColor(colors.black)
  122.     for k, v in ipairs(options) do
  123.         term.setCursorPos(1, 2 + k - math.max(optionsel - height*.5, 0))
  124.         if k == optionsel then
  125.             term.setBgColor(highlightcolor)
  126.             term.setTextColor(colors.white)
  127.         else
  128.             term.setBgColor(bgcolor)
  129.             term.setTextColor(textcolor)
  130.         end
  131.         term.clearLine()
  132.         if type(v) == "table" then
  133.             if v[2] == "tweet" then
  134.                 term.write(v[3].." said "..v[5])
  135.             else
  136.                 term.write(v[4].." by ".. v[3])
  137.             end
  138.         else
  139.             term.write(v)
  140.         end
  141.     end
  142.     term.setBgColor(bgcolor)
  143.     term.setCursorPos(1, 3 + #options - math.max(optionsel - height*.5, 0))
  144.     term.clearLine()
  145.     if math.max(optionsel - height*.5, 0) == 0 then
  146.         term.setCursorPos(1, 2)
  147.         term.clearLine()
  148.     end
  149.     term.setCursorPos(1, 1)
  150.     term.clearLine()
  151.     print("Friends")
  152. end
  153.  
  154. function MyFriends(showOtherUserFriends)
  155.     if not showOtherUserFriends and not showingThisUsersFriends then
  156.         message("Reloading your friends...", 0)
  157.         LoadFriends()
  158.     end
  159.     local options = friends
  160.     local optionsel = 1
  161.     if type(friends) ~= "table" then
  162.         friends = {}
  163.         friends[#friends + 1] = "Add New"
  164.         friends[#friends + 1] = "Exit"
  165.         options = friends
  166.     end
  167.     while true do
  168.         drawFriendsList(optionsel)
  169.         local event, key = os.pullEvent("key")
  170.         if key == keys.up then
  171.             optionsel = math.max(optionsel - 1, 1)
  172.         elseif key == keys.down then
  173.             optionsel = math.min(optionsel + 1, #options)
  174.         elseif key == keys.pageUp then
  175.             optionsel = math.max(optionsel - (height - 1), 1)
  176.         elseif key == keys.pageDown then
  177.             optionsel = math.min(optionsel + (height - 1), #options)
  178.         elseif key == keys.home then
  179.             optionsel = 1
  180.         elseif key == keys["end"] then
  181.             optionsel = #options
  182.         elseif(key == keys.tab) then
  183.             term.setCursorBlink(false)
  184.             return false
  185.         elseif key == 28 then
  186.             if optionsel > #options - 1 then
  187.                 term.clear()
  188.                 return
  189.             elseif optionsel > #options - 2 and not showOtherUserFriends then
  190.                 addFriend()
  191.             else
  192.                 viewProfile(friends[optionsel])
  193.             end
  194.         end
  195.     end
  196. end
  197.  
  198. -- Chat Menu --
  199. function drawChat(chatMessages, msgString, curPos, scrollPos, chatScroll, group)
  200.     -- Draw Header
  201.     term.setBgColor(bgcolor)
  202.     term.setTextColor(textcolor)
  203.     term.clear()
  204.     term.setCursorPos(1, 1)
  205.     if type(group) == "string" then -- Use Group Chat
  206.         print(group.." Chat")
  207.     elseif type(group) == "table" then -- Use Personal Message Chat
  208.         print("Chat with "..group[1])
  209.     else -- Use Global Chat
  210.         print("Global Chat")
  211.     end
  212.     term.setTextColor(textcolor)
  213.     term.setBgColor(bgcolor)
  214.     -- Draw Currently shown messages
  215.     local drawY, msgLine = 2, 1
  216.     for k=chatScroll, chatScroll + height do
  217.         if chatMessages[k] then
  218.             local showString = chatMessages[k][2].."> "..chatMessages[k][3]
  219.             if not (k - chatScroll <= 0) and chatMessages[k - 1] then
  220.                 drawY = drawY + math.floor((#chatMessages[k - 1][3]/width) + 1)
  221.             end
  222.             msgLine = -1
  223.             for tx=1, #showString, width do
  224.                 term.setCursorPos(1, drawY + tx + msgLine)
  225.                 msgLine = msgLine + 1
  226.                 term.write(string.sub(showString, tx, tx + width))
  227.             end
  228.         end
  229.     end
  230.     -- Draw Text currently being input
  231.     term.setCursorPos(1 - scrollPos, height - 1)
  232.     term.setBgColor(imagebgcolor)
  233.     term.clearLine()
  234.     term.write(msgString)
  235.     term.setBgColor(bgcolor)
  236.     term.setCursorPos(1, height)
  237.     term.write("Enter to send, F5 to refresh, or tab to return.")
  238.     term.setCursorPos(curPos - scrollPos + 1, height - 1)
  239.     term.setCursorBlink(true)
  240. end
  241.  
  242. function Chat(group)
  243.     local getMessages, sendMessage
  244.     local msgString, curPos, scrollPos, chatScroll = "", 0, 0, 0
  245.     local ok
  246.     if type(group) == "string" then
  247.         if #group == 0 then group = nil end
  248.     end
  249.     if type(group) == "string" then -- Use Group Chat
  250.         getMessages = function (group) return social.chat_group.get(username, password, group) end
  251.         sendMessage = function (message, group) return social.chat_group.new(username, password, group, message) end
  252.     elseif type(group) == "table" then -- Use Personal Message Chat
  253.         getMessages = function (group) return social.chat_pm.get(username, password, group[1]) end
  254.         sendMessage = function (message, group) return social.chat_pm.new(username, password, group[1], message) end
  255.     else -- Use Global Chat
  256.         getMessages = function () return social.chat_global.get(username, password) end
  257.         sendMessage = function (message) return social.chat_global.new(username, password, message) end
  258.     end
  259.     chatrunning = true
  260.     ok, chatMessages = pcall(getMessages, username, password, group)
  261.     if not ok then
  262.         message(chatMessages)
  263.         chatMessages = {}
  264.     end
  265.     while chatrunning do
  266.         drawChat(chatMessages, msgString, curPos, scrollPos, chatScroll, group)
  267.         local event, key, p2, p3, p4, p5 = os.pullEvent()
  268.         if event == "key" then
  269.             if key == keys.tab then
  270.                 term.setCursorBlink(false)
  271.                 return
  272.             elseif(key == keys.up) then
  273.                 chatScroll = chatScroll - 1
  274.             elseif(key == keys.down) then
  275.                 chatScroll = chatScroll + 1
  276.             elseif(key == keys.left) then
  277.                 curPos = math.min(math.max(curPos - 1, 0), #msgString)
  278.                 if curPos - scrollPos < 1 then scrollPos = scrollPos - 1 end
  279.             elseif(key == keys.right) then
  280.                 curPos = math.min(curPos + 1, #msgString)
  281.                 if curPos - scrollPos > width then scrollPos = scrollPos + 1 end
  282.             elseif(key == keys.enter) then
  283.                 if #msgString > 0 then
  284.                     term.setCursorBlink(false)
  285.                     local ok, response = pcall(sendMessage, username, password, msgString, group)
  286.                     if response == true then
  287.                         msgString = ""
  288.                         -- Refresh messages.
  289.                         term.setCursorBlink(false)
  290.                         message("Refreshing Chat...", 0)
  291.                         ok, chatMessages = pcall(getMessages, username, password, group)
  292.                         if not ok then
  293.                             message(chatMessages)
  294.                             chatMessages = {}
  295.                         end
  296.                     else
  297.                         message(tostring(response))
  298.                     end
  299.                 end
  300.             elseif(key == keys.f5) then
  301.                 term.setCursorBlink(false)
  302.                 message("Refreshing Chat...", 0)
  303.                 ok, chatMessages = pcall(getMessages, username, password, group)
  304.                 if not ok then
  305.                     message(chatMessages)
  306.                     chatMessages = {}
  307.                 end
  308.             elseif(key == keys.f3) then
  309.                 Chat(read())
  310.             elseif(key == keys.f4) then
  311.                 Chat({username})
  312.             elseif(key == keys.backspace) then
  313.                 if curPos > 0 then
  314.                     msgString = string.sub(msgString, 1, curPos - 1) .. string.sub(msgString, curPos + 1, -1)
  315.                     curPos = curPos - 1
  316.                 end
  317.             elseif(key == keys.delete) then
  318.                 if curPos < #msgString then
  319.                     msgString = string.sub(msgString, 1, curPos) .. string.sub(msgString, curPos + 2, -1)
  320.                 end
  321.             end
  322.         elseif(event == "char") and #msgString < 255 then
  323.             local event, character = event, key
  324.             msgString = string.sub(msgString, 1, curPos) .. character .. string.sub(msgString, curPos + 1, -1)
  325.             curPos = curPos + 1
  326.             if curPos - scrollPos > width then scrollPos = scrollPos + 1 end
  327.         end
  328.     end
  329. end
  330.  
  331. -- Edit Profile Menu --
  332. local profilefieldsdefault = {
  333.     {"Full Name", ""},
  334.     {"Age", ""},
  335.     {"Forums Username", ""},
  336.     {"Image Location", ""},
  337.     {"Bio", ""},
  338. }
  339.  
  340. local profilefields = {}
  341.  
  342. function sendProfileUpdate(name, age, bio, forums, image)
  343.     local str
  344.     if image ~= "" then
  345.         local strx = paintutils.loadImage(image)
  346.         str = textutils.serialize(strx)
  347.     else
  348.         str = ""
  349.     end
  350.     return social.profile.edit(username, password, name, tonumber(age), bio, forums)
  351. end
  352.  
  353. function drawProfilePrompt(cursorpos, currow, curfield)
  354.     term.setCursorBlink(false)
  355.     term.setBgColor(bgcolor)
  356.     term.setTextColor(textcolor)
  357.     term.setCursorPos(1, height - 1)
  358.     term.write("Enter your profile then press Enter to send.")
  359.     term.setCursorPos(1, height)
  360.     term.write("Up or down switches row. Press Tab to cancel.")
  361.     for k, v in ipairs(profilefields) do
  362.         term.setCursorPos(1, 2 + k)
  363.         if v[1] ~= "Bio" then
  364.             print(v[1]..": "..v[2].." ")
  365.         else
  366.             term.clearLine()
  367.             term.write("Bio")
  368.             term.setCursorPos(1, 3 + k)
  369.             print(v[2] .. " ")
  370.         end
  371.     end
  372.     if profilefields[currow][1] == "Bio" then
  373.         term.setCursorPos(cursorpos + 1, 3 + currow)
  374.     else
  375.         term.setCursorPos(cursorpos + #profilefields[currow][1] + 3, 2 + currow)
  376.     end
  377.     term.setCursorBlink(true)
  378. end
  379.  
  380. function resetFields2()
  381.     for k, v in ipairs(profilefieldsdefault) do
  382.         profilefields[k] = profilefieldsdefault[k]
  383.     end
  384. end
  385.  
  386. function updateProfilePrompt()
  387.     term.setBgColor(bgcolor)
  388.     term.setTextColor(textcolor)
  389.     term.clear()
  390.     term.setCursorPos(1, 1)
  391.     term.write("Update Profile")
  392.     resetFields2()
  393.     local cursorpos, currow = 0, 1
  394.     local curfield = profilefields[currow]
  395.     local e, p1, p2, p3, p4
  396.     while true do
  397.         drawProfilePrompt(cursorpos, currow, curfield)
  398.         e, p1, p2, p3, p4 = os.pullEvent()
  399.         if(e == "key") then
  400.             if(p1 == keys.up) then
  401.                 currow = math.max(currow - 1, 1)
  402.                 curfield = profilefields[currow]
  403.             elseif(p1 == keys.down) then
  404.                 currow = math.min(currow + 1, #profilefields)
  405.                 curfield = profilefields[currow]
  406.             elseif(p1 == keys.left) then
  407.                 cursorpos = math.max(cursorpos - 1, 0)
  408.             elseif(p1 == keys.right) then
  409.                 cursorpos = math.min(cursorpos + 1, #curfield[2])
  410.             elseif(p1 == keys.enter) then
  411.                 term.setCursorBlink(false)
  412.                 message("Sending Message...", 0)
  413.                 local success = sendProfileUpdate(profilefields[1][2], profilefields[2][2], profilefields[5][2], profilefields[3][2], profilefields[4][2])
  414.                 if success then
  415.                     term.setCursorBlink(false)
  416.                     message("Profile Updated!")
  417.                     return
  418.                 else
  419.                     term.setCursorBlink(false)
  420.                     message(success)
  421.                 end
  422.             elseif(p1 == keys.backspace) then
  423.                 if cursorpos > 0 then
  424.                     curfield[2] = string.sub(profilefields[currow][2], 1, cursorpos - 1) .. string.sub(profilefields[currow][2], cursorpos + 1, -1)
  425.                     cursorpos = cursorpos - 1
  426.                 end
  427.             elseif(p1 == keys.tab) then
  428.                 term.setCursorBlink(false)
  429.                 return false
  430.             else
  431.                 local event, character = os.pullEvent("char")
  432.                 profilefields[currow][2] = string.sub(profilefields[currow][2], 1, cursorpos) .. character .. string.sub(profilefields[currow][2], cursorpos + 1, -1)
  433.                 cursorpos = cursorpos + 1
  434.             end
  435.         elseif(e == "char") then
  436.             local event, character = e, p1
  437.             profilefields[currow][2] = string.sub(profilefields[currow][2], 1, cursorpos) .. character .. string.sub(profilefields[currow][2], cursorpos + 1, -1)
  438.             cursorpos = cursorpos + 1
  439.         end
  440.         sleep()
  441.     end
  442. end
  443.  
  444. -- Send Message Menu --
  445. local msgfieldsdefault = {
  446.     {"Header", ""},
  447.     {"Body", ""},
  448. }
  449.  
  450. local msgfields = {
  451.     {"Header", ""},
  452.     {"Body", ""},
  453. }
  454.  
  455. function sendMessage(header, message)
  456.     if header == "" and #message <= 255 then
  457.         return social.post.tweet(username, password, message)
  458.     else
  459.         if header == "" then header = string.sub(message, 0, 30) .. "..." end
  460.         return social.post.new(username, password, header, message)
  461.     end
  462. end
  463.  
  464. function drawMessagePrompt(cursorpos, currow, curfield)
  465.     term.setCursorBlink(false)
  466.     term.setBgColor(bgcolor)
  467.     term.setTextColor(textcolor)
  468.     for k, v in ipairs(msgfields) do
  469.         term.setCursorPos(1, 2 + k)
  470.         if v[1] ~= "Body" then
  471.             print(v[1]..": "..v[2].." ")
  472.         else
  473.             term.clearLine()
  474.             term.write("Message Body")
  475.             term.setCursorPos(1, 3 + k)
  476.             print(string.rep(" ", #v[2]))
  477.             term.setCursorPos(1, 3 + k)
  478.             print(v[2] .. " ")
  479.         end
  480.     end
  481.     if msgfields[currow][1] == "Body" then
  482.         term.setCursorPos((cursorpos%width) + 1, 3 + currow + math.floor(cursorpos/width))
  483.     else
  484.         term.setCursorPos(cursorpos + #msgfields[currow][1] + 3, 2 + currow)
  485.     end
  486.     term.setCursorBlink(true)
  487. end
  488.  
  489. function resetFields()
  490.     for k, v in ipairs(msgfieldsdefault) do
  491.         msgfields[k] = msgfieldsdefault[k]
  492.     end
  493. end
  494.  
  495. function sendMessagePrompt()
  496.     term.setBgColor(bgcolor)
  497.     term.setTextColor(textcolor)
  498.     term.clear()
  499.     term.setCursorPos(1, 1)
  500.     term.write("Write Message")
  501.     resetFields()
  502.     local cursorpos, currow = 0, 1
  503.     local curfield = msgfields[currow]
  504.     local e, p1, p2, p3, p4
  505.     while true do
  506.         term.setCursorPos(1, 1)
  507.         term.write("Write Message")
  508.         term.setCursorPos(1, height - 1)
  509.         term.write("Type in a message then press Enter to send.")
  510.         term.setCursorPos(1, height)
  511.         term.write("Up or down switches row. Press Tab to cancel.")
  512.         drawMessagePrompt(cursorpos, currow, curfield)
  513.         e, p1, p2, p3, p4 = os.pullEvent()
  514.         if(e == "key") then
  515.             if(p1 == keys.up) then
  516.                 currow = math.max(currow - 1, 1)
  517.                 curfield = msgfields[currow]
  518.                 cursorpos = math.min(cursorpos, #curfield[2])
  519.             elseif(p1 == keys.down) then
  520.                 currow = math.min(currow + 1, #msgfields)
  521.                 curfield = msgfields[currow]
  522.                 cursorpos = math.min(cursorpos, #curfield[2])
  523.             elseif(p1 == keys.left) then
  524.                 cursorpos = math.max(cursorpos - 1, 0)
  525.             elseif(p1 == keys.right) then
  526.                 cursorpos = math.min(cursorpos + 1, #curfield[2])
  527.             elseif(p1 == keys.enter) then
  528.                 term.setCursorBlink(false)
  529.                 if #msgfields[2][2] > 0 then
  530.                     message("Sending Message...", 0)
  531.                     local success = sendMessage(msgfields[1][2], msgfields[2][2])
  532.                     if success then
  533.                         term.setCursorBlink(false)
  534.                         message("Message Sent!")
  535.                         return
  536.                     else
  537.                         term.setCursorBlink(false)
  538.                         message(success)
  539.                     end
  540.                 else
  541.                     message("You must type in a message.")
  542.                 end
  543.             elseif(p1 == keys.backspace) then
  544.                 if cursorpos > 0 then
  545.                     curfield[2] = string.sub(msgfields[currow][2], 1, cursorpos - 1) .. string.sub(msgfields[currow][2], cursorpos + 1, -1)
  546.                     cursorpos = cursorpos - 1
  547.                 end
  548.             elseif(p1 == keys.delete) then
  549.                 if cursorpos < #msgfields[currow][2] then
  550.                     curfield[2] = string.sub(msgfields[currow][2], 1, cursorpos) .. string.sub(msgfields[currow][2], cursorpos + 2, -1)
  551.                 end
  552.             elseif(p1 == keys.tab) then
  553.                 term.setCursorBlink(false)
  554.                 return false
  555.             else
  556.                 local event, character = os.pullEvent("char")
  557.                 msgfields[currow][2] = string.sub(msgfields[currow][2], 1, cursorpos) .. character .. string.sub(msgfields[currow][2], cursorpos + 1, -1)
  558.                 cursorpos = cursorpos + 1
  559.             end
  560.         elseif(e == "char") then
  561.             local event, character = e, p1
  562.             msgfields[currow][2] = string.sub(msgfields[currow][2], 1, cursorpos) .. character .. string.sub(msgfields[currow][2], cursorpos + 1, -1)
  563.             cursorpos = cursorpos + 1
  564.         end
  565.         sleep()
  566.     end
  567. end
  568.  
  569. -- Messages menu --
  570. function drawMessage(Author, Header, Body, Time)
  571.     term.setCursorPos(1, 1)
  572.     term.clearLine()
  573.     term.write(Header.." by "..Author)
  574.     term.setCursorPos(1, 2)
  575.     term.write("Message Body")
  576.     term.setCursorPos(1, 3)
  577.     print(Body)
  578.     term.setCursorPos(1, height)
  579.     term.clearLine()
  580.     term.write("Press Enter to return, or Space to view profile.")
  581. end
  582.  
  583. function viewMessage(msgID)
  584.     term.setBgColor(bgcolor)
  585.     term.clear()
  586.     local MessageType = messages[msgID][2]
  587.     local Author, Header, Body
  588.     if MessageType == "tweet" then
  589.         Author, Header, Body = messages[msgID][3], "Tweet", messages[msgID][5]
  590.     else
  591.         Author, Header, Body = messages[msgID][3], messages[msgID][4], messages[msgID][5]
  592.     end
  593.     while true do
  594.         drawMessage(Author, Header or "Message", Body)
  595.         local event, key = os.pullEvent("key")
  596.         if key == 28 or key == keys.tab then
  597.             return
  598.         elseif key == keys.space then
  599.             term.clear()
  600.             message("Loading Profile", 0)
  601.             viewProfile(Author)
  602.             term.clear()
  603.         elseif key == keys.delete then
  604.             local ok, response = pcall(social.post.delete, username, password, msgID)
  605.             if ok and response == true then
  606.                 message("Message deleted.")
  607.                 return
  608.             elseif not response == "You do not have permission to delete this." then
  609.                 message(response)
  610.             end
  611.         end
  612.     end
  613. end
  614.  
  615. function drawMessageList(optionsel, list, showUsers)
  616.     term.setBgColor(bgcolor)
  617.     term.clear()
  618.     local options = list or messages
  619.     for k, v in ipairs(options) do
  620.         term.setCursorPos(1, 2 + k - math.max(math.min(optionsel - height*.5, #options - (height - 2)), 0))
  621.         if k == optionsel then
  622.             term.setBgColor(highlightcolor)
  623.             term.setTextColor(hitextcolor)
  624.         else
  625.             term.setBgColor(bgcolor)
  626.             term.setTextColor(textcolor)
  627.         end
  628.         term.clearLine()
  629.         if type(v) == "table" then
  630.             if v[2] == "tweet" then
  631.                 term.write(v[3].." said "..v[5])
  632.             else
  633.                 term.write(v[4].." by ".. v[3])
  634.             end
  635.         else
  636.             term.write(v)
  637.         end
  638.     end
  639.     term.setBgColor(bgcolor)
  640.     term.setTextColor(textcolor)
  641.     term.setCursorPos(1, 3 + #options - math.min(optionsel - height*.5, 0))
  642.     term.clearLine()
  643.     if math.max(optionsel - height*.5, 0) == 0 then
  644.         term.setCursorPos(1, 2)
  645.         term.clearLine()
  646.     end
  647.     -- draw header
  648.     term.setCursorPos(1, 1)
  649.     term.clearLine()
  650.     print((showUsers and "Users") or "Messages")
  651.     -- draw scroll bar
  652.     if #options > height - 2 then
  653.         local scrollBarHeight = math.max((1 - (#options/height))*(height - 1), 1)
  654.         local scrollBarPos = ((optionsel/#options)*(height - 1 - scrollBarHeight)) + 2
  655.         term.setBgColor(headercolor)
  656.         for y=scrollBarPos, scrollBarPos + scrollBarHeight - 1 do
  657.             term.setCursorPos(width, scrollBarPos)
  658.             term.write(" ")
  659.         end
  660.     end
  661.     term.setBgColor(bgcolor)
  662. end
  663.  
  664. function viewMessageList(showUserMessages, list)
  665.     if showingUserMessages and not showUserMessages then
  666.         message("Reloading all messages...", 0)
  667.         LoadMessages()
  668.     end
  669.     local options = list or messages
  670.     local optionsel = 1
  671.     local width, height = term.getSize()
  672.     while true do
  673.         drawMessageList(optionsel)
  674.         local event, key = os.pullEvent("key")
  675.         if key == keys.up then
  676.             optionsel = math.max(optionsel - 1, 1)
  677.         elseif key == keys.down then
  678.             optionsel = math.min(optionsel + 1, #options)
  679.         elseif key == (keys.pageUp) then
  680.             optionsel = math.max(optionsel - (height - 1), 1)
  681.         elseif key == (keys.pageDown) then
  682.             optionsel = math.min(optionsel + (height - 1), #options)
  683.         elseif key == keys.home then
  684.             optionsel = 1
  685.         elseif key == keys["end"] then
  686.             optionsel = #options
  687.         elseif key == keys.f5 then
  688.             message("Reloading all messages...", 0)
  689.             LoadMessages()
  690.         elseif(key == keys.tab) then
  691.             term.setCursorBlink(false)
  692.             return false
  693.         elseif key == 28 then
  694.             if optionsel > #options - 1 then
  695.                 term.clear()
  696.                 return
  697.             else
  698.                 viewMessage(optionsel)
  699.             end
  700.         end
  701.     end
  702. end
  703.  
  704. -- Show User List Menu --
  705.  
  706. function viewUserList()
  707.     local options = users
  708.     local optionsel = 1
  709.     while true do
  710.         drawMessageList(optionsel, users, true)
  711.         local event, key = os.pullEvent("key")
  712.         if key == keys.up then
  713.             optionsel = math.max(optionsel - 1, 1)
  714.         elseif key == keys.down then
  715.             optionsel = math.min(optionsel + 1, #options)
  716.         elseif key == keys.pageUp then
  717.             optionsel = math.max(optionsel - (height - 1), 1)
  718.         elseif key == keys.pageDown then
  719.             optionsel = math.min(optionsel + (height - 1), #options)
  720.         elseif key == keys.home then
  721.             optionsel = 1
  722.         elseif key == keys["end"] then
  723.             optionsel = #options
  724.         elseif(key == keys.tab) then
  725.             term.setCursorBlink(false)
  726.             return false
  727.         elseif key == 28 then
  728.             if optionsel > #options - 1 then
  729.                 term.clear()
  730.                 return
  731.             else
  732.                 viewProfile(users[optionsel] or "Tag360")
  733.             end
  734.         end
  735.     end
  736. end
  737.  
  738. -- View Profile Menu --
  739. function drawProfileImageBG()
  740.     local str1, str2 = " ", " "
  741.     str1 = string.rep(str1, 8)
  742.     str2 = string.rep(str2, 6)
  743.     term.setBgColor(imagebgcolor)
  744.     for k=2, 6 do
  745.         term.setCursorPos(1, k)
  746.         term.write(str1)
  747.     end
  748.     term.setBgColor(textcolor)
  749.     for k=3, 5 do
  750.         term.setCursorPos(2, k)
  751.         term.write(str2)
  752.     end
  753. end
  754.  
  755. function drawProfile(profile, image)
  756.     term.setCursorPos(1, 1)
  757.     term.setBgColor(bgcolor)
  758.     term.write("Viewing "..tostring(profile[2]).."'s profile")
  759.     drawProfileImageBG()
  760.     if image then
  761.         paintutils.drawImage(image, 2, 3)
  762.     end
  763.     term.setBgColor(bgcolor)
  764.     term.setTextColor(textcolor)
  765.     term.setCursorPos(10, 3)
  766.     term.write("Real Name: ".. (profile[3] or "Hidden"))
  767.     term.setCursorPos(10, 4)
  768.     term.write("Age: ".. (profile[4] or "Hidden"))
  769.     term.setCursorPos(10, 5)
  770.     term.write("Forum Name: ".. (profile[6] or "Unknown"))
  771.     term.setCursorPos(1, 7)
  772.     term.setBgColor(imagebgcolor)
  773.     term.clearLine()
  774.     term.write("Infomation")
  775.     term.setBgColor(bgcolor)
  776.     if not profile[5] then profile[5] = "This user has not created a description." end
  777.     local y = 0
  778.     for k=1, #tostring(profile[5]), width do
  779.         y = y + 1
  780.         term.setCursorPos(1, 7 + y)
  781.         term.write(string.sub(tostring(profile[5]), k, k + width))
  782.     end
  783.     term.setCursorPos(1, height)
  784.     term.write("Press f to view friends, or Space to view messages.")
  785.     term.setBgColor(bgcolor)
  786. end
  787.  
  788. function viewProfile(profileID)
  789.     term.clear()
  790.     message("Loading Profile...", 0)
  791.     local profile = social.profile.get(username, password, profileID)
  792.     profile[2] = profileID
  793.     if not type(profile) == "table" then
  794.         message(tostring(profile))
  795.         profile = {"","","","",""}
  796.     end
  797.     local hasIcon, startBio = string.find(tostring(profile[5]), "!image!")
  798.     local image = {}
  799.     if hasIcon then
  800.         image = textutils.unserialize(string.sub(tostring(profile[5]), 0, hasIcon))
  801.         profile[5] = textutils.unserialize(string.sub(tostring(profile[5]), hasIcon, -1))
  802.     end
  803.     term.clear()
  804.     while true do
  805.         drawProfile(profile, image)
  806.         local event, key = os.pullEvent("key")
  807.         if key == 28 or key == keys.tab then
  808.             return
  809.         elseif key == keys.space then
  810.             messages = social.post.getuser(username, password, profileID)
  811.             messages[#messages + 1] = "Exit"
  812.             showingUserMessages = true
  813.             viewMessageList(true)
  814.         elseif key == keys.f then
  815.             ok, friends = pcall(social.friends.get, username, password, profileID)
  816.             showingThisUsersFriends = false
  817.             if ok then
  818.                 friends[#friends + 1] = "Exit"
  819.                 MyFriends(true)
  820.             else
  821.                 message("This user has no friends.", -1)
  822.             end
  823.         end
  824.     end
  825. end
  826.  
  827. function addFriend()
  828.     term.write("Add Friend:")
  829.     local response = social.friends.add(username, password, read())
  830.     if response == true then
  831.         term.setCursorBlink(false)
  832.         message("Friend Added!", -1)
  833.         return LoadFriends()
  834.     else
  835.         term.setCursorBlink(false)
  836.         message(response or "This user does not exist.", -1)
  837.         return
  838.     end
  839. end
  840.  
  841. -- View Your Profile Menu --
  842. function ViewYourProfile()
  843.     viewProfile(username)
  844. end
  845.  
  846. -- Sign In Menu --
  847. function AfterSigningIn()
  848.     message("Loading Messages...", 0)
  849.     pcall(LoadMessages)
  850.     message("Loading Friends...", 0)
  851.     pcall(LoadFriends)
  852.     message("Loading Users...", 0)
  853.     pcall(LoadUsers)
  854. end
  855.  
  856. function SignIn()
  857.     term.clear()
  858.     term.setBgColor(bgcolor)
  859.     term.setTextColor(textcolor)
  860.     term.setCursorPos(1, 1)
  861.     term.clearLine()
  862.     print("Sign In")
  863.     term.write("Username: ")
  864.     username = io.read()
  865.     term.write("Password: ")
  866.     password = read('*')
  867.     local result = user.login(username, password)
  868.     if result == true then
  869.         options = optionsSignedIn
  870.         local file = fs.open("SocialNetworkSignIn", "w")
  871.         file.writeLine(username)
  872.         file.writeLine(password)
  873.         file.close()
  874.         AfterSigningIn()
  875.     elseif result == "Could not verify credentials." then
  876.         message("The username or password you typed in is incorrect.", -1)
  877.     else
  878.         message(result, -1)
  879.     end
  880. end
  881.  
  882. function Register()
  883.     term.clear()
  884.     term.setBgColor(bgcolor)
  885.     term.setTextColor(textcolor)
  886.     term.setCursorPos(1, 1)
  887.     term.clearLine()
  888.     print("Register")
  889.     term.write("Username: ")
  890.     username = io.read()
  891.     term.write("Password: ")
  892.     password = read('*')
  893.     term.write("Confirm Password: ")
  894.     local matchPassword = read('*')
  895.     if not password == matchPassword then
  896.         return false
  897.     end
  898.     term.write("Email address: ")
  899.     local email = ""
  900.     email = read()
  901.     while email == "" do
  902.         print("Our new forgotten password system uses emails to allow users to change their passwords by sending them an email.")
  903.         print("It is recommended that you type in an email address.")
  904.         email = read()
  905.     end
  906.     local response = user.register(username, password, email)
  907.     if response == true then
  908.         options = optionsSignedIn
  909.         local file = fs.open("SocialNetworkSignIn", "w")
  910.         file.writeLine(username)
  911.         file.writeLine(password)
  912.         file.close()
  913.         message("Account registered.", -1)
  914.     else
  915.         message(result, -1)
  916.     end
  917. end
  918.  
  919. function SignOut()
  920.     if fs.exists("SocialNetworkSignIn") then
  921.         fs.delete("SocialNetworkSignIn")
  922.     end
  923.     username, password = nil, nil
  924.     message("Signed out.", -1)
  925.     options = optionsSignedOut
  926. end
  927.  
  928. function Exit()
  929.     running = false
  930. end
  931.  
  932. optionsSignedOut = {{"Login", SignIn}, {"Register", Register}, {"Exit", Exit}}
  933. optionsSignedIn = {{"Global Chat", Chat},
  934.     {"All Messages", viewMessageList},
  935.     {"All Users", viewUserList},
  936.     {"Create New Post", sendMessagePrompt},
  937.     {"Update My Profile", updateProfilePrompt},
  938.     {"My Profile", ViewYourProfile},
  939.     {"My Friends", MyFriends},
  940.     {"Sign Out", SignOut},
  941.     {"Exit", Exit}
  942. }
  943.  
  944. -- Attempt to read from file
  945. local file = fs.open("SocialNetworkSignIn", "r")
  946. if file then
  947.     username = file.readLine()
  948.     password = file.readLine()
  949.     file.close()
  950.     message("Signing in...", 0)
  951.     if user.login(username, password) then
  952.         options = optionsSignedIn
  953.         AfterSigningIn()
  954.     else
  955.         options = optionsSignedOut
  956.     end
  957. else
  958.     options = optionsSignedOut
  959. end
  960.  
  961. -- Main menu --
  962. function drawMainMenu()
  963.     term.setBgColor(bgcolor)
  964.     term.setTextColor(textcolor)
  965.     term.setCursorPos(1, 1)
  966.     term.clear()
  967.     print("Main Menu")
  968.     for k, v in ipairs(options) do
  969.         term.setCursorPos(1, 4 + k)
  970.         if k == optionsel then
  971.             term.setBgColor(highlightcolor)
  972.             term.setTextColor(hitextcolor)
  973.         else
  974.             term.setBgColor(bgcolor)
  975.             term.setTextColor(textcolor)
  976.         end
  977.         term.clearLine()
  978.         term.write(v[1])
  979.     end
  980.     term.setBgColor(bgcolor)
  981.     term.setCursorPos(1, height - 3)
  982. end
  983.  
  984. function mainMenu()
  985.     while running do
  986.         drawMainMenu()
  987.         local event, key = os.pullEvent("key")
  988.         if key == 200 then
  989.             optionsel = math.max(optionsel - 1, 1)
  990.         elseif key == 208 then
  991.             optionsel = math.min(optionsel + 1, #options)
  992.         elseif key == 28 then
  993.             options[optionsel][2]()
  994.         end
  995.     end
  996. end
  997.  
  998. local signedin = false
  999. running = true
  1000. local ok, err = pcall(mainMenu)
  1001. term.setCursorPos(1, 1)
  1002. term.setBackgroundColor(colors.black)
  1003. term.clear()
  1004. if not ok then
  1005.     term.setTextColor(colors.red)
  1006.     print(err)
  1007. end
  1008. term.setTextColor(colors.white)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement