/*****************************************************************************
*
* MAIN
*
* ENTRY:
* argc - count of the command line arguments.
* argv - vector of strings containing the command line arguments.
*
****************************************************************************/
int __cdecl
main(INT argc, CHAR **argv)
{
// struct tm * pTimeDate;
// time_t curtime;
SYSTEMTIME st;
WCHAR TimeStamp[ MAX_TIME_DATE_LEN ];
WCHAR *CmdLine;
WCHAR **argvW;
WCHAR szTitleFormat[50];
DWORD dwSize;
PLOGONID pTerm;
UINT TermCount;
ULONG Status;
int i, rc, TitleLen;
BOOLEAN MatchedOne = FALSE;
setlocale(LC_ALL, ".OCP");
/*
* Massage the command line.
*/
argvW = MassageCommandLine((DWORD)argc);
if (argvW == NULL) {
ErrorPrintf(IDS_ERROR_MALLOC);
return(FAILURE);
}
/*
* parse the cmd line without parsing the program name (argc-1, argv+1)
*/
rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
/*
* Check for error from ParseCommandLine
*/
if (rc && (rc & PARSE_FLAG_NO_PARMS) )
help_flag = TRUE;
if ( help_flag || rc ) {
if (!help_flag) {
Usage(TRUE);
return(FAILURE);
} else {
Usage(FALSE);
return(SUCCESS);
}
}
// If no remote server was specified, then check if we are running under Terminal Server
if ((!IsTokenPresent(ptm, TOKEN_SERVER) ) && (!AreWeRunningTerminalServices()))
{
ErrorPrintf(IDS_ERROR_NOT_TS);
return(FAILURE);
}
/*
* Open the specified server
*/
if( ServerName[0] ) {
hServerName = WinStationOpenServer( ServerName );
if( hServerName == NULL ) {
StringErrorPrintf(IDS_ERROR_SERVER,ServerName);
PutStdErr( GetLastError(), 0 );
return(FAILURE);
}
}
/*
* if no timeout was specified, use default
*/
if ( !IsTokenPresent(ptm, TOKEN_TIME) )
Seconds = RESPONSE_TIMEOUT;
/*
* allocate a buffer for the message header
*/
if ( (MsgText = (PWCHAR)malloc(MAX_IDS_LEN * sizeof(WCHAR))) == NULL ) {
ErrorPrintf(IDS_ERROR_MALLOC);
return(FAILURE);
}
MsgText[0] = 0;
/*
* set up message header text: sender and timestamp
*/
GetCurrentUserName(CurrUserName, USERNAME_LENGTH);
/*
* Get the current Winstation Id for this process
*/
gCurrentLogonId = GetCurrentLogonId();
/*
* Form message title string.
*/
dwSize = sizeof(szTitleFormat) / sizeof(WCHAR);
LoadString(NULL,IDS_TITLE_FORMAT,szTitleFormat,dwSize);
TitleLen = (wcslen(szTitleFormat) + wcslen(CurrUserName) + 1) * sizeof(WCHAR) + ( 2 * sizeof( TimeStamp ) );
MsgTitle = (PWCHAR)malloc(TitleLen);
if( MsgTitle == NULL )
{
ErrorPrintf(IDS_ERROR_MALLOC);
return(FAILURE);
}
_snwprintf(MsgTitle, TitleLen, szTitleFormat, CurrUserName);
TimeStamp[0] = 0;
GetLocalTime( &st );
GetDateFormat( LOCALE_USER_DEFAULT ,
DATE_SHORTDATE ,
&st ,
NULL ,
TimeStamp,
MAX_TIME_DATE_LEN );
wcscat(MsgTitle , TimeStamp);
TimeStamp[0] = 0;
GetTimeFormat( LOCALE_USER_DEFAULT ,
TIME_NOSECONDS ,
&st ,
NULL ,
TimeStamp,
MAX_TIME_DATE_LEN );
wcscat(MsgTitle , L" " );
wcscat(MsgTitle , TimeStamp);
/*
* if message was specified on the command line, add it to MsgText string
*/
if ( IsTokenPresent(ptm, TOKEN_MESSAGE) ) {
MsgText = realloc(MsgText, (wcslen(MsgText) + wcslen(MsgLine) + 1) * sizeof(WCHAR));
if ( MsgText == NULL ) {
ErrorPrintf(IDS_ERROR_MALLOC);
return(FAILURE);
}
wcscat(MsgText, MsgLine);
} else {
/*
* Message was not on the command line. If STDIN is connected to
* the keyboard, then prompt the user for the message to send,
* otherwise just read STDIN.
*/
if ( _isatty( _fileno(stdin) ) )
Message(IDS_MESSAGE_PROMPT);
while ( wfgets(MsgLine, MAX_IDS_LEN, stdin) != NULL ) {
MsgText = (PWCHAR)realloc(
MsgText,
(wcslen(MsgText) + wcslen(MsgLine) + 1) * sizeof(WCHAR) );
if ( MsgText == NULL ) {
ErrorPrintf(IDS_ERROR_MALLOC);
return(FAILURE);
}
wcscat(MsgText, MsgLine);
}
/*
* When we fall through, we either have an eof or a problem with
* STDIN
*/
if ( feof(stdin) ) {
/*
* If we get here then we hit eof on STDIN. First check to make
* sure that we did not get an eof on first wfgets
*/
if ( !wcslen(MsgText) ) {
ErrorPrintf(IDS_ERROR_EMPTY_MESSAGE);
return(FAILURE);
}
} else {
/*
* The return from wfgets was not eof so we have an STDIN
* problem
*/
ErrorPrintf(IDS_ERROR_STDIN_PROCESSING);
return(FAILURE);
}
}
/*
* Is the ids_input really a file indirection?
*/
if ( ids_input[0] == L'@' ) {
/*
* Open the input file and read the names into the NameList
*/
if ( !LoadFileToNameList(&ids_input[1]) )
return(FAILURE);
/*
* Ok, let's get in touch
*/
file_flag = TRUE;
} else {
_wcslwr( ids_input );
NameList = (WCHAR **)malloc( 2 * sizeof( WCHAR * ) );
if ( NameList == NULL ) {
ErrorPrintf(IDS_ERROR_MALLOC);
return(FAILURE);
}
NameList[0] = ids_input;
NameList[1] = NULL;
NameListCount = 1;
}
/*
* Enumerate across all the WinStations and send the message
* to them if there are any matches in the NameList
*/
if ( WinStationEnumerate(hServerName, &pTerm, &TermCount) ) {
if ( SendMessageIfTarget(pTerm, TermCount, MsgTitle, MsgText) )
MatchedOne = TRUE;
WinStationFreeMemory(pTerm);
} else{
Status = GetLastError();
ErrorPrintf(IDS_ERROR_WINSTATION_ENUMERATE, Status);
return(FAILURE);
}
/*
* Check for at least one match
*/
if ( !MatchedOne ) {
if( file_flag )
StringErrorPrintf(IDS_ERROR_NO_FILE_MATCHING, &ids_input[1]);
else
StringErrorPrintf(IDS_ERROR_NO_MATCHING, ids_input);
return(FAILURE);
}
return(SUCCESS);
} /* main() */