mirror of
https://github.com/mt-mods/mail.git
synced 2025-04-30 08:21:44 -04:00
Use global contacts functions and reconfigure add/remove functions
This commit is contained in:
parent
b5285fbff1
commit
addc9eb9bc
2 changed files with 63 additions and 29 deletions
41
gui.lua
41
gui.lua
|
@ -236,7 +236,7 @@ end
|
||||||
function mail.compile_contact_list(name, selected, playernames)
|
function mail.compile_contact_list(name, selected, playernames)
|
||||||
-- TODO: refactor this - not just compiles *a* list, but *the* list for the contacts screen (too inflexible)
|
-- TODO: refactor this - not just compiles *a* list, but *the* list for the contacts screen (too inflexible)
|
||||||
local formspec = {}
|
local formspec = {}
|
||||||
local contacts = mail.getContacts(name)
|
local contacts = mail.getPlayerContacts(name)
|
||||||
|
|
||||||
if playernames == nil then
|
if playernames == nil then
|
||||||
local length = 0
|
local length = 0
|
||||||
|
@ -535,8 +535,7 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
|
|
||||||
elseif fields.delete then
|
elseif fields.delete then
|
||||||
if messages[selected_idxs.messages[name]] then
|
if messages[selected_idxs.messages[name]] then
|
||||||
table.remove(messages,selected_idxs.messages[name])
|
mail.setStatus(name, messages[selected_idxs.messages[name]].id, "deleted")
|
||||||
mail.setMessages(name, messages)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if boxtab_index == 1 then
|
if boxtab_index == 1 then
|
||||||
|
@ -564,20 +563,20 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local contacts = mail.getContacts(name)
|
-- add new contacts if some receivers aren't registered
|
||||||
|
local contacts = mail.getPlayerContacts(name)
|
||||||
local recipients = mail.parse_player_list(fields.to)
|
local recipients = mail.parse_player_list(fields.to)
|
||||||
local changed = false
|
local isNew = true
|
||||||
for _,v in pairs(recipients) do
|
for r_,recipient in ipairs(recipients) do
|
||||||
if contacts[string.lower(v)] == nil then
|
for c_,contact in ipairs(contacts) do
|
||||||
contacts[string.lower(v)] = {
|
if contact.name == recipient then
|
||||||
name = v,
|
isNew = false
|
||||||
note = "",
|
break
|
||||||
}
|
end
|
||||||
changed = true
|
end
|
||||||
|
if isNew then
|
||||||
|
mail.addContact(name, {name = recipient, note = ""})
|
||||||
end
|
end
|
||||||
end
|
|
||||||
if changed then
|
|
||||||
mail.setContacts(name, contacts)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.after(0.5, function()
|
minetest.after(0.5, function()
|
||||||
|
@ -612,7 +611,7 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
|
|
||||||
elseif formname == "mail:selectcontact" then
|
elseif formname == "mail:selectcontact" then
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local contacts = mail.getContacts(name)
|
local contacts = mail.getPlayerContacts(name)
|
||||||
local draft = message_drafts[name]
|
local draft = message_drafts[name]
|
||||||
|
|
||||||
-- get indexes for fields with selected rows
|
-- get indexes for fields with selected rows
|
||||||
|
@ -682,7 +681,7 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
|
|
||||||
elseif formname == "mail:contacts" then
|
elseif formname == "mail:contacts" then
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local contacts = mail.getContacts(name)
|
local contacts = mail.getPlayerContacts(name)
|
||||||
|
|
||||||
if fields.contacts then
|
if fields.contacts then
|
||||||
local evt = minetest.explode_table_event(fields.contacts)
|
local evt = minetest.explode_table_event(fields.contacts)
|
||||||
|
@ -752,7 +751,7 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
|
|
||||||
elseif formname == "mail:editcontact" then
|
elseif formname == "mail:editcontact" then
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
local contacts = mail.getContacts(name)
|
local contacts = mail.getPlayerContacts(name)
|
||||||
|
|
||||||
if fields.save then
|
if fields.save then
|
||||||
if selected_idxs.contacts[name] and selected_idxs.contacts[name] ~= "#NEW#" then
|
if selected_idxs.contacts[name] and selected_idxs.contacts[name] ~= "#NEW#" then
|
||||||
|
@ -768,7 +767,7 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
return true
|
return true
|
||||||
|
|
||||||
else
|
else
|
||||||
contacts[string.lower(fields.name)] = contact
|
mail.setContact(name, contact)
|
||||||
contacts[selected_idxs.contacts[name]] = nil
|
contacts[selected_idxs.contacts[name]] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -780,10 +779,8 @@ function mail.handle_receivefields(player, formname, fields)
|
||||||
name = fields.name,
|
name = fields.name,
|
||||||
note = fields.note,
|
note = fields.note,
|
||||||
}
|
}
|
||||||
contacts[string.lower(contact.name)] = contact
|
mail.addContact(name, contact)
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.setContacts(name, contacts)
|
|
||||||
mail.show_contacts(name)
|
mail.show_contacts(name)
|
||||||
|
|
||||||
elseif fields.back then
|
elseif fields.back then
|
||||||
|
|
51
storage.lua
51
storage.lua
|
@ -3,11 +3,6 @@ function mail.getMailFile(playername)
|
||||||
return mail.maildir .. "/" .. saneplayername .. ".json"
|
return mail.maildir .. "/" .. saneplayername .. ".json"
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.getContactsFile(playername)
|
|
||||||
local saneplayername = string.gsub(playername, "[.|/]", "")
|
|
||||||
return mail.maildir .. "/contacts/" .. saneplayername .. ".json"
|
|
||||||
end
|
|
||||||
|
|
||||||
function mail.getMessages()
|
function mail.getMessages()
|
||||||
local messages = mail.read_json_file(mail.maildir .. "/mail.messages.json")
|
local messages = mail.read_json_file(mail.maildir .. "/mail.messages.json")
|
||||||
if messages then
|
if messages then
|
||||||
|
@ -156,8 +151,24 @@ function mail.setStatus(player, msg_id, status)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.getContacts(playername)
|
function mail.getContactsFile()
|
||||||
return mail.read_json_file(mail.getContactsFile(playername))
|
return mail.maildir .. "/mail.contacts.json"
|
||||||
|
end
|
||||||
|
|
||||||
|
function mail.getContacts()
|
||||||
|
local contacts = mail.read_json_file(mail.maildir .. "/mail.contacts.json")
|
||||||
|
return contacts
|
||||||
|
end
|
||||||
|
|
||||||
|
function mail.getPlayerContacts(playername)
|
||||||
|
local contacts = mail.getContacts()
|
||||||
|
local playerContacts = {}
|
||||||
|
for _, contact in ipairs(contacts) do
|
||||||
|
if contact.owner == playername then
|
||||||
|
table.insert(playerContacts, {name = contact.name, note = contact.note})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return playerContacts
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.pairsByKeys(t, f)
|
function mail.pairsByKeys(t, f)
|
||||||
|
@ -188,6 +199,32 @@ function mail.setContacts(playername, contacts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mail.addContact(playername, contact)
|
||||||
|
local contacts = mail.getContacts()
|
||||||
|
local newContact = {owner = playername, name = contact.name, note = contact.note}
|
||||||
|
table.insert(contacts, 1, newContact)
|
||||||
|
if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
minetest.log("error","[mail] Save failed - messages may be lost!")
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function mail.setContact(playername, updated_contact)
|
||||||
|
local contacts = mail.getContacts()
|
||||||
|
for _, contact in ipairs(contacts) do
|
||||||
|
if contact.owner == playername and contact.name == updated_contact.name then
|
||||||
|
contacts[_] = {owner = playername, name = updated_contact.name, note = updated_contact.note}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
minetest.log("error","[mail] Save failed - messages may be lost!")
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function mail.read_json_file(path)
|
function mail.read_json_file(path)
|
||||||
local file = io.open(path, "r")
|
local file = io.open(path, "r")
|
||||||
|
|
Loading…
Add table
Reference in a new issue