From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 12-JUN-1991 01:24:48.22
To:	ARISIA::EVERHART
CC:	
Subj:	GET NEW MAIL COUNT WITH CALLABLE MAIL!

From:	RELAY-INFO-VAX@CRVAX.SRI.COM@SMTP@CRDGW2
To:	Everhart@Arisia@MRGATE

Received:  by crdgw1.ge.com (5.57/GE 1.100)
	 id AA12012; Tue, 11 Jun 91 13:57:32 EDT
Message-Id:  <9106111757.AA12012@crdgw1.ge.com>
Received: From CUNYVM.CUNY.EDU by CRVAX.SRI.COM with TCP; Tue, 11 JUN 91 08:59:56 PDT
Received: from DM0MPI11 by CUNYVM.CUNY.EDU (IBM VM SMTP R1.2.2MX) with BSMTP id 8678; Tue, 11 Jun 91 11:59:05 EDT
X-Delivery-Notice:  SMTP MAIL FROM does not correspond to sender.
Received: from DM0MPI53.BITNET (DM0MPI53@DM0MPI12) by DM0MPI11 (Mailer R2.08)
 with BSMTP id 0887; Tue, 11 Jun 91 17:58:47 GMT
Comments: This is gatewayed mail.  WARNING:  Mail may not
          necessarily be returnable thru this path.
Date:    Tue, 11 Jun 91 17:50  +0200
From: "Joseph Huber ,MPI Muenchen, -372" <HUBER%DM0MPI53.BITNET@CUNYVM.CUNY.EDU>
Organization: Max-Planck-Institut fuer Physik, Muenchen
Subject: GET NEW MAIL COUNT WITH CALLABLE MAIL!
To: INFO-VAX@KL.SRI.COM
X-Original-To: RSCS%"INFO-VAX@KL.SRI.COM"

