From:	CSBVAX::CSBVAX::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 16-DEC-1988 07:02
To:	MRGATE::"ARISIA::EVERHART"
Subj:	callable mail in V5, short example


Received: From KL.SRI.COM by CRVAX.SRI.COM with TCP; Fri, 16 DEC 88 03:02:40 PDT
Received: from uwavm.acs.washington.edu (oly.acs.washington.edu) by KL.SRI.COM with TCP; Fri, 16 Dec 88 02:56:39 PST
Received: from UWAVM.ACS.WASHINGTON.EDU by uwavm.acs.washington.edu ; Fri, 16 Dec 88 02:55:26 PST
Received: from FHCRCVAX.BITNET (JOE) by UWAVM.ACS.WASHINGTON.EDU (Mailer X1.25)
 with BSMTP id 3277; Fri, 16 Dec 88 02:55:24 PST
Date: Fri, 16 Dec 88 02:55 PST
From: Joe Meadows <JOE%FHCRCVAX.BITNET@uwavm.acs.washington.edu>
Subject: callable mail in V5, short example
To: info-vax@kl.sri.COM
X-VMS-To: IN%"info-vax@kl.sri.com"

Hello!

I thought a few of you folks running VMS 5.0-? might be interested in
this tidbit. The following C code will display the count of new mail
messages for a given user, followed by a directory listing of those
new messages. This code uses the as-yet-undocumented callable mail
routines. Anyway, if you have my UNSDL utility you can create the one
necessary header file ($MAILDEF.H) required for this to work (or, you
can make it the hard way....)

Just define a foreign command to run this program. If you pass it no
parameters it will display your own new mail message count and directory,
or you can pass it a list of usernames (separated by spaces), for which
it will display the new mail message count and directory.

This is just an example program, without frills added. I will of course
add some frills as time permits, but I thought most people would prefer
a nice minimal example!

        Cheers,
        Joe Meadows     joe@fhcrcvax.bitnet

I call this "CHECKMAIL.C"

#include ctype
#include jpidef
#include "$maildef.h"

struct itmlst
{
  short buffer_length;
  short item_code;
  long buffer_address;
  long return_length_address;
};

main (int argc, char *argv[])
{
  char user[13];
  int ucontext = 0, userlen = 0, sts, i;
  struct itmlst
    null_list[] = {{0,0,0,0}},
    jpi_list[]  = {{sizeof(user) - 1,JPI$_USERNAME,user,&userlen},
                   {0,0,0,0}};

  sts = mail$user_begin(&ucontext,null_list,null_list);
  if (!(sts & 1)) return;

  if (argc > 1)
  {
    for (i = 1; i < argc; ++i)
    {
      display_user(argv[i],&ucontext);
    }
  }
  else
  {
    sys$getjpiw(0,0,0,jpi_list,0,0,0);
    user[userlen] = '\0';

    while(isspace(user[--userlen])) /* suck up trailing spaces */
      user[userlen] = '\0';

    display_user(user,&ucontext);
  }

  mail$user_end(&ucontext,null_list,null_list);
}

display_user(char *str, long *ucontext)
{
  char from[80], subject[252], date[23], dir[252];
  long fromlen, subjectlen, datelen, dirlen, size, sts, i;
  int mcontext = 0, fcontext = 0;
  short flags = MAIL$M_NEWMSG, new;
  struct itmlst
    null_list[] = {0,0,0,0},
    filist[] = {{0,MAIL$_MAILFILE_NAME,dir,0}, {0,0,0,0}},
    mblist[] = {{4,MAIL$_MESSAGE_FILE_CTX,&fcontext,0}, {0,0,0,0}},
    mslist[] = {{sizeof(flags),MAIL$_MESSAGE_FLAGS,&flags,0},
                {sizeof("NEWMAIL"),MAIL$_MESSAGE_FOLDER,"NEWMAIL",0},
                {0,0,0,0}},
    milist[] = {{0,MAIL$_MESSAGE_NEXT,0,0},
                {0,MAIL$_NOSIGNAL,0,0},
                {0,0,0,0}},
    molist[] = {{sizeof(from),MAIL$_MESSAGE_FROM,from,&fromlen},
                /* MAIL$_MESSAGE_REPLY_PATH could be used
                   instead, for a somewhat shorter FROM line. */
                {sizeof(subject),MAIL$_MESSAGE_SUBJECT,subject,&subjectlen},
                {sizeof(date),MAIL$_MESSAGE_DATE,date,&datelen},
                {sizeof(size),MAIL$_MESSAGE_SIZE,&size,0},
                {0,0,0,0}},
    uilist[] = {{0,MAIL$_USER_USERNAME,0,0}, {0,0,0,0}},
    uolist[] = {{sizeof(new),MAIL$_USER_NEW_MESSAGES,&new,0},
                {sizeof(dir),MAIL$_USER_FULL_DIRECTORY,dir,&dirlen},
                {0,0,0,0}};

  upcase(str);
  uilist[0].buffer_length = strlen(str);
  uilist[0].buffer_address = str;
  sts = mail$user_get_info(ucontext,uilist,uolist);
  if (sts & 1)
  {
    printf("%s has %d new message%s\n\n",str,new,new==1?"":"s");
    if (new > 0)
    {
      mail$mailfile_begin(&fcontext,null_list,null_list);
      filist[0].buffer_length = dirlen;
      mail$mailfile_open(&fcontext,filist,null_list);
      mail$message_begin(&mcontext,mblist,null_list);
      mail$message_select(&mcontext,mslist,null_list);
      i = 0;
      while (sts & 1)
      {
        sts = mail$message_info(&mcontext,milist,molist);
        if (sts & 1)
        {
            printf("#%d %.*s\n",++i,fromlen,from);
            printf("date: %.20s (%d line%s)\n",date,size,size==1?"":"s");
            if (subjectlen)
              printf("subj: %.*s\n",subjectlen,subject);
        }
      }
      mail$message_end(&mcontext,null_list,null_list);
      mail$mailfile_close(&fcontext,null_list,null_list);
      mail$mailfile_end(&fcontext,null_list,null_list);
    }
  }
}

upcase(str)
  char *str;
{
  while (*str)
  {
    *str = toupper(*str);
    ++str;
  }
}

-- end of checkmail.c --

