TinyMail Multiplatform (Mac OS X, Windows)

Developed or developing a new product in PureBasic? Tell the world about it.
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

TinyMail Multiplatform (Mac OS X, Windows)

Post by jamirokwai »

Dear Board,

I'd like to present a small tool, which you may download from http://www.quadworks.de/?artikel=2009-0 ... 1-00&ln=en
or, if you prefer, via a direct link: http://www.quadworks.de/download.php?ar ... nyMail.zip

Edit: you may download it again, as I succeeded in hiding the Dock-Icon on Mac OS X, and making it an Universal Binary (thanks to lexvictory) - please tell me if it works on Mac OS X PPC!

It's a small tool using my own small pop3-routines to ask whether there are mails inside one pop3-account. It's not perfect, but everything you need is inside the archive. And here is the full code (which also is included in the archive)...

I forgot to add: it compiles great with PB 4.40b2 on Mac OS X x86, Mac OS X ppc, and Windows x86. Couldn't test x64, nor Linux...

Code: Select all

Global ServerName$      = ""
Global Port             = 0
Global User$            = ""
Global Password$        = ""
Global Nachrichten      = 0
Global Intervall        = 120
Global *POP3Buffer      = AllocateMemory(2048)
Global POP3_LastAnswer$ = "" ; the last answer of the pop3-server

; - Errorcodes
#POP_OK              =  0
#POP_Refused         =  1
#POP_NetWorkError    =  4
#POP_NoConnection    =  5
#POP_No_Answer       =  7

Procedure.l POP3_Get_From_Server(Connection, Query$) ; OK - geprueft
 POP3_Last_Error = 0
 POP3_LastAnswer$ = "" 
 If Connection = 0
  ProcedureReturn #POP_NoConnection
 EndIf ; in no connection, exit
 *POP3Buffer = ReAllocateMemory(*POP3Buffer, 1024)
 SendNetworkString(Connection, Query$ + Chr(13) + Chr(10)) ; send string with added Carriage Return and Line Feed
 Result            = ReceiveNetworkData(Connection, *POP3Buffer, 2048)  ; result = Bytes read
 If result = -1    : POP3_Last_Error = #POP_NetWorkError : ProcedureReturn POP3_Last_Error : EndIf ; result = -1? error, exit 
 BackResult        = Result ; maximum Buffer capacity of connection
 Speicher          = Result ; current size of buffer content 
 While Speicher < MailLaenge ; do, While MailLaenge Not reached
  Result   = ReceiveNetworkData(Connection, *POP3Buffer + Speicher, 2048) ; result = bytes read from connection
  Speicher = Speicher + Result ; increase size of buffer content AND position inside buffer 
 Wend
 POP3_LastAnswer$  = PeekS(*POP3Buffer, Speicher)
 If FindString(POP3_LastAnswer$, "-ERR", 0) <> 0 : POP3_Last_Error = #POP_NetWorkError : Else : POP3_Last_Error = #POP_OK : EndIf
 POP3_Downloading  = 0 
 ProcedureReturn POP3_Last_Error
EndProcedure

Procedure.l POP3_Open_Server(Server$, PortNumber) ; OK - geprueft
 POP3_Last_Error = 0
 POP3_LastAnswer$ = ""
 Connection = OpenNetworkConnection(Server$, PortNumber, #PB_Network_TCP)
 If Connection = 0
  POP3_Last_Error = #POP_NoConnection
 Else
  Result = ReceiveNetworkData(Connection, *POP3Buffer, 2048)
  If Result = 0
   POP3_Last_Error = #POP_No_Answer
  Else 
   POP3_LastAnswer$ = PeekS(*POP3Buffer, Result)
   If FindString(POP3_LastAnswer$, "+OK", 0) <> 0
    POP3_Last_Error = #POP_OK
    ProcedureReturn Connection
   Else
    POP3_Last_Error  = #POP_Refused
   EndIf
  EndIf
 EndIf
 ProcedureReturn 0
EndProcedure

Procedure IntervallCheck(*Value)
 Repeat
  Connection = POP3_Open_Server(ServerName$, Port) 
  POP3_Get_From_Server(Connection, "USER " + User$)
  POP3_Get_From_Server(Connection, "PASS " + Password$)
  If POP3_Get_From_Server(Connection, "STAT") = #POP_OK
   tmp$        = Trim(Mid(POP3_LastAnswer$, FindString(POP3_LastAnswer$, " ", 0)))
   Nachrichten = Val(Left(tmp$, FindString(tmp$, " ", 0)))
  EndIf
  POP3_Get_From_Server(Connection, "QUIT")
  Delay(*Value * 1000)
 Until *value = -1
EndProcedure

If InitNetwork() = 0
 MessageRequester("Attention!", "TinyMail could not activate a network-connection.")
 End
EndIf

ReadFile(0,GetPathPart(ProgramFilename()) + "TinyMail.ini")
ServerName$ = ReadString(0,#PB_Ascii)
Port        = Val(ReadString(0,#PB_Ascii))
User$       = ReadString(0,#PB_Ascii)
Password$   = ReadString(0,#PB_Ascii)
Intervall   = Val(ReadString(0,#PB_Ascii))
CloseFile(0)

ExamineDesktops()
OpenWindow(0,DesktopWidth(0) - 40, DesktopHeight(0) - 20, 40, 20, "",#PB_Window_BorderLess)
StickyWindow(0,1)
ButtonGadget(0,-2,-2,44,24,"",#PB_Text_Center)
Checker = CreateThread(@IntervallCheck(), Intervall)

Repeat
 SetGadgetText(0, Str(Nachrichten))
 Select WaitWindowEvent(250)
  Case #PB_Event_Gadget
    Select EventGadget()
     Case 0 : Exit = 1
    EndSelect
 EndSelect
 Delay(250)
Until Exit = 1

PauseThread(Checker)
KillThread(Checker)
FreeMemory(*POP3Buffer)
FreeGadget(0)
CloseWindow(0)
Regards,
JamiroKwai