LogoutWarning.rbfrm 14.2 KB
Newer Older
Rusty Myers's avatar
Rusty Myers committed
1
#tag Window
Begin Window LogoutWarning
   BackColor       =   16777215
   Backdrop        =   ""
   CloseButton     =   False
   Composite       =   False
   Frame           =   7
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   237
   ImplicitInstance=   True
   LiveResize      =   False
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   False
   MaxWidth        =   32000
   MenuBar         =   ""
   MenuBarVisible  =   False
   MinHeight       =   64
   MinimizeButton  =   False
   MinWidth        =   64
   Placement       =   2
   Resizeable      =   False
   Title           =   "Idle Logout"
   Visible         =   True
   Width           =   600
   Begin PushButton BLogOut
      AutoDeactivate  =   True
      Bold            =   ""
      ButtonStyle     =   0
      Cancel          =   ""
      Caption         =   "Log Out"
      Default         =   False
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   397
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   1
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   197
      Underline       =   ""
      Visible         =   True
      Width           =   80
   End
   Begin PushButton BMoreTime
      AutoDeactivate  =   False
      Bold            =   ""
      ButtonStyle     =   0
      Cancel          =   ""
      Caption         =   "More time"
      Default         =   True
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   500
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   2
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   197
      Underline       =   ""
      Visible         =   True
      Width           =   80
   End
   Begin Label WarningTitle
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   39
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   112
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      Selectable      =   False
      TabIndex        =   3
      TabPanelIndex   =   0
      Text            =   "PSU Idle Logout"
      TextAlign       =   1
      TextColor       =   0
      TextFont        =   "System"
      TextSize        =   30
      TextUnit        =   0
      Top             =   0
      Transparent     =   False
      Underline       =   ""
      Visible         =   True
      Width           =   377
   End
   Begin Label WarningMessage
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   84
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   False
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   True
      Scope           =   0
      Selectable      =   False
      TabIndex        =   4
      TabPanelIndex   =   0
      Text            =   "This Mac is idle.\r\rClick the ""More Time"" button to continue using the Mac.\r\rOtherwise, an automatic logout will occur and all unsaved documents will be LOST!"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   34
      Transparent     =   False
      Underline       =   ""
      Visible         =   True
      Width           =   560
   End
   Begin Label Seconds
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   25
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      Selectable      =   False
      TabIndex        =   6
      TabPanelIndex   =   0
      Text            =   "Seconds remaining before automatic logout..."
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   0
      TextUnit        =   0
      Top             =   127
      Transparent     =   False
      Underline       =   ""
      Visible         =   True
      Width           =   301
   End
   Begin Thread IdleThread
      Height          =   32
      Index           =   -2147483648
      Left            =   13
      LockedInPosition=   False
      Priority        =   5
      Scope           =   0
      StackSize       =   0
      TabPanelIndex   =   0
      Top             =   268
      Width           =   32
   End
   Begin Thread CountdownThread
      Height          =   32
      Index           =   -2147483648
      Left            =   57
      LockedInPosition=   False
      Priority        =   5
      Scope           =   0
      StackSize       =   0
      TabPanelIndex   =   0
      Top             =   268
      Width           =   32
   End
   Begin Label TimeLabel
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   51
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   268
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      Selectable      =   False
      TabIndex        =   5
      TabPanelIndex   =   0
      Text            =   ""
      TextAlign       =   1
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   30
      TextUnit        =   0
      Top             =   166
      Transparent     =   False
      Underline       =   ""
      Visible         =   True
      Width           =   64
   End
End
#tag EndWindow

