PowerBASIC Peer Support Forums
 

Go Back   PowerBASIC Peer Support Forums > User to user Discussions > Source Code

Notices

Source Code PowerBASIC and related source code. Please do not post questions or discussions, just source code.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old Apr 24th, 2009, 12:26 PM
Mel Bishop Mel Bishop is offline
Member
 
Join Date: May 1999
Location: Portales, New Mexico
Posts: 2,932
E-Mail Checker

Code:
' This program checks your e-mail every x minutes (user definable)
' and if there's something there, offers to bring up your default
' mail program.
'
' Couple of routines I pirated from PB's board:
'
' From: Mike Doty                                               '
' At: http://www.powerbasic.com/support/pbforums/showthread.php?t=40406
'
' From Kev Peel                                                 '
' http://www.powerbasic.com/support/pbforums/showthread.php?t=16318
'
' Much obligated.
'
' All you have to do is create a plain text "checkmail.ini" file
' with the following format:
'
' Line #1, Your e-mail host
' Line #2, Your e-mail user name
' Line #3, Your e-mail password
' Line #4, Default notify with an audible alert (ON/OFF) that
'          something is there. This can be toggled on/off with
'          the F-2 function key.
' Line #5, Time inteval (in minutes) to wait between check loops
'
' This format is pretty ridgid since there's no error/format
' checking, yet.
'
' Works for me here. Hope it works for you there.
'
#COMPILE EXE                                                    '
#BREAK ON                                                       '
'#CONSOLE OFF                                                   ' No place to end it except in
#INCLUDE "win32api.inc"                                         ' Task Manager
GLOBAL gsResults AS STRING                                      '
%BUFFER_LEN = 1024                                              '
                                                                '
                                                                '
FUNCTION PBMAIN () AS LONG                                      '
  LOCAL sPop3Host, sUser, sPassword AS STRING                   '
  LOCAL hTCP, Result, NumberOfMessages, MsgSize AS LONG         '
  GLOBAL Euro AS STRING                                         '
  GLOBAL tLoop AS SINGLE                                        '
                                                                '
  LOCAL sResults AS STRING  'optionally display results         '
                                                                '
    fi$ = "checkmail.ini"                                       '
    te$ = DIR$(fi$)                                             '
    IF te$ = "" THEN BEEP : EXIT FUNCTION                       '
                                                                '
    OPEN fi$ FOR INPUT AS #1                                    '
    LINE INPUT #1,te$                                           ' e-Mail host
    sPop3Host = REMOVE$(te$," ")                                '
                                                                '
    LINE INPUT #1,te$                                           ' Your user name
    sUser = REMOVE$(te$," ")                                    '
                                                                '
    LINE INPUT #1,te$                                           ' Password
    sPassWord = REMOVE$(te$," ")                                '
                                                                '
    LINE INPUT #1,te$                                           '
    Euro = REMOVE$(te$," ")                                     ' EuroSiren notification
    Euro = UCASE$(euro)                                         ' How many times to cycle
                                                                ' thru the hi/lo tones.
    LINE INPUT #1,te$                                           '
    tLoop = VAL(te$) * 60                                       ' Convert minutes to seconds
                                                                '
    CLOSE #1                                                    ' On or Off. Toggle with F-2
                                                                ' function key.
                                                                '
    CLS                                                         ' Briefly display your
    PRINT;"          ISP: ";sPop3Host                           ' parameters.
    PRINT;"      User ID: ";sUser                               '
    PRINT;"     Password: ";sPassWord                           '
    PRINT;"         Euro: ";Euro                                '
    PRINT;"Loop Interval: ";te$;" minutes."                     '
    delay 3                                                     '
    CLS                                                         '
                                                                '
    DO                                                          '<-: Master loop.
                                                                '  |
  hTCP = Pop3Connect ( sPop3Host, sUser, sPassword)             '  |
  IF hTCP > -1 THEN                                             '  |
    REM Pop account reached                                     '  |
    Result = Pop3GetStat (hTCP&, NumberOfMessages&, MsgSize&)   '  |
                                                                '  |
    IF Result = 0 THEN                                          '  |
      LogIt  "Messages:" + STR$(NumberOfMessages&)              '  |
      LogIt  "Length:"   + STR$(MsgSize&)                       '  |
    ELSE                                                        '  |
      LogIt "Error getting POP3 STAT"                           '  |
    END IF                                                      '  |
                                                                '  |
    Result = Pop3Quit (hTCP)                                    '  |
    IF result = -1 THEN                                         '<:|
                                                                ' ||
      LogIt "Error occurred closing server"                     ' ||
    ELSE                                                        '<:|
      LogIt "Bye, no problems"                                  ' ||
    END IF                                                      '<:|
  'error connecting to POP3                                     '  |
  ELSE                                                          '  |
    SELECT CASE hTCP                                            '  |
      CASE -152:LogIt "No OK on POP3 open"                      '  |
      CASE -152:LogIt "Bad user name"                           '  |
      CASE -153:LogIt "Bad password"                            '  |
      CASE ELSE:LogIt "Error connecting to POP3 "+ STR$(hTCP)   '  |
    END SELECT                                                  '  |
  END IF                                                        '  |
                                                                '  |
