#
#  COPYRIGHT:   (c) Awasu Pty. Ltd. 2003-11 (all rights reserved).
#               Unauthorized use of this code is prohibited.
# 
#  LICENSE:     This software is provided 'as-is', without any express 
#               or implied warranty.
# 
#               In no event will the author be held liable for any damages 
#               arising from the use of this software.
#   
#               Permission is granted to anyone to use this software 
#               for any non-commercial purpose and to alter it and
#               redistribute it freely, subject to the following restrictions: 
# 
#               - The origin of this software must not be misrepresented; 
#                 you must not claim that you wrote the original software. 
#                 If you use this software, an acknowledgment is requested
#                 but not required. 
# 
#               - Altered source versions must be plainly marked as such, 
#                 and must not be misrepresented as being the original software. 
#                 Altered source is encouraged to be submitted back to 
#                 the original author so it can be shared with the community. 
#                 Please share your changes. 
# 
#               - This notice may not be removed or altered from any 
#                 source distribution. 
#

import os
import sys
import win32api
import string 
import time

# --- GLOBAL CONSTANTS ------------------------------------------------

HookError = "LogChannelActivity hook error"

# --- GLOBAL DATA -----------------------------------------------------

gLogFilename = None
gLogFile = None
gEnableDateStamps = 0
gEnableTimeStamps = 0

# ---------------------------------------------------------------------

def processEvent( configFilename ) :

    # log the event 
    eventName = win32api.GetProfileVal( "Config" , "EventName" , "" , configFilename )
    logMsg( "EVENT: " + eventName )
    if win32api.GetProfileVal( "ChannelHookParameters" , "DumpConfigFiles" , 0 , configFilename ) :
        dumpConfigFile( configFilename )
        
# ---------------------------------------------------------------------

def processCommand( configFilename ) :

    # log the command
    commandName = win32api.GetProfileVal( "Config" , "CommandName" , "" , configFilename ) 
    commandName = string.upper( commandName )
    if commandName == "OPENLOGFILE" :
        onOpenLogFile( configFilename ) 
    elif commandName == "CLEARLOGFILE" : 
        onClearLogFile( configFilename ) 
    else :
        raise HookError , "Unknown channel hook command: "+commandName
        
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def onOpenLogFile( configFilename ) :

    # open the log file     
    gLogFile.close() 
    # FUDGE! spawnlp() is not available under Windows (why not?!)
    # We use startfile() instead and hope the user has set the log file's 
    # extension correctly :-( We need to use a non-blocking system call here
    # otherwise Awasu will think the script has timed out.
    #os.spawnlp( os.P_NOWAIT , "NOTEPAD.EXE" , gLogFilename )
    os.startfile( gLogFilename )
    
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

def onClearLogFile( configFilename ) :

    # clear the log file
    gLogFile.close() 
    fp = open( gLogFilename , "w" )
    fp.close()        
    
# ---------------------------------------------------------------------

def logMsg( msg ) :
    
    # log the message 
    now = time.strftime( "%H:%M:%S" )    
    if gEnableDateStamps :
        gLogFile.write( string.lower(time.strftime("%d%b%y")) + " " )
    if gEnableTimeStamps :
        gLogFile.write( time.strftime("%H:%M:%S") + " " )
    if gEnableDateStamps or gEnableTimeStamps :
        gLogFile.write( "| " )
    gLogFile.write( msg + "\n" )

# ---------------------------------------------------------------------

def dumpConfigFile( configFilename ) :

    # dump the config file
    fp = open( configFilename , "r" )
    logMsg( "--- BEGIN CONFIG FILE ---" )
    gLogFile.write( fp.read() ) 
    logMsg( "--- END CONFIG FILE ---" )
    
# --- MAIN ------------------------------------------------------------

# read the config file 
configFilename = sys.argv[1]
gLogFilename = win32api.GetProfileVal( "ChannelHookParameters" , "LogFile" , "" , configFilename )
if gLogFilename == "" :
    raise HookError , "No log file was configured." 
gLogFile = open( gLogFilename , "a" )
gEnableDateStamps = win32api.GetProfileVal( "GlobalChannelHookParameters" , "DateStamps" , 0 , configFilename )
gEnableTimeStamps = win32api.GetProfileVal( "GlobalChannelHookParameters" , "TimeStamps" , 0 , configFilename )

# process the command 
cmd = win32api.GetProfileVal( "Config" , "Command" , "" , configFilename )
cmd = string.upper( cmd )
if cmd == "PROCESSEVENT" :
    processEvent( configFilename )
elif cmd == "PROCESSCOMMAND" : 
    processCommand( configFilename )
else :
    raise HookError , "Unknown command: "+cmd

