/* UMSWatch */

//+ Defines & Includes
#include    <dos/dos.h>
#include    <dos/dosextens.h>
#include    <dos/rdargs.h>
#include    <exec/exec.h>
#include    <intuition/intuition.h>

#include    <clib/alib_protos.h>
#include    <clib/dos_protos.h>
#include    <clib/exec_protos.h>
#include    <clib/intuition_protos.h>
#include    <clib/ums_protos.h>

#include    <stdio.h>
#include    <string.h>
#include    <stdarg.h>
#include    <time.h>

#include    <pragmas/ums_pragmas.h>

#include    "libraries/UMS.h"
#include    "DLIB:UMSMatchConfigTags.c"
#include    "DLIB:UMSReadMsgTags.c"
#include    "DLIB:UMSWriteMsgTags.c"
#include    "DLIB:UMSReadConfigTags.c"
#include    "DLIB:UMSWriteConfigTags.c"
#include    "DLIB:UMSSearchTags.c"
#include    "DLIB:UMSSelectTags.c"

#define     STRING_LENGTH       60
#define     FAIL                20

enum long
{
    NAME,
    PASSWORD,
    SERVER,
    VECFILE,
    WINDOWX,
    WINDOWY,
    SIZE,
    AUTOPING,
    PRIORITY,
    DRAGBAR,
    RD_Size
};
//-
//+ Consts
const char  *Version  = "$VER: UMSWatch 1.3 (4.01.96)";

const char  *RD_Template =
{
    "N=NAME,"
    "P=PASSWORD,"
    "S=SERVER/K,"
    "V=VECFILE,"
    "X=WINDOWX/N,"
    "Y=WINDOWY/N,"
    "I=SIZE/N,"
    "A=AUTOPING/N,"
    "R=PRIORITY/N,"
    "D=DRAGBAR/S"
};

const long  Sinus[] =
{
         0,    571,   1141,   1711,   2281,   2850,   3418,   3985,
      4550,   5115,   5678,   6239,   6798,   7355,   7910,   8463,
      9013,   9560,  10104,  10645,  11184,  11718,  12249,  12777,
     13300,  13820,  14335,  14846,  15352,  15854,  16351,  16843,
     17330,  17812,  18288,  18759,  19224,  19683,  20136,  20583,
     21024,  21459,  21887,  22308,  22722,  23130,  23531,  23924,
     24311,  24690,  25061,  25425,  25781,  26129,  26470,  26802,
     27126,  27442,  27750,  28049,  28340,  28622,  28896,  29160,
     29416,  29663,  29901,  30130,  30350,  30560,  30761,  30953,
     31136,  31309,  31473,  31627,  31771,  31906,  32031,  32147,
     32252,  32348,  32435,  32511,  32577,  32634,  32680,  32717,
     32744,  32761,  32768,  32765,  32752,  32729,  32696,  32654,
     32601,  32538,  32466,  32384,  32292,  32190,  32079,  31957,
     31826,  31686,  31535,  31376,  31206,  31027,  30839,  30642,
     30435,  30219,  29993,  29759,  29516,  29263,  29002,  28732,
     28454,  28166,  27870,  27566,  27253,  26932,  26603,  26266,
     25921,  25568,  25207,  24839,  24463,  24079,  23689,  23291,
     22886,  22474,  22056,  21630,  21198,  20760,  20315,  19865,
     19408,  18945,  18477,  18003,  17523,  17038,  16548,  16053,
     15553,  15049,  14540,  14026,  13508,  12986,  12460,  11931,
     11397,  10861,  10321,   9778,   9232,   8683,   8131,   7577,
      7021,   6462,   5902,   5340,   4776,   4211,   3644,   3077,
      2508,   1939,   1369,    799,    228,   -342,   -913,  -1483,
     -2053,  -2622,  -3190,  -3758,  -4324,  -4889,  -5452,  -6014,
     -6574,  -7132,  -7688,  -8242,  -8793,  -9341,  -9887, -10429,
    -10968, -11504, -12037, -12566, -13091, -13612, -14129, -14642,
    -15150, -15654, -16153, -16647, -17136, -17619, -18098, -18571,
    -19038, -19500, -19955, -20405, -20848, -21285, -21716, -22140,
    -22557, -22968, -23371, -23767, -24157, -24539, -24913, -25280,
    -25639, -25990, -26334, -26670, -26997, -27316, -27627, -27930,
    -28224, -28510, -28787, -29055, -29314, -29565, -29806, -30039,
    -30262, -30477, -30682, -30877, -31064, -31240, -31408, -31566,
    -31714, -31853, -31982, -32101, -32211, -32311, -32401, -32481,
    -32551, -32612, -32662, -32703, -32734, -32755, -32766, -32767,
    -32758, -32739, -32710, -32671, -32623, -32564, -32496, -32418,
    -32329, -32232, -32124, -32006, -31879, -31743, -31596, -31440,
    -31275, -31100, -30915, -30721, -30518, -30306, -30084, -29853,
    -29614, -29365, -29107, -28841, -28566, -28282, -27989, -27688,
    -27379, -27061, -26735, -26402, -26060, -25710, -25352, -24987,
    -24614, -24233, -23846, -23451, -23048, -22639, -22223, -21801,
    -21371, -20936, -20494, -20045, -19591, -19130, -18664, -18192,
    -17715, -17232, -16744, -16251, -15753, -15251, -14743, -14232,
    -13715, -13195, -12671, -12143, -11611, -11075, -10537,  -9995,
     -9450,  -8902,  -8352,  -7799,  -7243,  -6686,  -6126,  -5565,
     -5001,  -4437,  -3871,  -3303,  -2735,  -2166,  -1597,  -1026,
      -456,    114,    685,   1255,   1825,   2395,   2963,   3531,
      4098,   4664,   5228,   5790,   6351,   6910,   7466,   8021,
      8573,   9122,   9669,  10213,  10753,  11291,  11825,  12355,
     12882,  13404,  13923,  14438,  14948,  15453,  15954,  16450,
     16941,  17427,  17907,  18383,  18852,  19316,  19774,  20226,
     20672,  21111,  21545,  21971,  22391,  22805,  23211,  23610,
     24002,  24387,  24764,  25134,  25497,  25851,  26198,  26537,
     26867,  27190,  27504,  27810,  28108,  28397,  28677,  28949,
     29212,  29466,  29711,  29947,  30175,  30392,  30601,  30801,
     30991,  31171,  31343,  31504,  31656,  31799,  31932,  32055,
     32169,  32272,  32366,  32451,  32525,  32589,  32644,  32689,
     32723,  32748,  32763,  32768,  32763
};
//-
//+ Variables
char    LoginName[ STRING_LENGTH ];
char    LoginServer[ STRING_LENGTH ];
char    LoginPassword[ STRING_LENGTH ];
char    VecFileName[ STRING_LENGTH ];

char    PortName[ 2 * STRING_LENGTH ];
char    WindowText[ STRING_LENGTH ];
char    TitleText[ 2 * STRING_LENGTH ];

char    *ToName = NULL;

struct Library  *UMSBase    = NULL;
struct RDArgs   *RD_Struct  = NULL;
struct Window   *Window     = NULL;
struct Screen   *PubScreen  = NULL;

struct MsgPort  *WatchPort  = NULL;
struct Message  *WatchMessage;

long    RD_Array[ RD_Size ];

UMSUserAccount  Account     = NULL;

BPTR    StdOutputHandle;

long    WindowX = 0;
long    WindowY = 0;
long    WindowSize    = 170;
long    WindowMiddle  = 85;
long    DragSize = 0;

long    AutoPing        = 0;
long    AutoPingCount   = 0;

long    Priority    = -100;

long    Mails;
long    Folder;
long    News;

