'////////////////////////////////////////////////////////////////////////
'// ACCOUNT JOGGER SCRIPT FOR 9.9
'// WRITTEN BY: Chriso
'// Login to a list of accounts to keep them alive. This script can also
'// be used to create a list of accounts.
'////////////////////////////////////////////////////////////////////////

Const DELAY_BETWEEN = 30 '// Delay between each login in seconds
Const LOOP_TO_START = False  '// Loop back to top when end of list is reached?

'// Manually editing the AccountsToJog.txt file can be done by navigating
'// to %APPDATA%\Profiles\ then entering the profile folder you wish and
'// creating/opening "AccountsToJog.txt". Accounts go in this file in this
'// format (User // Pass), example:
'// Username1 // Password
'// Username2 // Password

Dim MyFile, MyAccounts, MyPos

'// Return Script Information
Sub Script(Name, Major, Minor, Build, Author, Commands, Description)
	Name = "Account Jogging Script"
	Major = 1
	Minor = 0
	Build = 0
	Author = "Chriso"
	Description = "Login to a list of accounts to keep them alive. This script can also be used to create a list of accounts."
End Sub

'// Initialize Timers in Disabled State
Sub Event_Load()
    AddTimer "AccountJogger", "Jogger_Timer", DELAY_BETWEEN * 1000, False
	MyFile = XDC.GetBotPath & "AccountsToJog.txt"
	MyPos = 0
	LoadAccounts
	SaveAccounts
	
	CreateCommand "jog", "AccountJogger", "Command_Jog", "", "account password", "Add an account to jog.", 1
	CreateCommand "jogging", "AccountJogger", "Command_Jogging", "", "", "Return a list of accounts being jogged.", 1
	CreateCommand "unjog", "AccountJogger", "Command_Unjog", "", "account", "Remove an account from jogger.", 1
	CreateCommand "jogstart", "AccountJogger", "Command_JogStart", "", "", "Start jogging.", 1
	CreateCommand "jogstop", "AccountJogger", "Command_JogStop", "", "", "Stop jogging.", 1
End Sub

Sub Command_Jog(CS)
	If InStr(CS.Message, " ") Then
		Dim MyAccount
		MyAccount = Split(CS.Message, " ", 2)
		If IsValidLength(MyAccount(0)) And IsValidLength(MyAccount(1)) Then
			If MyAccounts.Exists(MyAccount(0)) = False Then
				MyAccounts.Add MyAccount(0), MyAccount(1)
				AddChat vbYellow, "Added " & MyAccount(0) & " to jogging list, type /startjog to begin jogging."
				SaveAccounts
			Else
				AddChat vbRed, "Already jogging that account, please type /unjog [account] if you want to remove."
			End If
		Else
			AddChat vbRed, "Account or password was invalid."
		End If
	Else
		AddChat vbRed, "Please specify a password after the account."
	End If
End Sub

Sub Command_Unjog(CS)
	If MyAccounts.Exists(CS.Message) = True Then
		MyAccounts.Remove CS.Message
		AddChat vbYellow, "Removed " & CS.Message & " from jogging list."
		SaveAccounts
	Else
		AddChat vbRed, "That account is not being jogged."
	End If
End Sub

Sub Command_Jogging(CS)
	Dim Items, Keys, I
	Keys = MyAccounts.Keys
	Items = MyAccounts.Items
	If MyAccounts.Count > 0 Then
		AddChat vbYellow, "Listing " & UBound(Keys) + 1 & " accounts:"
		For I = 0 To UBound(Keys)
			AddChat vbYellow, "- " & Keys(I) & "     //      " & Items(I)
		Next
	Else
		AddChat vbRed, "There are no accounts being jogged, type /jog [account] [password] to add an account."
	End If
End Sub

Sub Command_JogStart(CS)
	If MyAccounts.Count > 0 Then
		AddChat vbGreen, "Jogging cycle has been started!"
		StartTimer "AccountJogger", "Jogger_Timer"
		MyPos = 0
		Jogger_Timer
	Else
		AddChat vbRed, "Cannot begin jogging, there are no accounts, type /jog [account] [password] to add an account."
	End If
End Sub

Sub Command_JogStop(CS)
	AddChat vbRed, "Jogging cycle has been stopped!"
	StopTimer "AccountJogger", "Jogger_Timer"
	MyPos = 0
End Sub

Sub Jogger_Timer()
	If MyPos > MyAccounts.Count - 1 Then
		If LOOP_TO_START Then
			AddChat vbGreen, "Jogging cycle complete, looping back to start of list..."
			MyPos = 0
		Else
			AddChat vbGreen, "Jogging cycle completed!"
			StopTimer "AccountJogger", "Jogger_Timer"
			MyPos = 0
			Exit Sub
		End If
	End If
	Dim Keys, Items
	Keys = MyAccounts.Keys
	Items = MyAccounts.Items
	GetBot.Config.Username = Keys(MyPos)
	GetBot.Config.Password = Items(MyPos)
	AddChat vbYellow, "Jogging account: " & GetBot.Config.Username & " ..."
	GetBot.Connect
	MyPos = MyPos + 1
End Sub

Function IsValidLength(A)
	IsValidLength = (Len(A) > 1 And Len(A) < 16)
End Function

Sub LoadAccounts()
	Dim MyContent, MySplit, MyLine, I
	Set MyAccounts = CreateObject("Scripting.Dictionary")
	MyContent = LoadFile(MyFile)
	MySplit = Split(MyContent, vbNewLine)
	For I = 0 To UBound(MySplit)
		If InStr(MySplit(I), " // ") Then
			MyLine = Split(MySplit(I), " // ", 2)
			If IsValidLength(MyLine(0)) And IsValidLength(MyLine(1)) Then MyAccounts.Add MyLine(0), MyLine(1)
		End If
	Next
End Sub

Sub SaveAccounts()
	Dim MyContent, I
	Dim Keys, Items
	Keys = MyAccounts.Keys
	Items = MyAccounts.Items
	For I = 0 To UBound(Keys)
		MyContent = MyContent & Keys(I) & " // " & Items(I) & vbNewLine
	Next
	SaveFile MyFile, MyContent
End Sub
