
' pop3.bas
'
' This example demonstrates socket i/o by accessing a remote pop3 server and
' listing the mailbox contents. To run this example, use the command
' bas pop3.bas <server> <username> <password>
' Note that the Sarian must be correctly configured to route to the
' pop3 server.


' the following line ensures that string comparisons are not case sensitive
option compare sbCaseInsensitive

' This function waits for a line from the given filenumber or times out
' parameters: timeout [in] - the timeout in msecs
'             fileno [in] - the input filenumber
'             result [out] - return status: 1 if we got a line, 0 if we timed out
' Return value is the received line.
' Note that a received line may be incomplete if we timed out.
' A line is terminated with CR (which is removed from the returned string)
' LF characters are ignored and discarded.

function waitforline(timeout, fileno, result)
local ch, n, myline

result = 1
myline = ""

while waitevents(timeout) = fileno
	ch = input(1, fileno)
	if asc(ch) <> 10 then
		if asc(ch) = 13 then
			waitforline = myline
			exit sub
		endif
		myline = myline & ch
	endif
wend
' timed out
result = 0
waitforline = myline

end function

function retrieve(num, fileno)
local retrline
	print #fileno, "TOP ", num, " 30", "\r\n"
	retrline = waitforline(10000, fileno, res)
	if res = 0 then
		exit sub
	endif
	if retrline = "+OK" or retrline like "+OK*" then
		print "Got ", retrline, "\n"
	else
		exit sub
	endif
retrloop:
	retrline = waitforline(10000, fileno, res)
	if res = 0 then
		exit sub
	endif
'	print "Got ", retrline, "\n"
	if retrline = "." then
		print "End of message\n"
		exit sub
	endif
	if retrline like "From:*" or retrline like "Subject:*" then
		print retrline, "\n"
	endif

	goto retrloop

	
end function


split command() by " " to dest, user, passwd

if isundef(dest) or isundef(user) or isundef(passwd) then
	print "\nUsage: pop3.bas server username password\n"
	exit sub
endif

'print "dest = ", dest, "\n"
'print "user = ", user, "\n"
'print "passwd = ", passwd, "\n"

on error goto errorlabel

open dest & ":110" for socket as 1
print "successfully opened socket\n"

dummy = setevent(0)
dummy = setevent(1)

a = waitforline(10000, 1, res)
if res = 0 then
	goto finished
endif
if a = "+OK" or a like "+OK*" then
	print "Got ", a, "\n"
else
	goto finished
endif

print #1, "USER ", user, "\r\n"
a = waitforline(10000, 1, res)
if res = 0 then
	goto finished
endif
if a = "+OK" or a like "+OK*" then
	print "Got ", a, "\n"
else
	goto finished
endif

print #1, "PASS ", passwd, "\r\n"
a = waitforline(10000, 1, res)
if res = 0 then
	goto finished
endif
if a = "+OK" or a like "+OK*" then
	print "Got ", a, "\n"
else
	goto finished
endif

print #1, "LIST\r\n"
a = waitforline(10000, 1, res)
if res = 0 then
	goto finished
endif
if a = "+OK" or a like "+OK*" then
	print "Got ", a, "\n"
else
	goto finished
endif
listcnt = 0
listloop:
	a = waitforline(10000, 1, res)
	if res = 0 then
		goto finished
	endif
	print "Got ", a, "\n"
	if a = "." then
		goto dolisting
	endif
	listcnt = listcnt + 1
	mylist[listcnt] = a
	goto listloop


dolisting:

for i = 1 to listcnt
split mylist[i] by " " to num, size
print "Entry ", num, " size = ", size, "\n"
retrieve(num, 1)
'print "List[", i, "] = ", mylist[i], "\n"
next i

finished:
close 1
print "Finished\n"
stop


errorlabel:
print "encountered an error opening the socket\n"
print error$, "\n"
