ComputerMan123

Client

Oct 22nd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. --MailClient, developed by NitrogenFingers--
  2.  
  3. w,h = term.getSize()
  4. umail = { }
  5. rmail = { }
  6. slc = 1
  7. serverid = 0
  8. side = "left"
  9.  
  10. confirmmsg = ""
  11. newmsg = ""
  12. newto = 0
  13.  
  14. emidx = 1
  15. emlist = { }
  16.  
  17. state = 1
  18. states = {
  19. ["TOP"] = {
  20. id = 1,
  21. options = 6
  22. },
  23. ["COMPOSETEXT"] = {
  24. id = 2
  25. },
  26. ["COMPOSEMENU"] = {
  27. id = 3,
  28. options = 2
  29. },
  30. ["UNREAD"] = {
  31. id = 4,
  32. options = 2
  33. },
  34. ["ARCHIVE"] = {
  35. id = 5,
  36. options = 4
  37. },
  38. ["CONFIRM"] = {
  39. id = 6,
  40. options = 1
  41. },
  42. ["CHANGENAME"] = {
  43. id = 7
  44. }
  45. }
  46.  
  47. -- Helper Functions --
  48.  
  49. function split(str, pattern)
  50. local t = { }
  51. local fpat = "(.-)" .. pattern
  52. local last_end = 1
  53. local s, e, cap = str:find(fpat, 1)
  54. while s do
  55. if s ~= 1 or cap ~= "" then
  56. table.insert(t,cap)
  57. end
  58. last_end = e+1
  59. s, e, cap = str:find(fpat, last_end)
  60. end
  61. if last_end <= #str then
  62. cap = str:sub(last_end)
  63. table.insert(t, cap)
  64. end
  65. return t
  66. end
  67.  
  68. -- Interface Drawing --
  69.  
  70. function printHeader()
  71. term.setCursorPos(1, 1)
  72. term.write("NITROGENFINGERS EMAIL CLIENT")
  73. printRight(1, "VERSION 1.0")
  74. term.setCursorPos(1, 2)
  75. term.write("Computer ID: "..os.getComputerID())
  76. end
  77.  
  78. function printConfirm()
  79. local boxsize = 26
  80. local ok = "[ OK ]"
  81. printCentered(8, string.rep("*", boxsize))
  82. printCentered(9, "*"..string.rep(" ", boxsize-2).."*")
  83. local ws = (boxsize - #confirmmsg - 2)/2
  84. printCentered(10, "*"..string.rep(" ", ws)..confirmmsg..string.rep(" ", ws).."*")
  85. ws = (boxsize - #ok - 2)/2
  86. printCentered(11, "*"..string.rep(" ", ws)..ok..string.rep(" ", ws).."*")
  87. printCentered(12, "*"..string.rep(" ", boxsize-2).."*")
  88. printCentered(13, string.rep("*", boxsize))
  89. end
  90.  
  91. function printChangeName()
  92. term.setCursorPos(4, 6)
  93. term.write("Enter new name: ")
  94. end
  95.  
  96. function printStored()
  97. term.setCursorPos(1,4)
  98. term.write("From: "..emlist[emidx].from)
  99. term.setCursorPos(1,5)
  100. term.write("Sent at: "..textutils.formatTime(tonumber(emlist[emidx].time)))
  101. term.setCursorPos(1,6)
  102. term.write("--------------------")
  103. term.setCursorPos(1,7)
  104. print(emlist[emidx].body)
  105. term.setCursorPos(1,h-1)
  106. term.write("Reading Email "..emidx.." of "..table.getn(emlist))
  107. end
  108.  
  109. function printArchiveMenu()
  110. local ws = string.rep(" ", 4)
  111. local pr, ba, de, ne
  112. if slc==1 then pr = "[ PREV ]" else pr = " PREV " end
  113. if slc==2 then ba = "[ BACK ]" else ba = " BACK " end
  114. if slc==3 then de = "[ DELETE ]" else de = " DELETE " end
  115. if slc==4 then ne = "[ NEXT ]" else ne = " NEXT " end
  116. term.setCursorPos(1,h)
  117. printCentered(h, ws..pr..ws..ba..ws..de..ws..ne)
  118. end
  119.  
  120. function printUnreadMenu()
  121. local full = ""
  122. if slc==1 then full = "[ ARCHIVE ] DELETE "
  123. else full = " ARCHIVE [ DELETE ]"
  124. end
  125. printCentered(h, full)
  126. end
  127.  
  128. function printCompose()
  129. term.setCursorPos(1,4)
  130. term.write("To:")
  131. term.setCursorPos(1,5)
  132. term.write("Body:")
  133. term.setCursorPos(1,h)
  134. term.write("Hit Enter twice to end the email.")
  135. end
  136.  
  137. function printComposeMenu()
  138. term.setCursorPos(1,h)
  139. term.clearLine()
  140. local optionstr = ""
  141. if slc == 1 then
  142. optionstr = " [ SEND ] DELETE "
  143. else
  144. optionstr = " SEND [ DELETE ] "
  145. end
  146. printCentered(h, optionstr)
  147. end
  148.  
  149. function printTopMenu()
  150. term.setCursorPos(1, h-1)
  151. term.write("Hi, "..os.getComputerLabel().."!")
  152. term.setCursorPos(1, h)
  153. term.write("You have "..table.getn(umail).." new message(s).")
  154.  
  155. printCentered(4, "Select Option:")
  156. if slc == 1 then printCentered(7, " [ COMPOSE EMAIL ] ")
  157. else printCentered(7, " COMPOSE EMAIL ") end
  158. if slc == 2 then printCentered(8, " [ UNREAD EMAILS ] ")
  159. else printCentered(8, " UNREAD EMAILS ") end
  160. if slc == 3 then printCentered(9, "[ ARCHIVED EMAILS ]")
  161. else printCentered(9, " ARCHIVED EMAILS ") end
  162. if slc == 4 then printCentered(10, " [ CHANGE NAME ] ")
  163. else printCentered(10, " CHANGE NAME ") end
  164. if slc == 5 then printCentered(11, " [ REFRESH EMAIL ] ")
  165. else printCentered(11, " REFRESH EMAIL ") end
  166. if slc == 6 then printCentered(12, " [ QUIT ] ")
  167. else printCentered(12, " QUIT ") end
  168. end
  169.  
  170. function printRight(height, value)
  171. local xpos = w - string.len(value)
  172. term.setCursorPos(xpos, height)
  173. term.write(value)
  174. end
  175.  
  176. function printCentered(height, value)
  177. local xpos = w/2 - string.len(value)/2
  178. term.setCursorPos(xpos, height)
  179. term.write(value)
  180. end
  181.  
  182. -- Email Tools --
  183.  
  184. function sendEmail()
  185. rednet.open(side)
  186. local newemail = "$EMAIL"..newto
  187. .."!SP!"..os.getComputerLabel()
  188. .."!SP!"..os.time().."!SP!"
  189. ..newmsg
  190. rednet.send(serverid, newemail)
  191. if rednet.receive(5)~=nil then
  192. confirmmsg = "MESSAGE SENT"
  193. else
  194. confirmmsg = "ERROR OCCURRED"
  195. end
  196. rednet.close(side)
  197. end
  198.  
  199. function readEmails()
  200. rednet.open(side)
  201. rednet.send(serverid, "$REQUEST")
  202. local id, mail = rednet.receive(5)
  203. if mail==nil then
  204. confirmmsg = "SERVER ERROR"
  205. rednet.close("left")
  206. return
  207. end
  208. mail = string.gsub(mail, "$RESPONSE", "")
  209.  
  210. local t = split(mail, "!SP!")
  211.  
  212. for i=1,table.getn(t),3 do
  213. table.insert(umail, {
  214. from = t[i],
  215. time = t[i+1],
  216. body = t[i+2]
  217. })
  218. end
  219. rednet.close(side)
  220. end
  221.  
  222. function readFromArchive()
  223. if not fs.exists(shell.resolve(".").."/archive") then return end
  224. local file = io.open(shell.resolve(".").."/archive", "r")
  225. local fullstr = ""
  226. local fline = file:read()
  227. while fline do
  228. fullstr=fullstr..fline
  229. fline = file:read()
  230. end
  231. file:close()
  232. local t = split(fullstr, "!SP!")
  233. for i=1,#t,3 do
  234. table.insert(rmail, {
  235. from = t[i],
  236. time = t[i+1],
  237. body = t[i+2]
  238. })
  239. end
  240. end
  241.  
  242. function writeToArchive()
  243. local file = io.open(shell.resolve(".").."/archive", "w")
  244. local arcstr = ""
  245. for i=1,#rmail do
  246. arcstr=arcstr.."!SP!"..rmail[i].from.."!SP!"..
  247. rmail[i].time.."!SP!"..rmail[i].body
  248. end
  249. file:write(arcstr)
  250. file:close()
  251. end
  252.  
  253. -- Interface Logic --
  254.  
  255. function runInterface()
  256. term.clear()
  257. while true do
  258. if state==states["TOP"].id then
  259. term.clear()
  260. printHeader()
  261. printTopMenu()
  262. local e,key = os.pullEvent("key")
  263. if key==200 and slc>1 then slc = slc-1
  264. elseif key==208 and slc<6 then slc=slc+1
  265. elseif key==28 then
  266. if slc==1 then
  267. state = states["COMPOSETEXT"].id
  268. newmsg = ""
  269. elseif slc==2 then
  270. if table.getn(umail)==0 then
  271. confirmmsg="No new mail."
  272. state=states["CONFIRM"].id
  273. else
  274. emidx=1
  275. emlist = umail
  276. state=states["UNREAD"].id
  277. end
  278. elseif slc==3 then
  279. if table.getn(rmail)==0 then
  280. confirmmsg="Archive Empty."
  281. state=states["CONFIRM"].id
  282. else
  283. emidx=1
  284. emlist = rmail
  285. state=states["ARCHIVE"].id
  286. end
  287. elseif slc==4 then
  288. state=states["CHANGENAME"].id
  289. elseif slc==5 then
  290. local ocount = table.getn(umail)
  291. readEmails()
  292. if ocount < table.getn(umail) then
  293. confirmmsg = "Messages Received!"
  294. else
  295. confirmmsg = "No new mail."
  296. end
  297. state = states["CONFIRM"].id
  298. elseif slc==6 then
  299. term.clear()
  300. term.setCursorPos(1,1)
  301. return
  302. end
  303. end
  304. elseif state==states["CHANGENAME"].id then
  305. term.clear()
  306. printHeader()
  307. printChangeName()
  308. local nname = io.read()
  309. if #nname>25 then
  310. nname = string.sub(nname, 1, 25)
  311. end
  312. os.setComputerLabel(nname)
  313. state=states["TOP"].id
  314. elseif state==states["COMPOSETEXT"].id then
  315. term.clear()
  316. printHeader()
  317. printCompose()
  318. term.setCursorPos(5, 4)
  319. newto = tonumber(io.read())
  320. term.setCursorPos(1, 6)
  321. local newmail=""
  322. repeat
  323. newmail = io.read().."\n"
  324. newmsg=newmsg..newmail.." "
  325. until newmail=="\n"
  326. state = states["COMPOSEMENU"].id
  327. elseif state==states["COMPOSEMENU"].id then
  328. printComposeMenu()
  329. local e, key = os.pullEvent("key")
  330. if key==203 and slc>1 then
  331. slc=slc-1
  332. elseif key==205 and slc<2 then
  333. slc=slc+1
  334. elseif key==28 then
  335. if slc==1 then
  336. sendEmail()
  337. state = states["CONFIRM"].id
  338. else
  339. state = states["TOP"].id
  340. end
  341. end
  342. elseif state==states["ARCHIVE"].id then
  343. term.clear()
  344. printHeader()
  345. printStored()
  346. printArchiveMenu()
  347. local e, key = os.pullEvent("key")
  348. if key==203 and slc>1 then
  349. slc=slc-1
  350. elseif key==205 and slc<4 then
  351. slc=slc+1
  352. elseif key==28 then
  353. if slc==1 and emidx>1 then emidx=emidx-1
  354. elseif slc==2 then state=states["TOP"].id
  355. elseif slc==3 then
  356. table.remove(rmail, emidx)
  357. writeToArchive()
  358. if #rmail==0 then
  359. confirmmsg= "Archive Empty."
  360. state=states["CONFIRM"].id
  361. end
  362. if emidx>#rmail then emidx=#rmail end
  363. elseif slc==4 and emidx<#rmail then
  364. emidx=emidx+1
  365. end
  366. end
  367. elseif state==states["UNREAD"].id then
  368. term.clear()
  369. printHeader()
  370. printStored()
  371. printUnreadMenu()
  372. local e, key = os.pullEvent("key")
  373. if key==203 and slc>1 then
  374. slc=slc-1
  375. elseif key==205 and slc<2 then
  376. slc=slc+1
  377. elseif key==28 then
  378. if slc==1 then
  379. table.insert(rmail, {
  380. from = emlist[emidx].from,
  381. time = emlist[emidx].time,
  382. body = emlist[emidx].body
  383. })
  384. writeToArchive()
  385. end
  386. table.remove(umail, emidx)
  387. emlist = umail
  388. if table.getn(umail)==0 then
  389. confirmmsg="Inbox Empty."
  390. state=states["CONFIRM"].id
  391. end
  392. end
  393. elseif state==states["CONFIRM"].id then
  394. printConfirm()
  395. local e, key = os.pullEvent("key")
  396. if key==28 then
  397. state = states["TOP"].id
  398. end
  399. end
  400. end
  401. end
  402.  
  403. -- On Startup --
  404.  
  405. function getPrefs()
  406. local file = io.open(shell.resolve(".").."/pref")
  407. if not file then
  408. print("pref file not found! Make sure to include"
  409. .." it in your mail directory.")
  410. return false
  411. end
  412. local sidl = file:read()
  413. sidl = string.gsub(sidl, "serverid=", "")
  414. print(sidl)
  415. sidl = tonumber(sidl)
  416. if sidl==0 then
  417. print("Client prefs not initialized- check readme for detals.")
  418. return false
  419. end
  420. serverid = sidl
  421. sidl = file:read()
  422. sidl = string.gsub(sidl, "side=", "")
  423. side = sidl
  424. return true
  425. end
  426.  
  427. if not getPrefs() then return end
  428. readFromArchive()
  429. rednet.broadcast("Computer: "..os.getComputerID().." is now on network.")
  430. if os.getComputerLabel() == nil then
  431. os.setComputerLabel("New User")
  432. end
  433.  
  434. runInterface()
Add Comment
Please, Sign In to add comment