POP3 User Library

Developed or developing a new product in PureBasic? Tell the world about it.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

Can someone show me a working example of using the

Code: Select all

POP3_GetMsg(messageNumber, *callBack)

command from this library.

All I am getting is an Invalid Page Fault.

Seems to be happening when the command is called. It never accesses
the *callBack. I probably have that handled wrong, but since the
calbacks for the other commands are working for me I don't think so.

TIA
Terry

[Edited to update]I still am unable to get this library command to do anything but fail. Sure would appreciate some advice.
Last edited by TerryHough on Wed Sep 29, 2004 4:40 pm, edited 1 time in total.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Brendan, I'm still having problems retreiving the the msg content.
Here's what I got so far:

Code: Select all

Global pop_server.s, username.s, password.s, *buffer, inTransaction
inTransaction = 0

pop_server.s = "pop.SECRET"
username.s = "SECRET"
password.s = "SECRET"


Procedure messageCallBack(Messages, param1, param2)
  Select Messages
    Case #POP3MSG_DOWNLOADCOMPLETE
      ; Now you can send the message text to your window/text buffer/whatever here...
      ; Tmp.s = Chr(10) + Chr(10) + PeekS(*buffer)
      inTransaction = 0
      
    Case #POP3MSG_ERROR
      ; check the last error and presume that the connection is lost
      inTransaction = 0
    Case #POP3MSG_DOWNLOADPROGRESS
      ; you could do something with the percentage (param1) like update a progress bar or whatever...
  EndSelect
EndProcedure


If POP3_Init()
  POP3lib_Version.s = POP3_GetLibraryVersionInfo()
  ; MessageRequester("About", "You are using POP3lib version " + POP3lib_Version)
  
  If POP3_Connect(pop_server.s, 110)
    
    If POP3_Logon(username.s, password.s)
      
      Messages = POP3_GetMsgCount()
      msgSize = POP3_GetMsgSize( Messages ) + 1000
      *buffer = AllocateMemory(msgSize) ; Allocate memory same size as message pluss 1 K extra.
      If Messages > 1 Or Messages = 0
        Msg.s = "There are " + Str(Messages) + " messages waiting for your."
      Else
        Msg.s = "There is " + Str(Messages) + " message waiting for your."
      EndIf
      Msg.s + Chr(10) + "Filesize of the newest message is: " + Str(msgSize / 1024) + " kb. ("+Str(msgSize)+" bytes)."
      
      If POP3_ParseMsgHeader( Messages )
        Message_from.s = POP3_HeaderFrom()
        Message_date.s = POP3_HeaderDate()
        Message_subject.s = POP3_HeaderSubject()
        Msg.s + Chr(10) + "Message was send at " + Message_date + " by " + Message_from
        Msg.s + Chr(10) + "Message subject: " + Message_subject
        
        ;- Getting the message text
        If inTransaction = 0
          If POP3_BufferMsgBody( Messages, *buffer, @messageCallBack() )
            inTransaction = 1
          EndIf
        EndIf
        
      Else
        Msg.s + Chr(10) + "Error parsing message headers."
      EndIf
      
      Msg.s + Chr(10) + Chr(10) + "Message content: " + PeekS(*buffer)
      
      MessageRequester("Result", Msg.s)
      POP3_Logoff() ; Logoff the conection.
      
    Else
      
    EndIf
    
    POP3_Disconnect() ; Disconnect from the POP server.
    
  Else
    Debug "Error connection to the server."
  EndIf
  
Else
  MessageRequester("Error occured", "An error occured while initializing the POP3 lib." + Chr(10) + "Please contact the developer with" + Chr(10) + "the error code below:", #MB_OK|#MB_ICONERROR)
EndIf

End

;EOF
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@GeoTrail

I haven't been successful with portions of this library and BrendanE hasn't
visited for awhile or replied. (See my earlier post).

Too bad, because it could be a very useful library. Wish I had the source
to work with and maybe I could get it going.

I modified your code a bit trying to make it a bit more understandable and
handle multiple messages properly. Maybe you can pick up a clue here
and get further yourself.

Code: Select all

Code remved in last edit.  See new code in a later post.
Good luck,
Terry
Last edited by TerryHough on Wed Sep 29, 2004 4:40 pm, edited 2 times in total.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Nice work Terry. Thanks for the post.
I'll study it closer after dinner :)

Cheers *S*

Would be great if someone could take over the development of this lib.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@GeoTrail

Thanks, hope it will help you.

I edited the code above to make the error translation global since the
original post.

Yes, it would be nice if the library could be further developed. I would
probably volunterr but AFAIK the source wasn't made available by
BrendanE plus I haven't a clue how to create a library. I know a bit
about POP3 though and have written some of these functions outside the library.

Terry
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

BrendanE comes online sometimes. So we should make a call to make him develop further :P

BrendanE, please listen!

8)
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Thanks Terry.
When I run the code now I get an error code 0.
The previous code before you edited worked. Atleast with the tenline routine.

