'////////////////////////////////////////////////////////////////////////
'// USER MONITOR SCRIPT FOR 9.4
'// WRITTEN BY: Chriso
'// This script will /whois a list of users to extend a friends list by
'// showing which monitored users are online.
'////////////////////////////////////////////////////////////////////////

'// Please note this script will 'flood' your chat window with /whois
'// responses, therefore it is recommended that you enable this script on
'// a profile which is hidden.

'// Interval between /whois requests for each user in the list
Const Interval = 6000

Dim LastUser, Mon, Waiter

'// Return Script Information
Sub Script(Name, Major, Minor, Build, Author, Commands, Description)
	Name = "User Monitor Script"
	Major = 1
	Minor = 0
	Build = 0
	Author = "Chriso"
	Commands = "check, monitor, monitored, unmonitor"
	Description = "Monitor a list of users periodically to determine who is online, this script will basically extend your friends list."
End Sub

'// Called when the script is loaded for a particular bot
Sub Event_Load()
	CreateCommand "check", "UserMonitor", "Command_Check", "", "user", "Returns the current status of the specified user.", 0, True
    CreateCommand "monitor", "UserMonitor", "Command_Monitor", "mon", "user", "Add a user to the monitor list to be periodically checked.", 0, True
    CreateCommand "monitored", "UserMonitor", "Command_Monitored", "", "", "Return a list of users being monitored in the console.", 1, True
    CreateCommand "unmonitor", "UserMonitor", "Command_Unmonitor", "unmon", "user", "Remove a user from the monitor list.", 0, True
	Set Mon = New Monitor
	Mon.ReadMonitor
	Mon.SaveMonitor
	Waiter = 0
	AddTimer "UserMonitor", "Timer_Monitor", Interval
End Sub

'// Called when the script is unloaded for a particular bot
Sub Event_Unload()

End Sub

Sub Timer_Monitor()
	If GetBot.IsOnline Then
		If Mon.Position > UBound(Mon.Users) Then Mon.Position = 0: Waiter = -3
		If CInt(Waiter) < 0 Then Waiter = Waiter + 1: Exit Sub
		If InStr(Mon.Users(Mon.Position), "%") Then
			Dim S
			S = Split(Mon.Users(Mon.Position), "%")
			Send "/whois " & GetBot.GetAccount(S(0))
			LastUser = GetBot.GetAccount(S(0))
		End If	
		Mon.Position = Mon.Position + 1
	End If
End Sub

'// Callback for "Check" Command
Sub Command_Check(CS)
	Dim I
	For I = 0 To UBound(Mon.Users)
		If InStr(Mon.Users(I), "%") Then
			Dim S
			S = Split(Mon.Users(I), "%")
			If LCase(S(0)) = LCase(CS.Message) Then
				If Len(S(1)) = 0 Then 
					CS.Reply S(0) & " is currently offline."
				Else
					CS.Reply S(0) & " is online using " & S(1) & "."
				End If
				Exit Sub
			End If
		End If
	Next
	CS.Reply CS.Message & " is not being monitored."
End Sub

'// Callback for "Monitor" Command
Sub Command_Monitor(CS)
	If Mon.AddMonitor(CS.Message) Then
		CS.Reply "Added " & CS.Message & " to monitor list."
	Else
		CS.Reply CS.Message & " is already being monitored."
	End If
End Sub

'// Callback for "Unmonitor" Command
Sub Command_Unmonitor(CS)
    If Mon.DeleteMonitor(CS.Message) Then
		CS.Reply "Removed " & CS.Message & " from monitor list."
	Else
		CS.Reply CS.Message & " was not being monitored."
	End If
End Sub

'// Callback for "Monitored" Command
Sub Command_Monitored(CS)
	AddChat vbWhite, "Monitored Users:"
    Dim I
	For I = 0 To UBound(Mon.Users)
		If InStr(Mon.Users(I), "%") Then
			Dim S
			S = Split(Mon.Users(I), "%")
			If Len(S(1)) = 0 Then 
				AddChat vbRed, "- " & S(0) & " is currently offline."
			Else
				AddChat vbGreen, "- " & S(0) & " is online using " & S(1) & "."
			End If
		End If
	Next
End Sub

'// Called when the server broadcasts an error message (RED MESSAGE)
Sub Event_ServerError(CE)
	If CE.Message = "That user is not logged on." Then
		Mon.EditMonitor GetBot.GetAccount(LastUser), ""
	End If
End Sub

'// Called when the server broadcasts information (YELLOW MESSAGE)
Sub Event_ServerInfo(CE)
	If Matches(CE.Message, "* is using *") Then
		Dim User, Client, Game
		User = Split(CE.Message, " ")(0)
		If GetBot.GetAccount(User) = GetBot.GetAccount(LastUser) Then
			Client = Split(CE.Message, " is using ")(1)
			If InStr(Client, " called ") Then Game = Split(Client, " called ")(1)
			Client = Split(Client, " in a ")(0)
			Mon.EditMonitor User, Client, Game
			CE.Cancel
		End If
	End If
End Sub

Class Monitor
	
	Public Users
	Public Position

	Public Function StripStar(X)
		If InStr(X, "*") Then
			StripStar = Split(X, "*")(1)
		Else
			StripStar = X
		End If
	End Function

	Public Sub SaveMonitor()
		Dim Content
		Content = Join(Users, vbNewLine)
		XDC.SaveFile XDC.GetBotPath & "Monitor.dat", Content
	End Sub

	Public Sub ReadMonitor()
		Dim Content, Lines, I
		Content = XDC.LoadFile(XDC.GetBotPath & "Monitor.dat")
		Lines = Split(Content, vbNewLine)
		ReDim Users(0)
		For I = 0 To UBound(Lines)
			If Len(Lines(I)) Then
				If InStr(Lines(I),"%") Then
					If Len(Users(UBound(Users))) Then ReDim Preserve Users(UBound(Users)+1)
					Users(UBound(Users)) = Lines(I)
				End If
			End If
		Next
	End Sub
	
	Public Function AddMonitor(Username)
		AddMonitor = False
		Username = StripStar(Username)
		If Len(Username) = 0 Then Exit Function
		Dim I
		For I = 0 To UBound(Users)
			If InStr(Users(I), "%") Then
				Dim S
				S = Split(Users(I), "%")
				If LCase(S(0)) = LCase(Username) Then Exit Function
			End If
		Next
		If Len(Users(UBound(Users))) Then ReDim Preserve Users(UBound(Users)+1)
		Users(UBound(Users)) = Username & "%"
		AddMonitor = True
		SaveMonitor
	End Function

	Public Sub EditMonitor(Username, Status)
		Username = StripStar(Username)
		If Len(Username) = 0 Then Exit Sub
		Dim I
		For I = 0 To UBound(Users)
			If InStr(Users(I), "%") Then
				Dim S
				S = Split(Users(I), "%")
				If LCase(S(0)) = LCase(Username) Then
					Users(I) = Username & "%" & Status
					SaveMonitor
					Exit Sub
				End If
			End If
		Next
	End Sub

	Public Function DeleteMonitor(Username)
		DeleteMonitor = False
		Username = StripStar(Username)
		If Len(Username) = 0 Then Exit Function
		Dim I
		For I = 0 To UBound(Users)
			Dim S
			S = Split(Users(I), "%")
			If LCase(S(0)) = LCase(Username) Then
				Dim R
				For R = I To UBound(Users)-1
					Users(R) = Users(R+1)
				Next
				ReDim Preserve Users(UBound(Users)-1)
				DeleteMonitor = True
				SaveMonitor
				Exit Function
			End If
		Next
	End Function

End Class
