[GB.NET.POP3]

* NEW: Allow enumeration of messages using FOR EACH
* BUG: Free POPClient instances created when using _get
* OPT: Add status codes to report wrong username or password when logging in
* OPT: When reading single lines use Line Input


git-svn-id: svn://localhost/gambas/trunk@5095 867c0c6c-44f3-4631-809d-bfa615b0a4ec
This commit is contained in:
Sebastian Kulesz 2012-08-28 22:59:38 +00:00
parent c5bbf1c0de
commit b3c5b7a761
7 changed files with 122 additions and 91 deletions

View file

@ -28,6 +28,22 @@ Update
C
i
3
WrongUsername
C
i
4
WrongPassword
C
i
5
SERVER_POSITIVE_RESPONSE
C
s
+OK
SERVER_NEGATIVE_RESPONSE
C
s
-ERR
#Pop3Client
C
@ -39,30 +55,6 @@ Status
r
i
SERVER_POSITIVE_RESPONSE
C
s
+OK
SERVER_NEGATIVE_RESPONSE
C
s
-ERR
CR
V
s
LF
V
s
CRLF
V
s
END_MULTILINE
V
s
Username
p
s
@ -107,6 +99,10 @@ _get
m
_POPMessage
(msgid)i
_next
m
_POPMessage
Open
m
b

View file

@ -16,17 +16,19 @@ Public Sub Main()
Dim i As Variant
Dim a As Variant
Dim a As _POPMessage
POP.Encrypt = Net.None
POP.Username = "USER"
POP.Username = "USERNAME"
POP.Password = "PASSWORD"
POP.Server = "pop3.SERVER.com"
POP.Open()
'Print POP.Exec("CAPA")
'POP.WaitForConnection()
'i = POP.Stat()
@ -37,22 +39,26 @@ Public Sub Main()
'Print POP.Size, POP.Count
'a = POP.List()
' i = POP.List()
' POP.List(4)
'
'
' Wait 3
'
'i = POP[4]
' a = POP[0]
a = POP[4].Size
i = POP[4].UniqueID
'a = POP[4].Size
'Stop
Print POP[1].ListingID
' Print POP[2].ListingID
' Print POP[3].ListingID
' Print POP[4].ListingID
'
' i = POP.UniqueID(4)
'a = POP.UniqueID()
Stop
'
' POP.List()
'
@ -60,21 +66,24 @@ Public Sub Main()
'Print POP.Retrieve(4)
'POP.NOOP()
'
' i = POP.UniqueID()
For i = 1 To 10
For Each a In POP
'
a = POP.List()
'Print POP.Send("NOOP")
'Wait 0.1
Print a.ListingID
'Print a.UniqueID
Next
' a = POP.List()
' 'Print POP.Send("NOOP")
'
' 'Wait 0.1
' Next
'Wait 2
POP.Quit()
POP.Close()
POP.Disconnect()
' Stop
POP = Null
End

View file

@ -13,3 +13,11 @@ Public Const Transaction As Integer = 2
''The client has issued the QUIT command and is waiting for a response
Public Const Update As Integer = 3
Public Const WrongUsername As Integer = 4
Public Const WrongPassword As Integer = 5
Public Const SERVER_POSITIVE_RESPONSE As String = "+OK"
Public Const SERVER_NEGATIVE_RESPONSE As String = "-ERR"

View file

@ -34,7 +34,6 @@
Export
''Use plaintext or an ssl encrypted conenction
'TODO: Add to the constructor
Property Encrypt As Integer
Private $iEncrypt As Integer
@ -46,16 +45,6 @@ Private $iStatus As Integer
''Both share the same API.
Private $oClient As POPClient
Public Const SERVER_POSITIVE_RESPONSE As String = "+OK"
Public Const SERVER_NEGATIVE_RESPONSE As String = "-ERR"
Static Public CR As String = "\r"
Static Public LF As String = "\n"
Static Public CRLF As String = CR & LF
Static Public END_MULTILINE As String = CRLF & Chr(46) & CRLF
''Returns or sets the username used to authenticate to the server.
''It cannot be modified after Open() is executed
Property Username As String
@ -93,14 +82,14 @@ Private $msgCache As New Collection
''Returns True if _Text_ begins with "+OK"
Static Public Function isPositive(Text As String) As Boolean
Return (Text Begins SERVER_POSITIVE_RESPONSE)
Return (Text Begins POP3.SERVER_POSITIVE_RESPONSE)
End
''Returns True if _Text_ begins with "-ERR"
Static Public Function isNegative(Text As String) As Boolean
Return (Text Begins SERVER_NEGATIVE_RESPONSE)
Return (Text Begins POP3.SERVER_NEGATIVE_RESPONSE)
End
@ -115,10 +104,14 @@ Static Public Function StripOK(Text As String) As String
End
Public Sub _get(msgid As Integer) As _POPMessage
Public Function _get(msgid As Integer) As _POPMessage
Dim oPOPMInstance As _POPMessage
If msgid = 0 Then msgid = 1
If msgid > Me.Count Then Return
If Not $msgCache.Exist(msgid) Then
oPOPMInstance = New _POPMessage(msgid, Me)
@ -131,12 +124,32 @@ Public Sub _get(msgid As Integer) As _POPMessage
End
Public Sub _next() As _PopMessage
Dim i As Integer
Dim oPOPMInstance As _POPMessage
If IsNull(Enum.Index) Then
Enum.Index = 1
Endif
If Enum.Index > Me.Count Then
Enum.Stop()
Return
Endif
oPOPMInstance = _get(Enum.Index)
Inc Enum.Index
Return oPOPMInstance
End
''Establish a connection. Choose Client depending on Encrypt
''Port defaults to 110 for TCPClient and 995 for SSLClient
Public Function Open() As Boolean
Dim $bUser, $bPass As Boolean
If Not Me.Server Then
Error.Raise("Server not set")
Endif
@ -177,21 +190,20 @@ Public Function Open() As Boolean
$iStatus = POP3.Authorization
'Set welcome message
Welcome = Trim($oClient.GetLine())
Debug "Logging in"
If _User(Me.Username) Then $bUser = True
If Not _User(Me.Username) Then
$iStatus = POP3.WrongUsername
Else If Not _Pass(Me.Password) Then
$iStatus = POP3.WrongPassword
Else
$iStatus = POP3.Transaction
Endif
If _Pass(Me.Password) Then $bPass = True
'If Hardened Then
' If UserOK And PassOK And NOOP() Then $iStatus = POP3.Transaction
' Else
If $bUser And $bPass Then $iStatus = POP3.Transaction
' Endif
Return ($iStatus = POP3.Transaction)
Return $iStatus
End
@ -229,6 +241,8 @@ Public Function Close() As Boolean
Debug "Disconnecting"
$oClient.Disconnect()
$msgCache = Null
Return isPositive(Response)
End
@ -254,7 +268,9 @@ Public Function Stat() As Integer[]
End
Private Function _NOOP() As Boolean
''Returns a positive response if the Client is in Transaction state.
''Can be used as a keep-alive feature
Public Function Ping() As Boolean
Dim Response As String
@ -264,14 +280,6 @@ Private Function _NOOP() As Boolean
End
''Returns a positive response if the Client is in Transaction state.
''Can be used as a keep-alive feature
Public Function Ping() As Boolean
Return _NOOP()
End
Public Function Reset() As Boolean
Dim Response As String
@ -439,7 +447,7 @@ End
''Clears inbox and message cache
Private Sub _Refresh()
Dim sResponse As Integer[][]
Dim sResponse As Integer[]
'Dim sMessage As String

View file

@ -85,12 +85,14 @@ Public Function GetLine() As String
Dim Response As String
Dim sLine As String
While Response Not Ends Pop3Client.CRLF
' While Response Not Ends Pop3Client.CRLF
'
' Read #sProcess, sLine, Lof(sProcess)
'
' Response &= sLine
' Wend
Read #sProcess, sLine, Lof(sProcess)
Response &= sLine
Wend
Line Input #sProcess, sLine
If Response Not Begins "+" Then
Error.Raise("Invalid POP response: " & Response)

View file

@ -119,23 +119,25 @@ End
''Get data from the stream until the EOL is reached
Public Function GetLine() As String
Dim Response As String
Dim sResponse As String
Dim sLine As String
While Response Not Ends Pop3Client.CRLF
Line Input #sSocket, sResponse
Read #sSocket, sLine, Lof(sSocket)
'While Response Not Ends Pop3Client.CRLF
Response &= sLine
Wend
'Read #sSocket, sLine, Lof(sSocket)
If Response Not Begins "+" Then
Error.Raise("Invalid POP response: " & Response)
'Response &= sLine
' Wend
If sResponse Not Begins "+" Then
Error.Raise("Invalid POP response: " & sResponse)
Endif
'Print Response
'Print sResponse
Return Response
Return sResponse
End

View file

@ -128,3 +128,9 @@ Public Sub Clear()
$sUID = ""
End
Public Sub _free()
$oPOPClient = Null
End