'  PRINT;"Results in one string"                                '  |
'  PRINT;gsResults                                              '  |
'  PRINT;"Number of messages: "  + STR$(NumberOfMessages)       '  |
'  PRINT"       Message size: "  + STR$(MsgSize&)               '  |
                                                                '  |
    IF NumberOfMessages > 0 THEN                                '  |
    IF Euro = "ON" THEN EuroSiren(2)                            '  |
    BringUpDefault                                              '  |
    END IF                                                      '  |
                                                                '  |
    delay (tLoop)                                               '  | 5-minute delay between loops
    LOOP                                                        '<-:
                                                                '
  WAITKEY$                                                      '
END FUNCTION                                                    '
                                                                '
SUB LogIt(s AS STRING)                                          '
    s = s + $CRLF                                               '
    gsResults = gsResults + s                                   '
    END SUB                                                     '
                                                                '
  '-------------------------------------------------------------'
' Connect to POP3 mail server.                                  '
'                                                               '
FUNCTION Pop3Connect (Pop3Host AS STRING, USER AS STRING, _     '
                      password AS STRING) AS LONG               '
    LOCAL nTCP   AS LONG                                        '
    LOCAL Buffer AS STRING                                      '
    'added this                                                 '
    LOCAL LstErr AS STRING                                      '
                                                                '
    ON ERROR GOTO Pop3Error                                     '
    LstErr = "Error opening POP3 server"                        '
    nTCP = FREEFILE                                             '
    TCP OPEN "pop3" AT Pop3Host AS nTCP                         '
    TCP LINE nTCP, Buffer                                       '
    IF LEFT$(Buffer, 3) <> "+OK" THEN                           '
        LstErr = "No OK on POP3 open"                           '
        ERROR 151                                               '
    END IF                                                      '
    TCP PRINT nTCP, "USER " + USER                              '
    TCP LINE nTCP, Buffer                                       '
    IF LEFT$(Buffer, 3) <> "+OK" THEN                           '
        LstErr = "Bad mail user name"                           '
        ERROR 152                                               '
    END IF                                                      '
    TCP PRINT nTCP, "PASS " + password                          '
    TCP LINE nTCP, Buffer                                       '
    IF LEFT$(Buffer, 3) <> "+OK" THEN                           '
        LstErr = "Bad mail password"                            '
        ERROR 153                                               '
    END IF                                                      '
    LstErr = ""                                                 '
    FUNCTION = nTCP                                             '
    Pop3ConnErr:                                                '
    EXIT FUNCTION                                               '
Pop3Error:                                                      '
    CLOSE nTCP                                                  '
    FUNCTION = -(ERRCLEAR)                                      '
    RESUME Pop3ConnErr                                          '
END FUNCTION                                                    '
                                                                '
'---------------------------------------------------------------'
' Disconnect from POP3 mail server                              '
'                                                               '
FUNCTION Pop3Quit (BYVAL nTCP AS LONG) AS LONG                  '
    LOCAL Buffer AS STRING                                      '
    TCP PRINT nTCP, "QUIT"                                      '
    TCP LINE nTCP, Buffer                                       '
    IF LEFT$(Buffer, 3) <> "+OK" THEN                           '
        FUNCTION = -1  'error                                   '
        PRINT;"Error"                                           '
    END IF                                                      '
    CLOSE nTCP                                                  '
                                                                '
END FUNCTION                                                    '
                                                                '
'---------------------------------------------------------------'
' Get the status of the POP3 account                            '
'                                                               '
FUNCTION Pop3GetStat (BYVAL nTCP AS LONG, _                     '
                        Messages AS LONG, _                     '
                        MsgSize AS LONG) AS LONG                '
                                                                '
    LOCAL Buffer AS STRING                                      '
    'add this                                                   '
    LOCAL LstErr AS STRING                                      '
    TCP PRINT nTCP, "STAT"                                      '
    TCP LINE nTCP, Buffer                                       '
    IF LEFT$(Buffer, 3) <> "+OK" THEN                           '
        FUNCTION = -154  'error                                 '
        LstErr = "Error getting POP3 STAT"                      '
        EXIT FUNCTION                                           '
    END IF                                                      '
    Messages = VAL(PARSE$(Buffer, " ", 2))                      '
    MsgSize  = VAL(PARSE$(Buffer, " ", 3))                      '
    FUNCTION = 0                                                '
