/************************************************************************/
/*         SumNew:  Print a summary of new messages to stdout.          */
/************************************************************************/

/*
 * Demo sent by: SteveX@omx.OCUnix.On.Ca (Steve Tibbett)
 *
 * Adapted to DICE by stefanb
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <exec/types.h>
#include <clib/ums_protos.h>
#include <clib/exec_protos.h>
#include <pragmas/ums_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <dos/dos.h>

UMSUserAccount Handle;
struct Library *UMSBase;
extern struct Library *SysBase;

struct Subject
        {
        struct Subject *Next;
        long Count;
        char *Text;
        } *Subjects;

/************************************************************************/
/*           This gets called just before the program exits.            */
/************************************************************************/
void
cleanup(void)
{
if (Handle) UMSLogout(Handle);
if (UMSBase) CloseLibrary(UMSBase);
}

/************************************************************************/
/*             This gets jumped to whenever Ctrl-C is hit.              */
/************************************************************************/
void
breaker(void)
{
printf("^C\n");
fflush(stdout);
}

/************************************************************************/
/*               If there is an error, print it and exit.               */
/************************************************************************/
void
CheckErr(void)
{
UMSError err;

err=UMSErrNum(Handle);

if (err != UMSERR_OK)
        {
        fprintf(stderr,"UMS-Error: %3d, '%s'\n",err,UMSErrTxt(Handle));
        exit(20);
        }
}

/************************************************************************/
/*                Add this subject to the subject list.                 */
/************************************************************************/
void
AddSubjectToList(char *SubText)
{
struct Subject *Sub;
struct Subject *NewSub;

Sub=Subjects;
while (Sub)
        {
        if (stricmp(SubText, Sub->Text)==0)
                {
                Sub->Count++;
                return;
                };

        Sub=Sub->Next;
        };

NewSub=malloc(strlen(SubText)+1+sizeof(struct Subject));
if (NewSub==0) return;

NewSub->Text=(char *)(NewSub+1);
strcpy(NewSub->Text, SubText);
NewSub->Count=1;

if (Subjects==0)
        {
        Subjects=NewSub;
        NewSub->Next=0;
        } else
        {
        if (stricmp(NewSub->Text, Subjects->Text)<0)
                {
                NewSub->Next=Subjects;
                Subjects=NewSub;
                } else
                {
                Sub=Subjects;
                while (Sub->Next)
                        {
                        if (stricmp(NewSub->Text, Sub->Next->Text)<0)
                                {
                                NewSub->Next=Sub->Next;
                                Sub->Next=NewSub;
                                break;
                                };

                        Sub=Sub->Next;
                        };

                if (Sub->Next==0)
                        {
                        Sub->Next=NewSub;
                        NewSub->Next=0;
                        };
                };
        };
}

/************************************************************************/
/*                      Main entry/exit and loop                        */
/************************************************************************/
int
main(int argc, char *argv[])
{
char *login,*passwd;
UMSMsgNum result;
char SubjLine[256];

if (argc!=3)
        {
        printf("Usage: %s \"User Name\" \"Password\"\n", argv[0]);
        exit(0);
        };

onbreak(breaker);

UMSBase=OpenLibrary("ums.library", 9);
if (UMSBase==0)
        {
        fprintf(stderr, "Error: Can't open ums.library.\n");
        exit(0);
        };

login=argv[1];
passwd=argv[2];

if ((Handle=UMSLogin(login,passwd))==NULL)
        {
        fprintf(stderr,"UMS login failed.\n");
        exit(20);
        }

/****************************************************************************

   1. Flag all new messages.

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

      Set the local flag bit 0.

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

*****************************************************************************/

printf("Searching...");
fflush(stdout);

result=UMSSelectTags(Handle,
        UMSTAG_SelMask,         UMSUSTATF_ViewAccess|UMSUSTATF_PostPoned|UMSUSTATF_Old,
        UMSTAG_SelMatch,        UMSUSTATF_ViewAccess,
        UMSTAG_SelWriteLocal,   TRUE,
        UMSTAG_SelSet,          (1L<<0),
        UMSTAG_SelUnset,        0,
        TAG_DONE);

CheckErr();
if (result==0)
        printf("No new messages.");
        else printf("%d new messages found.  Summarizing...\n", result);


/****************************************************************************

   2. Read in all the subjects.

*****************************************************************************/

if (result)
        {
        long Succ;
        long HdrLength;
        long TextLength;
        char *Subject;
        char *Group;
        struct Subject *Sub;

        result=0;

        while (TRUE)
                {
                result=UMSSearchTags(Handle,
                        UMSTAG_SearchLast, result,
                        UMSTAG_SearchDirection, 1,
                        UMSTAG_SearchLocal, TRUE,
                        UMSTAG_SearchMask, (1L<<0),
                        UMSTAG_SearchMatch, (1L<<0),
                        TAG_DONE);

                if (result==0)
                        break;

                Succ=ReadUMSMsgTags(Handle,
                        UMSTAG_RMsgNum, result,
                        UMSTAG_RHeaderLength, &HdrLength,
                        UMSTAG_RTextLength, &TextLength,
                        UMSTAG_RNoUpdate, 1,
                        UMSTAG_RSubject, &Subject,
                        UMSTAG_RGroup, &Group,
                        TAG_DONE);


                if (Succ)
                        {
                        sprintf(SubjLine, "%-33s  In: %s", Subject, Group?Group:":::Mail:::");
                        AddSubjectToList(SubjLine);
                        } else
                        printf("UMS-Error: %3d, '%s'\n",UMSErrNum(Handle),UMSErrTxt(Handle));

                if (SetSignal(0,0)&SIGBREAKF_CTRL_C)
                        {
                        cleanup();
                        exit(0);
                        }
                };

        Sub=Subjects;
        while (Sub)
                {
                printf("%4d: %s\n", Sub->Count, Sub->Text);
                Sub=Sub->Next;
                };
        };

cleanup();
return(0);
}