Many posters in recent time were asking for a way to get the new mail count
into DCL procedures-
and have been answered by program fragments digging into vmsmail_profile
or mail.mai .
BUT since VMS 5.0, there is a comfortable CALLABLE MAIL interface available in
the run-time library, which allows to do such things in a clean way (and
presumable compatible with future VMS releases!).
Callable mail was not documented with VMS 5.0, but at least the 5.4 new
features manual (chapter 21) lists the routines, and refers to the new
VMS Utility Routines Manual, which describes the interface in detail.
Already in 1989 an excellent description was distributed through INFO-VAX
by "?" .
Based on this description, I quickly made the following example programs,
included as a VMS_SHARE set.
Enjoy and extend it (and forget the crude old programs, the internal vmsmail_
profile and mail.mai structure will stay undocumented and subject to changes
from release to release) !
$! ------------------ CUT HERE -----------------------
$ v='f$verify(f$trnlnm("SHARE_VERIFY"))'
$!
$! This archive created by VMS_SHARE Version 7.1-004  3-AUG-1989
$!   On 11-JUN-1991 17:12:57.68   By user HUBER
$!
$! This VMS_SHARE Written by:
$!    Andy Harper, Kings College London UK
$!
$! Acknowledgements to:
$!    James Gray       - Original VMS_SHARE
$!    Michael Bednarek - Original Concept and implementation
$!
$! TO UNPACK THIS SHARE FILE, CONCATENATE ALL PARTS IN ORDER
$! AND EXECUTE AS A COMMAND PROCEDURE  (  @name  )
$!
$! THE FOLLOWING FILE(S) WILL BE CREATED AFTER UNPACKING:
$!       1. GET_NEW_MAIL_COUNT.FOR;1
$!       2. COUNT_NEW_MAIL.FOR;2
$!       3. COUNT_NEW_MAIL.COM;6
$!
$set="set"
$set symbol/scope=(nolocal,noglobal)
$f=f$parse("SHARE_TEMP","SYS$SCRATCH:.TMP_"+f$getjpi("","PID"))
$e="write sys$error  ""%UNPACK"", "
$w="write sys$output ""%UNPACK"", "
$ if f$trnlnm("SHARE_LOG") then $ w = "!"
$ if f$getsyi("version") .ges. "V4.4" then $ goto START
$ e "-E-OLDVER, Must run at least VMS 4.4"
$ v=f$verify(v)
$ exit 44
$UNPACK: SUBROUTINE ! P1=filename, P2=checksum
$ if f$search(P1) .eqs. "" then $ goto file_absent
$ e "-W-EXISTS, File ''P1' exists. Skipped."
$ delete/nolog 'f'*
$ exit
$file_absent:
$ if f$parse(P1) .nes. "" then $ goto dirok
$ dn=f$parse(P1,,,"DIRECTORY")
$ w "-I-CREDIR, Creating directory ''dn'."
$ create/dir 'dn'
$ if $status then $ goto dirok
$ e "-E-CREDIRFAIL, Unable to create ''dn'. File skipped."
$ delete/nolog 'f'*
$ exit
$dirok:
$ w "-I-PROCESS, Processing file ''P1'."
$ define/user sys$output nl:
$ EDIT/TPU/NOSEC/NODIS/COM=SYS$INPUT 'f'/OUT='P1'
PROCEDURE Unpacker ON_ERROR ENDON_ERROR;SET(FACILITY_NAME,"UNPACK");SET(
SUCCESS,OFF);SET(INFORMATIONAL,OFF);f:=GET_INFO(COMMAND_LINE,"file_name");
buff:=CREATE_BUFFER(f,f);p:=SPAN(" ")@r&LINE_END;POSITION(BEGINNING_OF(buff))
;LOOP EXITIF SEARCH(p,FORWARD)=0;POSITION(r);ERASE(r);ENDLOOP;POSITION(
BEGINNING_OF(buff));g:=0;LOOP EXITIF MARK(NONE)=END_OF(buff);x:=
ERASE_CHARACTER(1);IF g = 0 THEN IF x="X" THEN MOVE_VERTICAL(1);ENDIF;IF x=
"V" THEN APPEND_LINE;MOVE_HORIZONTAL(-CURRENT_OFFSET);MOVE_VERTICAL(1);ENDIF;
IF x="+" THEN g:=1;ERASE_LINE;ENDIF;ELSE IF x="-" THEN g:=0;ENDIF;ERASE_LINE;
ENDIF;ENDLOOP;p:="`";POSITION(BEGINNING_OF(buff));LOOP r:=SEARCH(p,FORWARD);
EXITIF r=0;POSITION(r);ERASE(r);COPY_TEXT(ASCII(INT(ERASE_CHARACTER(3))));
ENDLOOP;o:=GET_INFO(COMMAND_LINE,"output_file");WRITE_FILE(buff,o);
ENDPROCEDURE;Unpacker;EXIT;
$ delete/nolog 'f'*
$ CHECKSUM 'P1'
$ IF CHECKSUM$CHECKSUM .eqs. P2 THEN $ EXIT
$ e "-E-CHKSMFAIL, Checksum of ''P1' failed."
$ ENDSUBROUTINE
$START:
$ create/nolog 'f'
X      INTEGER FUNCTION GET_NEW_MAIL_COUNT(count)
XC
XC-----------------------------------------
XC
XC   Author   :- J.Huber               11-JUN-1991
XC
XC=========================================
XC
XC   Purpose   : return VMS new mail count (using callable mail)
XC   Returned value  : VMS condition code
XC   Inputs    : -
XC   Outputs   : integer*2 count by reference
XC
XC=========================================
XC +
XC Declarations.
XC -
X      IMPLICIT NONE
X      INTEGER*2 count
X      INCLUDE '($maildef)/nolist'
X      STRUCTURE /mail_item_array/
X        INTEGER*2 len
X        INTEGER*2 item_code
X        INTEGER*4 address
X        INTEGER*4 ret_len_addr
X      END STRUCTURE
X      INTEGER mail$user_begin,mail$user_end,mail$user_get_info
X      INTEGER ucontext
X      INTEGER*2 nmess
X      VOLATILE nmess
X
X      RECORD /mail_item_array/ newmess(3)
X      RECORD /mail_item_array/ null
X
X      count = 0
X      CALL mailsetup(newmess(1),MAIL$_NOSIGNAL,%descr(0),)
X      CALL mailsetup(newmess(2),MAIL$_USER_NEW_MESSAGES,%descr(nmess),)
X      get_new_mail_count = mail$user_begin(ucontext,null,null)
X      IF (get_new_mail_count) THEN
X        get_new_mail_count = mail$user_get_info(ucontext,null,newmess)
X        IF (get_new_mail_count) THEN
X          get_new_mail_count = mail$user_end(ucontext,null,null)
X          IF (get_new_mail_count) THEN
X            count = nmess
X          ENDIF
X        ENDIF
X      ENDIF
X      END
X
X      SUBROUTINE mailsetup(item,code,string,ret_len)
X      IMPLICIT NONE
X      CHARACTER*(*) string
X      INTEGER ret_len
X      INTEGER code
X      STRUCTURE /mail_item/
X        INTEGER*2 len
X        INTEGER*2 item_code
X        INTEGER*4 address
X        INTEGER*4 ret_len_addr
X      END STRUCTURE
X
X      RECORD /mail_item/ item
X      item.len=len(string)
X      item.address=%loc(string)
X      item.item_code=code
X      item.ret_len_addr=%loc(ret_len)
X      END
$ CALL UNPACK GET_NEW_MAIL_COUNT.FOR;1 337581206
$ create/nolog 'f'
X      PROGRAM COUNT_NEW_MAIL
XC
XC-----------------------------------------
XC
XC   Author   :- J.Huber               11-JUN-1991
XC
XC=========================================
XC
XC   Purpose   : Put VMS new mail count into a DCL symbol
XC   Inputs    : -
XC   Outputs   : DCL symbol MAIL_COUNT (re)defined
XC
XC   External routines referenced: get_new_mail_count
XC=========================================
XC +
XC Declarations.
XC -
X      IMPLICIT NONE
X      INTEGER get_new_mail_count,code
X      INTEGER*2 nmail
X      CHARACTER*7 symbol
XC + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
XC Entry Point.
XC - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
X      code = get_new_mail_count(nmail)
X      IF (code) THEN
X        WRITE(symbol,'(I7)') nmail
X        CALL LIB$SET_SYMBOL('MAIL_COUNT',symbol)
X      ENDIF
X      CALL exit(code)
X      END
$ CALL UNPACK COUNT_NEW_MAIL.FOR;2 174733986
$ create/nolog 'f'
X$! this file is just an example how to use the count_new_mail program.
X$ run count_new_mail
X$ if $status`032
X$ then
X$  mail_count = f$edit(mail_count,"COLLAPSE,TRIM")
X$  count = 'mail_count'
X$LOOP:
X$  if count .gt. 0`032
X$  then
X$   define/user sys$output NL:
X$   MAIL
XREAD
XEXTRACT NEWMAIL.TMP
XDELETE
XEXIT
X$ write sys$exit "Do whatever you want to do with new mail files NEWMAIL.TMP
V"
X$   count = count -1
X$   goto loop
X$  endif
X$ endif
X$exit
$ CALL UNPACK COUNT_NEW_MAIL.COM;6 408898856
$ v=f$verify(v)
$ EXIT
--------------------cut here----------------------------------------------------
Joseph "Sepp" Huber, Max-Planck-Institut fuer Physik,Munich, Germany
huber@uxmpim.mppmu.mpg.de | HUBER@DM0MPI53.BITNET | HEPnet: 13673::HUBER