long    MsgNumber;
long    MsgChain;
long    NewMsgChain;

long    *Edge           = NULL;
long    *OrigPoint      = NULL;
long    *RealPoint      = NULL;
long    *OldRealPoint   = NULL;

long    RotateX;
long    RotateY;
long    RotateZ;

long    Mode    = 0;
long    Points  = 0;
long    Edges   = 0;

BOOL    DragBar = FALSE;
//-

char *stristr( char *, char *);

//+ main()
main()
{
    Open_All();

    CheckMails();

    while( TRUE)
    {
        if( WatchMessage = GetMsg( WatchPort))
        {
            FreeVec( WatchMessage);

            CheckMails();
        }

        if( AutoPing)
        {
            if( !( AutoPingCount--))
            {
                AutoPingCount = AutoPing;

                if( !( UMSServerControl( LoginServer, UMSSC_Ping)))   CheckMails();
            }
        }

        BuildDisplay();

        if( ++RotateX == 360) RotateX = 0;
        if( ++RotateY == 360) RotateY = 0;
        if( ++RotateZ == 360) RotateZ = 0;

        chkabort();

        Delay( 1L);
    }

    Close_All( NULL);
}
//-

//+ C_Break()
C_Break()
{
    Close_All( NULL);
} //-
//+Open_All()
Open_All()
{
    onbreak( C_Break);

    if( !( StdOutputHandle = Output()))
        Close_All( FAIL);

    if( !( UMSBase = ( struct Library *) OpenLibrary( "ums.library", 11L)))
    {
        FPuts( StdOutputHandle, "can`t open ums.library V11+!\n");
        Close_All( FAIL);
    }

    if( !( RD_Struct = ReadArgs( RD_Template, RD_Array, NULL)))
    {
        FPuts( StdOutputHandle, "required argument missing!\n");
        Close_All( FAIL);
    }

    CheckArgs();

    sprintf( PortName, "User: %s Server: %s", LoginName, LoginServer);

    Forbid();

    if( FindPort( PortName))
    {
        Permit();

        FPrintf( StdOutputHandle, "UMSWatch \x22%s\x22 is already running!\n", PortName);
        Close_All( FAIL);
    }

    if( !( WatchPort = CreateMsgPort()))
    {
        Permit();

        FPuts( StdOutputHandle, "can't create msgport!\n");
        Close_All( FAIL);
    }

    WatchPort->mp_Node.ln_Name = PortName;
    AddPort( WatchPort);

    Permit();

    if( !( PubScreen = LockPubScreen( NULL)))
    {
        FPuts( StdOutputHandle, "can't lock pubscreen!\n");
        Close_All( FAIL);
    }

    if( DragBar) DragSize = PubScreen->BarHeight - 1;

    if( !( Window = OpenWindowTags( NULL,   WA_Left,            WindowX,
                                            WA_Top,             WindowY,
                                            WA_InnerWidth,      WindowSize,
                                            WA_InnerHeight,     WindowSize,
                                            WA_DragBar,         DragBar,
                                            WA_CloseGadget,     FALSE,
                                            WA_DepthGadget,     DragBar,
                                            WA_RMBTrap,         TRUE,
/*                                            WA_IDCMP,           IDCMP_CLOSEWINDOW, */
                                            WA_Title,           NULL,
                                            WA_AutoAdjust,      FALSE,
                                            WA_Activate,        FALSE,
                                            WA_PubScreen,       PubScreen,
                                            WA_SimpleRefresh,   TRUE,
                                            WA_NoCareRefresh,   TRUE,
                                            TAG_DONE)))
    {
        FPuts( StdOutputHandle, "can't open window!\n");
        Close_All( FAIL);
    }

    PrepareObject();

    SetTaskPri( FindTask( NULL), Priority);
}
//-
//+Close_All( int ExitError)
Close_All( int ExitError)
{
    if( RD_Struct)          FreeArgs( RD_Struct);

    if( WatchPort)
    {
        RemPort( WatchPort);

        while( WatchMessage = GetMsg( WatchPort)) FreeVec( WatchMessage);

        DeleteMsgPort( WatchPort);
    }

    if( Edge)               FreeVec( Edge);
    if( OrigPoint)          FreeVec( OrigPoint);
    if( RealPoint)          FreeVec( RealPoint);
    if( OldRealPoint)       FreeVec( OldRealPoint);

    if( PubScreen)          UnlockPubScreen( NULL, PubScreen);
    if( Window)             CloseWindow( Window);

    if( Account)            UMSLogout( Account);
    if( ToName)             FreeUMSConfig( Account, ToName);
    if( UMSBase)            CloseLibrary( UMSBase);

    exit( ExitError);
}
//-

