'// Clan Info Script will return all information about a specified clan
'// if it exists on the Battle.net website.
'// 
'// USAGE:
'// .clanactivity/claninfo (Current Channel or Your Clan)
'// .clanactivity/claninfo tag (Specified Tag)
'// .clanactivity/claninfo tag, uswest OR lordaeron OR 0
'// .clanactivity/claninfo tag, useast OR azeroth OR 1
'// .clanactivity/claninfo tag, europe OR northrend OR 2
'// .clanactivity/claninfo tag, asia OR kalimdor OR 3

Sub Script(Name, Major, Minor, Build, Author, Commands, Description)
	Name = "Clan Info Script"
	Major = 1
	Minor = 1
	Build = 0
	Author = "Chriso"
	Commands = "clanactivity, claninfo"
	Description = "Return clan information from Battle.Net site."
End Sub

Sub Event_Load()
	CreateCommand "clanactivity", "ClanInfo", "Command_ClanActivity", "", "[clan[, server]]", "Return clan activity of a specific clan from Battle.Net's site.", 0, True
	CreateCommand "claninfo", "ClanInfo", "Command_ClanInfo", "", "[clan[, server]]", "Return clan information from Battle.Net's site.", 0, True
End Sub

Sub Command_ClanInfo(CS)
	Dim strServer, strClanTag
	'Parse Entered Text
	If GetServerTag(CS, strServer, strClanTag) = False Then Exit Sub
	
	Dim PageStr, ClanName, ClanTag, DateCreated, Total, Tag, PageContent, Ret
	PageStr = "ClanTag=" & strClanTag & "&Gateway=" & strServer
	'Parse HTML
	If ParseContent(CS, PageStr, ClanName, ClanTag, DateCreated, Total, Ret) = False Then Exit Sub
	
    'Count Information
    Dim lngGrunts, lngPeons, lngInit, lngShamans, lngWeek, lngMonth
	lngGrunts = 0
	lngPeons = 0
	lngInit = 0
	lngShamans = 0
	lngWeek = 0
	lngMonth = 0
    Dim Shamans, Chief
    For i = 0 To UBound(Ret)
        If Len(Ret(i)) > 0 Then
            Dim S
            S = Split(Ret(i), "/")
            
            Select Case S(0)
                Case "4"
                    Chief = S(1)
                Case "3"
                    lngShamans = lngShamans + 1
                    Shamans = Shamans & S(1) & ", "
                Case "2"
                    lngGrunts = lngGrunts + 1
                Case "1"
                    lngPeons = lngPeons + 1
                Case "0"
                    lngInit = lngInit + 1
            End Select
            
            If InStr(1, S(3), "Days") > 0 Then
                Dim CountDays
                CountDays = Left(S(3), InStr(S(3), " ") - 1)
                
				If CLng(CountDays) > 30 Then 
					lngMonth = lngMonth + 1
				ElseIf CLng(CountDays) > 7 Then 
					lngWeek = lngWeek + 1
				End If
            End If
        End If
    Next
	Erase Ret
    
	If Len(Shamans)> 0 Then Shamans = Left(Shamans, Len(Shamans)-2)
	
	CS.Reply ClanName & " (Clan " & ClanTag & "@" & strServer & ") was created on " & DateCreated & " is currently led by " & Chief & " and has " & Total & " members (" & lngShamans & " shamans, " & lngGrunts & " grunts, " & lngPeons & " peons, " & lngInit & " initiates)."
	If Len(Shamans) > 0 Then CS.Reply "Shamans: " & Shamans
	
	Dim strOut
	If lngWeek <> "0" Then
		strOut = lngWeek & " members have been inactive for over a week"
	End If
	If lngMonth <> "0" Then
		If Len(strOut) > 0 Then strOut = strOut & ", and "
		strOut = strOut & lngMonth & " members have been inactive for over a month"
	End If
	If Len(strOut) > 0 Then
		CS.Reply strOut & "."
	End If
End Sub

Sub Command_ClanActivity(CS)
	Dim strServer, strClanTag
	'Parse Entered Text
	If GetServerTag(CS, strServer, strClanTag) = False Then Exit Sub
	
	Dim PageStr, ClanName, ClanTag, DateCreated, Total, Tag, PageContent, Ret
	PageStr = "ClanTag=" & strClanTag & "&Gateway=" & strServer
	'Parse HTML
	If ParseContent(CS, PageStr, ClanName, ClanTag, DateCreated, Total, Ret) = False Then Exit Sub
	
    'Count Information
	Dim Inactive
	ReDim Inactive(0)
    For i = 0 To UBound(Ret)
        If Len(Ret(i)) > 0 Then
            Dim S
            S = Split(Ret(i), "/")
            
            If InStr(1, S(3), "Days") > 0 Then
                Dim CountDays
                CountDays = Left(S(3), InStr(S(3), " ") - 1)
                
				If CLng(CountDays) > 7 Then
					If Len(Inactive(0)) > 0 Then ReDim Preserve Inactive(UBound(Inactive)+1)
					Inactive(UBound(Inactive)) = S(1) & ": " & CountDays
				End If
            End If
        End If
    Next
	Erase Ret
    
	If Len(Inactive(0))> 0 Then
		Dim iter, outstr, header
		iter = 0
		header = True
		outstr = "Clan " & ClanTag & "@" & strServer & " has " & UBound(Inactive) + 1 & " inactive members: "
		For i = 0 to UBound(Inactive)
			iter = iter + 1
			outstr = outstr & Inactive(i) & ", "
			
			If header = True Then
				if iter > 6 Then
					CS.Reply Left(outstr,Len(outstr)-2)
					iter = 0
					outstr = ""
					header = False
				End If
			Else
				If iter > 10 Then
					CS.Reply Left(outstr,Len(outstr)-2)
					iter = 0
					outstr = ""
				End If
			End If					
		Next
		
		If Len(outstr) > 0 Then
			CS.Reply Left(outstr,Len(outstr)-2)
			outstr = ""
		End If
	End If
End Sub

Function AppendArray(oldArr, newArr)
    Dim CountA, CountB
    CountA = UBound(oldArr) + 1
    CountB = UBound(oldArr) + UBound(newArr) + 1
    ReDim Preserve oldArr(CountB)
    For i = LBound(newArr) To UBound(newArr)
        oldArr(i + CountA) = newArr(i)
    Next
    AppendArray = oldArr
End Function

Function ParseUsers(Content)
    Dim Ret, Tag, Rows
    Tag = "<tr class=" & """" & "rankingRow" & """"
    Rows = Split(Content, Tag)
    ReDim Ret(UBound(Rows))
    
    For i = 0 To UBound(Rows)
        Rows(I) = Split(Rows(I), "</tr>")(0)
        Rows(I) = Mid(Rows(I), InStr(1, Rows(I), ">") + 1)
        Rows(I) = ReplaceHTML(Rows(I))
        Dim Cells
        Tag = "<td class=" & """" & "rankingRow"
        Cells = Split(Rows(i), Tag)
        If UBound(Cells) = 4 Then
            Dim Rank, User, DateJoin, DateSeen
            Tag = "/war3/ladder/portraits/clans/rank"
            Rank = Mid(Cells(1), InStr(1, Cells(1), Tag) + Len(Tag))
            Rank = Left(Rank, InStr(1, Rank, ".") - 1)
            
            Tag = "<span class = " & """" & "rankingName" & """" & ">"
            User = Mid(Cells(2), InStr(1, Cells(2), Tag) + Len(Tag))
            User = Mid(User, InStr(1, User, ">") + 1)
            User = Left(User, InStr(1, User, "<") - 1)
            
            DateJoin = Mid(Cells(3), InStr(1, Cells(3), ">") + 1)
            DateJoin = Left(DateJoin, InStr(1, DateJoin, "<") - 1)
            
            DateSeen = Mid(Cells(4), InStr(1, Cells(4), ">") + 1)
            DateSeen = Left(DateSeen, InStr(1, DateSeen, "<") - 1)
            
            Ret(i) = Rank & "/" & User & "/" & DateJoin & "/" & DateSeen
        End If
    Next
    
    ParseUsers = Ret
End Function

Function GetServerTag(CS, strServer, strClanTag)
	GetServerTag = False
	If Len(CS.Message) = 0 Then
		strServer = GetBot.Config.ServerNameEx(True)
		Dim ch
		ch = LCase(GetBot.Self.Channel)
		If Len(ch) > 5 Then
			If Left(ch, 5) = "clan " Then
				If ch <> "clan recruitment" Then
					strClanTag = Mid(ch, 6)
				End If
			End If
		End If
		
		If Len(strClanTag) = 0 Then
			If GetBot.Clan.InClan Then strClanTag = GetBot.Clan.Tag
		End If
	Else
		If InStr(CS.Message, ", ") > 0 Then
			S = Split(CS.Message, ", ")
			strClanTag = S(0)
			Select Case LCase(S(1))
			Case "0", "uswest", "lordaeron"
				strServer = "Lordaeron"
			Case "1", "useast", "azeroth"
				strServer = "Azeroth"
			Case "2", "europe", "northrend"
				strServer = "Northrend"
			Case "3", "asia", "kalimdor"
				strServer = "Kalimdor"
			Case Else
				CS.Reply "Invalid server specified, leave blank for current server!"
				Exit Function
			End Select
		Else
			strClanTag = CS.Message
			strServer = GetBot.Config.ServerNameEx(True)
		End If
	End If
	strClanTag = Trim(strClanTag)
	strServer = Trim(strServer)
	If Len(strClanTag) = 0 Then
		CS.Reply "Clan tag was not specified!"
		Exit Function
	End If
	If Len(strServer) = 0 Then
		CS.Reply "Server was not specified!"
		Exit Function
	End If
	GetServerTag = True
End Function

Function ParseContent(CS, PageStr, ClanName, ClanTag, DateCreated, Total, Users)
	ParseContent = False
	
	Dim Content
    Content = scInet.OpenURL("http://classic.battle.net/war3/ladder/w3xp-clan-profile.aspx?" & PageStr)
    Tag = "<span style = " & """" & "FONT-SIZE: 15pt; font-variant: small-caps; letter-spacing: 3px; font-style: italic; COLOR: #ffffff; FONT-FAMILY: arial,helvetica,sans-serif; font-weight: bold;" & """" & ">"
    If InStr(1, Content, Tag) > 0 Then
        ClanName = Mid(Content, InStr(1, Content, Tag) + Len(Tag))
        ClanName = Left(ClanName, InStr(1, ClanName, "<") - 1)
		
		ClanTag = Mid(ClanName, InStr(1, ClanName, "[") + 1)
		ClanTag = Left(ClanTag, InStr(1, ClanTag, "]") - 1)
		ClanName = Left(ClanName, InStr(1, ClanName, " [") - 1)
    End If
	
    If Len(ClanName) = 0 Then
		CS.Reply "Clan " & strClanTag & " does not exist on " & strServer &" or unable to parse data."
		Exit Function
	End If
    
    Tag = "Creation Date: "
    If InStr(1, Content, Tag) > 0 Then
        DateCreated = Mid(Content, InStr(1, Content, Tag) + Len(Tag))
        DateCreated = Mid(DateCreated, InStr(1, DateCreated, ">") + 1)
        DateCreated = ReplaceHTML(Left(DateCreated, InStr(1, DateCreated, "<") - 1))
    End If
    
    Tag = "Total Members:"
    If InStr(1, Content, Tag) > 0 Then
        Total = Mid(Content, InStr(1, Content, Tag) + Len(Tag))
        Total = Mid(Total, InStr(1, Total, "<span style = "))
        Total = Mid(Total, InStr(1, Total, ">") + 1)
		Total = ReplaceHTML(Left(Total, InStr(1, Total, " ") - 1))
    End If
    
    'Parse Current Page
    Dim Ret
    Ret = ParseUsers(Content)
    
    'Retrieve Pages to Request
    Tag = "<p class = " & """" & "info" & """" & ">"
    If InStr(1, Content, Tag) > 0 Then
        PageContent = Mid(Content, InStr(1, Content, Tag) + Len(Tag))
        PageContent = Left(PageContent, InStr(1, PageContent, "</td>") - 1)
        
        Tag = "<a HREF="
        Dim Anchors, Pages
        Anchors = Split(PageContent, Tag)
        ReDim Pages(UBound(Anchors))
        For i = 0 To UBound(Anchors)
            If InStr(Anchors(i), "</a>") > 0 Then
                Pages(i) = Mid(Anchors(i), InStr(Anchors(i), "'>") + 2)
                Pages(i) = Left(Pages(i), InStr(Pages(i), "</a>") - 1)
				Dim Ret2
				'Parse pages on the fly
				Ret2 = ParseUsers(scInet.OpenURL("http://classic.battle.net/war3/ladder/w3xp-clan-profile.aspx?" &  PageStr & "&PageNo=" & Pages(i)))
				Ret = AppendArray(Ret, Ret2)
            End If
        Next
		Erase Pages
		Erase Anchors
    End If
	Users = Ret

	ParseContent = True
End Function

Function ReplaceHTML(S)
    ReplaceHTML = Trim(Replace(Replace(Replace(S, vbTab, ""), "  ", ""), vbNewLine, ""))
End Function
