From:	CSBVAX::CSBVAX::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 21-JAN-1989 11:31
To:	MRGATE::"ARISIA::EVERHART"
Subj:	Fixing new mail counts, in V5.0-n (callable mail)


Received: From KL.SRI.COM by CRVAX.SRI.COM with TCP; Thu,  5 JAN 89 18:19:50 PDT
Received: from uwavm.acs.washington.edu (OLY.ACS.WASHINGTON.EDU) by KL.SRI.COM with TCP; Thu, 5 Jan 89 17:49:17 PST
Received: from UWAVM.ACS.WASHINGTON.EDU by uwavm.acs.washington.edu ; Thu, 05 Jan 89 17:40:04 PST
Received: from FHCRCVAX.BITNET (JOE) by UWAVM.ACS.WASHINGTON.EDU (Mailer X1.25)
 with BSMTP id 0356; Thu, 05 Jan 89 17:40:02 PST
Date: Thu, 5 Jan 89 17:40 PST
From: Joe Meadows <JOE%FHCRCVAX.BITNET@uwavm.acs.washington.edu>
Subject: Fixing new mail counts, in V5.0-n (callable mail)
To: info-vax@kl.sri.COM
X-VMS-To: IN%"info-vax@kl.sri.com"

I wrote the following short program to reset new mail counts back to something
reasonable. When I ran it on my system, it crashed when trying to access a
very old mail file (VMS V3.n version I suspect). I thought this was queer,
so I went into mail and did a SET FILE to this same mail file, and I promptly
got logged off the system! I just thought I'd mention this in case anyone
else runs into this problem. After renaming the mail file, things went well.

So, here's some code that you might want to run once a month, once a night,
or perhaps even never. It could use some enhancements of course (like
selecting a specific user), but this code (along with the earlier posted
examples) should be enough to get you on your way. You do have VMS5.0-n
or higher right?

Cheers,
Joe Meadows     joe@fhcrcvax.bitnet

p.s. Is anyone getting tired of these examples?

$ create/log FIXCOUNT.C
$DECK
#include ctype
#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[33];
  char maildir[253];
  short new, cnt;
  int ucontext = 0, userlen, mlen, sts, i;
  struct itmlst
    null_list[] = {{0,0,0,0}},
    uilist1[] = {{0,MAIL$_USER_FIRST,0,0}, {0,0,0,0}},
    uilist2[] = {{0,MAIL$_USER_NEXT,0,0},
                 {0,MAIL$_NOSIGNAL,0,0},
                 {0,0,0,0}},
    uilist3[] = {{0,MAIL$_USER_USERNAME,user,0},
                 {sizeof(cnt),MAIL$_USER_SET_NEW_MESSAGES,&cnt,0},
                 {0,0,0,0}},
    uolist[] = {{sizeof(user) - 1,MAIL$_USER_RETURN_USERNAME,user,&userlen},
                {sizeof(maildir) - 1,MAIL$_USER_FULL_DIRECTORY,maildir,&mlen},
                {sizeof(new),MAIL$_USER_NEW_MESSAGES,&new,0},
                {0,0,0,0}};

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

  sts = mail$user_get_info(&ucontext,uilist1,uolist);
  while (sts & 1)
  {
    user[userlen] = '\0';
    maildir[mlen] = '\0';
    if (new > 0)
    {
      cnt = fix_user(user,maildir);
      if ((cnt != -1) && (cnt != new))
      {
        printf("Adjusting %s new mail count, from %d to %d\n",user,new,cnt);
        uilist3[0].buffer_length = userlen;
        mail$user_set_info(&ucontext,uilist3,null_list);
      }
    }

    sts = mail$user_get_info(&ucontext,uilist2,uolist);
  }

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




fix_user(char *user, char *maildir)
{
  long sts, new = -1, mcontext = 0, fcontext = 0;
  short flags = MAIL$M_NEWMSG;
  struct itmlst
    null_list[] = {{0,0,0,0}},
    ns_list[] = {{0,MAIL$_NOSIGNAL,0,0},{0,0,0,0}},
    filist[] = {{strlen(maildir),MAIL$_MAILFILE_NAME,maildir,0},
                {0,MAIL$_NOSIGNAL,0,0},
                {0,0,0,0}},
    mblist[] = {{4,MAIL$_MESSAGE_FILE_CTX,&fcontext,0},
                {0,MAIL$_NOSIGNAL,0,0},
                {0,0,0,0}},
    mslist[] = {{sizeof(flags),MAIL$_MESSAGE_FLAGS,&flags,0},
                {sizeof("NEWMAIL"),MAIL$_MESSAGE_FOLDER,"NEWMAIL",0},
                {0,MAIL$_NOSIGNAL,0,0},
                {0,0,0,0}},
    milist[] = {{0,MAIL$_MESSAGE_NEXT,0,0},
                {0,MAIL$_NOSIGNAL,0,0},
                {0,0,0,0}};

  if (mail$mailfile_begin(&fcontext,ns_list,null_list) & 1)
  {
    if (mail$mailfile_open(&fcontext,filist,null_list) & 1)
    {
      if (mail$message_begin(&mcontext,mblist,null_list) & 1)
      {
        new = 0;
        sts = mail$message_select(&mcontext,mslist,null_list);
        while (sts & 1)
        {
          sts = mail$message_info(&mcontext,milist,null_list);
          if (sts & 1) ++new;
        }
        mail$message_end(&mcontext,ns_list,null_list);
      }
      mail$mailfile_close(&fcontext,ns_list,null_list);
    }
    mail$mailfile_end(&fcontext,ns_list,null_list);
  }
  return(new);
}
$EOD

