/* Zeus Who's Online v1.1 (c)1996 by Rod Schnell
 *
 */


#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define	USE_DOORLIB

#include <proto/pipeline.h>
#include <proto/pipeutil.h>
#include <nodes.h>

struct Library 		*PipelineBase;
struct Library		*PipeUtilBase;
struct SharedData	*Shared;
long					 LineNumber;
ULONG					 WriteFlags;

/* creates a random number in interval [from;to] */
/* not used
int	number(int from, int to)
{
	/* ensure we don't divide by 0 or a negative number! */
	if(to <= from) to = (from + 1);

	rand();
	rand();
	rand();
	rand();
	rand();
	rand();
	rand();
	
	return(((rand() / (RAND_MAX/((to-from) + 1)))) + from);
}
*/

/* GetWhoNotes */
/* not used 
VOID GetWhoNotes( UBYTE *username  )
{
  ULONG len, count, pick;
  UBYTE *tx, *tbuff, tpath[108];
  BOOL success = FALSE;
	
	LPuts("[1;37m");
	strncpy(tpath, Shared->Paths->User, 50);
	if(AddPart(tpath, username, 108))
	{
		/* Turn any spaces in name into underscores */
		tx = tpath;
		count = 0;
		while ( *tx != '\0' )
		{
			if ( *tx == ' ' )
			{
				*tx = '_';
				count++;
			}
			tx++;
		}

		if(AddPart(tpath, "WhoNotes", 108))
		{
			tx = tbuff = ReadWholeFile ( tpath, &len ) ;
			if ( tbuff )
			{
				while ( *tx != '\0' )
				{
					if ( *tx == '\n' )
					{
						if(*(tx+1) != '\0')
							count++;
						*tx = '\0';
					}
					tx++;
				}

				/* pick a random number between 1 and count  (textline number) */
				pick = number(1, count);

				count = 1;

				/* Be very carefull with this next loop, has potential to read    */
				/* beyond the end of the buffer if pick variable is set too high! */
				
				tx = tbuff;
				while ( tx )
				{
					if (count == pick) break;
					if ( *tx == '\0' )
					{
						count++;
					}
					tx++;
				}
				LPuts(tx);
				LPuts("\n");
    	  FreeMem ( tbuff, len ) ;
    	  success = TRUE;
  	  }
	  }
	}
  if( !success ) LPuts( "How the heck do I change this?\n" );
}
*/

int main(int argc, char *argv[])
{
int i;
struct Line				*le, *TLine;
struct tm ontime;


BOOL realnames = FALSE, showsysop = FALSE, showlocal = FALSE, whonote = FALSE;

char obuff[256], location[80], otbuff[6];

static UBYTE   *VersTag = "\0$VER: WhosOnline 1.1 "__AMIGADATE__;

	if (argc < 2 || !strcmp(argv[1], "?"))
	{
		Printf("Syntax: LastCallers <Line number> [REALNAMES] [SHOWSYSOP] [SHOWLOCAL] [WHONOTE]");
		exit(RETURN_WARN);
	}

	for(i = 2; i < argc; i++)
	{
		if(!stricmp(argv[i], "REALNAMES")) realnames = TRUE;
		if(!stricmp(argv[i], "SHOWSYSOP")) showsysop = TRUE;
		if(!stricmp(argv[i], "SHOWLOCAL")) showlocal = TRUE;
		if(!stricmp(argv[i], "WHONOTE"))   showlocal = TRUE;
	}
	
	/* Seed the random number generator for later use */

	srand(time(NULL));

	if (PipelineBase = OpenLibrary("zeus.library", 0))
	{
		if ( PipeUtilBase = OpenLibrary ( "zmf.library", 0L ) )
		{
			if( Shared = GetShared() )
			{
				LineNumber = atol( argv[1] );
				if( le = GetLine( LineNumber ))
				{
					WriteFlags = NULL;

					/* Display Header */

					LPuts("[1;30mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
					LPuts("[44m[37mLine  User Name            Calling From      Baud  Login Status                \n");
					LPuts("[30;40mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");

					i = 0;
					do
					{
/*
						if( GetHotChar(LineNumber, TRUE)) break;
*/
						if( TLine = GetLine( i ) )
						{
							/* Tline->le_User will be NULL if there's no user online */

							if(TLine->le_User)
							{
								if(showsysop || stricmp(Shared->SysVars->sv_SysOpName, TLine->le_User->ur_Name) || !stricmp(le->le_User->ur_Name, TLine->le_User->ur_Name))
								{
									if( TLine->le_RealBaud || showlocal || !stricmp(le->le_User->ur_Name, TLine->le_User->ur_Name) )
									{
										GMTime(TLine->le_LogOnTime, &ontime);

										sprintf(location, "%s, %s", TLine->le_User->ur_Town, TLine->le_User->ur_County);
										sprintf(otbuff, "%2.2d:%2.2d", ontime.tm_hour, ontime.tm_min);

										if(TLine->le_User->ur_LastBaudRate == 0)
										{
											if(realnames) sprintf(obuff, "[1;36m%3d   %-20.20s %-16.16s  LOCAL %s %-22.22s\n", TLine->le_Number, TLine->le_User->ur_Name, location, otbuff, TLine->le_Status);
											else sprintf(obuff, "[1;36m%3d   %-20.20s %-16.16s  LOCAL %s %-22.22s\n", TLine->le_Number, TLine->le_User->ur_Alias, location, otbuff, TLine->le_Status);
										}
										else
										{
											if(realnames) sprintf(obuff, "[1;36m%3d   %-20.20s %-16.16s %6ld %s %-22.22s\n", TLine->le_Number, TLine->le_User->ur_Name, location, TLine->le_RealBaud, otbuff, TLine->le_Status);
											else sprintf(obuff, "[1;36m%3d   %-20.20s %-16.16s %6ld %s %-22.22s\n", TLine->le_Number, TLine->le_User->ur_Alias, location, TLine->le_RealBaud, otbuff, TLine->le_Status);
										}
										LPuts(obuff);
/*
										GetWhoNotes( TLine->le_User->ur_Name ) ;
*/
									}
								}
							}
						}
						i++;
					}	while (i <= Shared->HiLine);

					/* Display Footer */
					LPuts("[30mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
				}
				else
				{
					Printf( "Illegal line number!\n\n" );
				}
			}
			else
			{
				Printf( "Cannot get SharedData -- Is Zeus running?\n\n" );
			}
			CloseLibrary ( PipeUtilBase ) ;
		}
		else
		{
			Printf ( "Can't open zmf.library!\n" ) ;
		}
		CloseLibrary(PipelineBase);
	}
	else
	{
		Printf( "Cannot open Zeus Library!\n\n" );
	}
}

