This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SGunner2014's profile picture

Help with an email system

Started by SGunner2014, 20 September 2014 - 04:17 PM
SGunner2014 #1
Posted 20 September 2014 - 06:17 PM
Hi,

I made an email program for a server that, when it receives a message saying "read", it will read a file on the server, and will send that back as a string.
However, this does not work as when the server is meant to loop back to the beginning, it does not. It then returns "attempt to index nil" when I try to query the server again from the client.

Also, now the server and client don't work at all?
What has happened?

The Code:

Server:


--#[Email Server]--
local st = 1
sleep(0.1)
while st == 1 do
rednet.open("right")
print("STARTED")
local event, id, text = os.pullEvent() --#Checks for compose
if event == "rednet_message" then
  if text == "EmailCompose" then
    print("CEmail R")
    local st = 2
    local st = 2
    while st == 2 do
	  local event,id, text = os.pullEvent() --#Checks for UID
	  if event == "rednet_message" then
	    print(text..": UID")
		  local UserID = text
		  local st = 3
		  while st == 3 do
		    print("DEBUG: 3")
		    local event, id, text = os.pullEvent() --#Checks for SID
		    if event == "rednet_message" then
			  print("DEBUG: 4")
			  local SendID = text
			  local st = 4
			  while st == 4 do
			    print("DEBUG: 5")
			    local event, id, text = os.pullEvent() --#Checks for message
			    if event == "rednet_message" then
				  print("DEBUG: 6")
				  local message = text
				  local st = 5
				  while st == 5 do
				    local event, id, text = os.pullEvent() --#Checks for end command
				    if event == "rednet_message" then
					  if text == "finito" then
					    sleep(0.1)
					    rednet.send(id, "true")
					    local h = fs.open(SendID..".em", "a")
					    h.writeLine("User ID "..UserID.." sent a message")
					    h.writeLine("Message: "..message)
					    shell.run("startup")		  
					    else
					    print("FAILED AT 5.")
					    local st = 1
					  end
				    end
				  end
			    end
			  end
		    end
		  end
	  end
    end
  elseif text == "EmailRead" then
    print("ERead")
    local st = 100
    rednet.send(id, "true")
    while st == 100 do
	  local event, id, text = os.pullEvent()
	  if event == "rednet_message" then
	    local UserID = text
	    local h = fs.open(UserID..".em", "r")
	    emailCache = h.readAll()
	    sleep(0.1)
	    rednet.send(id, emailCache)
	    local st = 1
	  end
    end
  end
end
end

The Client:


--#[Email Client]--
term.clear()
term.setCursorPos(1,1)
print("User Referrer ID:")
local userid = read()
print("Command: ")
local s = 1
while s == 1 do
local command = read()
if command == "compose" then
  print("Composing new email.")
  rednet.open("right")
  rednet.send(2, "EmailCompose")
  sleep(0.1)
  rednet.send(2, userid)
  sleep(0.1)
  print("User to send to:")
  local sendid = read()
  rednet.send(2, sendid)
  print("Message:")
  local messageSend = read()
  rednet.send(2, messageSend)
  sleep(0.1)
  rednet.send(2, "finito")
  local s = 2
  while s == 2 do
    local event, id, text = os.pullEvent()
    if event == "rednet_message" then
      if text == "true" then
        print("Success!")
        sleep(2)
        local s = 1
      else
        print("Success!")
        sleep(2)
        local s = 1
      end
    end
  end
elseif command == "read" then
  print("Querying server.")
  rednet.send(2, "EmailRead")
  local s = 100
  while s == 100 do
  local event, id, text = os.pullEvent()
  if event == "rednet_message" then
    if text == "true" then
      rednet.send(id, userid)
      local s = 101
      while s == 101 do
        local event, id, text = os.pullEvent()
        if event == "rednet_message" then
          print(text)
          sleep(5)
          local s = 1
        end
      end
    else
      print("Error at end")
      sleep(2)
      local s = 1
    end
  end
  end
end
end
KingofGamesYami #2
Posted 20 September 2014 - 08:20 PM
Ok, so just glancing through the server code, I see you are using a variable with many while loops. You define it locally to each loop, which means it will not work as you want.

Specifically:

                                  while st == 5 do
                                    local event, id, text = os.pullEvent() --#Checks for end command
                                    if event == "rednet_message" then
                                          if text == "finito" then
                                            sleep(0.1)
                                            rednet.send(id, "true")
                                            local h = fs.open(SendID..".em", "a")
                                            h.writeLine("User ID "..UserID.." sent a message")
                                            h.writeLine("Message: "..message)
                                            shell.run("startup")                  
                                            else
                                            print("FAILED AT 5.")
                                            local st = 1
                                          end
                                    end

To the loop, st will always be 5, because you never change it within that scope.

Same thing as doing this:

local s = 1
print( s )
do --#do ... end is simply a scope limit, it runs the code once.
 local s = 2
 print( s )
end
print( s )

Instead, change it to this:

local s = 1
print( s )
do
  s = 2 --#note: global declaration of variable "s"
  print( s )
end
print( s )
Edited on 20 September 2014 - 06:22 PM
SGunner2014 #3
Posted 20 September 2014 - 08:21 PM
Okay, thanks :)/>
Going to fix that now and test it.

Thanks,
- Sam
SGunner2014 #4
Posted 20 September 2014 - 08:35 PM
Okay, so now there's another problem.

The server will stop doing anything after a while and when I try to enter a command on the client, nothing is sent back to it.
Do you know how I could fix this?

Thanks,
- Sam
KingofGamesYami #5
Posted 20 September 2014 - 08:48 PM
Please post your current code, or update the OP with the edited code.
SGunner2014 #6
Posted 20 September 2014 - 09:02 PM
Here's the updated code:


--[Email Server]--
st = 1
sleep(0.1)
while st == 1 do
rednet.open("right")
print("STARTED")
local event, id, text = os.pullEvent() --#Checks for compose
if event == "rednet_message" then
  if text == "EmailCompose" then
    print("CEmail R")
    st = 2
    while st == 2 do
	  local event,id, text = os.pullEvent() --#Checks for UID
	  if event == "rednet_message" then
	    print(text..": UID")
		  local UserID = text
		  st = 3
		  while st == 3 do
		    print("DEBUG: 3")
		    local event, id, text = os.pullEvent() --#Checks for SID
		    if event == "rednet_message" then
			  print("DEBUG: 4")
			  local SendID = text
			  st = 4
			  while st == 4 do
			    print("DEBUG: 5")
			    local event, id, text = os.pullEvent() --#Checks for message
			    if event == "rednet_message" then
				  print("DEBUG: 6")
				  local message = text
				  st = 5
				  while st == 5 do
				    local event, id, text = os.pullEvent() --#Checks for end command
				    if event == "rednet_message" then
					  if text == "finito" then
					    sleep(0.1)
					    rednet.send(id, "true")
					    local h = fs.open(SendID..".em", "a")
					    h.writeLine("User ID "..UserID.." sent a message")
					    h.writeLine("Message: "..message)
					    shell.run("startup")		  
					    else
					    print("FAILED AT 5.")
					    st = 1
					  end
				    end
				  end
			    end
			  end
		    end
		  end
	  end
    end
  elseif text == "EmailClear" then
    st = 8
    while st == 8 do
	  local event, id, text = os.pullEvent()
	  if event == "rednet_message" then
	    fs.delete(text..".em")
	    sleep(0.1)
	    rednet.send(id, "true")
	    st = 1
	  end
    end
  elseif text == "EmailRead" then
    print("ERead")
    st = 100
    rednet.send(id, "true")
    while st == 100 do
	  print("true1")
	  local event, id, text = os.pullEvent()
	  if event == "rednet_message" then
	    local UserID = text
	    local fileExist = fs.exists(UserID..".em")
	    if fileExist == true then
	    local h = fs.open(UserID..".em", "r")
	    emailCache = h.readAll()
	    sleep(0.1)
	    rednet.send(id, emailCache)
	    st = 1
	    elseif fileExist == false then
		  rednet.send(id, "false")
	    end
	  end
    end
  end
end
end


--[Email Client]--
term.clear()
term.setCursorPos(1,1)
print("Emails BETA")
print("Type exit at any point to exit")
print("Enter your UID:")
local userid = read()
print("Command: ")
s = 1
while s == 1 do
local command = read()
if command == "compose" then
  print("Composing new email.")
  rednet.open("right")
  rednet.send(2, "EmailCompose")
  sleep(0.1)
  rednet.send(2, userid)
  sleep(0.1)
  print("User to send to:")
  local sendid = read()
  rednet.send(2, sendid)
  print("Message:")
  local messageSend = read()
  rednet.send(2, messageSend)
  sleep(0.1)
  rednet.send(2, "finito")
  s = 2
  while s == 2 do
    local event, id, text = os.pullEvent()
    if event == "rednet_message" then
	  if text == "true" then
	    print("Success!")
	    sleep(2)
	    s = 1
	  else
	    print("Success!")
	    sleep(2)
	    s = 1
	  end
    end
  end
elseif command == "clear" then
  print("Sending clear request to server.")
  rednet.send(2, "EmailClear")
  sleep(0.1)
  rednet.send(2, userid)
  s = 106
  while s == 106 do
  local event, id, text = os.pullEvent()
  if event == "rednet_message" then
    if text == "true" then
	  print("Inbox cleared!")
	  s = 1
    else
	  print("Inbox not cleared!")
	  s = 1
    end
  end
  end
elseif command == "exit" then
  print("Exiting...")
  sleep(2)
  shell.run("startup")
elseif command == "read" then
  print("Querying server.")
  rednet.send(2, "EmailRead")
  s = 100
  while s == 100 do
  print("true")
  local event, id, text = os.pullEvent()
  if event == "rednet_message" then
    if text == "true" then
	  print("true1")
	  rednet.send(id, userid)
	  s = 101
	  while s == 101 do
	    local event, id, text = os.pullEvent()
	    if event == "rednet_message" then
		  if text == "false" then
		  print("Could not get emails!")
		  else
		  print(text)
		  s = 1
		  end
	    end
	  end
    else
	  print("Error at end")
	  sleep(2)
	  s = 1
    end
  end
  end
end
end

Thanks,
- Sam