diff --git a/ChatSaver.lua b/ChatSaver.lua index 36f3406..958fa62 100644 --- a/ChatSaver.lua +++ b/ChatSaver.lua @@ -13,9 +13,14 @@ function core:OnInitialize() end function core:OnEnable() + self:RawHook(SlashCmdList,'JOIN','JoinChannel',true); + self:RawHook(SlashCmdList,'LEAVE','LeaveChannel',true); self:Hook('ToggleChatChannel','ToggleChatChannel',true); self:RegisterEvent('CHANNEL_UI_UPDATE','RejoinChannels'); + if(core.firstrun) then + self:RegisterEvent('CHAT_MSG_CHANNEL_NOTICE','SetupChatSaver'); + end end function core:SlashCommand() @@ -23,25 +28,12 @@ function core:SlashCommand() end function core:RejoinChannels(...) - print('RejoinChannels'); - if(core.firstrun) then - print('setup cat server'); - core:SetupChatSaver(); - end - local currentChannels = {}; for i = 1,select('#',GetChannelList()) do currentChannels[select(i,GetChannelList())] = true end - local sortedChannels = {}; for channel,_ in pairs(ChatSaverDB) do - table.insert(sortedChannels,channel); - end - - table.sort(sortedChannels, function(a,b) return ChatSaverDB[a].index < ChatSaverDB[b].index end); - - for _,channel in pairs(sortedChannels) do if(currentChannels[channel] == nil) then JoinPermanentChannel(channel); -- does not place in chat frame properly for index,_ in pairs(ChatSaverDB[channel].frames) do @@ -51,11 +43,9 @@ function core:RejoinChannels(...) end self:UnregisterEvent('CHANNEL_UI_UPDATE'); - self:RegisterEvent('CHAT_MSG_CHANNEL_NOTICE','ProcessChannelChanges'); end -function core:SetupChatSaver() - print('SetupChatSaver()'); +function core:SetupChatSaver(...) for frame = 1,NUM_CHAT_WINDOWS do local chatWindowChannels = { GetChatWindowChannels(frame) }; for i = 1,#chatWindowChannels,2 do @@ -72,36 +62,32 @@ function core:SetupChatSaver() end end end + + self:UnregisterEvent('CHAT_MSG_CHANNEL_NOTICE'); end -function core:ProcessChannelChanges(_,message,_,_,_,_,_,_,index,name,...) - print('ProcessChannelChanges()'); - --no. on rejoins and change stored frames - if(message == 'YOU_JOINED') then - local zone = 1; - for frame = 1,NUM_CHAT_WINDOWS do - local chatWindowChannels = { GetChatWindowChannels(frame) }; - for i = 1,#chatWindowChannels,2 do - if(chatWindowChannels[i] == name) then - zone = chatWindowChannels[i + 1]; - break; - end - end - end +function core:JoinChannel(msg) + self.hooks[SlashCmdList].JOIN(msg); + + local name = gsub(msg, "%s*([^%s]+).*", "%1"); - if(zone == 0) then - print('Saving channel ',name); - ChatSaverDB[name] = {}; - ChatSaverDB[name]['frames'] = {}; - ChatSaverDB[name]['index'] = index; - ChatSaverDB[name]['frames'][DEFAULT_CHAT_FRAME:GetID()] = true; - end - elseif(message == 'YOU_LEFT') then - print('Removing channel ',name); - --ChatSaverDB[name] = nil; + if(strlen(name) > 0 and string.match(name,"%a+")) then + ChatSaverDB[name] = {}; + ChatSaverDB[name]['frames'] = {}; + ChatSaverDB[name]['index'] = GetChannelName(name); + ChatSaverDB[name]['frames'][DEFAULT_CHAT_FRAME:GetID()] = true; end end +function core:LeaveChannel(msg) + self.hooks[SlashCmdList].LEAVE(msg); + + local number = gsub(msg, "%s*([^%s]+).*", "%1"); + local _,name = GetChannelName(number); + + ChatSaverDB[name] = nil; +end + function core:ToggleChatChannel(checked,channel) if(ChatSaverDB[channel] == nil) then return;