-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoMail.lua
More file actions
149 lines (139 loc) · 3.95 KB
/
AutoMail.lua
File metadata and controls
149 lines (139 loc) · 3.95 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
-- ==================== --
-- AUTOMAIL FUNCTIONS --
-- ==================== --
-- Assess all mail to claim their contents and determine whether to delete them or not.
SLASH_AUTOMAIL1 = "/automail"
SlashCmdList.AUTOMAIL = function (arg)
whitelist = Split(arg, ", ")
mails = GetInboxNumItems()
if mails > 0 then
-- Acquire enclosed item from first available mail or delete the mail if it contains nothing.
message = ""
for i = 1, mails, 1 do
claimed, content = TryClaimMailContent(i, whitelist)
if claimed then
message = "Mail contains " .. content
break
end
end
-- Print the resulting mail transaction to the user.
Print(message)
else
CloseMail()
end
end
-- Claims the content of a mail provided it is free. Returns the status of the mail before any content was claimed.
function TryClaimMailContent(index, whitelist)
packageIcon, stationeryIcon, sender, subject, money, CODAmount, daysLeft, itemCount, wasRead, wasReturned, textCreated = GetInboxHeaderInfo(index)
claimed = false
contentString = ""
-- If sender is whitelisted, leave their mail for the player.
if IsWhitelisted(whitelist, sender) then
return false
end
-- Assess mail content.
if money > 0 then
-- Take money.
contentString = GetMoneyString(money)
TakeInboxMoney(index)
claimed = true
elseif itemCount and itemCount > 0 then
-- Take item.
contentString = AppendIfNotEmpty(contentString, " and ")
contentString = GetNameFromIcon(packageIcon)
if CODAmount == 0 then
TakeInboxItem(index)
claimed = true
else
contentString = contentString .. " awaiting " .. GetMoneyString(CODAmount) .. " COD"
end
else
-- Identify as mail spam.
contentString = "spam"
DeleteInboxItem(index)
claimed = true
if not wasRead then
AddIgnore(sender)
end
end
-- Append sender's name to content string.
contentString = contentString .. " from " .. sender
return claimed, contentString
end
-- Get a string representing the trinity of coins from an arbitrary amount of copper.
function GetMoneyString(copper)
copper = tonumber(copper)
gold, silver = 0
if copper > 0 then
-- Calculate gold, silver and copper values.
gold = math.floor(copper / 10000)
copper = copper - (gold * 10000)
silver = math.floor(copper / 100)
copper = copper - (silver * 100)
end
-- Construct money string.
moneyString = ""
if gold > 0 then
moneyString = gold .. "g"
end
if silver > 0 then
moneyString = AppendIfNotEmpty(moneyString, " ")
affix = silver .. "s"
moneyString = moneyString .. affix
end
if copper > 0 then
moneyString = AppendIfNotEmpty(moneyString, " ")
affix = copper .. "c"
moneyString = moneyString .. affix
end
return moneyString
end
-- Interpret the item name from the icon path.
function GetNameFromIcon(icon)
startChar = string.find(icon, "_%u") + 1
endChar = string.find(icon, "_%d")
endChar = endChar and endChar - 1 or string.len(icon)
icon = string.sub(icon, startChar, endChar)
icon = string.gsub(icon, "_", " ")
return icon
end
-- Determine whether a sender is whitelisted.
function IsWhitelisted(whitelist, sender)
for i = 0, table.getn(whitelist), 1 do
if sender == whitelist[i] then
return true
end
end
end
-- ============================== --
-- HELPER AND UTILITY FUNCTIONS --
-- ============================== --
-- Splits a string separated by a delimiter into an array of strings.
function Split(str, delimiter)
array = {}
n = string.len(str)
for i = 0, n, 1 do
index = string.find(str, delimiter)
if index then
name = string.sub(str, 0, index - 1)
str = string.sub(str, index + 2)
array[i] = name
elseif table.getn(array) > 0 then
array[i] = str
break
else
break
end
end
return array
end
-- Appends an affix to a base string, if the base is not an empty string.
function AppendIfNotEmpty(base, affix)
return string.len(base) > 0 and base .. affix or ""
end
-- Print a message to the console, if it exists.
function Print(message)
if DEFAULT_CHAT_FRAME then
DEFAULT_CHAT_FRAME:AddMessage(message)
end
end