tekkub / friendswithbenefits

WoW Addon - Syncs your friend list

This URL has Read+Write access

friendswithbenefits / FriendsWithBenefits.lua
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 1 ----------------------
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 2 -- Locals --
3 ----------------------
4
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 5 local db, currop, currfriend, hasannounced
6 local initadds, friendlist = true, {}
7
8
9 ------------------------------
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 10 -- Util Functions --
11 ------------------------------
12
13 local function Print(...) print("|cFF33FF99Friends With Benefits|r:", ...) end
14
15 local debugf = tekDebug and tekDebug:GetFrame("FriendsWithBenefits")
16 local function Debug(...) if debugf then debugf:AddMessage(string.join(", ", tostringall(...))) end end
17
18
19 -----------------------------
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 20 -- Event Handler --
21 -----------------------------
22
23 local f = CreateFrame("frame")
24 f:SetScript("OnEvent", function(self, event, ...) if self[event] then return self[event](self, event, ...) end end)
25 f:RegisterEvent("ADDON_LOADED")
26
27
28 function f:ADDON_LOADED(event, addon)
29 if addon:lower() ~= "friendswithbenefits" then return end
30
31 LibStub("tekKonfig-AboutPanel").new(nil, "FriendsWithBenefits")
f1482b38 » tekkub 2009-01-09 Add about panel 32
33 Debug("Loading DB")
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 34 local factionrealm = UnitFactionGroup("player").. " - "..GetRealmName()
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 35
36 FriendsWithBenefitsDB = FriendsWithBenefitsDB or {}
37 if FriendsWithBenefitsDB.factionrealm then -- Migrate data from dongle-style DB
38 for i,v in pairs(FriendsWithBenefitsDB.factionrealm) do FriendsWithBenefitsDB[i] = v end
39 FriendsWithBenefitsDB.profileKeys, FriendsWithBenefitsDB.factionrealm = nil
40 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 41 FriendsWithBenefitsDB[factionrealm] = FriendsWithBenefitsDB[factionrealm] or {}
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 42 db = FriendsWithBenefitsDB[factionrealm]
43 db.friends, db.removed, db.notes = db.friends or {}, db.removed or {}, db.notes or {}
44
45 if not db.removed[string.lower(UnitName("player"))] then db.friends[string.lower(UnitName("player"))] = true end
46
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 47 self:UnregisterEvent("ADDON_LOADED")
48 self.ADDON_LOADED = nil
49
50 if IsLoggedIn() then self:PLAYER_LOGIN() else self:RegisterEvent("PLAYER_LOGIN") end
51 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 52
53
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 54 function f:PLAYER_LOGIN()
55 self:RegisterEvent("CHAT_MSG_SYSTEM")
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 56 self:RegisterEvent("FRIENDLIST_UPDATE")
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 57 ShowFriends()
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 58
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 59 self:UnregisterEvent("PLAYER_LOGIN")
60 self.PLAYER_LOGIN = nil
61 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 62
63
64 local orig1 = AddFriend
65 AddFriend = function(name, ...)
66 Debug("Function AddFriend", name, ...)
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 67 currop, currfriend = "ADD", string.lower(name)
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 68 return orig1(name, ...)
69 end
70
71
72 local orig2 = RemoveFriend
73 RemoveFriend = function(i, ...)
74 Debug("Function RemoveFriend", i, ...)
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 75 local name = type(i) == "number" and GetFriendInfo(i) or i
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 76 currop, currfriend = "REM", string.lower(name)
77 return orig2(i, ...)
78 end
79
80
81 local orig3 = SetFriendNotes
1982a808 » tekkub 2009-01-09 Sync friend notes as well 82 SetFriendNotes = function(i, note, ...)
83 Debug("Function SetFriendNotes", i, note, ...)
84 local name = type(i) == "number" and GetFriendInfo(i) or i
85 db.notes[string.lower(name)] = note
86 return orig3(i, note, ...)
87 end
88
89
90 -- ERR_FRIEND_ERROR = "Unknown friend response from server."
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 91 -- ERR_FRIEND_NOT_FOUND = "Player not found."
92 -- ERR_FRIEND_WRONG_FACTION = "Friends must be part of your alliance."
93 local rxadd = string.gsub(ERR_FRIEND_ADDED_S, "%%s", "(.+)") -- ERR_FRIEND_ADDED_S = "%s added to friends."
94 local rxrem = string.gsub(ERR_FRIEND_REMOVED_S, "%%s", "(.+)") -- ERR_FRIEND_REMOVED_S = "%s removed from friends list."
95 function f:CHAT_MSG_SYSTEM(event, text)
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 96 if text == ERR_FRIEND_ERROR then return self:Abort("An error has occured.") end
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 97
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 98 local _, _, addname = string.find(text, rxadd)
99 local remname = not addname and select(3, string.find(text, rxrem))
100 if not addname and not remname and (not self.FRIENDLIST_UPDATE or text ~= ERR_FRIEND_NOT_FOUND and text ~= ERR_FRIEND_WRONG_FACTION) then return end
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 101 if not currfriend then return end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 102 Debug("Processing chat message", text, addname, remname, currfriend)
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 103
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 104 if text == ERR_FRIEND_NOT_FOUND then
105 if currop == "REM" then return self:Abort("'Not found' error when removing a friend.") end
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 106 db.removed[currfriend] = true
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 107 db.friends[currfriend] = nil
108 if self.FRIENDLIST_UPDATE then Print(string.format("Cannot find player %q on this realm.", currfriend)) end
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 109 elseif text == ERR_FRIEND_WRONG_FACTION then
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 110 if currop == "REM" then return self:Abort("'Wrong faction' error when removing a friend.") end
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 111 db.removed[currfriend] = true
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 112 db.friends[currfriend] = nil
113 if self.FRIENDLIST_UPDATE then Print(string.format("Player %q is the wrong faction.", currfriend)) end
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 114 elseif addname then
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 115 if currop == "REM" then return self:Abort("'Friend added' message when removing a friend.") end
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 116 if string.lower(addname) ~= currfriend then return self:Abort("Name mismatch while adding a friend.") end
117 Debug("Friend added", currfriend)
118 db.friends[currfriend] = true
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 119 db.removed[currfriend] = nil
120 friendlist[currfriend] = true
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 121 elseif remname then
122 if currop == "ADD" then return self:Abort("'Friend removed' message when adding a friend.") end
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 123 if string.lower(remname) ~= currfriend then return self:Abort("Name mismatch while removing a friend.") end
124 Debug("Friend removed", currfriend)
125 db.removed[currfriend] = true
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 126 db.friends[currfriend] = nil
127 friendlist[currfriend] = nil
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 128 db.notes[currfriend] = nil
1982a808 » tekkub 2009-01-09 Sync friend notes as well 129 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 130
131 if self.FRIENDLIST_UPDATE then self:FRIENDLIST_UPDATE() end
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 132 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 133
134
135 function f:FRIENDLIST_UPDATE(event)
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 136 if event then self:UnregisterEvent("FRIENDLIST_UPDATE") end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 137
138 if initadds then
139 for i=1,GetNumFriends() do
140 if not GetFriendInfo(i) then
322b4d19 » tekkub 2009-03-01 Return out instead, so that... 141 Print("Server returned invalid friend data")
142 return
143 else
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 144 local name, _, _, _, _, _, note = GetFriendInfo(i)
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 145 name = string.lower(name)
146 friendlist[name] = note or ""
147 if db.removed[name] then
148 if not hasannounced then
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 149 Print("Updating friend list. Please do not add or remove friends until complete.")
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 150 hasannounced = true
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 151 end
152 Debug("RemoveFriend", name)
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 153 return RemoveFriend(name)
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 154 else db.friends[name] = true end
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 155 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 156 end
157 initadds = nil
158 end
159
160 for name in pairs(db.friends) do
34a36086 » tekkub 2009-01-09 Migrate SV out of DongleDB ... 161 if not friendlist[name] and string.lower(UnitName("player")) ~= name then
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 162 if not hasannounced then
163 Print("Updating friend list. Please do not add or remove friends until complete.")
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 164 hasannounced = true
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 165 end
166 if name ~= string.lower(UnitName("player")) then
167 Debug("AddFriend", name)
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 168 return AddFriend(name)
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 169 end
170 end
171 end
172
173 for i=1,GetNumFriends() do
1982a808 » tekkub 2009-01-09 Sync friend notes as well 174 local name, _, _, _, _, _, note = GetFriendInfo(i)
175 if not name then
322b4d19 » tekkub 2009-03-01 Return out instead, so that... 176 Print("Server returned invalid friend data")
177 return
178 else
1cac9561 » tekkub 2009-03-01 Give a warning instead of t... 179 name = string.lower(name)
180 if db.notes[name] and db.notes[name] ~= note then SetFriendNotes(name, db.notes[name])
181 elseif note ~= "" then db.notes[name] = note end
182 end
183 end
1982a808 » tekkub 2009-01-09 Sync friend notes as well 184
185 if hasannounced then Print("Update completed.") end
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 186 Debug("Cleaning up")
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 187 self:UnregisterEvent("FRIENDLIST_UPDATE")
188 self.FRIENDLIST_UPDATE, self.Cleanup = nil
189 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 190
191
192 function f:Abort(msg)
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 193 self:UnregisterAllEvents()
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 194 self.FRIENDLIST_UPDATE, self.Cleanup, self.CHAT_MSG_SYSTEM, self.Abort = nil
466cd539 » tekkub 2009-01-09 Migrate event handler away ... 195 self:SetScript("OnEvent", nil)
196 Print(msg, "Disabling for the rest of this session.")
f4d5e65c » tekkub 2009-01-09 Migrate print and debug fun... 197 end
446c5a85 » tekkub 2008-03-25 Updating TOC to 20400 198