Microsoft Chat Server Development Guide |
---|
// SessionMonitor.cpp : Implementation of CSessionMonitor
#include "stdafx.h"
#include "LogMonitor.h"
#include "SessionMonitor.h"
// Name of file used to hold the Chat session log
static const char *LOGFILE = "C:\\CHATSESS.LOG";
// All extension require a short-name property
STDMETHODIMP CSessionMonitor::get_Name(BSTR *pbstrName)
{
// This property defines the short-name used to identify this extension
// The caller is responsible for freeing the memory
*pbstrName = SysAllocString(L"SessionMonitor");
return S_OK;
}
// The chat server calls this to allow an extension to initialize
// Result should be set to success or failure
STDMETHODIMP CSessionMonitor::Init(IChatServer *pServer, IChatRegistrar* pRegistrar, long *Result)
{
BSTR Events;
// We're using a smart point CComPtr, so no need to AddRef()
m_pRegistrar = pRegistrar;
//allocate a BSTR of events to bind to
Events = SysAllocString(L"OnNewUser,OnCloseUser");
// Bind to user events, Result returns success if we bound ok
m_pRegistrar->AddServerEvent(Events, Result);
SysFreeString(Events);
return S_OK;
}
// This method is called when a new user logs into the server
// pfCancel is used to cancel the event on pre-update notifications
STDMETHODIMP CSessionMonitor::OnNewUser(IChatUser *pUser, VARIANT_BOOL varbPostUpdate, long *pfCancel)
{
USES_CONVERSION;
BSTR Nick;
// get the users Nick name and remember the time they logged in
pUser->get_Nick(&Nick);
m_LoginTimes[OLE2A(Nick)] = time(NULL);
SysFreeString(Nick);
return S_OK;
}
// This method is called when a user disconnects from the server
STDMETHODIMP CSessionMonitor::OnCloseUser(IChatUser *pUser)
{
USES_CONVERSION;
BSTR Nick;
time_t loginTime;
map<string, time_t>::iterator it;
// Get the nick for this user
pUser->get_Nick(&Nick);
it = m_LoginTimes.find(OLE2A(Nick));
// Check to see if we can find a start time for this Nick
if( it != m_LoginTimes.end() ) {
time_t now = time(NULL);
char nowstr[32];
char buf[128];
lstrcpy(nowstr, asctime(localtime(&now)));
// Format a message for this nick's login duration
loginTime = it->second;
wsprintf(buf, "%S\t%s\t%s\n", Nick, asctime(localtime(&loginTime)), nowstr);
m_LoginTimes.erase(it); // Remove this nick from the logintimes
// Append this message to the LOGFILE
HANDLE hFile = CreateFile(LOGFILE, GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile != INVALID_HANDLE_VALUE ) {
DWORD len;
// Seek to the end of the file
SetFilePointer(hFile, 0, 0, FILE_END);
len = strlen(buf);
WriteFile(hFile, buf, len, &len, NULL);
CloseHandle(hFile);
}
}
SysFreeString(Nick);
return S_OK;
}
© 1998 Microsoft Corporation. All rights reserved.