home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / library / fox / workgrp / readmail.prg < prev    next >
Text File  |  1993-08-27  |  11KB  |  434 lines

  1. * Readmail.prg
  2. *
  3. * This program controls the Read Mail menu option.
  4. * FoxPro uses MAPI calls to retrieve all messages
  5. * in the inbox. They are then displayed in a FoxPro
  6. * dialog for further editing.
  7.  
  8. PRIVATE oldsafe,oldexact,origin,messnum,messageid,;
  9.     oldmessid,islogon,filepick,getid,oldtalk,morigin,retval
  10.  
  11. IF SET("TALK") = "ON"
  12.     SET TALK OFF
  13.     m.oldtalk= "ON"
  14. ELSE
  15.     m.oldtalk= "OFF"
  16. ENDIF
  17.  
  18. * Mailsession variable is a FoxPro defined
  19. * public variable used to store the MS MAIL
  20. * session channel for use with subsequent mail
  21. * calls. The Islogon variable is set TRUE if a
  22. * session (saved in Mailsession) is already running
  23. islogon=.F.
  24. DO CASE
  25.     CASE TYPE('mailsession')#'N'
  26.         PUBLIC mailsession
  27.         mailsession = 0
  28.         mailsession=mapilib('LOGON')
  29.     CASE m.mailsession=0
  30.         mailsession=mapilib('LOGON')
  31.     OTHERWISE
  32.         islogon=.T.
  33. ENDCASE
  34.  
  35. IF m.mailsession=0  &&failed to logon, so return
  36.     SET TALK &oldtalk
  37.     RETURN
  38. ENDIF
  39.  
  40. * Now setup variables for program
  41. DIMENSION filearr[1]
  42. DIMENSION reciparr[1]
  43. reciparr=''
  44. recippick=''
  45. origin = ''
  46. oldmessid = ""
  47. messageid = ""
  48. morigin = ""
  49. messnum = 1
  50. oldsafe = SET('SAFETY')
  51. oldexact = SET('EXACT')
  52. olddelete = SET('DELETE')
  53. SET SAFETY OFF
  54. SET EXACT ON
  55. SET DELETE ON
  56.  
  57. * This is a database cursor used to
  58. * save all the messages from the mapiMesg
  59. * cursor. Three add'l fields are added at
  60. * the end. The origin is used to store address
  61. * of message originator. The messnum is unique
  62. * key used in relations with other databases below.
  63. CREATE CURSOR MESSAGES ;
  64.     (reserved N(10),;
  65.     subject C(254),;
  66.     notetext m,;
  67.     messagtype C(254),;
  68.     daterecved C(16),;
  69.     convertnid C(254),;
  70.     flags N(10),;
  71.     recipcount N(10),;
  72.     filecount N(10),;
  73.     origin C(254),;
  74.     messnum N(4),;
  75.     messageid C(100))
  76.  
  77. * This cursor stores recipients associated
  78. * with each message. This is a 1-Many child
  79. * file with messages.
  80. CREATE CURSOR recips;
  81.     (messnum N(4),;
  82.     recipclass N (1),;
  83.     address C(254))
  84.  
  85. * The files database store file attachments
  86. * for each message. This is also 1-Many file.
  87. CREATE CURSOR FILES ;
  88.     (reserved N(10),;
  89.     flags N(10),;
  90.     position N(10),;
  91.     pathname C(254),;
  92.     filename C(254),;
  93.     filetype C(254),;
  94.     messnum N(4))
  95.  
  96. * Create a cursor to store each unique
  97. * address in messages.
  98. =mapilib('newcursor','mapirecip','addresses')
  99. INDEX ON address TAG address
  100.  
  101. * This is core program which continues to
  102. * loop through messages until all are read.
  103. * The getnextnote function basically calls
  104. * the MPFindNext and MPReadMail functions
  105. * to retrieve and then read mail. Note that
  106. * oldmessid is initially set to null string.
  107. WAIT WINDOW 'Reading mail...' NOWAIT
  108. DO WHILE .T.
  109.     messageid = mapilib('getnextnote',m.mailsession,m.oldmessid)
  110.     IF EMPTY(m.messageid)
  111.         EXIT
  112.     ENDIF
  113.     DO process_message
  114.     oldmessid = m.messageid
  115.     messnum = m.messnum+1
  116. ENDDO
  117. WAIT CLEAR
  118.  
  119. IF RECCOUNT('messages')=0
  120.     WAIT WINDOW 'No messages were available.'
  121. ELSE
  122.     SELECT MESSAGES
  123.     GO TOP
  124.     DO fileselect        && this routine sets arrays
  125.     DO readmail.spr    && call screen
  126.     DO cleanup
  127. ENDIF
  128.  
  129. *Finished with screen, now cleanup
  130. IF !islogon
  131.     =mapilib('LOGOFF',m.mailsession)
  132.     mailsession = 0
  133. ENDIF
  134. SET SAFETY &oldsafe
  135. SET EXACT &oldexact
  136. SET DELETE &olddelete
  137. SET TALK &oldtalk
  138.  
  139.  
  140.  
  141. *!*********************************************************************
  142. *!
  143. *!      FUNCTION: cleanup
  144. *!
  145. *!*********************************************************************
  146. * clean up here at the end
  147. * IMPORTANT!!! The MPReadMail function creates temp
  148. * files for all mail attachments. You must dispose of them
  149. * when done reading the mail. If you don't, each time you
  150. * reread mail, a new temp file is created which could be a
  151. * duplicate of one already there.
  152. PROCEDURE cleanup
  153.     SELECT FILES
  154.     SCAN
  155.         DELETE FILE (pathname)
  156.     ENDSCAN
  157.     USE IN addresses
  158.     USE IN FILES
  159.     USE IN recips
  160.     USE IN MESSAGES
  161.     USE IN mapimesg
  162.     USE IN mapirecip
  163.     USE IN mapiorig
  164.     USE IN mapifile
  165.     IF USED('detailreci')
  166.         USE IN detailreci
  167.     ENDIF
  168.     RETURN
  169.  
  170.  
  171.     *!*********************************************************************
  172.     *!
  173.     *!      FUNCTION: btnval
  174.     *!
  175.     *!*********************************************************************
  176.     * This procedure handles the VALID statement for
  177.     * the navigational buttons.
  178. PROCEDURE btnval
  179.     DO CASE
  180.         CASE m.btn1 = 1  &&Previous Message
  181.             SKIP -1
  182.             IF BOF()
  183.                 WAIT WINDOW 'Top of file.' NOWAIT
  184.                 GO TOP
  185.             ENDIF
  186.         CASE m.btn1 = 2  &&Browse locate
  187.             DEFINE WINDOW messwind FROM 1,1 TO 15,35;
  188.                 SYSTEM GROW CLOSE ZOOM FLOAT
  189.             MOVE WINDOW messwind CENTER
  190.             BROWSE WINDOW messwind NOEDIT NODELETE NOMENU ;
  191.                 FONT 'ms sans serif',10 ;
  192.                 FIELD subject TITLE 'Select Message'
  193.             RELEASE WINDOW messwind
  194.         CASE m.btn1 = 3  &&Next Message
  195.             SKIP 1
  196.             IF EOF()
  197.                 WAIT WINDOW 'Bottom of file.' NOWAIT
  198.                 GO BOTTOM
  199.             ENDIF
  200.     ENDCASE
  201.     DO fileselect
  202.     SHOW GETS
  203.     RETURN
  204.  
  205.  
  206.     *!*********************************************************************
  207.     *!
  208.     *!      FUNCTION: btn2val
  209.     *!
  210.     *!*********************************************************************
  211.     * This procedure handles the Mail function
  212.     * buttons
  213. PROCEDURE btn2val
  214.     DO CASE
  215.         CASE m.btn2 = 1
  216.             =mapilib('sendnote')
  217.         CASE m.btn2 = 2
  218.             DO reply_message
  219.         CASE m.btn2 = 3
  220.             DO delete_message
  221.     ENDCASE
  222.     SHOW GETS
  223.     RETURN
  224.  
  225.  
  226.     *!*********************************************************************
  227.     *!
  228.     *!      FUNCTION: fileselect
  229.     *!
  230.     *!*********************************************************************
  231.     * This routine refreshes the arrays when the record pointer
  232.     * in the message database is moved. These arrays are used in
  233.     * the recipients and files lists.
  234. PROCEDURE fileselect
  235.     PRIVATE getmessnum
  236.     getmessnum=messages.messnum
  237.     morigin=LOOKUP(addresses.name,ALLT(messages.origin),addresses.address,'address')
  238.     SELECT DISTINCT filename FROM FILES;
  239.         WHERE files.messnum = m.getmessnum;
  240.         INTO ARRAY filearr
  241.     IF _TALLY=0
  242.         DIMENSION filearr[1]
  243.         filearr=''
  244.     ENDIF
  245.     filepick=0
  246.     SELECT DISTINCT a.name FROM addresses A,recips B;
  247.         WHERE a.address=ALLT(b.address) AND b.messnum = m.getmessnum;
  248.         INTO ARRAY reciparr
  249.     IF _TALLY=0
  250.         DIMENSION reciparr[1]
  251.         reciparr=''
  252.     ENDIF
  253.     recippick=IIF(_TALLY=0,'',reciparr[1])
  254.     RETURN
  255.  
  256.  
  257.     *!*********************************************************************
  258.     *!
  259.     *!      FUNCTION: getstatus
  260.     *!
  261.     *!*********************************************************************
  262.     * This function displays the status of a particular message
  263.     * which is determined by the flags fields in the message cursor.
  264. PROCEDURE getstatus
  265.     PRIVATE getstatus
  266.     DO CASE
  267.         CASE flags=1
  268.             getstatus = '(Unread)'
  269.         CASE flags=2
  270.             getstatus = '(Receipt Requested)'
  271.         CASE flags=3
  272.             getstatus = '(Unread, Receipt Requested)'
  273.         CASE flags=4
  274.             getstatus = '(Sent)'
  275.         CASE flags=5
  276.             getstatus = '(Unread, Sent)'
  277.         CASE flags=6
  278.             getstatus = '(Receipt Requested, Sent)'
  279.         CASE flags=7
  280.             getstatus = '(Unread, Receipt Requested, Sent)'
  281.         OTHERWISE
  282.             getstatus = ''
  283.     ENDCASE
  284.     RETURN getstatus
  285.  
  286.  
  287.     *!*********************************************************************
  288.     *!
  289.     *!      FUNCTION: getdetail
  290.     *!
  291.     *!*********************************************************************
  292.     * This calls the MAPI function which displays the mail
  293.     * name details dialog. This is triggered either thru the
  294.     * button next to the origin or the recipient list box.
  295. PROCEDURE getdetail
  296.     PARAMETER maddress
  297.     IF EMPTY(m.maddress)
  298.         RETURN
  299.     ENDIF
  300.     SELECT addresses
  301.     LOCATE FOR name=ALLTRIM(m.maddress)
  302.     SCATTER MEMVAR MEMO
  303.     IF USED('detailreci')
  304.         SELECT detailreci
  305.         GATHER MEMVAR MEMO
  306.     ELSE
  307.         =mapilib('newcursor','mapirecip','detailreci')
  308.         INSERT INTO detailreci FROM MEMVAR
  309.     ENDIF
  310.     =mapilib('details',m.mailsession,'detailreci')
  311.     SELECT MESSAGES
  312.     RETURN
  313.  
  314.  
  315.     *!*********************************************************************
  316.     *!
  317.     *!      FUNCTION: process_message
  318.     *!
  319.     *!*********************************************************************
  320.     * This function is called in the main processing loop
  321.     * which repeated polls for the next message.
  322. PROCEDURE process_message
  323.     * Check to see if new address in address database,
  324.     * else add it. Do this for both origin and recipient
  325.     * databases.
  326.     SELECT mapiorig
  327.     IF !SEEK(ALLT(address),'addresses')
  328.         SCATTER MEMVAR MEMO
  329.         INSERT INTO addresses FROM MEMVAR
  330.     ENDIF
  331.     m.origin = address
  332.     SELECT mapirecip
  333.     SCAN
  334.         SCATTER MEMVAR MEMO
  335.         IF !SEEK(ALLT(address),'addresses')
  336.             INSERT INTO addresses FROM MEMVAR
  337.         ENDIF
  338.         INSERT INTO recips FROM MEMVAR
  339.     ENDSCAN
  340.     * Now check for files. If files were
  341.     * associated with message add them to
  342.     * files cursor.
  343.     IF mapimesg.filecount#0
  344.         *add file attachments
  345.         SELECT mapifile
  346.         SCAN
  347.             SCATTER MEMVAR MEMO
  348.             INSERT INTO FILES FROM MEMVAR
  349.         ENDSCAN
  350.     ENDIF
  351.     *add new message to messages database
  352.     SELECT mapimesg
  353.     SCATTER MEMVAR MEMO
  354.     INSERT INTO MESSAGES FROM MEMVAR
  355.     RETURN
  356.  
  357.  
  358.     *!*********************************************************************
  359.     *!
  360.     *!      FUNCTION: delete_message
  361.     *!
  362.     *!*********************************************************************
  363.     * This function deletes a message (both from Inbox and screen).
  364. PROCEDURE delete_message
  365.     PRIVATE getnum
  366.     IF !mapilib('mpalert','Delete message?')
  367.         RETURN
  368.     ENDIF
  369.     =mapilib('deletenote',m.mailsession,ALLT(messageid))
  370.     getnum=messnum
  371.     DELETE
  372.     SELECT recips
  373.     DELETE ALL FOR messnum=m.getnum
  374.     * Don't forget to delete temp files.
  375.     SELECT FILES
  376.     SCAN FOR messnum=m.getnum
  377.         DELETE FILE (pathname)
  378.         DELETE
  379.     ENDSCAN
  380.     * Now refresh screen with next record
  381.     SELECT MESSAGES
  382.     SKIP
  383.     IF EOF()
  384.         GO BOTTOM
  385.     ENDIF
  386.     DO fileselect
  387.     RETURN
  388.  
  389.  
  390.     *!*********************************************************************
  391.     *!
  392.     *!      FUNCTION: reply_message
  393.     *!
  394.     *!*********************************************************************
  395.     * The reply function opens a selected message in the standard
  396.     * MAPI SendNote dialog for replying to sender. Note: we don't
  397.     * update FoxPro screen here to show change. This would involve
  398.     * deleting current message and then polling for new one.
  399. PROCEDURE reply_message
  400.     PRIVATE getnum,getorigin
  401.     SELECT MESSAGES
  402.     getnum=messages.messnum
  403.     getorigin=messages.origin
  404.     SCATTER MEMVAR MEMO
  405.     * add a little extra space for replying
  406.     m.notetext=CHR(13)+CHR(13)+CHR(13)+m.notetext
  407.     m.recipcount = 1
  408.     * Add "RE:" as is done with MS Mail when replying
  409.     m.subject = 'RE: '+m.subject
  410.     SELECT mapimesg
  411.     GATHER MEMVAR MEMO
  412.     SELECT mapirecip
  413.     ZAP
  414.     SELECT addresses
  415.     LOCATE FOR address=ALLT(m.getorigin)
  416.     SCATTER MEMVAR MEMO
  417.     * reset class to 1 -- To: class
  418.     m.recipclass = 1 &&set To flag
  419.     INSERT INTO mapirecip FROM MEMVAR
  420.     SELECT mapifile
  421.     ZAP
  422.     IF mapimesg.filecount=0
  423.         APPEND BLANK
  424.     ELSE
  425.         SELECT FILES
  426.         SCAN FOR messnum=m.getnum
  427.             SCATTER MEMVAR MEMO
  428.             INSERT INTO mapifile FROM MEMVAR
  429.         ENDSCAN
  430.     ENDIF
  431.     retval=mapilib("sendmail",m.mailsession,"mapiMesg","mapiRecip","mapiFile")
  432.     SELECT MESSAGES
  433.     RETURN
  434.