thefool, maybe we could send him an email og PM and maybe he'll hear our cry :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

TerryHough wrote:@GeoTrail

Thanks, hope it will help you.

I edited the code above to make the error translation global since the
original post.

Yes, it would be nice if the library could be further developed. I would
probably volunterr but AFAIK the source wasn't made available by
BrendanE plus I haven't a clue how to create a library. I know a bit
about POP3 though and have written some of these functions outside the library.

Terry
Haven't been able to make a lib myself, I have made lots of nice snippies that could be usefull as libs for lots of people. I think anyways.

Perhaps someone could give me, or us a crash course in creating libs???
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

use tailbite.

I think its just as writing dlls.

Every procedure that should be available for external use (the library functions..) is proceduredll NAME(blah.s)

and internal procedures is just procedure NAME(evenmoreblah.s)

But im not 100% sure on how to make the description file ..
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Yeah I've seen people using Tailbite.
But I have no idea how or where to start.
That's why a quick tutorial would be nice.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

You may as well write your procedures and save them into an include file ("anything.pb") This way, you'll know always that your code is written as it should.

Regards,
El_Choni
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

El_Choni wrote:You may as well write your procedures and save them into an include file ("anything.pb") This way, you'll know always that your code is written as it should.

Regards,
Yep.

.. Unless you're sharing sans source ..

