Advertisement
o5a

Postal 7.3 char name fix (BlackBook.lua)

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