#tag WindowCode
	#tag Method, Flags = &h0
		Function IdleSeconds() As Integer
		  // Set up variables for idle time
		  // Shell result
		  Dim mIdleSecs As String
		  // Shell exit code
		  Dim merrCode As  Integer
		  // Set up shell
		  Dim s As Shell
		  s=New Shell
		  s.Mode = 0
		  
		  // Check idle time
		  //s.Execute code to check idle time from USB input devices
		  s.Execute "/bin/echo $((`/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"
		  // Set results to mIdleResult
		  mIdleSecs = s.Result
		  // Set error code to mIdleError
		  merrCode = s.ErrorCode
		  // Log mIdleResult for debugging
		  // System.Log(System.LogLevelError, "Method: " + midleSecs)
		  // LogToFile("mIdleSeconds: " + str(mIdleSecs))
		  Return val(mIdleSecs)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function RemoteControlCheck() As Boolean
		  // Set up variables for idle time
		  Dim stablished as String = "ESTABLISHED"
		  
		  // Shell result
		  Dim rccheck As String
		  // Shell exit code
		  Dim merrCode As  Integer
		  // Set up shell
		  Dim s As Shell
		  s=New Shell
		  s.Mode = 0
		  
		  
		  // Check idle time
		  //s.Execute code to check for connection
		  s.Execute "/usr/sbin/netstat -n | /usr/bin/grep '.5900'| /usr/bin/awk '{print $6}'"
		  
		  // Set results to rccheck
		  // Make the entire returned text uppercase in case Apple ever changes the case to CaMeL CaSe, which would break our code:
		  rccheck = Uppercase(s.Result)
		  
		  //Testing line below
		  // rccheck ="ESTABLISHED"
		  
		  //LogToFile("rccheck: " + rccheck)
		  // Set error code to merrCode
		  merrCode = s.ErrorCode
		  
		  // LogToFile("rccheck: "+ rccheck)
		  // LogToFile("stablished: "+ stablished)
		  
		  If ( InStr( rccheck, stablished ) > 0 ) then
		    return true
		  else
		    //LogToFile("rccheck: false")
		    return false
		  End if
		  
		  // System.Log(System.LogLevelError, "Method: " + midleSecs)
		  // LogToFile("mIdleSeconds: " + str(mIdleSecs))
		  
		  
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function WaitForIdleTime(paramNumSecondsIdleToWait as integer) As Boolean
		  Dim mIdleResult As Integer
		  mIdleResult = IdleSeconds()
		  
		  while ( mIdleResult < paramNumSecondsIdleToWait )
		    
		    // Wait pIdleLoopDelaySeconds:
		    App.SleepCurrentThread( pIdleLoopDelaySeconds * 1000 )
		    
		    // Check idle time:
		    mIdleResult = IdleSeconds()
		    
		    LogToFile(CurrentMethodName + ": Idle Seconds: " + str(mIdleResult))
		    
		  wend
		  
		  // if there is a ARD/VNC session, don't idle out
		  // netstat -n | grep '.5900'
		  if ( RemoteControlCheck() ) then
		    //LogToFile("rcCheck = true")
		    LogToFile(CurrentMethodName + ": Someone is controlling via ARD/VNC, waiting...")
		    return false
		  else
		    //LogToFile("rcCheck = false")
		    LogToFile(CurrentMethodName + ": No one is controlling via ARD/VNC, continuing...")
		    return  true
		  end if
		  
		  
		End Function
	#tag EndMethod


	#tag Property, Flags = &h0
		pComputerIdleAfterNumSeconds As Integer = 900
	#tag EndProperty

	#tag Property, Flags = &h0
		pDefaultPrefsFileName As String = "edu.psu.its.clc.IdleLogoutSettings.plist"
	#tag EndProperty

	#tag Property, Flags = &h0
		pDefaultPrefsFSPath As String = "/Library/CLMadmin/Config/"
	#tag EndProperty

	#tag Property, Flags = &h0
		pIdleLoopDelaySeconds As Integer = 30
	#tag EndProperty

	#tag Property, Flags = &h0
		pIgnoreGroup As String = "admin"
	#tag EndProperty

	#tag Property, Flags = &h0
		pIgnoreUser As String = "macadmin"
	#tag EndProperty

	#tag Property, Flags = &h0
		pMoreTimeAskedFor As Boolean = true
	#tag EndProperty

	#tag Property, Flags = &h0
		pWaitForUserPromptSeconds As Integer = 90
	#tag EndProperty