END FUNCTION                                                    '
                                                                '
' From Kev Peel                                                 '
' http://www.powerbasic.com/support/pbforums/showthread.php?t=16318
'                                                                '
'
FUNCTION BringUpDefault AS LONG

    LOCAL sName AS STRING, zTmp AS ASCIIZ * %BUFFER_LEN, zMailClient AS ASCIIZ * %BUFFER_LEN

  ' Get mail name...
    sName = GetReg(%HKEY_LOCAL_MACHINE, "SOFTWARE\Clients\Mail", "", "")

  ' Get mail program name...
    zTmp = GetReg(%HKEY_LOCAL_MACHINE, "SOFTWARE\Clients\Mail\" + sName + "\shell\open\command", "", "")

  ' Must expand environment strings like %ProgramFiles% (if any)
    ExpandEnvironmentStrings zTmp, zMailClient, SIZEOF(zMailClient)

  ' If in quotes, then get whats inside the quotes...
    IF INSTR(zMailClient, CHR$(34)) THEN zMailClient = PARSE$(zMailClient, CHR$(34), 2)

    IF zMailClient = "" THEN

     ' Can't find it...
    MessageBox 0, "Couldn't find the default mail client", "MainClientFinder", %MB_ICONHAND

    ELSE

     ' Ask to execute mail client
    IF MessageBox(0, "Messages in your in-box: " + zMailClient + $CRLF + $CRLF + "Run the default mail client now?", _
                     "MainClientFinder", %MB_ICONQUESTION OR %MB_YESNO) = %IDYES THEN
    ShellExecute 0, "open", zMailClient, "", "", %SW_SHOW
    END IF

    END IF

    END FUNCTION

FUNCTION GetReg(BYVAL iLocation AS LONG, _
                 BYVAL sSubKeys AS STRING, _
                BYVAL sValueName AS STRING, _
                BYVAL sDefault AS STRING) AS STRING

LOCAL hKey AS DWORD, _
   zRegVal AS ASCIIZ * %BUFFER_LEN

    IF iLocation = 0 THEN iLocation = %HKEY_CURRENT_USER
    IF RegOpenKeyEx(iLocation, TRIM$(sSubKeys, "\"), 0, %KEY_READ, hKey) = %ERROR_SUCCESS THEN
    IF RegQueryValueEx(hKey, BYCOPY sValueName, 0, %REG_SZ, zRegVal, %BUFFER_LEN) _
                   <> %ERROR_SUCCESS THEN GOTO RegStringDefault:
    ELSE
    RegStringDefault:
    zRegVal = sDefault
    END IF
    IF hKey THEN RegCloseKey hKey
    FUNCTION = zRegVal
    END FUNCTION


SUB Delay(DelayTime AS SINGLE)                      '
    LOCAL StartTime  AS DOUBLE                      '
    LOCAL EndTime    AS DOUBLE                      '   Time to finish
                                                    '
    StartTime = TIMER                               '
    EndTime = StartTime + DelayTime                 '   Add the delay.
                                                    '
    IF EndTime > 86400 THEN                         '<: Wrap-Around
    EndTime = EndTime - 86400                       ' | at midnight.
    END IF                                          '<:
                                                    '
    DO UNTIL StartTime => EndTime                   '   Keep looping until.....
    StartTime = TIMER                               '
    an$ = INKEY$                                    '
    IF an$ = CHR$(27) THEN EXIT LOOP                '
                                                    '
    IF an$ = CHR$(0,60) THEN                        '<-: F2 key to toggle Euro
    IF Euro = "ON" THEN                             '<:|
    Euro = "OFF"                                    ' ||
    ELSE                                            '<:|
    Euro = "ON"                                      ' ||
    END IF                                          '<:|
    CLS                                             '  |
    PRINT;"Euro: ";Euro                             '  |
    END IF                                          '<-:
    SLEEP 0                                         '
    LOOP                                            '
    END SUB                                         '
                                                    '
SUB EuroSiren(Number AS LONG)
    LOCAL x AS LONG

    FOR x = 1 TO number
    winbeep  500,300
    winbeep 1000,300
    NEXT x

    END SUB
__________________
I'm a conservative ditto-head and d*mn proud of it on both counts!
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright © 1999-2010 PowerBASIC, Inc. All Rights Reserved.
Error in my_thread_global_end(): 1 threads didn't exit