Merge Cyrodiil check into mainline

Sasky [06-12-14 - 04:54]
Merge Cyrodiil check into mainline
Filename
AutoInvite.lua
diff --git a/AutoInvite.lua b/AutoInvite.lua
index 46e4f80..a16cdd5 100644
--- a/AutoInvite.lua
+++ b/AutoInvite.lua
@@ -24,7 +24,7 @@ SLASH_COMMANDS["/ai"] = function(str)
 end

 --Main callback - sends invites
-AutoInvite.callback = function(eventCode, messageType, from, message)
+AutoInvite.callback = function(_, messageType, from, message)
 	if not AutoInvite.listening then
 		d("WARN: AutoInvite not listening properly. Please reset. ('/ai help' for commands)")
 		return
@@ -50,6 +50,18 @@ AutoInvite.callback = function(eventCode, messageType, from, message)
 	--d("Checking message '" .. string.lower(message) .."' ?= '" .. AutoInvite.watch .."'")
 end

+AutoInvite.isCyrodiil = function(unit)
+    if unit == nil then unit = "player" end
+    d("Current zone: '" .. GetUnitZone(unit) .. "'")
+    return GetUnitZone(unit) == "Cyrodiil"
+end
+
+local function b(v) if v then return "T" else return "F" end end
+
+SLASH_COMMANDS["/zdb9"] = function()
+    d("In Cyrodiil? " .. b(AutoInvite.isCyrodiil()))
+end
+
 AutoInvite.guildLookup = function(channel, acctName)
 	local guildId = 0
 	if channel == CHAT_CHANNEL_GUILD_1 or channel == CHAT_CHANNEL_OFFICER_1 then guildId = GetGuildId(1) end
@@ -59,19 +71,30 @@ AutoInvite.guildLookup = function(channel, acctName)
 	if channel == CHAT_CHANNEL_GUILD_5 or channel == CHAT_CHANNEL_OFFICER_5 then guildId = GetGuildId(5) end

 	if guildId == 0 then d("Error - couldn't invite on channel: " .. channel) end
-
+
 	local aName
 	for i=1,GetNumGuildMembers(guildId) do
 		aName = GetGuildMemberInfo(guildId,i)
 		if aName == acctName then
-			hasChar, charName, zone = GetGuildMemberCharacterInfo(guildId,i)
+			local hasChar, charName, zone = GetGuildMemberCharacterInfo(guildId,i)
 			-- Might use zone check to NOT auto-invite people outside current zone if leader in Cyrodiil
-			if hasChar then
-				return charName:gsub("%^.+", "")
-			else
-				d("Could not find player name for " .. acctName .. ". Please manually invite.")
-				return ""
-			end
+
+			if not hasChar then
+                d("Could not find player name for " .. acctName .. ". Please manually invite.")
+                return ""
+            end
+
+            charName = charName:gsub("%^.+", "")
+
+            d("In Cyrodiil? " .. b(AutoInvite.isCyrodiil()) .. " / Zone: " .. zone)
+
+            if AutoInvite.isCyrodiil() and zone ~= "Cyrodiil" then
+                d("Player " .. charName .. " is not in Cyrodiil but in " .. zone)
+                d("Blocking invite to prevent crashes.")
+                return ""
+            end
+
+            return charName
 		end
 	end
 end