//+ PrepareObject()
PrepareObject()
{
    BPTR    VecFileHandle;
    char    *VecBuffer;
    char    *TempPointer_1;
    char    *TempPointer_2;

    long    Count;

    struct FileInfoBlock    *VecFileInfoBlock;

    if( VecFileHandle = Open( VecFileName, MODE_OLDFILE))
    {
        if( VecFileInfoBlock = ( long *) AllocVec( sizeof( struct FileInfoBlock), MEMF_ANY|MEMF_CLEAR))
        {
            if( ExamineFH( VecFileHandle, VecFileInfoBlock))
            {
                if( VecBuffer = ( long *) AllocVec( VecFileInfoBlock->fib_Size + 1, MEMF_ANY|MEMF_CLEAR))
                {
                    if( VecFileInfoBlock->fib_Size == Read( VecFileHandle, VecBuffer, VecFileInfoBlock->fib_Size))
                    {
                        if( TempPointer_1 = stristr( VecBuffer, "Mode"))
                        {
                            TempPointer_1 += 4;   /* strlen( "Mode") = 4 */

                            while( isspace( *( ++TempPointer_1)));
                            Mode = atol( TempPointer_1);
                        }

                        if( TempPointer_1 = stristr( VecBuffer, "Points"))
                        {
                            TempPointer_1 += 6;   /* strlen( "Points") = 6 */

                            while( isspace( *( ++TempPointer_1)));
                            Points = atol( TempPointer_1) * 3;

                            if( TempPointer_2 = stristr( TempPointer_1, ";"))
                            {
                                if( !( OrigPoint = ( long *) AllocVec( Points * 4, MEMF_ANY|MEMF_CLEAR)))   Close_All( FAIL);

                                Count = 0;

                                while(( Count != Points) && ( TempPointer_1 = stristr( TempPointer_1, ",")) && ( TempPointer_1 < TempPointer_2))
                                {
                                    TempPointer_1++;

                                    while( isspace( *( ++TempPointer_1)));
                                    OrigPoint[ Count ] = atol( TempPointer_1) * WindowSize / 362;

                                    Count++;
                                }
                            }
                        }

                        if( TempPointer_1 = stristr( VecBuffer, "Edges"))
                        {
                            TempPointer_1 += 5;   /* strlen( "Edges") = 5 */

                            while( isspace( *( ++TempPointer_1)));
                            Edges = atol( TempPointer_1) * 4;

                            if( TempPointer_2 = stristr( TempPointer_1, ";"))
                            {
                                if( !( Edge = ( long *) AllocVec( Edges * 4, MEMF_ANY|MEMF_CLEAR)))   Close_All( FAIL);

                                Count = 0;

                                while(( Count != Edges) && ( TempPointer_1 = stristr( TempPointer_1, ",")) && ( TempPointer_1 < TempPointer_2))
                                {
                                    TempPointer_1++;

                                    while( isspace( *( ++TempPointer_1)));

                                    Edge[ Count ] = atol( TempPointer_1) * 3;

                                    if(( Edge[ Count ] >= Points) || ( Edge[ Count ] < 0)) Edge[ Count ] = 0;

                                    Count++;
                                }
                            }
                        }
                    }
                    else FPuts( StdOutputHandle, "can`t read vecfile!");

                    FreeVec( VecBuffer);
                }
                else FPuts( StdOutputHandle, "out of memory!");
            }
            else FPuts( StdOutputHandle, "suspicious error!");

            FreeVec( VecFileInfoBlock);
        }
        else FPuts( StdOutputHandle, "out of memory!");

        Close( VecFileHandle);
    }
    else FPuts( StdOutputHandle, "can`t open vecfile!");

    if( !( RealPoint    = ( long *) AllocVec( Points * 4, MEMF_ANY|MEMF_CLEAR)))   Close_All( FAIL);
    if( !( OldRealPoint = ( long *) AllocVec( Points * 4, MEMF_ANY|MEMF_CLEAR)))   Close_All( FAIL);
}
//-