;^)
@}--`--,-- A rose by any other name ..
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@GeoTrail

I have played with this a bit more. See the updated code following.

I still have problems with the POP3_GetMsgBody(I, @myCallBack)
command giving Invalid Page Faults

In this version the buffered
POP3_BufferMsgBody(I, *buffer, @messageCallBack())
is working. But the #POP3MSG_DOWNLOADPROGRESS message seems
to kill getting the message properly for me.

Code: Select all


Global Err$
Global Msg.s
Global password.s
Global pop_server.s
Global Temp$
Global Tmp.s
Global username.s

Global *buffer
Global Error
Global inTransaction.b : inTransaction = 0
Global LogonStatus.b : LogonStatus = #FALSE
Global MsgLen.l
Global msgSize.l
Global I.w : I = 0

*buffer = AllocateMemory(10000)

; Replace the following three items with your POP3 server information.
pop_server.s = "pop.secret.net" ; Server's address
username.s   = "secret"         ; User's ID  
password.s   = "secret"         ; User's pasword

Procedure FixUpMessage()
  ; --- fix up message ----
  ; I only want to see the text portion of the message, no HTML, etc.
  Temp$ = PeekS(*buffer)
  Pos = FindString(Temp$,"quoted-printable",1)
  ; --- strip message type description ---
  Temp$ = Trim(Mid(Temp$,Pos+20,Len(Temp$)))
  ; --- trim message text trailer from the first ?? lines ---
  Pos = FindString(Temp$,"------=_",1)
  If pos
    Temp$ = Mid(Temp$,1,Pos-1)
  EndIf
  ; --- end of message fix up ---
EndProcedure

Procedure TranslateError()
  Error = POP3_GetLastError()
  Select Error
  Case #POP3_ERR_UNINITIALISED; The library is uninitialised - call POP3_Init()
  Case #POP3_ERR_MEMORY
    Err$ = "Unable To allocate sufficient memory For task."
  Case #POP3_ERR_NETWORK
    Err$ = "Unable to initialise the network."
  Case #POP3_ERR_CONNECTION
    Err$ = "Unable to connect to POP3 server."
  Case #POP3_ERR_LOGINFAILED
    Err$ = "Login failed."
  Case #POP3_ERR_NOSUCHMSG
    Err$ = "Message specified does not exist."
  Case #POP3_ERR_ALREADYCONNECTED
    Err$ = "Trying to connect to server while already connected."
  Case #POP3_ERR_TIMEDOUT
    Err$ = "No response received in the specified time."
  Case #POP3_ERR_FILEERROR
    Err$ = "Unable to open/write to file."
  Case #POP3_ERR_THREAD
    Err$ = "Failed to create new thread."
  Case #POP3_ERR_BUSY
    Err$ = "The library is busy handling a prior request."
  Case #POP3_ERR_LOGOFF
    Err$ = "Logoff command failed."
  Default
    Err$ = "Unrecognized error condition"  
  EndSelect
  Msg.s + Chr(10) + Err$
  SetGadgetText(4,Msg.s)
  ; The following MessageRequester isn't really needed because the
  ; program's display is showing these errors too, but that display
  ; may disappear before the user can read it.
  MessageRequester("POP3","An error has occurred." + Chr(10) + Chr(10) + "Error code: " + Str(Error) + Chr(10) + Err$,#MB_ICONERROR)
  ActivateGadget(4)  ; Create a window event to resume loop
EndProcedure

Procedure myCallback(messageLine$)
  Msg.s + messageLine$ + Chr(10)
EndProcedure

Procedure messageCallBack(Message, param1, param2)
  Select Message
  Case #POP3MSG_DOWNLOADCOMPLETE
    ; Message download is complete, send the message text to your window/text
    inTransaction = 0
    MsgLen = param1
    
  Case #POP3MSG_ERROR
    ; check the last error and presume that the connection is lost
    TranslateError()
    inTransaction = 0
    
  Case #POP3MSG_DOWNLOADPROGRESS
    ; Show progress percentage (param1) in a progress bar gadget
    ; The 100% DownloadProgress doesn't seem to ever be issued by library
    ; When active, I don't get the entire message as requested ????
    ; SetGadgetState(2, param1)
    
  EndSelect
EndProcedure

Procedure GetMsgText()
  If POP3_GetMsgBody(I, @myCallBack)
    ; Normal end
  Else
    MessageRequester("POP3","Your POP3_GetMsgBody request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR)
  EndIf
EndProcedure

Procedure GetMsgBody()
  If POP3_BufferMsgBody(I, *buffer, @messageCallBack())
    ; Normal call
  Else
    MessageRequester("POP3","Your POP3_BufferMsgBody request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR)
  EndIf
  ;  While inTransaction = 1
  ;  Wend
  Repeat
    Delay(1000)
  Until inTransacton = 0
  ;  PercentComplete.f = (MsgLen / msgSize) * 100
  ;  SetGadgetState(2, PercentComplete)
  SetGadgetState(2, 100) ; Show complete on progress bar
  FixUpMessage()
  Msg.s + Temp$
  ;  MessageRequester("Debug","MsgLen is: " + Str(MsgLen),0)
EndProcedure

Procedure GetMsgLines()
  ; Get the 1st 10 lines of a message
  MaxLinesToGet.l = 17  ; Allow for header and 10 lines to be received
  If POP3_BufferMsgBodyLines(I,*buffer, MaxLinesToGet)
    ; Successfully downloaded the 10 lines of the message
    SetGadgetState(2, 100) ; Show complete on progress bar
    FixUpMessage()
    Msg.s + Temp$
  Else
    MessageRequester("POP3","Your POP3_BufferMsgHeader request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR)
  EndIf
EndProcedure

; ----------------------------- Main code --------------------------
If OpenWindow(0, 0, 0, 300, 300,#PB_Window_SystemMenu, "POP3")
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
  
  If CreateGadgetList(WindowID())
    ProgressBarGadget(2, 10, 285, 280, 10, 0, 100, #PB_ProgressBar_Smooth)
    ButtonGadget(3,10,260,280,20,"Next")
    EditorGadget(4,10,10,280,250)
    
    ; Initialize the POP3 LIbrary
    Msg.s = "Attempting to initiate the POP3_Library functions..."
    SetGadgetText(4,Msg.s)
    If POP3_Init()
      Msg.s + Chr(10) + "POP3_Library successfully initiated..."
      POP3lib_Version.s = POP3_GetLibraryVersionInfo()
      Msg.s + Chr(10) + "You are using POP3lib version " + POP3lib_Version
      Msg.s + Chr(10) + "Attempting to connect to " + pop_server.s
      SetGadgetText(4,Msg.s)
      
      ; Try to connect to the server
      If POP3_Connect(pop_server.s, 110)
        Msg.s + Chr(10) + "Connected successfully to " + pop_server.s
        Msg.s + Chr(10) + "Attempting to log on to server..."
        SetGadgetText(4,Msg.s)
        ; Logon to the server
        If POP3_Logon(username.s, password.s)
          LogonStatus = #TRUE
          Msg.s + Chr(10) + "Logged on to server successfully..." + Chr(10)
          SetGadgetText(4,Msg.s)
          ; Check for available messages
          Messages = POP3_GetMsgCount()
          If Messages = 1
            Msg.s + Chr(10) + "There is " + Str(Messages) + " message waiting for you." + Chr(10)
          Else
            Msg.s + Chr(10) + "There are " + Str(Messages) + " messages waiting for you." + Chr(10)
          EndIf
          SetGadgetText(4,Msg.s)
        Else  ; Couldn't logon using POP3_Logon
          TranslateError()
          Msg.s + Chr(10) + Err$
          SetGadgetText(4,Msg.s)
          Quit = #TRUE
        EndIf
        
      Else  ; Couldn't connect using POP3_Connect
        TranslateError()
        Msg.s + Chr(10) + Err$
        SetGadgetText(4,Msg.s)
        Quit = #TRUE
      EndIf
      
    Else  ; Couldn't initiate the POP3_Library
      TranslateError()
      Msg.s + Chr(10) + Err$
      SetGadgetText(4,Msg.s)
      Quit = #TRUE
    EndIf
    
    ; Processing loop
    Repeat
      Select WaitWindowEvent()
      Case #PB_Event_CloseWindow    ; Window close box clicked
        Quit = #TRUE
        
      Case #PB_Event_Menu
        Select EventMenuID()        ; See which menu has been selected
          
        Case #PB_Shortcut_Escape    ; ESC key pressed
          Quit = #TRUE
        EndSelect   ; End #PB_Event_Menu selection
        
      Case #PB_Event_Gadget
        Select EventGadgetID()
          
        Case 3 ; Next button pushed
          SetGadgetState(2,0)
          SetGadgetText(4,"")
          If Messages
            ; Loop through the messages
            I + 1
            If I > Messages
              ResizeGadget(4,10,10,280,40)
              SetGadgetText(4,"There are no more messages")
            Else
              MsgLeft.l = Messages - I
              If MsgLeft = 1
                Msg.s = "There is " + Str(MsgLeft) + " remaining message waiting for you." + Chr(10)
              Else
                Msg.s = "There are " + Str(MsgLeft) + " remaining messages waiting for you." + Chr(10)
              EndIf
              SetGadgetState(2, 0)
              msgSize = POP3_GetMsgSize(I)
              ReAllocateMemory(*buffer, msgSize) ; Allocate memory same size as message pluss 1 K extra.
              Msg.s + Chr(10) + "Filesize of the message " + Str(I)+ " is: " + Str(msgSize)+" bytes." + Chr(10)
              
              If POP3_ParseMsgHeader(I)
                Message_from.s    = POP3_HeaderFrom()
                Message_date.s    = POP3_HeaderDate()
                Message_subject.s = POP3_HeaderSubject()
                Msg.s + Chr(10) + "Message was sent at " + Message_date + " by " + Message_from
                Msg.s + Chr(10) + "Message subject: " + Message_subject + Chr(10)
                Msg.s + Chr(10) + "Message content: " + Chr(10)
                
                ; ------------------------------------------------------
                ; Getting the message text - choose one of three methods
                
                ; (1) Get the messge text using POP3_GetMsgBody - Failure
                ;     Causes an Invalid Page Fault
                ;GetMsgText()
                
                ; (2) Get the message using POP3_BufferMsgBody - Successful
                GetMsgBody()
                
                ; (3) Get the first ten lines of the message - Successful
                ;     Limited to the requested number of lines per message
                ;GetMsgLines()
                
              Else
                Msg.s + Chr(10) + "Error parsing message headers."
                
              EndIf
              SetGadgetText(4,Msg.s)
            EndIf
          Else
            ; No messages available
          EndIf
        EndSelect   ; End #PB_Event_Gadget selection
      EndSelect   ; End WaitWindowEvent selection
    Until Quit    ; End processing loop
  
    ; If we are logged in, we should logoff before disconnecting
    If LogonStatus  
      ; Logoff the server
      If POP3_Logoff()
        ; Success
      Else
        TranslateError()
        Msg.s + Chr(10) + Err$
        SetGadgetText(4,Msg.s)
      EndIf
    EndIf   
    
    ; Disconnect from the POP server.
    POP3_Disconnect()  ; No errors are generated by this function
  EndIf   ; End CreateGadgetList
EndIf   ; End Open Window
End
Regards,
Terry
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Wow, very impressive Terry.
Thank you so much for spending so much time on this :)

I'll test it properly as soon as I can :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

I've fixed progressbar bug.

Now it works :P :P :P

Code: Select all

; Replace the following three items with your POP3 server information. 
pop_server.s = "pop.secret.net" ; Server's address 
username.s   = "secret"         ; User's ID  
password.s   = "secret"         ; User's pasword

Global Err$ 
Global Msg.s 
Global password.s 
Global pop_server.s 
Global Temp$ 
Global Tmp.s 
Global username.s 

Global *buffer 
Global Error 
Global inTransaction.b : inTransaction = 0 
Global LogonStatus.b : LogonStatus = #False 
Global MsgLen.l 
Global msgSize.l 
Global I.w : I = 0 

*buffer = AllocateMemory(10000) 

; ----------------------------- Progress Bar --------------------------
; This code fix the progressbar problem
; The progressbar updater is in separated thread 

Global pBar
Global pBarUpdater

Procedure pBar()
  Repeat
    Delay(100)    
    SetGadgetState(2, pBar) 
  ForEver 
EndProcedure

Procedure pBarUpdaterStart()
  pBar=0
  pBarUpdater=CreateThread(@pBar(),0)
EndProcedure

Procedure pBarUpdaterEnd()  
  KillThread(pBarUpdater)
  SetGadgetState(2, pBar) 
EndProcedure

; ---------------------------- POP 3 Functions --------------------------

Procedure FixUpMessage() 
  ; --- fix up message ---- 
  ; I only want to see the text portion of the message, no HTML, etc. 
  Temp$ = PeekS(*buffer) 
  Pos = FindString(Temp$,"quoted-printable",1) 
  ; --- strip message type description --- 
  Temp$ = Trim(Mid(Temp$,Pos+20,Len(Temp$))) 
  ; --- trim message text trailer from the first ?? lines --- 
  Pos = FindString(Temp$,"------=_",1) 
  If Pos 
    Temp$ = Mid(Temp$,1,Pos-1) 
  EndIf 
  ; --- end of message fix up --- 
EndProcedure 

Procedure TranslateError() 
  Error = POP3_GetLastError() 
  Select Error 
    Case #POP3_ERR_UNINITIALISED; The library is uninitialised - call POP3_Init() 
    Case #POP3_ERR_MEMORY 
      Err$ = "Unable To allocate sufficient memory For task." 
    Case #POP3_ERR_NETWORK 
      Err$ = "Unable to initialise the network." 
    Case #POP3_ERR_CONNECTION 
      Err$ = "Unable to connect to POP3 server." 
    Case #POP3_ERR_LOGINFAILED 
      Err$ = "Login failed." 
    Case #POP3_ERR_NOSUCHMSG 
      Err$ = "Message specified does not exist." 
    Case #POP3_ERR_ALREADYCONNECTED 
      Err$ = "Trying to connect to server while already connected." 
    Case #POP3_ERR_TIMEDOUT 
      Err$ = "No response received in the specified time." 
    Case #POP3_ERR_FILEERROR 
      Err$ = "Unable to open/write to file." 
    Case #POP3_ERR_THREAD 
      Err$ = "Failed to create new thread." 
    Case #POP3_ERR_BUSY 
      Err$ = "The library is busy handling a prior request." 
    Case #POP3_ERR_LOGOFF 
      Err$ = "Logoff command failed." 
    Default 
      Err$ = "Unrecognized error condition"  
  EndSelect 
  Msg.s + Chr(10) + Err$ 
  SetGadgetText(4,Msg.s) 
  ; The following MessageRequester isn't really needed because the 
  ; program's display is showing these errors too, but that display 
  ; may disappear before the user can read it. 
  MessageRequester("POP3","An error has occurred." + Chr(10) + Chr(10) + "Error code: " + Str(Error) + Chr(10) + Err$,#MB_ICONERROR) 
  ActivateGadget(4)  ; Create a window event to resume loop 
EndProcedure 

Procedure myCallback(messageLine$) 
  Msg.s + messageLine$ + Chr(10) 
EndProcedure 

Procedure messageCallBack(Message, param1, param2) 
  Select Message 
    Case #POP3MSG_DOWNLOADCOMPLETE 
      ; Message download is complete, send the message text to your window/text  
      pBar=100      
      inTransaction = 0 
      MsgLen = param1  
      
    Case #POP3MSG_ERROR 
      ; check the last error and presume that the connection is lost 
      TranslateError() 
      inTransaction = 0 
      
    Case #POP3MSG_DOWNLOADPROGRESS 
      ; Show progress percentage (param1) in a progress bar gadget 
      pBar=param1
  EndSelect 
EndProcedure 

Procedure GetMsgText() 
  If POP3_GetMsgBody(I, @myCallBack) 
    ; Normal end 
  Else 
    MessageRequester("POP3","Your POP3_GetMsgBody request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR) 
  EndIf 
EndProcedure 

Procedure GetMsgBody() 
  pBarUpdaterStart()
  If POP3_BufferMsgBody(I, *buffer, @messageCallBack()) 
    ; Normal call  
  Else 
    MessageRequester("POP3","Your POP3_BufferMsgBody request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR) 
  EndIf 
  ;  While inTransaction = 1 
  ;  Wend 
  Repeat 
    Delay(1000) 
  Until inTransacton = 0 
  ;  PercentComplete.f = (MsgLen / msgSize) * 100 
  ;  SetGadgetState(2, PercentComplete) 
  pBarUpdaterEnd() 
     
  FixUpMessage() 
  Msg.s + Temp$ 
  ;  MessageRequester("Debug","MsgLen is: " + Str(MsgLen),0) 
EndProcedure 

Procedure GetMsgLines() 
  ; Get the 1st 10 lines of a message 
  MaxLinesToGet.l = 17  ; Allow for header and 10 lines to be received 
  If POP3_BufferMsgBodyLines(I,*buffer, MaxLinesToGet) 
    ; Successfully downloaded the 10 lines of the message 
    SetGadgetState(2, 100) ; Show complete on progress bar 
    FixUpMessage() 
    Msg.s + Temp$ 
  Else 
    MessageRequester("POP3","Your POP3_BufferMsgHeader request failed:" + Chr(10) + "Error code: " + Str(POP3_GetLastError()) + Chr(9) + POP3_GetLastErrorString(),#MB_ICONERROR) 
  EndIf 
EndProcedure 

; ----------------------------- Main code -------------------------- 
If OpenWindow(0, 0, 0, 300, 300,#PB_Window_SystemMenu, "POP3") 
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, #PB_Shortcut_Escape) 
  
  If CreateGadgetList(WindowID()) 
    ProgressBarGadget(2, 10, 285, 280, 10, 0, 100, #PB_ProgressBar_Smooth) 
    ButtonGadget(3,10,260,280,20,"Next") 
    EditorGadget(4,10,10,280,250) 
    
    ; Initialize the POP3 LIbrary 
    Msg.s = "Attempting to initiate the POP3_Library functions..." 
    SetGadgetText(4,Msg.s) 
    If POP3_Init() 
      Msg.s + Chr(10) + "POP3_Library successfully initiated..." 
      POP3lib_Version.s = POP3_GetLibraryVersionInfo() 
      Msg.s + Chr(10) + "You are using POP3lib version " + POP3lib_Version 
      Msg.s + Chr(10) + "Attempting to connect to " + pop_server.s 
      SetGadgetText(4,Msg.s) 
      
      ; Try to connect to the server 
      If POP3_Connect(pop_server.s, 110) 
        Msg.s + Chr(10) + "Connected successfully to " + pop_server.s 
        Msg.s + Chr(10) + "Attempting to log on to server..." 
        SetGadgetText(4,Msg.s) 
        ; Logon to the server 
        If POP3_Logon(username.s, password.s) 
          LogonStatus = #True 
          Msg.s + Chr(10) + "Logged on to server successfully..." + Chr(10) 
          SetGadgetText(4,Msg.s) 
          ; Check for available messages 
          Messages = POP3_GetMsgCount() 
          If Messages = 1 
            Msg.s + Chr(10) + "There is " + Str(Messages) + " message waiting for you." + Chr(10) 
          Else 
            Msg.s + Chr(10) + "There are " + Str(Messages) + " messages waiting for you." + Chr(10) 
          EndIf 
          SetGadgetText(4,Msg.s) 
        Else  ; Couldn't logon using POP3_Logon 
          TranslateError() 
          Msg.s + Chr(10) + Err$ 
          SetGadgetText(4,Msg.s) 
          Quit = #True 
        EndIf 
        
      Else  ; Couldn't connect using POP3_Connect 
        TranslateError() 
        Msg.s + Chr(10) + Err$ 
        SetGadgetText(4,Msg.s) 
        Quit = #True 
      EndIf 
      
    Else  ; Couldn't initiate the POP3_Library 
      TranslateError() 
      Msg.s + Chr(10) + Err$ 
      SetGadgetText(4,Msg.s) 
      Quit = #True 
    EndIf 
    
    ; Processing loop 
    Repeat 
      Select WaitWindowEvent() 
        Case #PB_Event_CloseWindow    ; Window close box clicked 
          Quit = #True 
          
        Case #PB_Event_Menu 
          Select EventMenuID()        ; See which menu has been selected 
            
            Case #PB_Shortcut_Escape    ; ESC key pressed 
              Quit = #True 
          EndSelect   ; End #PB_Event_Menu selection 
          
        Case #PB_Event_Gadget 
          Select EventGadgetID() 
            
            Case 3 ; Next button pushed 
              SetGadgetState(2,0) ;-pBAR = 0
              SetGadgetText(4,"") 
              If Messages 
                ; Loop through the messages 
                I + 1 
                If I > Messages 
                  ResizeGadget(4,10,10,280,40) 
                  SetGadgetText(4,"There are no more messages") 
                Else 
                  MsgLeft.l = Messages - I 
                  If MsgLeft = 1 
                    Msg.s = "There is " + Str(MsgLeft) + " remaining message waiting for you." + Chr(10) 
                  Else 
                    Msg.s = "There are " + Str(MsgLeft) + " remaining messages waiting for you." + Chr(10) 
                  EndIf 
                  SetGadgetState(2, 0) ;-PBar=0
                  msgSize = POP3_GetMsgSize(I) 
                  ReAllocateMemory(*buffer, msgSize) ; Allocate memory same size as message pluss 1 K extra. 
                  Msg.s + Chr(10) + "Filesize of the message " + Str(I)+ " is: " + Str(msgSize)+" bytes." + Chr(10) 
                  
                  If POP3_ParseMsgHeader(I) 
                    Message_from.s    = POP3_HeaderFrom() 
                    Message_date.s    = POP3_HeaderDate() 
                    Message_subject.s = POP3_HeaderSubject() 
                    Msg.s + Chr(10) + "Message was sent at " + Message_date + " by " + Message_from 
                    Msg.s + Chr(10) + "Message subject: " + Message_subject + Chr(10) 
                    Msg.s + Chr(10) + "Message content: " + Chr(10) 
                    
                    ; ------------------------------------------------------ 
                    ; Getting the message text - choose one of three methods 
                    
                    ; (1) Get the messge text using POP3_GetMsgBody - Failure 
                    ;     Causes an Invalid Page Fault 
                    ;GetMsgText() 
                    
                    ; (2) Get the message using POP3_BufferMsgBody - Successful 
                    GetMsgBody() 
                    
                    ; (3) Get the first ten lines of the message - Successful 
                    ;     Limited to the requested number of lines per message 
                    ;GetMsgLines() 
                    
                  Else 
                    Msg.s + Chr(10) + "Error parsing message headers." 
                    
                  EndIf 
                  SetGadgetText(4,Msg.s) 
                EndIf 
              Else 
                ; No messages available 
              EndIf 
          EndSelect   ; End #PB_Event_Gadget selection 
      EndSelect   ; End WaitWindowEvent selection 
    Until Quit    ; End processing loop 
    
    ; If we are logged in, we should logoff before disconnecting 
    If LogonStatus  
      ; Logoff the server 
      If POP3_Logoff() 
        ; Success 
      Else 
        TranslateError() 
        Msg.s + Chr(10) + Err$ 
        SetGadgetText(4,Msg.s) 
      EndIf 
    EndIf    
    
    ; Disconnect from the POP server. 
    POP3_Disconnect()  ; No errors are generated by this function 
  EndIf   ; End CreateGadgetList 
EndIf   ; End Open Window 
End 

Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply