/*
 * SelectMail.c (adapted from SelectMail.mod)
 *
 */

#include <clib/ums_protos.h>
#include <pragmas/ums_pragmas.h>
#include <stdio.h>
#include <stdlib.h>

extern struct Library *UMSBase;
UMSUserAccount account;

/* get UMS error number and print error text */
void CheckErr(void)
{
 UMSError err;

 err=UMSErrNum(account);

 /* got an error? */
 if (err != UMSERR_OK)
  {
   fprintf(stderr,"UMS-Error: %3d, '%s'\n",err,UMSErrTxt(account));
   UMSLogout(account);
   exit(20);
  }
}

int main(int argc, char *argv[])
{
 char *login,*passwd,*select;
 UMSMsgNum result;

 if (argc<3)
  {
   fprintf(stderr,"Usage: %s <login> <passwd> [<select name>]\n",argv[0]);
   exit(0);
  }

 /* Read parameters */
 login=argv[1];
 passwd=argv[2];
 if (argc>3)
  select=argv[3]; /* select name is the same as login name */
 else
  select=login;

 /* log into UMS system */
 if ((account=UMSLogin(login,passwd))==NULL)
  {
   fprintf(stderr,"Login failed.\n");
   exit(20);
  }

 /****************************************************************************
   1. For all messages with

       (GlobalFlags & (ViewAccess|PostPoned|Old)) == ViewAccess

      set the local flag bit 0.

      These are all NEW messages, which can be seen by the user.
 *****************************************************************************/

 printf("Looking for new messages: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_SelMask,       UMSUSTATF_ViewAccess |
                                                    UMSUSTATF_PostPoned  |
                                                    UMSUSTATF_Old,
                              UMSTAG_SelMatch,      UMSUSTATF_ViewAccess,

                              UMSTAG_SelWriteLocal, TRUE,
                              UMSTAG_SelSet,        (1L<<0),
                              UMSTAG_SelUnset,      0,
                              TAG_DONE);
 printf("%d found.\n",result);
 CheckErr();

 /****************************************************************************
   2. For all messages with

       ToName field == select name

      set the local flag bit 1.

      These are all messages addressed to the user.
 *****************************************************************************/

 printf("selecting by name: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_WToName,       select,

                              UMSTAG_SelWriteLocal, TRUE,
                              UMSTAG_SelSet,        (1L<<1),
                              UMSTAG_SelUnset,      0,
                              UMSTAG_SelQuick,      TRUE,
                              TAG_DONE);
 printf("%d selected.\n",result);
 CheckErr();

 /****************************************************************************
   3. For all messages with

       (LocalFlags & (Bit 0 and Bit 1)) == (Bit 0 and Bit 1)

      set the local flag bit 4.

      This operation builds the intersection set of the operation 1 and 2,
      thus setting the local flag bit 4 in all new messages, which are
      addressed to the user.
 *****************************************************************************/

 printf("  new messages amongst these: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_SelReadLocal,  TRUE,
                              UMSTAG_SelMask,       (1L<<0) | (1L<<1),
                              UMSTAG_SelMatch,      (1L<<0) | (1L<<1),

                              UMSTAG_SelWriteLocal, TRUE,
                              UMSTAG_SelSet,        (1L<<4),
                              UMSTAG_SelUnset,      0,
                              UMSTAG_SelQuick,      TRUE,
                              TAG_DONE);
 printf("%d selected.\n",result);
 CheckErr();

 /****************************************************************************
   4. For all messages with

       FromName field == select name

      set local flag bit 2.

      These are all messages written by the user.
 *****************************************************************************/

 printf("looking for messages written by you: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_WFromName,     select,

                              UMSTAG_SelWriteLocal, TRUE,
                              UMSTAG_SelSet,        (1L<<2),
                              UMSTAG_SelUnset,      0,
                              UMSTAG_SelQuick,      TRUE,
                              TAG_DONE);
 printf("%d found.\n",result);
 CheckErr();

 /****************************************************************************
   5. For all messages with a parent message and

       (parent's LocalFlags & Bit 2) == Bit 2

      set local flag bit 3.

      These are all replies to messages written by the user.
 *****************************************************************************/

 printf("selecting replies to your msgs: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_SelReadLocal,  TRUE,
                              UMSTAG_SelParent,     TRUE,
                              UMSTAG_SelMask,       (1L<<2),
                              UMSTAG_SelMatch,      (1L<<2),

                              UMSTAG_SelWriteLocal, TRUE,
                              UMSTAG_SelSet,        (1L<<3),
                              UMSTAG_SelUnset,      0,
                              UMSTAG_SelQuick,      TRUE,
                              TAG_DONE);
 printf("%d selected.\n",result);
 CheckErr();

 /****************************************************************************
   6. For all messages with

       (LocalFlags & (Bit 0 and Bit 3)) == (Bit 0 and Bit 3)

      set the local flag bit 4.

      This operation builds the intersection set of the operation 4 and 5,
      thus setting the local flag bit 4 in all new messages, which are
      replies to messages written by the user.
 *****************************************************************************/

 printf("  new messages amongst these: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_SelReadLocal,  TRUE,
                              UMSTAG_SelMask,       (1L<<0) | (1L<<3),
                              UMSTAG_SelMatch,      (1L<<0) | (1L<<3),

                              UMSTAG_SelWriteLocal, TRUE,
                              UMSTAG_SelSet,        (1L<<4),
                              UMSTAG_SelUnset,      0,
                              UMSTAG_SelQuick,      TRUE,
                              TAG_DONE);
 printf("%d selected.\n",result);
 CheckErr();

 /****************************************************************************
   7. For all messages with

       (LocalFlags & Bit 4) == Bit 4

      set the selected user flag.

      This operation builds the union set of the operation 3 and 6, thus
      setting the selected user flag in all new messages, which are either
      messages addressed to the user or replies to messages written by the
      user.
      The news reader will now show these messages first.
 *****************************************************************************/

 printf("totally selected: ");
 fflush(stdout);
 result=UMSSelectTags(account,UMSTAG_SelReadLocal,  TRUE,
                              UMSTAG_SelMask,       (1L<<4),
                              UMSTAG_SelMatch,      (1L<<4),

                              UMSTAG_SelSet,        UMSUSTATF_Selected,
                              UMSTAG_SelUnset,      0,
                              TAG_DONE);
 printf("%d.\n",result);
 CheckErr();

 UMSLogout(account);
 return(0);
}