//+ Rotate()
Rotate()
{
    long    Count;

    long    CosX, CosY, CosZ;
    long    SinX, SinY, SinZ;
    long    BufferX, BufferY, BufferZ;
    long    RealX, RealY, RealZ;

    CosX    = Sinus[ RotateX + 90 ];
    CosY    = Sinus[ RotateY + 90 ];
    CosZ    = Sinus[ RotateZ + 90 ];
    SinX    = Sinus[ RotateX ];
    SinY    = Sinus[ RotateY ];
    SinZ    = Sinus[ RotateZ ];

    Count = Points;

    while( Count)
    {
        Count -= 3;

        /* X-rotation */
        BufferZ = ( OrigPoint[ Count + 2 ] * CosX - OrigPoint[ Count + 1 ] * SinX) >> 15;
        BufferY = ( OrigPoint[ Count + 2 ] * SinX + OrigPoint[ Count + 1 ] * CosX) >> 15;

        /* Y-rotation */
        BufferX = ( OrigPoint[ Count ] * CosY - BufferZ * SinY) >> 15;
        RealZ   = ( OrigPoint[ Count ] * SinY + BufferZ * CosY) >> 15;

        /* Z-rotation */
        RealX   = ( BufferX * CosZ - BufferY * SinZ) >> 15;
        RealY   = ( BufferY * CosZ + BufferX * SinZ) >> 15;

        OldRealPoint[ Count ]     = RealPoint[ Count ];
        OldRealPoint[ Count + 1 ] = RealPoint[ Count + 1 ];

        RealPoint[ Count ]     = (  RealX + WindowMiddle);
        RealPoint[ Count + 1 ] = ( -RealY + WindowMiddle + DragSize);
    }
}
//-
//+ BuildDisplay()
BuildDisplay()
{
    long    Count = 0;

    long    AXDiff  = 0;
    long    AYDiff  = 0;
    long    BXDiff  = 0;
    long    BYDiff  = 0;

    SHORT   Poly[ 8 ];
    SHORT   OldPoly[ 8 ];

    long    Sight;
    long    OldSight;

    Rotate();

    Count = Edges;

    if( TextLength( Window->RPort, WindowText, strlen( WindowText)) < WindowSize)
    {
        SetAPen( Window->RPort, 3);
        Move( Window->RPort, 5, WindowSize + DragSize - 1);

        if( TextLength( Window->RPort, TitleText, strlen( TitleText)) < WindowSize)
            Text( Window->RPort, TitleText, strlen( TitleText));
        else
            Text( Window->RPort, WindowText, strlen( WindowText));
    }

    while( Count)
    {
        Count -= 4;

        if( Mode == 0)
        {
            AXDiff = OldRealPoint[ Edge[ Count + 1 ]]      - OldRealPoint[ Edge[ Count ]];
            AYDiff = OldRealPoint[ Edge[ Count + 1 ] + 1 ] - OldRealPoint[ Edge[ Count ] + 1 ];
            BXDiff = OldRealPoint[ Edge[ Count + 3 ]]      - OldRealPoint[ Edge[ Count ]];
            BYDiff = OldRealPoint[ Edge[ Count + 3 ] + 1 ] - OldRealPoint[ Edge[ Count ] + 1 ];
            OldSight = AXDiff * BYDiff - AYDiff * BXDiff;
        }

        if(( Mode == 1) || ( OldSight > 0))
        {
            OldPoly[ 0 ] = OldRealPoint[ Edge[ Count + 1 ]];
            OldPoly[ 1 ] = OldRealPoint[ Edge[ Count + 1 ] + 1 ];
            OldPoly[ 2 ] = OldRealPoint[ Edge[ Count + 2 ]];
            OldPoly[ 3 ] = OldRealPoint[ Edge[ Count + 2 ] + 1 ];
            OldPoly[ 4 ] = OldRealPoint[ Edge[ Count + 3 ]];
            OldPoly[ 5 ] = OldRealPoint[ Edge[ Count + 3 ] + 1 ];
            OldPoly[ 6 ] = OldRealPoint[ Edge[ Count ]];
            OldPoly[ 7 ] = OldRealPoint[ Edge[ Count ] + 1];
        }

        if( Mode == 0)
        {
            AXDiff = RealPoint[ Edge[ Count + 1 ]]      - RealPoint[ Edge[ Count ]];
            AYDiff = RealPoint[ Edge[ Count + 1 ] + 1 ] - RealPoint[ Edge[ Count ] + 1 ];
            BXDiff = RealPoint[ Edge[ Count + 3 ]]      - RealPoint[ Edge[ Count ]];
            BYDiff = RealPoint[ Edge[ Count + 3 ] + 1 ] - RealPoint[ Edge[ Count ] + 1 ];
            Sight = AXDiff * BYDiff - AYDiff * BXDiff;
        }

        if(( Mode == 1) || ( Sight > 0))
        { 
            Poly[ 0 ] = RealPoint[ Edge[ Count + 1 ]];
            Poly[ 1 ] = RealPoint[ Edge[ Count + 1 ] + 1 ];
            Poly[ 2 ] = RealPoint[ Edge[ Count + 2 ]];
            Poly[ 3 ] = RealPoint[ Edge[ Count + 2 ] + 1 ];
            Poly[ 4 ] = RealPoint[ Edge[ Count + 3 ]];
            Poly[ 5 ] = RealPoint[ Edge[ Count + 3 ] + 1 ];
            Poly[ 6 ] = RealPoint[ Edge[ Count ]];
            Poly[ 7 ] = RealPoint[ Edge[ Count ] + 1];
        }

        if(( Mode == 1) || ( OldSight > 0))
        {
            SetAPen( Window->RPort, 0);

            Move( Window->RPort, OldPoly[ 6 ], OldPoly[ 7 ]);

            PolyDraw( Window->RPort, 4, OldPoly);
        }

        if((( Mode == 1) || Sight > 0))
        {
            if( Mails)
                SetAPen( Window->RPort, 2);
            else
                SetAPen( Window->RPort, 1);

            Move( Window->RPort, Poly[ 6 ], Poly[ 7 ]);

            PolyDraw( Window->RPort, 4, Poly);
        }
    }
}
//-

