Repository navigation
Expand file tree
/
Copy pathParcel.lua
More file actions
419 lines (358 loc) · 12.5 KB
/
Copy pathParcel.lua
File metadata and controls
419 lines (358 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
local ADDON, ns = ...
local Parcel = LibStub("AceAddon-3.0"):NewAddon(ADDON, "AceConsole-3.0", "AceEvent-3.0")
ns.Addon = Parcel
_G.Parcel = Parcel
local Compat = ns.Compat
local Options = ns.Options
local Mail = ns.Mail
local Queue = ns.Queue
local defaults = {
profile = {
modules = {},
collect = {
minInterval = 0.30,
keepFreeSlots = 1,
verbose = true,
deleteEmptied = true,
readNext = true,
postmasterNames = "",
-- Everything on, so a fresh install collects the way it always did.
filters = {
ahSold = true, ahWon = true, ahExpired = true,
ahCancelled = true, ahOutbid = true,
postmaster = true, player = true,
},
},
send = {
autoSubject = true,
autofillRecipient = true,
},
archive = {
retentionDays = 60,
maxEntries = 5000,
},
alerts = {
expiryDays = 3,
},
auction = {
countVendor = true,
announceVendor = true,
period = "month",
},
minimap = {
hide = false,
},
ui = {
theme = "blizzard",
moneyIcons = true,
minimapIcon = "envelope",
hideBlizzardMail = true,
position = {},
},
},
global = {
characters = {},
recipients = {},
known = {},
archive = { entries = {} },
vendor = { entries = {} },
pending = { entries = {} },
drafts = {},
},
}
function Parcel:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("ParcelDB", defaults, true)
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
AceConfig:RegisterOptionsTable(ADDON .. "_options", Options:Build(self))
self.optionsFrame = AceConfigDialog:AddToBlizOptions(ADDON .. "_options", "Parcel")
AceConfig:RegisterOptionsTable(ADDON .. "_profiles", LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db))
AceConfigDialog:AddToBlizOptions(ADDON .. "_profiles", "Profiles", "Parcel")
self.db.RegisterCallback(self, "OnProfileChanged", "ApplyProfile")
self.db.RegisterCallback(self, "OnProfileCopied", "ApplyProfile")
self.db.RegisterCallback(self, "OnProfileReset", "ApplyProfile")
self:ApplyProfile()
end
-- The framework resolves its theme at file scope, before AceDB exists, so the
-- saved choice is pushed across once the profile is available.
function Parcel:ApplyProfile()
ns.Theme:Apply()
end
function Parcel:OnEnable()
self:RegisterChatCommand("parcel", "HandleCommand")
ns.Migrations:Run()
end
function Parcel:ResetWindowPositions()
ns.Window:ResetPosition()
self:Print("Window position reset.")
end
function Parcel:GetCollectSetting(info)
return self.db.profile.collect[info[#info]]
end
function Parcel:SetCollectSetting(info, value)
self.db.profile.collect[info[#info]] = value
end
function Parcel:GetFilterSetting(info)
return ns.Filters:IsEnabled(info[#info])
end
function Parcel:SetFilterSetting(info, value)
ns.Filters:SetEnabled(info[#info], value)
end
function Parcel:GetFeatureSetting(info)
return ns.Features:IsEnabled(info[#info])
end
function Parcel:SetFeatureSetting(info, value)
ns.Features:SetEnabled(info[#info], value)
end
function Parcel:GetSendSetting(info)
return self.db.profile.send[info[#info]]
end
function Parcel:SetSendSetting(info, value)
self.db.profile.send[info[#info]] = value
end
function Parcel:GetAlertSetting(info)
return self.db.profile.alerts[info[#info]]
end
function Parcel:SetAlertSetting(info, value)
self.db.profile.alerts[info[#info]] = value
end
function Parcel:GetArchiveSetting(info)
return self.db.profile.archive[info[#info]]
end
function Parcel:SetArchiveSetting(info, value)
self.db.profile.archive[info[#info]] = value
end
function Parcel:GetUISetting(info)
return self.db.profile.ui[info[#info]]
end
function Parcel:SetUISetting(info, value)
local key = info[#info]
self.db.profile.ui[key] = value
ns.Theme:Apply()
ns.Events:Trigger("Parcel.UI.Changed", key, value)
end
function Parcel:OpenOptions()
if self.optionsFrame and Settings and Settings.OpenToCategory then
Settings.OpenToCategory(self.optionsFrame.name)
end
end
function Parcel:PrintStatus()
self:Print(("%s, interface %d, profile %s."):format(
Options:GetFlavorLabel(), Compat.interface, self.db:GetCurrentProfile()))
-- What Collect will and will not do rests entirely on this number, so it is
-- said out loud next to the one on the backpack button. A gap between them
-- is a profession bag with room in it, which no mail attachment can use.
local free = Compat:GetFreeBagSlots()
local reported = Compat:ReportedFreeBagSlots()
if reported and reported ~= free then
self:Print(("%d bag slots free for mail, %d free altogether."):format(free, reported))
else
self:Print(("%d bag slots free."):format(free))
end
if not MailFrame or not MailFrame:IsShown() then
self:Print("Open a mailbox to see what is in it.")
return
end
Mail:Refresh()
local shown, total = Mail:GetCounts()
local money, attachments, expiring = Mail:GetTotals()
if total > shown then
self:Print(("%d mails, %d shown. %d attachments and %s waiting."):format(
total, shown, attachments, ns.Money(money)))
else
self:Print(("%d mails, %d attachments and %s waiting."):format(
shown, attachments, ns.Money(money)))
end
if expiring > 0 then
self:Print(("%d expire within a day."):format(expiring))
end
end
-- Parcel learns your characters as you log into them, the same way Postal does,
-- because no client API lists the rest of your account. This says what it knows
-- so the answer is checkable rather than guessed at.
function Parcel:PrintKnownNames()
local characters = ns.Roster:GetCharacters()
local names = {}
for name in pairs(characters) do
names[#names + 1] = name
end
table.sort(names)
if #names == 0 then
self:Print("No characters known yet on this realm and faction.")
else
self:Print("Characters seen: " .. table.concat(names, ", "))
end
local known = {}
for name in pairs(ns.Roster:GetKnown()) do
known[#known + 1] = name
end
table.sort(known)
if #known > 0 then
self:Print("Mailed before: " .. table.concat(known, ", "))
end
self:Print("Log in to a character once and it starts suggesting itself, "
.. "or name one now with /parcel alt add <name>.")
end
BINDING_HEADER_PARCEL = "Parcel"
BINDING_NAME_PARCEL_TOGGLE = "Open or close Parcel"
BINDING_NAME_PARCEL_COLLECT = "Collect all mail"
BINDING_NAME_PARCEL_HISTORY = "Open mail history"
function ParcelBinding_Toggle()
ns.Window:Toggle()
end
function ParcelBinding_Collect()
if not ns.Collect:IsMailOpen() then
ns.Addon:Print("You are not at a mailbox.")
return
end
ns.Collect:Start(ns.Filters:Predicate())
end
function ParcelBinding_History()
ns.Window:Show("history")
end
function Parcel:PrintDiagnostics()
local stats = ns.Archive.stats
self:Print(("Parcel %s on %s, saved data version %s."):format(
ns.Compat:GetAddOnMetadata("Version") or "unknown", ns.Options:GetFlavorLabel(),
tostring(ns.Migrations:Version())))
self:Print(("Mail session open: %s. Queue running: %s."):format(
tostring(ns.Collect:IsMailOpen()), tostring(ns.Queue:IsRunning())))
self:Print(("Captures %d, records seen %d, created %d, updated %d, incomplete %d, no store %d."):format(
stats.captures, stats.seen, stats.created, stats.updated, stats.incomplete, stats.noStore))
self:Print(("History holds %d entries for %s."):format(
ns.Archive:Count(), ns.Archive:CurrentCharacter()))
ns.Mail:Refresh()
local records = ns.Mail:GetRecords()
self:Print(("Inbox right now: %d records."):format(#records))
local matched, missing = 0, 0
for index, record in ipairs(records) do
local entry = ns.Archive:EntryFor(record)
if entry then
matched = matched + 1
else
missing = missing + 1
if missing <= 5 then
self:Print((" no entry: [%s] from %s, %.4f days left, items=%d, %s"):format(
tostring(record.subject), tostring(record.sender),
record.daysLeft or -1, record.itemCount or 0,
ns.Archive:WhyIncomplete(record) or "complete"))
-- Which slots the header is claiming against what the client
-- will actually name, because a header saying it carries
-- something the slots do not is what holds a mail unfiled.
local slots = {}
for slot = 1, ATTACHMENTS_MAX_RECEIVE do
if ns.Mail:HasAttachment(record.index, slot) then
slots[#slots + 1] = ("%d=%s"):format(slot,
tostring(GetInboxItem(record.index, slot)))
end
end
self:Print(" slots: " .. (#slots > 0 and table.concat(slots, ", ") or "none"))
end
end
end
self:Print(("Of those, %d already have a history entry and %d do not."):format(matched, missing))
for index, record in ipairs(records) do
if index <= 6 then
self:Print((" live: [%s] %s type=%s money=%d"):format(
tostring(record.sender), tostring(record.subject),
tostring(record.mailType), record.money or 0))
end
end
local entries = ns.Archive:GetEntries()
local first = math.max(1, #entries - 7)
self:Print(("Last %d history entries:"):format(math.min(8, #entries)))
for index = first, #entries do
local entry = entries[index]
self:Print((" [%s] %s dir=%s type=%s disp=%s money=%d char=%s"):format(
tostring(entry.who), tostring(entry.subj), tostring(entry.dir),
tostring(entry.mtype), tostring(entry.disp), entry.money or 0,
tostring(entry.char)))
end
local committed, settled, pending = ns.Ledger:Stats()
self:Print(("Ledger: %d committed, %d settled, %d pending."):format(committed, settled, pending))
local counts, waiting = ns.Archive:DispositionCounts()
local parts = {}
for disposition, count in pairs(counts) do
parts[#parts + 1] = ("%s %d"):format(disposition, count)
end
table.sort(parts)
self:Print("History by state: " .. table.concat(parts, ", "))
self:Print(("Still waiting: %d received mails."):format(waiting))
local vendorTotal = ns.Auction:VendorTotals(nil, ns.Archive:CurrentCharacter())
self:Print(("Vendor sales: %d recorded, worth %s, counted in profit: %s."):format(
ns.Vendor:Count(), ns.Money(vendorTotal), tostring(ns.Auction:CountsVendor())))
self:Print(("Item names learned: %d."):format(ns.ItemNames:Count()))
self:Print(("Settles: %d by transaction, %d by handle, %d missed."):format(
stats.settledByLedger, stats.settledByHandle, stats.settleMissed))
end
function Parcel:PrintArchiveSize()
local entries = ns.Archive:Count()
local bytes = ns.Archive:EstimateBytes()
local limit = self.db.profile.archive.maxEntries
self:Print(("History holds %d of a possible %d entries, roughly %.0f KB."):format(
entries, limit, bytes / 1024))
if entries >= limit * 0.9 then
self:Print("Close to the ceiling. The oldest entries go first when it is reached.")
end
end
-- Character names keep their capitalisation, so only the verb is lowercased.
function Parcel:HandleAltCommand(rest)
local verb, name = rest:match("^(%S*)%s*(.*)$")
verb = (verb or ""):lower()
name = strtrim(name or "")
if verb == "add" and name ~= "" then
if ns.Roster:AddAlt(name) then
self:Print(("Added %s. It will suggest itself when you type."):format(name))
else
self:Print(("%s is already known."):format(name))
end
elseif (verb == "remove" or verb == "delete") and name ~= "" then
if ns.Roster:RemoveAlt(name) then
self:Print(("Removed %s."):format(name))
else
self:Print(("%s was not in the list."):format(name))
end
else
self:Print("Usage: /parcel alt add <name>, or /parcel alt remove <name>.")
end
end
function Parcel:HandleCommand(input)
local word, rest = strtrim(input or ""):match("^(%S*)%s*(.*)$")
local command = (word or ""):lower()
rest = rest or ""
if command == "" or command == "config" or command == "options" then
self:OpenOptions()
elseif command == "status" then
self:PrintStatus()
elseif command == "collect" then
ns.Collect:Start(ns.Filters:Predicate())
elseif command == "stop" then
ns.Collect:Stop()
elseif command == "alts" or command == "names" then
self:PrintKnownNames()
elseif command == "alt" then
self:HandleAltCommand(rest)
elseif command == "open" or command == "show" then
ns.Window:Show()
elseif command == "minimap" then
local wanted = not ns.Features:IsEnabled("broker")
ns.Features:SetEnabled("broker", wanted)
self:Print(wanted and "Minimap button shown." or "Minimap button hidden.")
elseif command == "diag" then
self:PrintDiagnostics()
elseif command == "size" then
self:PrintArchiveSize()
elseif command == "expiring" then
ns.Alerts:Report(true)
elseif command == "history" or command == "outbox" then
-- Works away from a mailbox, which is the point of keeping an archive.
ns.Window:Show("history")
else
-- Built from the same table the Help tab reads, so the two cannot drift.
local names = {}
for _, entry in ipairs(ns.Options.commands) do
if entry[1] ~= "" then names[#names + 1] = entry[1] end
end
self:Print("Unknown command. Try: " .. table.concat(names, ", ") .. ".")
end
end