#tag EndWindowCode

#tag Events BLogOut
	#tag Event
		Sub Action()
		  // Testing code
		  Dim s As Shell
		  s=New Shell
		  s.Mode = 0
		  
		  // s.execute "say logout" // Use this line to test logouts
		  
		  LogToFile("User chose LogOut")
		  // No more time, please
		  pMoreTimeAskedFor = false
		  
		  LogToFile("Kill open threads")
		  CountdownThread.Kill
		  IdleThread.Kill
		  // Log out the user
		  
		  LogToFile("Quitting")
		  
		  s.execute "/usr/bin/sudo /Library/CLMshared/psuRebootNow.pl" // Use this line to test logouts
		  
		  Quit
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events BMoreTime
	#tag Event
		Sub Action()
		  
		  // Testing code
		  Dim s As Shell
		  s=New Shell
		  s.Mode = 0
		  // s.execute "say More Time"
		  
		  LogToFile("More Time Requested")
		  // Ask for more time
		  //pMoreTimeAskedFor = true
		  // Resume IdleThread
		  IdleThread.Resume
		  // Stop the current countdown
		  CountdownThread.Kill
		  // Get rid of the window
		  LogoutWarning.Hide
		  
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events IdleThread
	#tag Event
		Sub Run()
		  LogToFile(CurrentMethodName + ": --->")
		  
		  // User has been idle 0 seconds so far
		  Dim idleTime as Integer = 0
		  // How long to wait between checking idle time
		  // Dim gLoopDelay as Integer = 30
		  // pIdleTime = How long to wait before considering computer abandonded
		  
		  pMoreTimeAskedFor = false
		  do
		    if ( WaitForIdleTime( pComputerIdleAfterNumSeconds ) ) then
		      LogToFile(CurrentMethodName + ": CountdownThread Start")
		      CountdownThread.Run
		    else
		      // Do Nothing...
		      LogToFile(CurrentMethodName + ": Not Time Yet...")
		    end if
		    //LogToFile("pMoreTimeAskedFor = " + str(pMoreTimeAskedFor))
		    
		  Loop until ( pMoreTimeAskedFor )
		  
		  LogToFile(CurrentMethodName + ": <---")
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events CountdownThread
	#tag Event
		Sub Run()
		  LogToFile(CurrentMethodName + ": --->")
		  
		  // Set how long to wait before logging out user, set from global variable for LogOutDelay
		  Dim pLogoutDelay as Integer = pWaitForUserPromptSeconds
		  
		  LogoutWarning.Show()
		  // Stop the idle thread
		  IdleThread.Suspend
		  
		  
		  // While we're waiting for the countdown to finish...
		  While pLogoutDelay >= 0
		    // Set label to the time left
		    TimeLabel.setString(str(pLogoutDelay))
		    // Refresh it
		    TimeLabel.Refresh
		    // take a second away from the time left
		    pLogoutDelay = pLogoutDelay - 1
		    // do nothing for 1 second
		    
		    App.SleepCurrentThread( 1000 ) // sleep 1 second
		    
		  Wend
		  
		  // If we're here, the user didn't cancel or log out manaully
		  
		  // Testing mode, use say instead of logout
		  Dim s As Shell
		  s=New Shell
		  s.Mode = 0
		  
		  // s.Execute "say Logging Out" // Testing line
		  
		  pMoreTimeAskedFor = false
		  
		  LogToFile(CurrentMethodName + ": Time Out for Response. Log User Out...")
		  
		  
		  LogToFile(CurrentMethodName + ": <---")
		  
		  s.Execute "/usr/bin/sudo /Library/CLMshared/psuRebootNow.pl" // Run script to reboot Mac
		  
		  // Quit app
		  Quit
		  
		End Sub
	#tag EndEvent
#tag EndEvents