//+ CheckMails()
CheckMails()
{
    if(( Account = UMSRLogin( LoginServer, LoginName, LoginPassword)) > NULL)
    {
        if( ToName = ReadUMSConfigTags( Account, UMSTAG_CfgUserName, LoginName,
                                                 TAG_DONE))
        {
            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelSet,          0,
                                    UMSTAG_SelUnset,        ( 1L<<0) | ( 1L<<1) | ( 1L<<2) | ( 1L<<3) | ( 1L<<4),
                                    TAG_DONE);

            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelSet,          ( 1L<<0),
                                    UMSTAG_SelUnset,        0,
                                    UMSTAG_SelMask,         UMSUSTATF_Owner,
                                    UMSTAG_SelMatch,        UMSUSTATF_Owner,
                                    TAG_DONE);

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

            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelSet,          ( 1L<<2),
                                    UMSTAG_SelUnset,        0,
                                    UMSTAG_WGroup,          "",
                                    TAG_DONE);

            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelSet,          ( 1L<<3),
                                    UMSTAG_SelUnset,        0,
                                    UMSTAG_WFolder,         "",
                                    TAG_DONE);

            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelSet,          ( 1L<<4),
                                    UMSTAG_SelUnset,        0,
                                    UMSTAG_WToName,         ToName,
                                    TAG_DONE);
/*
            do
            {
                if( MsgNumber = UMSSearchTags( Account, UMSTAG_SearchLocal, TRUE,
                                                        UMSTAG_SearchLast,  MsgNumber,
                                                        UMSTAG_SearchMask,  ( 1L<<0),
                                                        UMSTAG_SearchMatch, ( 1L<<0),
                                                        TAG_DONE))
                {
                    UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                            UMSTAG_SelSet,          ( 1L<<4),
                                            UMSTAG_SelUnset,        0,
                                            UMSTAG_SelSubTree,      MsgNumber,
                                            TAG_DONE);
                }
            }
            while( MsgNumber);
*/
/*
            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelReadLocal,    TRUE,
                                    UMSTAG_SelSet,          0,
                                    UMSTAG_SelUnset,        ( 1L<<4),
                                    UMSTAG_SelMask,         ( 1L<<2),
                                    UMSTAG_SelMatch,        ( 1L<<2),
                                    TAG_DONE);
*/
/*
            do
            {
                if( MsgNumber = UMSSearchTags( Account, UMSTAG_SearchLocal, TRUE,
                                                        UMSTAG_SearchLast,  MsgNumber,
                                                        UMSTAG_SearchMask,  ( 1L<<0) | ( 1L<<2),
                                                        UMSTAG_SearchMatch, ( 1L<<0) | ( 0L<<2),
                                                        TAG_DONE))
                {
                    if( ReadUMSMsgTags( Account, UMSTAG_RMsgNum,  MsgNumber,
                                                 UMSTAG_RChainDn, &MsgChain,
                                                 TAG_DONE))
                    {
                        FreeUMSMsg( Account, MsgNumber);

                        while( MsgChain)
                        {
                            UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                                    UMSTAG_SelSet,          ( 1L<<4),
                                                    UMSTAG_SelUnset,        0,
                                                    UMSTAG_SelMsg,          MsgChain,
                                                    TAG_DONE);

                            if( ReadUMSMsgTags( Account, UMSTAG_RMsgNum,  MsgChain,
                                                         UMSTAG_RChainLt, &NewMsgChain,
                                                         TAG_DONE))
                            {
                                FreeUMSMsg( Account, MsgChain);
                                NewMsgChain = 0x00;
                            }

                            MsgChain = NewMsgChain;
                        }
                    }
                }
            }
            while( MsgNumber);
*/

            printf( "%d\n", UMSSelectTags( Account, UMSTAG_SelParent,       TRUE,
                                    UMSTAG_SelMask,         UMSUSTATF_Owner,
                                    UMSTAG_SelMatch,        UMSUSTATF_Owner,
                                    UMSTAG_SelWriteLocal,   TRUE,
                                    UMSTAG_SelSet,          ( 1L<<4),
                                    TAG_DONE));

            Mails =  UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                             UMSTAG_SelReadLocal,    TRUE,
                                             UMSTAG_SelSet,          0,
                                             UMSTAG_SelUnset,        0,
                                             UMSTAG_SelMask,         ( 1L<<0) | ( 1L<<1) | ( 1L<<2) | ( 1L<<3),
                                             UMSTAG_SelMatch,        ( 0L<<0) | ( 1L<<1) | ( 1L<<2) | ( 1L<<3),
                                             TAG_DONE);

            Folder = UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                             UMSTAG_SelReadLocal,    TRUE,
                                             UMSTAG_SelSet,          0,
                                             UMSTAG_SelUnset,        0,
                                             UMSTAG_SelMask,         ( 1L<<0) | ( 1L<<1) | ( 1L<<2) | ( 1L<<3),
                                             UMSTAG_SelMatch,        ( 0L<<0) | ( 1L<<1) | ( 1L<<2) | ( 0L<<3),
                                             TAG_DONE);

            News  =  UMSSelectTags( Account, UMSTAG_SelWriteLocal,   TRUE,
                                             UMSTAG_SelReadLocal,    TRUE,
                                             UMSTAG_SelSet,          0,
                                             UMSTAG_SelUnset,        0,
                                             UMSTAG_SelMask,         ( 1L<<0) | ( 1L<<1) | ( 1L<<2) | ( 1L<<4),
                                             UMSTAG_SelMatch,        ( 0L<<0) | ( 1L<<1) | ( 0L<<2) | ( 1L<<4),
                                             TAG_DONE);

            FreeUMSConfig( Account, ToName);
            ToName = NULL;
        }
        else
        {
            FPuts( StdOutputHandle, "can`t read realname!");
            Close_All( FAIL);
        }

        UMSLogout( Account);
        Account = NULL;
    }
    else
    {
        Account = NULL;
        FPuts( StdOutputHandle, "can`t login!");
        Close_All( FAIL);
    }

    sprintf( TitleText, "Mails: %lu News: %lu Foldered: %lu   ", Mails, News, Folder);
    sprintf( WindowText, "M: %lu N: %lu F: %lu", Mails, News, Folder);

    Move( Window->RPort, 5, WindowSize + DragSize - 1);
    ClearEOL( Window->RPort);
    RefreshWindowFrame( Window);

    if( DragBar) SetWindowTitles( Window, TitleText, TitleText);
    else         SetWindowTitles( Window, NULL,      TitleText);
} //-
//+ CheckArgs()
CheckArgs()
{
    if( RD_Array[ NAME ]) strncpy( LoginName, ( char *) RD_Array[ NAME ], STRING_LENGTH);
    else strcpy( LoginName, "Sysop");

    if( RD_Array[ PASSWORD ]) strncpy( LoginPassword, ( char *) RD_Array[ PASSWORD ], STRING_LENGTH);
    else LoginPassword[ 0 ] = 0x00;

    if( RD_Array[ SERVER ]) strncpy( LoginServer, ( char *) RD_Array[ SERVER ], STRING_LENGTH);
    else strcpy( LoginServer, "default");

    if( RD_Array[ VECFILE ]) strncpy( VecFileName, ( char *) RD_Array[ VECFILE ], STRING_LENGTH);
    else strcpy( VecFileName, "UMSWatch.vec");

    if( RD_Array[ WINDOWX ])    WindowX     = *(( long *) RD_Array[ WINDOWX ]);
    if( RD_Array[ WINDOWY ])    WindowY     = *(( long *) RD_Array[ WINDOWY ]);
    if( RD_Array[ SIZE ])       WindowSize  = *(( long *) RD_Array[ SIZE ]);

    if( RD_Array[ PRIORITY ])   Priority    = *(( long *) RD_Array[ PRIORITY ]);

    if( RD_Array[ AUTOPING ])   AutoPingCount = AutoPing = ( *(( long *) RD_Array[ AUTOPING ]) * 10);

    if( RD_Array[ DRAGBAR ])    DragBar     = TRUE;

    if( WindowSize < 10) WindowSize = 175;

    WindowMiddle = WindowSize / 2 + 4;
} //-

//+ char *stristr( char *SrcString, char *DstString)
char *stristr( char *SrcString, char *DstString)
{
    short DstLen = strlen( DstString);

    if( DstLen == 0) return( SrcString);

    while( *SrcString)
    {
        if( *SrcString == *DstString && strnicmp( SrcString, DstString, DstLen) == 0) return( SrcString);

        SrcString++;
    }
    return(NULL);
} //-
