pfgpastebin

mailclient

Sep 20th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. mailServerRednetID = 0
  2. awaitingmail = {}
  3.  
  4. function string:split(inSplitPattern, outResults)
  5.   if not outResults then
  6.     outResults = {}
  7.   end
  8.   local theStart = 1
  9.   local theSplitStart, theSplitEnd = string.find(self, inSplitPattern, theStart)
  10.   while theSplitStart do
  11.     table.insert(outResults,string.sub(self,theStart,theSplitStart - 1))
  12.     theStart = theSplitEnd+1
  13.     theSplitStart,theSplitEnd = string.find(self,inSplitPattern,theStart)
  14.    
  15.   end
  16.   table.insert(outResults, string.sub(self,theStart))
  17.   return outResults
  18. end
  19.  
  20. rednet.open('bottom')
  21. function listen()
  22.     while true do
  23.       local senderid, message, protocol = rednet.receive("mailreq")
  24.       awaitingmail.insert(0,{message.split(',')[1], string.gsub(message.split(',')[2], "[comma]", ",")})
  25.     end
  26. end
  27. function compose()
  28.     while true do
  29.       term.clear()
  30.       term.setCursorPos(1,1)
  31.       print("You have "..#awaitingmail.." new messages")
  32.       write("Read (r) or Compose (c) >")
  33.       answer = read()
  34.       print('')
  35.       if(answer == "c")then
  36.         write('To (id): ')
  37.         to = read();
  38.         print('')
  39.         write('Message: ')
  40.         msg = read();
  41.         if(rednet.send(mailServerRednetID, to .. "," .. string.gsub(msg,",","[comma]"), "mailreq"))then
  42.             print("Sent")
  43.         else
  44.             print("Didn't send (Uh oh!)")
  45.         end
  46.       end
  47.       if(answer == "r")then
  48.         write("Number (1 - ".. #awaitingmail .."): ")
  49.         num = tonumber(read())
  50.         print(awaitingmail[num][1])
  51.         print(awaitingmail[num][2])
  52.       end
  53.     end
  54. end
  55. parallel.waitForAll(listen, compose)
Advertisement
Add Comment
Please, Sign In to add comment