<!--#include file="config.asp"-->
<%
'Created 14th March 2003
varDate = now()
If application("HousecleanDelUsers") = True Then
	'delete people that have been idle for 20 mins
	Dim arrExpiredUsers()
	Dim i : i = 0
	
	Set rs=Server.CreateObject("ADODB.recordset")
	rs.Open "SELECT * FROM USERS WHERE ACTIVE=1 ORDER BY USERNAME", strConnString, 0, 1
	If rs.EOF Then
		'There are no users
	Else
		rs.MoveFirst
		Do
			Dim varUserID : varUserID = rs("USERID")
			Dim varRoomID : varRoomID = rs("ROOMID")
			Dim varLastPost : varLastPost = rs("LASTPOST")
			'now find out how long they have been idle for
			varLastPost = FormatDateTime(varLastPost, 4)
			varLastPost = CDate(varLastPost)
			'need the date as hh:mm
			Dim varNow : varNow = FormatDateTime(now(), 4)
			varNow = CDate(varNow)
			Dim varIdle : varIdle =  DateDiff("n", varLastPost, varNow)
			
			If varIdle >= application("KickOutTime")  OR varIdle < -application("KickOutTime") Then
				Dim arrayEU : arrayEU = "yes"
				ReDim Preserve arrExpiredUsers(i)
				arrExpiredUsers(i) = varUserID & ":" & varRoomID
				i = i + 1
			End If
			
			rs.MoveNext
		Loop Until rs.EOF
	End If
	rs.Close
	Set rs = Nothing
	'play with the array
	If arrayEU = "yes" Then
		Dim F
		For F = LBound(arrExpiredUsers) To UBound(arrExpiredUsers)
			'split the data
			Dim varData : varData = Split(arrExpiredUsers(F), ":")
			varSQL = 	"INSERT INTO MESSAGES" & _ 
						"(USERID, ROOMID, MESSAGE, STYLE, COLOUR, TIMEOFPOST) " & _
						"VALUES(" & varData(0) & ", '" & varData(1) & "', 'Has Logged Off', 'i', 'red', '" & now() & "');"
			Set conn=Server.CreateObject("ADODB.Connection")
			conn.Open strConnString
			conn.Execute(varSQL)
			conn.Close
			Set conn = Nothing
			'response.write "Logged out user " & varData(0) & " from room " & varData(1)
			varSQL = 	"UPDATE USERS SET " & _
						"ROOMID=0, " & _
						"LOGGEDOFF='" & varDate & "', " & _
						"ACTIVE=0 " & _
						"WHERE USERID = " & varData(0) & ";"
			Set conn=Server.CreateObject("ADODB.Connection")
			conn.Open strConnString
			conn.Execute(varSQL)
			conn.Close
			Set conn = Nothing
		Next
	End If
End If

If application("HousecleanDelRooms") = True Then
	'delete empty rooms
	Dim arrHCRooms()
	i = 0
	'get the rooms from the database
	varSQL = "SELECT * FROM ROOMS;"
	Set rs=Server.CreateObject("ADODB.recordset")
	rs.Open varSQL, strConnString
	If rs.EOF Then
		'There are no rooms
	Else
		rs.MoveFirst
		Do
			varRoomID = rs("ROOMID")
			Dim arrayR : arrayR = "yes"
			ReDim Preserve arrHCRooms(i)
			arrHCRooms(i) = varRoomID
			i = i + 1
			rs.MoveNext
		Loop Until rs.EOF
	End If
	rs.Close
	Set rs = Nothing
	If arrayR = "yes" Then
		For F = LBound(arrHCRooms) To UBound(arrHCRooms)
			varSQL = "SELECT Count(USERID) AS NoUsers FROM USERS WHERE ROOMID = " & arrHCRooms(F) & ";"
			Set rs=Server.CreateObject("ADODB.recordset")
			rs.Open varSQL, strConnString
			Dim varNoUsers : varNoUsers = rs("NoUsers")
			rs.Close
			Set rs = Nothing
			If varNoUsers = 0 AND NOT arrHCRooms(F) = 1 Then
				varSQL = "DELETE FROM ROOMS WHERE ROOMID = " & arrHCRooms(F) & ";"
				Set conn=Server.CreateObject("ADODB.Connection")
				conn.Open strConnString
				conn.Execute(varSQL)
				conn.Close
				set conn = Nothing
			End If	
		Next
	End If
End If

If NOT application("HousecleanDelMessages") = "Never" Then
	Dim varDelAfter : varDelAfter = application("HousecleanDelMessagesAfter")
	varDelAfter = CInt(varDelAfter)
	If application("HousecleanDelMessages") = "Time" Then
		varDelAfter = varDelAfter * -1
		varNow = now()
		Dim varNowDelAfter : varNowDelAfter = DateAdd("d", varDelAfter, varNow)
		varSQL = "DELETE FROM MESSAGES WHERE TIMEOFPOST < #" & varNowDelAfter & "#;"
		Set conn=Server.CreateObject("ADODB.Connection")
		conn.Open strConnString
		conn.Execute(varSQL)
		conn.Close
		Set conn = Nothing
	End If
	If application("HousecleanDelMessages") = "Number" Then
		varSQL = "SELECT Count(MESSAGEID) AS noMessages FROM MESSAGES;"
		Set rs=Server.CreateObject("ADODB.recordset")
		rs.Open varSQL, strConnString
		Dim varNoMessages : varNoMessages = CInt(rs("noMessages"))
		rs.Close
		Set rs = Nothing
		If varNoMessages > varDelAfter Then
			varSQL = "SELECT MESSAGEID FROM MESSAGES"
			Set rs=Server.CreateObject("ADODB.recordset")
			rs.Open varSQL, strConnString, 1, 1
			rs.MoveLast
			Dim varLastOne : varLastOne = rs("MESSAGEID")
			rs.Close
			Set rs = Nothing
			Dim varDeleteFrom : varDeleteFrom = CInt(varLastOne - varDelAfter)
			varSQL = "DELETE FROM MESSAGES WHERE MESSAGEID <= " & varDeleteFrom & ";"
			Set conn=Server.CreateObject("ADODB.Connection")
			conn.Open strConnString
			conn.Execute(varSQL)
			conn.Close
			Set conn = Nothing
		End If
	End If
End If
%>