Advertisement
LenweSaralonde

TransFaction.lua (modified for raids)

Dec 7th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.05 KB | None | 0 0
  1. --[[
  2.     Trans Faction (modified)
  3.  
  4.     Version : 2.1.0
  5.     Date    : 23/10/2012
  6.     Author  : Lenwë
  7.  
  8.     TODO:
  9.     - Fix long repeating messages
  10.     - Forbid listening other faction's characters.
  11. ]]
  12.  
  13. -- ===========================================
  14. -- Core
  15. -- ===========================================
  16.  
  17. local TransFaction_OldChatFrame_OnEvent
  18.  
  19. -- TransFaction_InitVars()
  20. --
  21. -- Init configuration variables
  22.  
  23. function TransFaction_InitVars()
  24.     TransFactionVersion = 'v2.1.0'
  25.  
  26.     TransFactionForward = nil
  27.     TransFactionIncomingMessages      = {}
  28.     TransFactionIncomingAddonMessages = {}
  29.     TransFactionLastSentMessage       = ''
  30.     TransFactionListenList            = {}
  31. end
  32.  
  33. -- (string) TransFaction_Trim( (string) str)
  34. --
  35. -- Remove spaces at the beginning and end of the string and turns
  36. -- multiple spaces into a single one
  37.  
  38. function TransFaction_Trim(str)
  39.     if(s == nil) then return "" end
  40.  
  41.     str = string.gsub(str, "%s+", " ")
  42.     str = string.gsub(str, "^%s", "")
  43.     str = string.gsub(str, "%s$", "")
  44.  
  45.     return str
  46. end
  47.  
  48.  
  49. -- TransFaction_OnLoad()
  50. --
  51. -- Script initialization.
  52.  
  53. function TransFaction_OnLoad()
  54.     TransFaction_InitVars();
  55.     TransFactionFrame:RegisterEvent("VARIABLES_LOADED")
  56.     TransFactionFrame:RegisterEvent("CHAT_MSG_EMOTE")
  57.     TransFactionFrame:RegisterEvent("CHAT_MSG_YELL")
  58.     TransFactionFrame:RegisterEvent("CHAT_MSG_SAY")
  59.     TransFactionFrame:RegisterEvent("CHAT_MSG_BN_WHISPER")
  60.     TransFactionFrame:RegisterEvent("CHAT_MSG_RAID")
  61.     TransFactionFrame:RegisterEvent("CHAT_MSG_RAID_LEADER")
  62.  
  63.     TransFactionFrame:SetScript("OnEvent",  TransFaction_OnEvent)
  64.  
  65.     TransFaction_OldChatFrame_OnEvent = ChatFrame_MessageEventHandler
  66.     ChatFrame_MessageEventHandler  = TransFaction_ChatFrame_OnEvent
  67. end
  68.  
  69. function TransFaction_ChatFrame_OnEvent(event, ... )
  70.     local arg1, arg2 = ...
  71.  
  72.     if (arg2 ~= nil and string.find(arg2, "^TF¶")) then
  73.         return
  74.     end
  75.  
  76.     TransFaction_OldChatFrame_OnEvent(event, ...)
  77. end
  78.  
  79. -- TransFaction_EnableForwarding((string) player)
  80. --
  81. -- Enable forwarding to player. Disable if player is nil
  82.  
  83. function TransFaction_EnableForwarding(player)
  84.     if ((player == '') or (player == nil)) then
  85.         DEFAULT_CHAT_FRAME:AddMessage("Forwarding disabled.", 1, 0, 1)
  86.         TransFactionForward = nil
  87.         return
  88.     end
  89.  
  90.     player = string.lower(player)
  91.  
  92.     local i, presenceID, presenceName, battleTag, isBattleTagPresence, toonName, toonID, client
  93.  
  94.     for i = 1, BNGetNumFriends(), 1 do
  95.         presenceID, presenceName, battleTag, isBattleTagPresence, toonName, toonID, client = BNGetFriendInfo(i)
  96.  
  97.         if (client == BNET_CLIENT_WOW and toonName ~= nil and player == string.lower(toonName)) then
  98.             DEFAULT_CHAT_FRAME:AddMessage("Forwarding enabled to " .. toonName .. ".", 1, 0, 1)
  99.             TransFactionForward = presenceID
  100.             return
  101.         end
  102.     end
  103.  
  104.     DEFAULT_CHAT_FRAME:AddMessage("Unknown player",1,0,0)
  105.     PlaySoundFile("Sound\\interface\\Error.wav")
  106. end
  107.  
  108. -- TransFaction_Listen((string) player)
  109. --
  110. -- Add player to listen list
  111.  
  112. function TransFaction_Listen(player)
  113.     if ((player == '') or (player == nil)) then
  114.         local p, str
  115.         DEFAULT_CHAT_FRAME:AddMessage("Listening to:", 1, 0, 1)
  116.         str = ''
  117.         for p,_ in pairs(TransFactionListenList) do
  118.             str = str..' '..p
  119.         end
  120.         DEFAULT_CHAT_FRAME:AddMessage(str, 1, 0, 1)
  121.  
  122.         return
  123.     end
  124.  
  125.     player = string.gsub(player, "-.+", "") -- Remove realm name
  126.     player = TransFaction_UppercaseFirst(player)
  127.  
  128.     DEFAULT_CHAT_FRAME:AddMessage("Listening to "..player..".", 1, 0, 1)
  129.     TransFactionListenList[player] = player
  130. end
  131.  
  132. -- TransFaction_StopListen((string) player)
  133. --
  134. -- Remove player from listen list
  135.  
  136. function TransFaction_StopListen(player)
  137.     if ((player == '') or (player == nil)) then
  138.         return
  139.     end
  140.  
  141.     player = string.gsub(player, "-.+", "") -- Remove realm name
  142.     player = TransFaction_UppercaseFirst(player)
  143.  
  144.     DEFAULT_CHAT_FRAME:AddMessage("No longer listening to "..player..".", 1, 0, 1)
  145.     TransFactionListenList[player] = nil
  146. end
  147.  
  148. -- TransFaction_ListenNone()
  149. --
  150. -- Clear listen list
  151.  
  152. function TransFaction_ListenNone()
  153.     DEFAULT_CHAT_FRAME:AddMessage("No longer listening to anyone.", 1, 0, 1)
  154.     TransFactionListenList = {}
  155. end
  156.  
  157. -- TransFaction_SendAddonMessage(message)
  158. --
  159. -- Forward chat message to other player
  160.  
  161. function TransFaction_SendAddonMessage(message)
  162.     if (TransFactionForward == nil) then
  163.         return
  164.     end
  165.  
  166.     local chunk, word, space
  167.  
  168.     message = string.gsub(message, '|', '¦')
  169.  
  170.     while (message ~= "") do
  171.         chunk   = string.sub(message, 1, 240)
  172.         message = string.sub(message, 241)
  173.  
  174.         if (message == "") then
  175.             chunk = chunk .. '¶'
  176.         end
  177.  
  178.         BNSendWhisper(TransFactionForward, 'TF¶' .. chunk)
  179.     end
  180. end
  181.  
  182. -- (string) TransFaction_UppercaseFirst( (string) str)
  183. --
  184. -- Return the lowercase string str with the first letter capitalized
  185. -- Multibyte support
  186.  
  187. function TransFaction_UppercaseFirst(str)
  188.     local i = 1
  189.  
  190.     while (string.len(string.upper(string.sub(str, 1, i))..string.lower(string.sub(str, i + 1))) < string.len(str)) do
  191.         i = i + 1
  192.     end
  193.  
  194.     return string.upper(string.sub(str, 1, i))..string.lower(string.sub(str, i + 1))
  195. end
  196.  
  197. -- TransFaction_OnEvent(this, event, ...)
  198. --
  199. -- Event handler
  200.  
  201. function TransFaction_OnEvent(this, event, ...)
  202.     local element, chunk, player
  203.     local arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13 = ...
  204.  
  205.     -- Init saved variables
  206.     if (event == "VARIABLES_LOADED") then
  207.         local s = string.gsub('TransFaction VERSION loaded.', "VERSION",    TransFactionVersion)
  208.         DEFAULT_CHAT_FRAME:AddMessage(s,1,1,1)
  209.         return
  210.  
  211.     -- AddonMessage
  212.     elseif (event == "CHAT_MSG_BN_WHISPER") then
  213.  
  214.         chunk = string.match(arg1, "^TF¶(.*)")
  215.         if (chunk == nil) then
  216.             return
  217.         end
  218.         -- Add chunk to message
  219.         if (TransFactionIncomingAddonMessages[arg13] == nil) then
  220.             TransFactionIncomingAddonMessages[arg13] = chunk
  221.         else
  222.             TransFactionIncomingAddonMessages[arg13] = TransFactionIncomingAddonMessages[arg13] .. chunk
  223.         end
  224.  
  225.         -- Final message received:
  226.         if (string.find(TransFactionIncomingAddonMessages[arg13], "¶$")) then
  227.             local addonMessage = string.gsub(TransFactionIncomingAddonMessages[arg13], '¶', '')
  228.             addonMessage = string.gsub(addonMessage, '¦', '|')
  229.             TransFactionIncomingAddonMessages[arg13] = nil
  230.             TransFaction_ExecuteCommand(addonMessage)
  231.         end
  232.  
  233.     -- Get an incoming chat message
  234.     else
  235.         -- Forwarding disabled : skip
  236.         if (TransFactionForward == nil) then
  237.             return
  238.         end
  239.  
  240.         -- Player not in listen list
  241.         player = string.gsub(arg2, "-.+", "") -- Remove realm name
  242.         if (TransFactionListenList[player] == nil) then
  243.             return
  244.         end
  245.  
  246.         -- I told that !
  247.         if ((arg1 == string.sub(TransFactionLastSentMessage, 1, 255)) and (arg2 == UnitName("player"))) then
  248.             return
  249.         end
  250.  
  251.         local mode = ''
  252.         local param = ''
  253.         local s1, s2
  254.         local message = arg1
  255.  
  256.         if     (event == "CHAT_MSG_SAY") then
  257.             command = 'S '..arg2..': '..message
  258.         elseif (event == "CHAT_MSG_YELL") then
  259.             command = 'Y '..arg2..': '..message
  260.         elseif (event == "CHAT_MSG_EMOTE") then
  261.             command = 'S ** '..arg2..' '..message..' **'
  262.         elseif ((event == "CHAT_MSG_RAID") or (event == "CHAT_MSG_RAID_LEADER")) then
  263.             command = 'R '..arg2..': '..message        
  264.         else
  265.             return
  266.         end
  267.         -- Foward to other character
  268.         TransFaction_SendAddonMessage(command)
  269.     end
  270. end
  271.  
  272. -- TransFaction_ExecuteCommand((string) commandLine)
  273. --
  274. -- Execute the given command
  275.  
  276. function TransFaction_ExecuteCommand(commandLine)
  277.     local c, a, a1, a2
  278.     local commandName, args, arg1, arg2
  279.  
  280.     -- Get command and parameters
  281.     commandName = ''
  282.     args = ''
  283.     arg1 = ''
  284.     arg2 = ''
  285.     for c, a in string.gmatch(commandLine, "%s*(%a+)%s*(.*)") do
  286.         if (c == nil) then commandName = '' else commandName = string.lower(c) end
  287.         if (a == nil) then args = '' else args = a end
  288.  
  289.         for a1, a2 in string.gmatch(args, "%s*([^%s]+)%s*(.*)") do
  290.             if (a1 == nil) then arg1 = '' else arg1 = a1 end
  291.             if (a2 == nil) then arg2 = '' else arg2 = a2 end
  292.         end
  293.     end
  294.  
  295.     local player = args
  296.     if ((player == nil) or string.find(player, "^%s*$")) then
  297.         player = UnitName("target")
  298.     end
  299.  
  300.     if (commandName == "forward") then
  301.         TransFaction_EnableForwarding(player)
  302.  
  303.     elseif (commandName == "listen") then
  304.         TransFaction_Listen(player)
  305.  
  306.     elseif (commandName == "stoplisten") then
  307.         TransFaction_StopListen(player)
  308.  
  309.     elseif (commandName == "listennone") then
  310.         TransFaction_ListenNone()
  311.  
  312.     elseif (commandName == "s") then
  313.         TransFactionLastSentMessage = args
  314.         SendChatMessage(args, 'SAY')
  315.  
  316.     elseif (commandName == "y") then
  317.         TransFactionLastSentMessage = args
  318.         SendChatMessage(args, 'YELL')
  319.  
  320.     elseif (commandName == "e") then
  321.         TransFactionLastSentMessage = args
  322.         SendChatMessage(args, 'EMOTE')
  323.        
  324.     elseif (commandName == "r") then
  325.         TransFactionLastSentMessage = arg2
  326.         SendChatMessage(arg2, 'RAID')  
  327.  
  328.     else
  329.         DEFAULT_CHAT_FRAME:AddMessage("Unknown command",1,0,0)
  330.         PlaySoundFile("Sound\\interface\\Error.wav")
  331.     end
  332. end
  333.  
  334.  
  335. -- ===========================================
  336. -- slash commands
  337. -- ===========================================
  338.  
  339. -- /trans option [value [value2]..]
  340. --
  341. --
  342.  
  343. SlashCmdList["TRANS"] = TransFaction_ExecuteCommand
  344.  
  345. SLASH_TRANS1 = "/trans"
  346. SLASH_TRANS2 = "/tf"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement