Advertisement
cyber_Ahn

mail Server

Feb 5th, 2016
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. local found = fs.exists("pw.db")
  2. if found == false then
  3. local file = fs.open("pw.db","w")
  4. file.writeLine("// pw database")
  5. file.close()
  6. end
  7. local found = fs.exists("mail.db")
  8. if found == false then
  9. local file = fs.open("mail.db","w")
  10. file.writeLine("// mail database")
  11. file.close()
  12. end
  13. --config--
  14. local channel = 210
  15. modem = peripheral.wrap("back")
  16. --vars--
  17. local power = true
  18. local sendBack = 1
  19. -- func split--
  20. function split(pString,pPattern)
  21. local Table = {}
  22. local fpat = "(.-)"..pPattern
  23. local last_end = 1
  24. local s,e,cap = pString:find(fpat,1)
  25. while s do
  26. if s ~= 1 or cap ~= "" then
  27. table.insert(Table,cap)
  28. end
  29. last_end = e+1
  30. s,e,cap = pString:find(fpat,last_end)
  31. end
  32. if last_end <= #pString then
  33. cap = pString:sub(last_end)
  34. table.insert(Table,cap)
  35. end
  36. return Table
  37. end
  38. --decode--
  39. function decode(msg)
  40. if msg == "shutdown" then
  41. --shell.run("shutdown")
  42. else
  43. data = split(msg,"#")
  44. if data[1] == "reg" then
  45. local file = fs.open("pw.db","a")
  46. file.writeLine(data[2])
  47. file.close()
  48. sleep(2)
  49. modem.transmit(sendBack,channel,"Register OK")
  50. elseif data[1] == "log" then
  51. file = fs.open("pw.db","r")
  52. fileDataX = {}
  53. local line = file.readLine()
  54. repeat
  55. table.insert(fileDataX,line)
  56. line = file.readLine()
  57. until line == nil
  58. file.close()
  59. local out = "not found"
  60. for iu=1,#fileDataX do
  61. dataX = fileDataX[iu]
  62. if data[2] == dataX then
  63. out = "ok"
  64. end                    
  65. end
  66. sleep(2)
  67. print(out)
  68. modem.transmit(sendBack,channel,out)
  69. elseif data[1] == "ask" then
  70. local mail_out = ""
  71. file = fs.open("mail.db","r")
  72. fileDataX = {}
  73. local line = file.readLine()
  74. repeat
  75. table.insert(fileDataX,line)
  76. line = file.readLine()
  77. until line == nil
  78. file.close()
  79. for iu=1,#fileDataX do
  80. dataX = fileDataX[iu]
  81. dataB = split(dataX,"#")
  82. if dataB[1] == data[2] then
  83. mail_out = mail_out..dataB[2].."#"
  84. end
  85. end
  86. print(mail_out)
  87. sleep(2)
  88. modem.transmit(sendBack,channel,mail_out)
  89. elseif data[1] == "delete" then
  90. file = fs.open("mail.db","r")
  91. local fileDataX = {}
  92. local line = file.readLine()
  93. repeat
  94. table.insert(fileDataX,line)
  95. line = file.readLine()
  96. until line == nil
  97. file.close()
  98. number = {}
  99. for iu=1,#fileDataX do
  100. dataX = fileDataX[iu]
  101. dataB = split(dataX,"#")
  102. if dataB[1] == data[2] then
  103. table.insert(number,tostring(iu))
  104. end
  105. end
  106. local xx = #number
  107. for ix=1,#number do
  108. print(number[ix])
  109. num = tonumber(number[xx])
  110. table.remove(fileDataX,num)
  111. xx = xx - 1
  112. end
  113. local file = fs.open("mail.db","w")
  114. for iux=1,#fileDataX do
  115. file.writeLine(fileDataX[iux])
  116. end
  117. file.close()
  118. sleep(2)
  119. modem.transmit(sendBack,channel,"deleted")
  120. elseif data[1] == "send" then
  121. dataC = split(data[2],"-")
  122. set_w = dataC[1].."#"..dataC[2]
  123. local file = fs.open("mail.db","a")
  124. file.writeLine(set_w)
  125. file.close()
  126. sleep(2)
  127. modem.transmit(sendBack,channel,"mail send")
  128. end
  129. end
  130. end
  131. --program--
  132. print("start mail Server")
  133. print("start modem on channel "..channel)
  134. modem.open(channel)
  135. print("mail Server started")
  136. --open pull event--
  137. while power == true do
  138. event, modemside, sendchan, replaychan, message, dist = os.pullEvent("modem_message")
  139. sendBack = replaychan
  140. print(message)
  141. decode(message)
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement