Advertisement
Waffles85

WoW 9.01 Pre-Patch Addon Postal BlackBook.lua Fix

Oct 21st, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.96 KB | None | 0 0
  1. local Postal = LibStub("AceAddon-3.0"):GetAddon("Postal")
  2. local Postal_BlackBook = Postal:NewModule("BlackBook", "AceEvent-3.0", "AceHook-3.0")
  3. local L = LibStub("AceLocale-3.0"):GetLocale("Postal")
  4. Postal_BlackBook.description = L["Adds a contact list next to the To: field."]
  5. Postal_BlackBook.description2 = L[ [[|cFFFFCC00*|r This module will list your contacts, friends, guild mates, alts and track the last 10 people you mailed.
  6. |cFFFFCC00*|r It will also autocomplete all names in your BlackBook.]] ]
  7.  
  8. -- luacheck: globals AUTOCOMPLETE_FLAG_ALL AUTOCOMPLETE_FLAG_BNET AUTOCOMPLETE_FLAG_NONE AUTOCOMPLETE_FLAG_FRIEND AUTOCOMPLETE_FLAG_IN_GUILD
  9.  
  10. local Postal_BlackBookButton
  11. local numFriendsOnList = 0
  12. local sorttable = {}
  13. local ignoresortlocale = {
  14.     ["koKR"] = true,
  15.     ["zhCN"] = true,
  16.     ["zhTW"] = true,
  17. }
  18. local enableAltsMenu = true
  19. local enableAllAltsMenu = true
  20. local Postal_BlackBook_Autocomplete_Flags = {
  21.     include = AUTOCOMPLETE_FLAG_ALL,
  22.     exclude = AUTOCOMPLETE_FLAG_BNET,
  23. }
  24.  
  25. function Postal_BlackBook:OnEnable()
  26.     if not Postal_BlackBookButton then
  27.         -- Create the Menu Button
  28.         Postal_BlackBookButton = CreateFrame("Button", "Postal_BlackBookButton", SendMailFrame)
  29.         Postal_BlackBookButton:SetWidth(25)
  30.         Postal_BlackBookButton:SetHeight(25)
  31.         Postal_BlackBookButton:SetPoint("LEFT", SendMailNameEditBox, "RIGHT", -2, 2)
  32.         Postal_BlackBookButton:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Up")
  33.         Postal_BlackBookButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Round")
  34.         Postal_BlackBookButton:SetDisabledTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Disabled")
  35.         Postal_BlackBookButton:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIcon-ScrollDown-Down")
  36.         Postal_BlackBookButton:SetScript("OnClick", function(self, button, down)
  37.             if Postal_DropDownMenu.initialize ~= Postal_BlackBook.BlackBookMenu then
  38.                 CloseDropDownMenus()
  39.                 Postal_DropDownMenu.initialize = Postal_BlackBook.BlackBookMenu
  40.             end
  41.             ToggleDropDownMenu(1, nil, Postal_DropDownMenu, self:GetName(), 0, 0)
  42.         end)
  43.         Postal_BlackBookButton:SetScript("OnHide", Postal_DropDownMenu.HideMenu)
  44.     end
  45.  
  46.     local db = Postal.db.profile.BlackBook
  47.  
  48.     SendMailNameEditBox:SetHistoryLines(15)
  49.     self:RawHook("SendMailFrame_Reset", true)
  50.     self:RawHook("MailFrameTab_OnClick", true)
  51.     if db.UseAutoComplete then
  52.         self:RawHookScript(SendMailNameEditBox, "OnChar")
  53.     end
  54.     self:HookScript(SendMailNameEditBox, "OnEditFocusGained")
  55.     self:RawHook("AutoComplete_Update", true)
  56.     self:RegisterEvent("MAIL_SHOW")
  57.     self:RegisterEvent("PLAYER_ENTERING_WORLD", "AddAlt")
  58.  
  59.     local exclude = bit.bor(db.AutoCompleteFriends and AUTOCOMPLETE_FLAG_NONE or AUTOCOMPLETE_FLAG_FRIEND,
  60.         db.AutoCompleteGuild and AUTOCOMPLETE_FLAG_NONE or AUTOCOMPLETE_FLAG_IN_GUILD)
  61.     Postal_BlackBook_Autocomplete_Flags.include = bit.bxor(
  62.         db.ExcludeRandoms and (bit.bor(AUTOCOMPLETE_FLAG_FRIEND, AUTOCOMPLETE_FLAG_IN_GUILD)) or AUTOCOMPLETE_FLAG_ALL, exclude)
  63.  
  64.     -- apply new flag filter to the editbox
  65.     AutoCompleteEditBox_SetAutoCompleteSource(SendMailNameEditBox, GetAutoCompleteResults, Postal_BlackBook_Autocomplete_Flags.include, Postal_BlackBook_Autocomplete_Flags.exclude)
  66.  
  67.     -- Delete Real ID database. Patch 4.0.1 onwards no longer allows addons to obtain Real ID information.
  68.     Postal.db.global.BlackBook.realID = nil
  69.     db.AutoCompleteRealIDFriends = nil
  70.  
  71.     -- Delete old recent data without faction and realm data
  72.     for i = #Postal.db.profile.BlackBook.recent, 1, -1 do
  73.         local p, r, f = strsplit("|", Postal.db.profile.BlackBook.recent[i])
  74.         if (not r) or (not f) then
  75.             tremove(Postal.db.profile.BlackBook.recent, i)
  76.         end
  77.     end
  78.  
  79.     -- For enabling after a disable
  80.     Postal_BlackBookButton:Show()
  81. end
  82.  
  83. function Postal_BlackBook:OnDisable()
  84.     -- Disabling modules unregisters all events/hook automatically
  85.     SendMailNameEditBox:SetHistoryLines(1)
  86.     Postal_BlackBookButton:Hide()
  87.     SendMailNameEditBox.autoCompleteParams = AUTOCOMPLETE_LIST.MAIL
  88. end
  89.  
  90. function Postal_BlackBook:MAIL_SHOW()
  91.     self:RegisterEvent("MAIL_CLOSED", "Reset")
  92.     self:RegisterEvent("PLAYER_LEAVING_WORLD", "Reset")
  93.     if self.AddAlt then self:AddAlt() end
  94. end
  95.  
  96. function Postal_BlackBook:Reset(event)
  97.     self:UnregisterEvent("MAIL_CLOSED")
  98.     self:UnregisterEvent("PLAYER_LEAVING_WORLD")
  99. end
  100.  
  101. -- We do this once on MAIL_SHOW because UnitFactionGroup() is only valid after
  102. -- PLAYER_ENTERING_WORLD and because Postal might be LoD due to AddOnLoader
  103. -- and PLAYER_ENTERING_WORLD won't fire in that scenerio.
  104. function Postal_BlackBook:AddAlt()
  105.     local realm = GetRealmName()
  106.     local faction = UnitFactionGroup("player")
  107.     local player = UnitName("player")
  108.     local level = UnitLevel("player")
  109.     local _, class = UnitClass("player")
  110.     if not realm or not faction or not player or not level or not class then return end
  111.     local namestring = ("%s|%s|%s|%s|%s"):format(player, realm, faction, level, class)
  112.     local db = Postal.db.global.BlackBook.alts
  113.     enableAltsMenu = false
  114.     enableAllAltsMenu = false
  115.     for i = #db, 1, -1 do
  116.         local p, r, f, l, c = strsplit("|", db[i])
  117.         if p == player and r == realm and f == faction then
  118.             tremove(db, i)
  119.         end
  120.         if p ~= player and r == realm and f == faction then
  121.             enableAltsMenu = true
  122.         end
  123.         if p ~= player and r ~= realm then
  124.             enableAllAltsMenu = true
  125.         end
  126.     end
  127.     tinsert(db, namestring)
  128.     table.sort(db)
  129.     self:UnregisterEvent("PLAYER_ENTERING_WORLD")
  130.     self.AddAlt = nil -- Kill ourselves so we only run it once
  131. end
  132.  
  133. function Postal_BlackBook.DeleteAlt(dropdownbutton, arg1, arg2, checked)
  134.     local realm = GetRealmName()
  135.     local faction = UnitFactionGroup("player")
  136.     local player = UnitName("player")
  137.     local db = Postal.db.global.BlackBook.alts
  138.     enableAltsMenu = false
  139.     enableAllAltsMenu  = false
  140.     for i = #db, 1, -1 do
  141.         if arg1 == db[i] then
  142.             tremove(db, i)
  143.         else
  144.             local p, r, f = strsplit("|", db[i])
  145.             if r == realm and f == faction and p ~= player then
  146.                 enableAltsMenu = true
  147.             end
  148.             if r ~= realm and p ~= player then
  149.             enableAllAltsMenu = true
  150.             end
  151.         end
  152.     end
  153.     CloseDropDownMenus()
  154. end
  155.  
  156. -- Only called on a mail that is sent successfully
  157. function Postal_BlackBook:SendMailFrame_Reset()
  158.     local name = strtrim(SendMailNameEditBox:GetText())
  159.     if name == "" then return self.hooks["SendMailFrame_Reset"]() end
  160.     SendMailNameEditBox:AddHistoryLine(name)
  161.  
  162.     local realm = GetRealmName()
  163.     local faction = UnitFactionGroup("player")
  164.     if not realm or not faction then return self.hooks["SendMailFrame_Reset"]() end
  165.  
  166.     local namestring = ("%s|%s|%s"):format(name, realm, faction)
  167.     local db = Postal.db.profile.BlackBook.recent
  168.     for k = 1, #db do
  169.         if namestring == db[k] then tremove(db, k) break end
  170.     end
  171.     tinsert(db, 1, namestring)
  172.     for k = #db, 21, -1 do
  173.         tremove(db, k)
  174.     end
  175.     local a, b, c = self.hooks["SendMailFrame_Reset"]()
  176.     if Postal.db.profile.BlackBook.AutoFill then
  177.         SendMailNameEditBox:SetText(name)
  178.         SendMailNameEditBox:HighlightText()
  179.     end
  180.     return a, b, c
  181. end
  182.  
  183. function Postal_BlackBook.ClearRecent(dropdownbutton, arg1, arg2, checked)
  184.     wipe(Postal.db.profile.BlackBook.recent)
  185.     CloseDropDownMenus()
  186. end
  187.  
  188. function Postal_BlackBook:MailFrameTab_OnClick(button, tab)
  189.     self.hooks["MailFrameTab_OnClick"](button, tab)
  190.     if Postal.db.profile.BlackBook.AutoFill and tab == 2 then
  191.         local realm = GetRealmName()
  192.         local faction = UnitFactionGroup("player")
  193.         local player = UnitName("player")
  194.  
  195.         -- Find the first eligible recently mailed
  196.         for i = 1, #Postal.db.profile.BlackBook.recent do
  197.             local p, r, f = strsplit("|", Postal.db.profile.BlackBook.recent[i])
  198.             if r == realm and f == faction and p ~= player then
  199.                 if p and SendMailNameEditBox:GetText() == "" then
  200.                     SendMailNameEditBox:SetText(p)
  201.                     SendMailNameEditBox:HighlightText()
  202.                     break
  203.                 end
  204.             end
  205.         end
  206.     end
  207. end
  208.  
  209. function Postal_BlackBook:OnEditFocusGained(editbox, ...)
  210.     -- Most other addons aren't hooking properly and do not pass in editbox at all.
  211.     SendMailNameEditBox:HighlightText()
  212. end
  213.  
  214. function Postal_BlackBook:AutoComplete_Update(editBox, editBoxText, utf8Position, ...)
  215.     if editBox ~= SendMailNameEditBox or not Postal.db.profile.BlackBook.DisableBlizzardAutoComplete then
  216.         self.hooks["AutoComplete_Update"](editBox, editBoxText, utf8Position, ...)
  217.     end
  218. end
  219.  
  220. -- OnChar fires before OnTextChanged
  221. -- OnChar does not fire for Backspace, Delete keys that shorten the text
  222. -- Hook player name autocomplete to look in our dbs first
  223. function Postal_BlackBook:OnChar(editbox, ...)
  224.     if editbox:GetUTF8CursorPosition() ~= strlenutf8(editbox:GetText()) then return end
  225.  
  226.     local db = Postal.db.profile.BlackBook
  227.     local text = strupper(editbox:GetText())
  228.     local textlen = strlen(text)
  229.     local realm = GetRealmName()
  230.     local faction = UnitFactionGroup("player")
  231.     local player = UnitName("player")
  232.     local newname
  233.  
  234.         -- Check all alt list
  235.     if db.AutoCompleteAllAlts then
  236.         local nosptext = text:gsub("%s*","") -- ignore spaces in matching fully-qualified names
  237.         local db = Postal.db.global.BlackBook.alts
  238.         for i = 1, #db do
  239.             local p, r, f = strsplit("|", db[i])
  240.             if p ~= player and r ~= realm then
  241.                 if strfind(strupper(p.."-"..r):gsub("%s*",""), nosptext, 1, 1) == 1 then
  242.                     newname = p.."-"..r
  243.                     break
  244.                 end
  245.             end
  246.         end
  247.     end
  248.  
  249.     -- Check alt list
  250.     if db.AutoCompleteAlts then
  251.        for pass = 1,2 do
  252.         local db = Postal.db.global.BlackBook.alts
  253.         for i = 1, #db do
  254.             local p, r, f = strsplit("|", db[i])
  255.             if r == realm and p ~= player and
  256.             ( (pass == 1 and f ~= faction) or
  257.               (pass == 2 and f == faction) ) -- prefer same faction, but don't require for alts
  258.             then
  259.                 if strfind(strupper(p), text, 1, 1) == 1 then
  260.                     newname = p
  261.                     break
  262.                 end
  263.             end
  264.         end
  265.        end
  266.     end
  267.  
  268.  
  269.     -- Check recent list
  270.     if not newname and db.AutoCompleteRecent then
  271.         local db2 = db.recent
  272.         for j = 1, #db2 do
  273.             local p, r, f = strsplit("|", db2[j])
  274.             if r == realm and f == faction and p ~= player then
  275.                 if strfind(strupper(p), text, 1, 1) == 1 then
  276.                     newname = p
  277.                     break
  278.                 end
  279.             end
  280.         end
  281.     end
  282.  
  283.     -- Check contacts list
  284.     if not newname and db.AutoCompleteContacts then
  285.         local db2 = db.contacts
  286.         for j = 1, #db2 do
  287.             local name = db2[j]
  288.             if strfind(strupper(name), text, 1, 1) == 1 then
  289.                 newname = name
  290.                 break
  291.             end
  292.         end
  293.     end
  294.  
  295.     -- Check RealID friends that are online :: rewrite  due to API changes - Jonny
  296.     if not newname and db.AutoCompleteFriends then
  297.         local numBNetTotal, numBNetOnline = BNGetNumFriends()
  298.         for i = 1, numBNetOnline do
  299.             local accountInfo = C_BattleNet.GetFriendAccountInfo(i)
  300.             if (accountInfo.gameAccountInfo.characterName and accountInfo.gameAccountInfo.clientProgram == BNET_CLIENT_WOW and CanCooperateWithGameAccount(accountInfo) and accountInfo.gameAccountInfo.wowProjectID == 1 ) then
  301.                 if strfind(strupper(accountInfo.gameAccountInfo.characterName), text, 1, 1) == 1 then
  302.                     newname = accountInfo.gameAccountInfo.characterName
  303.                     break
  304.                 end
  305.             end
  306.         end
  307.     end
  308.  
  309.     -- The Blizzard autocomplete is borked for guild. So we implement our own for guild autocomplete
  310.     if not newname and db.AutoCompleteGuild and IsInGuild() then
  311.         local numMembers, numOnline = GetNumGuildMembers(true)
  312.         for i = 1, numMembers do
  313.             local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName, achievementPoints, achievementRank, isMobile, canSoR = GetGuildRosterInfo(i)
  314.             if strfind(strupper(name), text, 1, 1) == 1 then
  315.                 newname = name
  316.                 break
  317.             end
  318.         end
  319.     end
  320.  
  321.     -- Call the original Blizzard function to autocomplete and for its popup
  322.     self.hooks[SendMailNameEditBox].OnChar(editbox, ...)
  323.  
  324.     -- Set our match if we found one (overriding Blizzard's match if there's one)
  325.     if newname then
  326.         editbox:SetText(newname)
  327.         editbox:HighlightText(textlen, -1)
  328.         editbox:SetCursorPosition(textlen)
  329.     end
  330. end
  331.  
  332. function Postal_BlackBook.SetSendMailName(dropdownbutton, arg1, arg2, checked)
  333.     SendMailNameEditBox:SetText(arg1)
  334.     if SendMailNameEditBox:HasFocus() then SendMailSubjectEditBox:SetFocus() end
  335.     CloseDropDownMenus()
  336. end
  337.  
  338. function Postal_BlackBook.AddContact(dropdownbutton, arg1, arg2, checked)
  339.     local name = strtrim(SendMailNameEditBox:GetText())
  340.     if name == "" then return end
  341.     local db = Postal.db.profile.BlackBook.contacts
  342.     for k = 1, #db do
  343.         if name == db[k] then return end
  344.     end
  345.     tinsert(db, name)
  346.     table.sort(db)
  347. end
  348.  
  349. function Postal_BlackBook.RemoveContact(dropdownbutton, arg1, arg2, checked)
  350.     local name = strtrim(SendMailNameEditBox:GetText())
  351.     if name == "" then return end
  352.     local db = Postal.db.profile.BlackBook.contacts
  353.     for k = 1, #db do
  354.         if name == db[k] then tremove(db, k) return end
  355.     end
  356. end
  357.  
  358. function Postal_BlackBook:SortAndCountNumFriends()
  359.     wipe(sorttable)
  360.  
  361.     local numFriends = C_FriendList.GetNumFriends();
  362.     for i = 1, numFriends do
  363.         sorttable[i] = C_FriendList.GetFriendInfoByIndex(i)
  364.     end
  365.  
  366.     -- removed lines causing issues
  367.  
  368.     -- Sort the list
  369.     --if numFriends > 0 and not ignoresortlocale[GetLocale()] then table.sort(sorttable) end
  370.  
  371.     -- Store upvalue
  372.     numFriendsOnList = numFriends
  373.     return numFriends
  374. end
  375.  
  376. function Postal_BlackBook.BlackBookMenu(self, level)
  377.     if not level then return end
  378.     local info = self.info
  379.     wipe(info)
  380.     if level == 1 then
  381.         info.isTitle = 1
  382.         info.text = L["Contacts"]
  383.         info.notCheckable = 1
  384.         UIDropDownMenu_AddButton(info, level)
  385.  
  386.         info.disabled = nil
  387.         info.isTitle = nil
  388.  
  389.         local db = Postal.db.profile.BlackBook.contacts
  390.         for i = 1, #db do
  391.             info.text = db[i]
  392.             info.func = Postal_BlackBook.SetSendMailName
  393.             info.arg1 = db[i]
  394.             UIDropDownMenu_AddButton(info, level)
  395.         end
  396.  
  397.         info.arg1 = nil
  398.         if #db > 0 then
  399.             info.disabled = 1
  400.             info.text = nil
  401.             info.func = nil
  402.             UIDropDownMenu_AddButton(info, level)
  403.             info.disabled = nil
  404.         end
  405.  
  406.         info.text = L["Add Contact"]
  407.         info.func = Postal_BlackBook.AddContact
  408.         UIDropDownMenu_AddButton(info, level)
  409.  
  410.         info.text = L["Remove Contact"]
  411.         info.func = Postal_BlackBook.RemoveContact
  412.         UIDropDownMenu_AddButton(info, level)
  413.  
  414.         info.disabled = 1
  415.         info.text = nil
  416.         info.func = nil
  417.         UIDropDownMenu_AddButton(info, level)
  418.  
  419.         info.hasArrow = 1
  420.         info.keepShownOnClick = 1
  421.         info.func = self.UncheckHack
  422.  
  423.         info.disabled = #Postal.db.profile.BlackBook.recent == 0
  424.         info.text = L["Recently Mailed"]
  425.         info.value = "recent"
  426.         UIDropDownMenu_AddButton(info, level)
  427.  
  428.         info.disabled = not enableAltsMenu
  429.         info.text = L["Alts"]
  430.         info.value = "alt"
  431.         UIDropDownMenu_AddButton(info, level)
  432.  
  433.         info.disabled = not enableAllAltsMenu
  434.         info.text = L["All Alts"]
  435.         info.value = "allalt"
  436.         UIDropDownMenu_AddButton(info, level)
  437.  
  438.         info.disabled = Postal_BlackBook:SortAndCountNumFriends() == 0
  439.         info.text = L["Friends"]
  440.         info.value = "friend"
  441.         UIDropDownMenu_AddButton(info, level)
  442.  
  443.         info.disabled = not IsInGuild()
  444.         info.text = L["Guild"]
  445.         info.value = "guild"
  446.         UIDropDownMenu_AddButton(info, level)
  447.  
  448.         wipe(info)
  449.         info.disabled = 1
  450.         info.notCheckable = 1
  451.         UIDropDownMenu_AddButton(info, level)
  452.         info.disabled = nil
  453.  
  454.         info.text = CLOSE
  455.         info.func = self.HideMenu
  456.         UIDropDownMenu_AddButton(info, level)
  457.  
  458.     elseif level == 2 then
  459.         info.notCheckable = 1
  460.         if UIDROPDOWNMENU_MENU_VALUE == "recent" then
  461.             local realm = GetRealmName()
  462.             local faction = UnitFactionGroup("player")
  463.             local player = UnitName("player")
  464.             local db = Postal.db.profile.BlackBook.recent
  465.             if #db == 0 then return end
  466.             for i = 1, #db do
  467.                 local p, r, f = strsplit("|", db[i])
  468.                 if r == realm and f == faction and p ~= player then
  469.                     info.text = p
  470.                     info.func = Postal_BlackBook.SetSendMailName
  471.                     info.arg1 = p
  472.                     UIDropDownMenu_AddButton(info, level)
  473.                 end
  474.             end
  475.  
  476.             info.disabled = 1
  477.             info.text = nil
  478.             info.func = nil
  479.             info.arg1 = nil
  480.             UIDropDownMenu_AddButton(info, level)
  481.             info.disabled = nil
  482.  
  483.             info.text = L["Clear list"]
  484.             info.func = Postal_BlackBook.ClearRecent
  485.             info.arg1 = nil
  486.             UIDropDownMenu_AddButton(info, level)
  487.  
  488.         elseif UIDROPDOWNMENU_MENU_VALUE == "alt" then
  489.             if not enableAltsMenu then return end
  490.             local db = Postal.db.global.BlackBook.alts
  491.             local realm = GetRealmName()
  492.             local faction = UnitFactionGroup("player")
  493.             local player = UnitName("player")
  494.             info.notCheckable = 1
  495.             for i = 1, #db do
  496.                 local p, r, f, l, c = strsplit("|", db[i])
  497.                 if r == realm and p ~= player then
  498.                     if l and c then
  499.                         local clr = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[c] or RAID_CLASS_COLORS[c]
  500.                         info.text = format("%s |cff%.2x%.2x%.2x(%d %s)|r", p, clr.r*255, clr.g*255, clr.b*255, l, LOCALIZED_CLASS_NAMES_MALE[c])
  501.                     else
  502.                         info.text = p
  503.                     end
  504.                     info.func = Postal_BlackBook.SetSendMailName
  505.                     info.arg1 = p
  506.                     UIDropDownMenu_AddButton(info, level)
  507.                 end
  508.             end
  509.  
  510.             info.disabled = 1
  511.             info.text = nil
  512.             info.func = nil
  513.             info.arg1 = nil
  514.             UIDropDownMenu_AddButton(info, level)
  515.             info.disabled = nil
  516.  
  517.             info.text = L["Delete"]
  518.             info.hasArrow = 1
  519.             info.keepShownOnClick = 1
  520.             info.func = self.UncheckHack
  521.             info.value = "deletealt"
  522.             UIDropDownMenu_AddButton(info, level)
  523.  
  524. elseif UIDROPDOWNMENU_MENU_VALUE == "allalt" then
  525.             if not enableAllAltsMenu then return end
  526.             local db = Postal.db.global.BlackBook.alts
  527.             local realm = GetRealmName()
  528.             local faction = UnitFactionGroup("player")
  529.             local player = UnitName("player")
  530.             local plre = player.."-"..realm
  531.             info.notCheckable = 1
  532.             for i = 1, #db do
  533.                 local p, r, f, l, c = strsplit("|", db[i])
  534.                 local pr = p.."-"..r
  535.                 if (pr ~= plre ) then
  536.                     if l and c then
  537.                         local clr = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[c] or RAID_CLASS_COLORS[c]
  538.                         info.text = format("%s-%s |cff%.2x%.2x%.2x(%d %s)|r", p, r, clr.r*255, clr.g*255, clr.b*255, l, LOCALIZED_CLASS_NAMES_MALE[c])
  539.                     else
  540.                         info.text = ("%s-%s"):format(p, r)
  541.                     end
  542.                     info.func = Postal_BlackBook.SetSendMailName
  543.                     info.arg1 = ("%s-%s"):format(p, r)
  544.                     UIDropDownMenu_AddButton(info, level)
  545.                 end
  546.             end
  547.  
  548.             info.disabled = 1
  549.             info.text = nil
  550.             info.func = nil
  551.             info.arg1 = nil
  552.             UIDropDownMenu_AddButton(info, level)
  553.             info.disabled = nil
  554.  
  555.             info.text = L["Delete"]
  556.             info.hasArrow = 1
  557.             info.keepShownOnClick = 1
  558.             info.func = self.UncheckHack
  559.             info.value = "deleteallalt"
  560.             UIDropDownMenu_AddButton(info, level)
  561.  
  562.             if DropDownList2 then -- ensure long lists stay on screen
  563.                 DropDownList2:SetClampedToScreen(true)
  564.             end
  565.  
  566.         elseif UIDROPDOWNMENU_MENU_VALUE == "friend" then
  567.             -- Friends list
  568.             local numFriends = Postal_BlackBook:SortAndCountNumFriends()
  569.  
  570.             -- 25 or less, don't need multi level menus
  571.             if numFriends > 0 and numFriends <= 25 then
  572.                 for i = 1, numFriends do
  573.                     local f = sorttable[i]
  574.                     info.text = tostring(f.name)
  575.                     info.func = Postal_BlackBook.SetSendMailName
  576.                     info.arg1 = tostring(f.name)
  577.                     UIDropDownMenu_AddButton(info, level)
  578.                 end
  579.             elseif numFriends > 25 then
  580.                 -- More than 25 people, split the list into multiple sublists of 25
  581.                 info.hasArrow = 1
  582.                 info.keepShownOnClick = 1
  583.                 info.func = self.UncheckHack
  584.                 for i = 1, math.ceil(numFriends/25) do
  585.                     info.text  = L["Part %d"]:format(i)
  586.                     info.value = "fpart"..i
  587.                     UIDropDownMenu_AddButton(info, level)
  588.                 end
  589.             end
  590.  
  591.         elseif UIDROPDOWNMENU_MENU_VALUE == "guild" then
  592.             if not IsInGuild() then return end
  593.             local numFriends = GetNumGuildMembers(true)
  594.             for i = 1, numFriends do
  595.                 local name, rank, rankIndex, level, class, zone, note, officernote, online, status, classFileName, achievementPoints, achievementRank, isMobile, canSoR = GetGuildRosterInfo(i)
  596.                 local c = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[classFileName] or RAID_CLASS_COLORS[classFileName]
  597.                 sorttable[i] = format("%s |cffffd200(%s)|r |cff%.2x%.2x%.2x(%d %s)|r", name, rank, c.r*255, c.g*255, c.b*255, level, class)
  598.             end
  599.             for i = #sorttable, numFriends+1, -1 do
  600.                 sorttable[i] = nil
  601.             end
  602.             if not ignoresortlocale[GetLocale()] then table.sort(sorttable) end
  603.             if numFriends > 0 and numFriends <= 25 then
  604.                 for i = 1, numFriends do
  605.                     info.text = sorttable[i]
  606.                     info.func = Postal_BlackBook.SetSendMailName
  607.                     info.arg1 = strmatch(sorttable[i], "(.*) |cffffd200")
  608.                     UIDropDownMenu_AddButton(info, level)
  609.                 end
  610.             elseif numFriends > 25 then
  611.                 -- More than 25 people, split the list into multiple sublists of 25
  612.                 info.hasArrow = 1
  613.                 info.keepShownOnClick = 1
  614.                 info.func = self.UncheckHack
  615.                 for i = 1, math.ceil(numFriends/25) do
  616.                     info.text  = L["Part %d"]:format(i)
  617.                     info.value = "gpart"..i
  618.                     UIDropDownMenu_AddButton(info, level)
  619.                 end
  620.             end
  621.         end
  622.  
  623.     elseif level >= 3 then
  624.         info.notCheckable = 1
  625.         if UIDROPDOWNMENU_MENU_VALUE == "deletealt" or UIDROPDOWNMENU_MENU_VALUE == "deleteallalt" then
  626.             local all = ( UIDROPDOWNMENU_MENU_VALUE == "deleteallalt" )
  627.             local db = Postal.db.global.BlackBook.alts
  628.             local realm = GetRealmName()
  629.             local faction = UnitFactionGroup("player")
  630.             local player = UnitName("player")
  631.             for i = 1, #db do
  632.                 local p, r, f, l, c = strsplit("|", db[i])
  633.                 if p ~= player and ( r == realm or all ) then
  634.                     p = all and p.."-"..r or p
  635.                     if l and c then
  636.                         local clr = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[c] or RAID_CLASS_COLORS[c]
  637.                         info.text = format("%s |cff%.2x%.2x%.2x(%d %s)|r", p, clr.r*255, clr.g*255, clr.b*255, l, LOCALIZED_CLASS_NAMES_MALE[c])
  638.                     else
  639.                         info.text = p
  640.                     end
  641.                     info.func = Postal_BlackBook.DeleteAlt
  642.                     info.arg1 = db[i]
  643.                     UIDropDownMenu_AddButton(info, level)
  644.                 end
  645.             end
  646.  
  647.         elseif strfind(UIDROPDOWNMENU_MENU_VALUE, "fpart") then
  648.             local startIndex = tonumber(strmatch(UIDROPDOWNMENU_MENU_VALUE, "fpart(%d+)")) * 25 - 24
  649.             local endIndex = math.min(startIndex+24, numFriendsOnList)
  650.             for i = startIndex, endIndex do
  651.                 local f = sorttable[i]
  652.                 info.text = tostring(f.name)
  653.                 info.func = Postal_BlackBook.SetSendMailName
  654.                 info.arg1 = tostring(f.name)
  655.                 UIDropDownMenu_AddButton(info, level)
  656.             end
  657.  
  658.         elseif strfind(UIDROPDOWNMENU_MENU_VALUE, "gpart") then
  659.             local startIndex = tonumber(strmatch(UIDROPDOWNMENU_MENU_VALUE, "gpart(%d+)")) * 25 - 24
  660.             local endIndex = math.min(startIndex+24, (GetNumGuildMembers(true)))
  661.             for i = startIndex, endIndex do
  662.                 local name = sorttable[i]
  663.                 info.text = sorttable[i]
  664.                 info.func = Postal_BlackBook.SetSendMailName
  665.                 info.arg1 = strmatch(sorttable[i], "(.*) |cffffd200")
  666.                 UIDropDownMenu_AddButton(info, level)
  667.             end
  668.         end
  669.  
  670.     end
  671. end
  672.  
  673. function Postal_BlackBook.SaveFriendGuildOption(dropdownbutton, arg1, arg2, checked)
  674.     Postal.SaveOption(dropdownbutton, arg1, arg2, checked)
  675.     local db = Postal.db.profile.BlackBook
  676.     local exclude = bit.bor(db.AutoCompleteFriends and AUTOCOMPLETE_FLAG_NONE or AUTOCOMPLETE_FLAG_FRIEND,
  677.         db.AutoCompleteGuild and AUTOCOMPLETE_FLAG_NONE or AUTOCOMPLETE_FLAG_IN_GUILD)
  678.     Postal_BlackBook_Autocomplete_Flags.include = bit.bxor(
  679.         db.ExcludeRandoms and (bit.bor(AUTOCOMPLETE_FLAG_FRIEND, AUTOCOMPLETE_FLAG_IN_GUILD)) or AUTOCOMPLETE_FLAG_ALL, exclude)
  680. end
  681.  
  682. function Postal_BlackBook.SetAutoComplete(dropdownbutton, arg1, arg2, checked)
  683.     local self = Postal_BlackBook
  684.     Postal.db.profile.BlackBook.UseAutoComplete = not checked
  685.     if checked then
  686.         if self:IsHooked(SendMailNameEditBox, "OnChar") then
  687.             self:Unhook(SendMailNameEditBox, "OnChar")
  688.         end
  689.     else
  690.         if not self:IsHooked(SendMailNameEditBox, "OnChar") then
  691.             self:RawHookScript(SendMailNameEditBox, "OnChar")
  692.         end
  693.     end
  694. end
  695.  
  696. function Postal_BlackBook.ModuleMenu(self, level)
  697.     if not level then return end
  698.     local info = self.info
  699.     wipe(info)
  700.     info.isNotRadio = 1
  701.     if level == 1 + self.levelAdjust then
  702.         info.keepShownOnClick = 1
  703.         info.text = L["Autofill last person mailed"]
  704.         info.func = Postal.SaveOption
  705.         info.arg1 = "BlackBook"
  706.         info.arg2 = "AutoFill"
  707.         info.checked = Postal.db.profile.BlackBook.AutoFill
  708.         UIDropDownMenu_AddButton(info, level)
  709.  
  710.         info.hasArrow = 1
  711.         info.keepShownOnClick = 1
  712.         info.func = self.UncheckHack
  713.         info.checked = nil
  714.         info.arg1 = nil
  715.         info.arg2 = nil
  716.         info.text = L["Name auto-completion options"]
  717.         info.value = "AutoComplete"
  718.         UIDropDownMenu_AddButton(info, level)
  719.         local listFrame = _G["DropDownList"..level]
  720.         self.UncheckHack(_G[listFrame:GetName().."Button"..listFrame.numButtons])
  721.  
  722.     elseif level == 2 + self.levelAdjust then
  723.         local db = Postal.db.profile.BlackBook
  724.         info.arg1 = "BlackBook"
  725.  
  726.         if UIDROPDOWNMENU_MENU_VALUE == "AutoComplete" then
  727.             info.text = L["Use Postal's auto-complete"]
  728.             info.arg2 = "UseAutoComplete"
  729.             info.checked = db.UseAutoComplete
  730.             info.func = Postal_BlackBook.SetAutoComplete
  731.             UIDropDownMenu_AddButton(info, level)
  732.  
  733.             info.func = Postal.SaveOption
  734.             info.disabled = not db.UseAutoComplete
  735.             info.keepShownOnClick = 1
  736.  
  737.             info.text = L["Alts"]
  738.             info.arg2 = "AutoCompleteAlts"
  739.             info.checked = db.AutoCompleteAlts
  740.             UIDropDownMenu_AddButton(info, level)
  741.  
  742.             info.text = L["All Alts"]
  743.             info.arg2 = "AutoCompleteAllAlts"
  744.             info.checked = db.AutoCompleteAllAlts
  745.             UIDropDownMenu_AddButton(info, level)
  746.  
  747.             info.text = L["Recently Mailed"]
  748.             info.arg2 = "AutoCompleteRecent"
  749.             info.checked = db.AutoCompleteRecent
  750.             UIDropDownMenu_AddButton(info, level)
  751.  
  752.             info.text = L["Contacts"]
  753.             info.arg2 = "AutoCompleteContacts"
  754.             info.checked = db.AutoCompleteContacts
  755.             UIDropDownMenu_AddButton(info, level)
  756.  
  757.             info.disabled = nil
  758.  
  759.             info.text = L["Friends"]
  760.             info.arg2 = "AutoCompleteFriends"
  761.             info.checked = db.AutoCompleteFriends
  762.             info.func = Postal_BlackBook.SaveFriendGuildOption
  763.             UIDropDownMenu_AddButton(info, level)
  764.  
  765.             info.text = L["Guild"]
  766.             info.arg2 = "AutoCompleteGuild"
  767.             info.checked = db.AutoCompleteGuild
  768.             UIDropDownMenu_AddButton(info, level)
  769.  
  770.             info.text = L["Exclude randoms you interacted with"]
  771.             info.arg2 = "ExcludeRandoms"
  772.             info.checked = db.ExcludeRandoms
  773.             UIDropDownMenu_AddButton(info, level)
  774.  
  775.             info.text = L["Disable Blizzard's auto-completion popup menu"]
  776.             info.arg2 = "DisableBlizzardAutoComplete"
  777.             info.checked = db.DisableBlizzardAutoComplete
  778.             info.func = Postal.SaveOption
  779.             UIDropDownMenu_AddButton(info, level)
  780.         end
  781.     end
  782. end
  783.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement