sockets

Sarah sarahr at genesearch.com.au
Tue Jul 9 20:32:01 EDT 2002


Sorry for the long email, but here are the basic routines I use for POP 
communications.
You'll need to customize to fit in with your field names instead of 
mine. Also, check the setupNextCall handler as that is specifically for 
my button ID.

Everything gets logged so debugging is quite easy.

Regards,
Sarah

-----------------------------
-- these variables are up here so all handlers in this button can use it
local socketAddr, msgList

on mouseUp
   put empty into fld "Log"
   put empty into fld "Email"
   put "Checking email..." into fld "Status"

   -- close any open sockets (in case there has been a failure already)
   repeat until the openSockets  is empty
     close socket line 1 of the openSockets
   end repeat

   -- open POP port & login (fld "POP" contains the address of my mail 
server)
   put fld "POP" & ":110" into socketAddr
   open socket to socketAddr with message "startEmail"
end mouseUp


-- this handler gets called after the socket has opened
on startEmail
   -- read the welcome message from the server
   read from socket socketAddr until CRLF
   put it & cr after fld "Log"

   -- send all the header stuff, checking for a response after each one
   -- the error checking is in the sendReceive handler
   sendReceive "USER " & fld "User"
   sendReceive "PASS " & the cHiddenPass of fld "Password"

   getList   -- this puts a list of waiting emails into msgList

   repeat for each line m in msgList
       put "Reading email..." into fld "Status"
       put getMsg(msgNum) into newMsg
       put newMsg & cr after fld "Email"

         -- delete message from server
         -- comment this out while you're testing
         sendReceive "DELE " & msgNum
   end repeat

   -- close the port before closing the socket
   sendReceive "QUIT"
   close socket socketAddr

   setupNextCall
end startEmail


on setupNextCall
   -- set up next check, making sure to delete any leftover pending 
messages
   put the pendingMessages into pendMsg
   repeat for each line L in pendMsg
     if L contains "button ID 1013" then cancel item 1 of L
   end repeat

   send mouseUp to button ID 1013 in (fld "Check" * 60) seconds
   put the seconds + (fld "Check" * 60) into checkTime
   convert checkTime to long time
   put "Next check at " & checkTime into fld "Status"
end setupNextCall


-- this function gets the list and extracts the number of waiting emails
on getList
   sendOnly "LIST"
   read from socket socketAddr until "." & CRLF
   put it into msgList
   -- delete header & footer
   put line 2 to -2 of msgList into msgList
   put msgList & cr after fld "Log"
end getList


-- this function retrieves the whole message if it is small enough
function getMsg msgNum
   sendOnly "RETR " & msgNum
   read from socket socketAddr until "." & CRLF
   return it
end getMsg


-- this routine sends data to the server and reads the response
-- POP responses always start with +OK or -ERR
on sendReceive theData
   put "-> " & theData & cr after fld "Log"
   write theData & CRLF to socket socketAddr
   read from socket socketAddr until CRLF
   put it after fld "Log"

   if word 1 of it is not "+OK" then
     close socket socketAddr
     exit to top
   end if
end sendReceive


-- this routine is for sending only, with no response expected
on sendOnly theData
   put "-> " & theData & cr after fld "Log"
   write theData & CRLF to socket socketAddr
end sendOnly


-- these 2 handlers are just to catch a connection failure
on socketTimeOut
   close socket socketAddr
   setupNextCall
   exit to top
end socketTimeOut

on socketError sockID, errMsg
   close socket socketAddr
   setupNextCall
   exit to top
end socketError

-----------------------------



On Wednesday, July 10, 2002, at 10:21  AM, Larry Forsgren wrote:

> Thanks for your reply Sarah,
>
> I actually did try to close the socket by sending "QUIT" to it
> using the write command but that didn't change anything.
> I only get a reply from the socket the first time so the quit
> command is not forwarded either. I also tried to wait for the
> connection to time out but I cannot access the socket again
> until I restart Revolution.
> Perhaps someone has a Rev example of how to use sockets
> to access a mail server.
> (I run this on Windows XP if that has any bearing on this).
>
> Larry





More information about the use-livecode mailing list