mirror of
https://github.com/mt-mods/mail.git
synced 2025-08-25 14:17:18 -04:00
View sent messages (new database, add maillists) (#26)
* Add tabheader & sent formspec * Add show_sent function and show sent messages * Remove comment on selected_idxs test (show_sent) * Add variable to keep the previous tab instead of going back to the first one * Remove index variable verification on mark read/unread buttons since they are necessarily clicked on inbox view * Resize messages table to be aligned with close button at the bottom * Show date time (#27) * Show date in message reading * Fix wrong registered dates Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com> * Rework header layout to add better space for date --------- Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com> * Add insertion of messages into global storage mail.messages.json * Receive player messages from global storage * Add automatic generation of status for a new message (unread) * Mark read/unread/delete a message * Fix messages loading * Show every message received/sent via specific functions * Use global contacts functions and reconfigure add/remove functions * Create mail lists formspec based on contacts * Add deleting contact * Add ability to create mail lists * Fix inability to edit contact * Rework on editing/deletion of contacts/maillists * Add at symbol as prefix on maillists view * Add ability to choose default status (to/cc/bcc) Signed-off-by: Athozus <athozus@gmail.com> * Add ability to add multiples players and choose their default status (to/cc/bcc) * Add ability to use maillist in messages and receive messages from them * Fix repetition of code causing a crash * Avoid multiples occurences of the same messages due to player both in maillist and receivers * Fix selected indexes for inbox/sent Now separated, fixed show_message() func selection of id from table dcl/read btn * Fix many issues related to maillists Notably : edit, delete, selection, creation, registration of players * Set up database version v3 and its migration from v2 + Check versions to choose v1->v2 or v2->v3 * Fix mtt.lua Due to old function getMessages(), replaced by getPlayerInboxMessages() * Add 10 seconds security to mtt.lua * Fix migrate.lua non-declared variable * Send msg table with string keys in mtt * Better log messages * Add message check * Fix mtt crash * Better syntax in storage.lua * Fix bcc forgotten in mail.send() * Fix mtt issue * Better compatibility for messages storage Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com> * Replace mail.split by builtin func Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com> * Use builtin split func Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com> * Use builtin split func in storage.lua * re-add mtt if * luacheck on PR * add check for an ancient issue with missing `to` field * Fix luacheck on storage.lua * Fix luacheck warnings in migrate.lua * Fix luacheck warnings in gui.lua * Fix luacheck (too long lines) in storage.lua * Unused loop values in migrate.lua * Whitespace line in gui.lua * Whitespace line (init.lua) * Whitespace line (api.lua) * Significantly improve maillist behaviour Replace maillist by its players when sending a message List of players separated by , Avoid doublons when editing more than 2 times a maillist * Fix luacheck * Fix table insertions at first index when no needed * Use funcs * Do not add maillist as a new contact when sending a mail * Fix removing elements from tables * Check maillists not added in contacts * storage rewrite wip * storage format docs * refactor ui components * show_compose cleanup * remove unused channel.lua * error -> err * status refactoring * contacts refactoring * maillist refactoring * docs * tests * fix some issues * re-enable migrations * contributors * prefix mail entries in the mod storage * internalize old mail-paths to migration module * add v1 and v2 player db examples and migration test * Ui improvements & fixes Move events code (if fields.x then) to events.lua (instead of inbox.lua), fix tab selection when going backward * Show most recent messages at first (outbox) * unified-inv fix --------- Signed-off-by: Athozus <athozus@gmail.com> Co-authored-by: SX <50966843+S-S-X@users.noreply.github.com> Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
This commit is contained in:
parent
b0a5bc7e47
commit
b3e0c158f7
35 changed files with 1500 additions and 960 deletions
27
ui/about.lua
Normal file
27
ui/about.lua
Normal file
|
@ -0,0 +1,27 @@
|
|||
local FORMNAME = "mail:about"
|
||||
|
||||
function mail.show_about(name)
|
||||
local formspec = [[
|
||||
size[8,5;]
|
||||
button[7.25,0;0.75,0.5;back;X]
|
||||
label[0,0;Mail]
|
||||
label[0,0.5;By cheapie]
|
||||
label[0,1;http://github.com/cheapie/mail]
|
||||
label[0,1.5;See LICENSE file for license information]
|
||||
label[0,2.5;NOTE: Communication using this system]
|
||||
label[0,3;is NOT guaranteed to be private!]
|
||||
label[0,3.5;Admins are able to view the messages]
|
||||
label[0,4;of any player.]
|
||||
]] .. mail.theme
|
||||
|
||||
minetest.show_formspec(name, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local playername = player:get_player_name()
|
||||
mail.show_mail_menu(playername)
|
||||
end)
|
89
ui/compose.lua
Normal file
89
ui/compose.lua
Normal file
|
@ -0,0 +1,89 @@
|
|||
local FORMNAME = "mail:compose"
|
||||
|
||||
function mail.show_compose(name, to, subject, body, cc, bcc)
|
||||
local formspec = [[
|
||||
size[8,9]
|
||||
button[0,0;1,1;tocontacts;To:]
|
||||
field[1.1,0.3;3.2,1;to;;%s]
|
||||
button[4,0;1,1;cccontacts;CC:]
|
||||
field[5.1,0.3;3.1,1;cc;;%s]
|
||||
button[4,0.75;1,1;bcccontacts;BCC:]
|
||||
field[5.1,1.05;3.1,1;bcc;;%s]
|
||||
field[0.25,2;8,1;subject;Subject:;%s]
|
||||
textarea[0.25,2.5;8,6;body;;%s]
|
||||
button[0.5,8.5;3,1;cancel;Cancel]
|
||||
button[4.5,8.5;3,1;send;Send]
|
||||
]] .. mail.theme
|
||||
|
||||
formspec = string.format(formspec,
|
||||
minetest.formspec_escape(to or ""),
|
||||
minetest.formspec_escape(cc or ""),
|
||||
minetest.formspec_escape(bcc or ""),
|
||||
minetest.formspec_escape(subject or ""),
|
||||
minetest.formspec_escape(body or ""))
|
||||
|
||||
minetest.show_formspec(name, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
if fields.send then
|
||||
local success, err = mail.send({
|
||||
from = name,
|
||||
to = fields.to,
|
||||
cc = fields.cc,
|
||||
bcc = fields.bcc,
|
||||
subject = fields.subject,
|
||||
body = fields.body,
|
||||
})
|
||||
if not success then
|
||||
minetest.chat_send_player(name, err)
|
||||
return
|
||||
end
|
||||
|
||||
-- add new contacts if some receivers aren't registered
|
||||
local contacts = mail.get_contacts(name)
|
||||
local recipients = mail.parse_player_list(fields.to)
|
||||
local isNew = true
|
||||
for _,recipient in ipairs(recipients) do
|
||||
if recipient:sub(1,1) == "@" then -- in case of maillist -- check if first char is @
|
||||
isNew = false
|
||||
else
|
||||
for _,contact in ipairs(contacts) do
|
||||
if contact.name == recipient then
|
||||
isNew = false
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
if isNew then
|
||||
mail.update_contact(name, {name = recipient, note = ""})
|
||||
end
|
||||
end
|
||||
|
||||
minetest.after(0.5, function()
|
||||
mail.show_mail_menu(name)
|
||||
end)
|
||||
|
||||
elseif fields.tocontacts or fields.cccontacts or fields.bcccontacts then
|
||||
mail.message_drafts[name] = {
|
||||
to = fields.to,
|
||||
cc = fields.cc,
|
||||
bcc = fields.bcc,
|
||||
subject = fields.subject,
|
||||
body = fields.body,
|
||||
}
|
||||
mail.show_select_contact(name, fields.to, fields.cc, fields.bcc)
|
||||
|
||||
elseif fields.cancel then
|
||||
mail.message_drafts[name] = nil
|
||||
|
||||
mail.show_mail_menu(name)
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
83
ui/contacts.lua
Normal file
83
ui/contacts.lua
Normal file
|
@ -0,0 +1,83 @@
|
|||
local FORMNAME = "mail:contacts"
|
||||
|
||||
local contacts_formspec = "size[8,9;]" .. mail.theme .. [[
|
||||
button[6,0.10;2,0.5;new;New]
|
||||
button[6,0.85;2,0.5;edit;Edit]
|
||||
button[6,1.60;2,0.5;delete;Delete]
|
||||
button[6,8.25;2,0.5;back;Back]
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0;5.75,9;contacts;#999,Name,Note]]
|
||||
|
||||
|
||||
function mail.show_contacts(name)
|
||||
local formspec = contacts_formspec .. mail.compile_contact_list(name, mail.selected_idxs.contacts[name])
|
||||
minetest.show_formspec(name, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local contacts = mail.get_contacts(name)
|
||||
|
||||
if fields.contacts then
|
||||
local evt = minetest.explode_table_event(fields.contacts)
|
||||
for k, _, i in mail.pairsByKeys(contacts) do
|
||||
if i == evt.row - 1 then
|
||||
mail.selected_idxs.contacts[name] = k
|
||||
break
|
||||
end
|
||||
end
|
||||
if evt.type == "DCL" and contacts[mail.selected_idxs.contacts[name]] then
|
||||
mail.show_edit_contact(
|
||||
name,
|
||||
contacts[mail.selected_idxs.contacts[name]].name,
|
||||
contacts[mail.selected_idxs.contacts[name]].note
|
||||
)
|
||||
end
|
||||
|
||||
elseif fields.new then
|
||||
mail.selected_idxs.contacts[name] = "#NEW#"
|
||||
mail.show_edit_contact(name, "", "")
|
||||
|
||||
elseif fields.edit and mail.selected_idxs.contacts[name] and contacts[mail.selected_idxs.contacts[name]] then
|
||||
mail.show_edit_contact(
|
||||
name,
|
||||
contacts[mail.selected_idxs.contacts[name]].name,
|
||||
contacts[mail.selected_idxs.contacts[name]].note
|
||||
)
|
||||
|
||||
elseif fields.delete then
|
||||
if contacts[mail.selected_idxs.contacts[name]] then
|
||||
-- delete the contact and set the selected to the next in the list,
|
||||
-- except if it was the last. Then determine the new last
|
||||
local found = false
|
||||
local last = nil
|
||||
for k in mail.pairsByKeys(contacts) do
|
||||
if found then
|
||||
mail.selected_idxs.contacts[name] = k
|
||||
break
|
||||
elseif k == mail.selected_idxs.contacts[name] then
|
||||
mail.delete_contact(name, contacts[mail.selected_idxs.contacts[name]].name)
|
||||
mail.selected_idxs.contacts[name] = nil
|
||||
found = true
|
||||
else
|
||||
last = k
|
||||
end
|
||||
end
|
||||
if found and not mail.selected_idxs.contacts[name] then
|
||||
-- was the last in the list, so take the previous (new last)
|
||||
mail.selected_idxs.contacts[name] = last
|
||||
end
|
||||
end
|
||||
|
||||
mail.show_contacts(name)
|
||||
|
||||
elseif fields.back then
|
||||
mail.show_mail_menu(name)
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
74
ui/edit_contact.lua
Normal file
74
ui/edit_contact.lua
Normal file
|
@ -0,0 +1,74 @@
|
|||
local FORMNAME = "mail:editcontact"
|
||||
|
||||
function mail.show_edit_contact(name, contact_name, note, illegal_name_hint)
|
||||
local formspec = [[
|
||||
size[6,7]
|
||||
button[4,6.25;2,0.5;back;Back]
|
||||
field[0.25,0.5;4,1;name;Player name:;%s]
|
||||
textarea[0.25,1.6;4,6.25;note;Note:;%s]
|
||||
button[4,0.10;2,1;save;Save]
|
||||
]]
|
||||
if illegal_name_hint == "collision" then
|
||||
formspec = formspec .. [[
|
||||
label[4,1;That name]
|
||||
label[4,1.5;is already in]
|
||||
label[4,2;your contacts.]
|
||||
]]
|
||||
elseif illegal_name_hint == "empty" then
|
||||
formspec = formspec .. [[
|
||||
label[4,1;The contact]
|
||||
label[4,1.5;name cannot]
|
||||
label[4,2;be empty.]
|
||||
]]
|
||||
end
|
||||
formspec = formspec .. mail.theme
|
||||
formspec = string.format(formspec,
|
||||
minetest.formspec_escape(contact_name or ""),
|
||||
minetest.formspec_escape(note or ""))
|
||||
minetest.show_formspec(name, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local contacts = mail.get_contacts(name)
|
||||
|
||||
if fields.save then
|
||||
if mail.selected_idxs.contacts[name] and mail.selected_idxs.contacts[name] ~= "#NEW#" then
|
||||
local contact = contacts[mail.selected_idxs.contacts[name]]
|
||||
if mail.selected_idxs.contacts[name] ~= string.lower(fields.name) then
|
||||
-- name changed!
|
||||
if #fields.name == 0 then
|
||||
mail.show_edit_contact(name, contact.name, fields.note, "empty")
|
||||
return true
|
||||
|
||||
elseif contacts[string.lower(fields.name)] ~= nil then
|
||||
mail.show_edit_contact(name, contact.name, fields.note, "collision")
|
||||
return true
|
||||
|
||||
else
|
||||
mail.update_contact(name, contact)
|
||||
contacts[mail.selected_idxs.contacts[name]] = nil
|
||||
end
|
||||
end
|
||||
contact.name = fields.name
|
||||
contact.note = fields.note
|
||||
mail.update_contact(name, contact)
|
||||
|
||||
else
|
||||
mail.update_contact(name, {
|
||||
name = fields.name,
|
||||
note = fields.note,
|
||||
})
|
||||
end
|
||||
mail.show_contacts(name)
|
||||
|
||||
elseif fields.back then
|
||||
mail.show_contacts(name)
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
53
ui/edit_maillists.lua
Normal file
53
ui/edit_maillists.lua
Normal file
|
@ -0,0 +1,53 @@
|
|||
local FORMNAME = "mail:editmaillist"
|
||||
|
||||
function mail.show_edit_maillist(playername, maillist_name, desc, players, illegal_name_hint)
|
||||
local formspec = [[
|
||||
size[6,7]
|
||||
button[4,6.25;2,0.5;back;Back]
|
||||
field[0.25,0.5;4,1;name;Maillist name:;%s]
|
||||
textarea[0.25,1.6;4,2;desc;Desc:;%s]
|
||||
textarea[0.25,3.6;4,4.25;players;Players:;%s]
|
||||
button[4,0.10;2,1;save;Save]
|
||||
]]
|
||||
if illegal_name_hint == "collision" then
|
||||
formspec = formspec .. [[
|
||||
label[4,1;That name]
|
||||
label[4,1.5;is already in]
|
||||
label[4,2;your maillists.]
|
||||
]]
|
||||
elseif illegal_name_hint == "empty" then
|
||||
formspec = formspec .. [[
|
||||
label[4,1;The maillist]
|
||||
label[4,1.5;name cannot]
|
||||
label[4,2;be empty.]
|
||||
]]
|
||||
end
|
||||
formspec = formspec .. mail.theme
|
||||
formspec = string.format(formspec,
|
||||
minetest.formspec_escape(maillist_name or ""),
|
||||
minetest.formspec_escape(desc or ""),
|
||||
minetest.formspec_escape(players or ""))
|
||||
minetest.show_formspec(playername, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
if fields.save then
|
||||
mail.update_maillist(name, {
|
||||
owner = name,
|
||||
name = fields.name,
|
||||
desc = fields.desc,
|
||||
players = mail.parse_player_list(fields.players)
|
||||
})
|
||||
mail.show_maillists(name)
|
||||
|
||||
elseif fields.back then
|
||||
mail.show_maillists(name)
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
116
ui/events.lua
Normal file
116
ui/events.lua
Normal file
|
@ -0,0 +1,116 @@
|
|||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= "mail:inbox" and formname ~= "mail:sent" then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
-- split inbox and sent msgs for different tests
|
||||
local entry = mail.get_storage_entry(name)
|
||||
|
||||
local messagesInbox = entry.inbox
|
||||
local messagesSent = entry.outbox
|
||||
|
||||
if fields.inbox then -- inbox table
|
||||
local evt = minetest.explode_table_event(fields.inbox)
|
||||
mail.selected_idxs.inbox[name] = evt.row - 1
|
||||
if evt.type == "DCL" and messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
mail.show_message(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
if fields.sent then -- sent table
|
||||
local evt = minetest.explode_table_event(fields.sent)
|
||||
mail.selected_idxs.sent[name] = evt.row - 1
|
||||
if evt.type == "DCL" and messagesSent[mail.selected_idxs.sent[name]] then
|
||||
mail.show_message(name, messagesSent[mail.selected_idxs.sent[name]].id)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
if fields.boxtab == "1" then
|
||||
mail.selected_idxs.boxtab[name] = 1
|
||||
mail.show_inbox(name)
|
||||
|
||||
elseif fields.boxtab == "2" then
|
||||
mail.selected_idxs.boxtab[name] = 2
|
||||
mail.show_sent(name)
|
||||
|
||||
elseif fields.read then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then -- inbox table
|
||||
mail.show_message(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then -- sent table
|
||||
mail.show_message(name, messagesSent[mail.selected_idxs.sent[name]].id)
|
||||
end
|
||||
|
||||
elseif fields.delete then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then -- inbox table
|
||||
mail.delete_mail(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then -- sent table
|
||||
mail.delete_mail(name, messagesSent[mail.selected_idxs.sent[name]].id)
|
||||
end
|
||||
|
||||
mail.show_mail_menu(name)
|
||||
|
||||
elseif fields.reply then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
local message = messagesInbox[mail.selected_idxs.inbox[name]]
|
||||
mail.reply(name, message)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then
|
||||
local message = messagesSent[mail.selected_idxs.sent[name]]
|
||||
mail.reply(name, message)
|
||||
end
|
||||
|
||||
elseif fields.replyall then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
local message = messagesInbox[mail.selected_idxs.inbox[name]]
|
||||
mail.replyall(name, message)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then
|
||||
local message = messagesSent[mail.selected_idxs.sent[name]]
|
||||
mail.replyall(name, message)
|
||||
end
|
||||
|
||||
elseif fields.forward then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
local message = messagesInbox[mail.selected_idxs.inbox[name]]
|
||||
mail.forward(name, message)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then
|
||||
local message = messagesSent[mail.selected_idxs.sent[name]]
|
||||
mail.forward(name, message)
|
||||
end
|
||||
|
||||
elseif fields.markread then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
mail.mark_read(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then
|
||||
mail.mark_read(name, messagesSent[mail.selected_idxs.sent[name]].id)
|
||||
end
|
||||
|
||||
mail.show_mail_menu(name)
|
||||
|
||||
elseif fields.markunread then
|
||||
if formname == "mail:inbox" and messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
mail.mark_unread(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
|
||||
elseif formname == "mail:sent" and messagesSent[mail.selected_idxs.sent[name]] then
|
||||
mail.mark_unread(name, messagesSent[mail.selected_idxs.sent[name]].id)
|
||||
end
|
||||
|
||||
mail.show_mail_menu(name)
|
||||
|
||||
elseif fields.new then
|
||||
mail.show_compose(name)
|
||||
|
||||
elseif fields.contacts then
|
||||
mail.show_contacts(name)
|
||||
|
||||
elseif fields.maillists then
|
||||
mail.show_maillists(name)
|
||||
|
||||
elseif fields.about then
|
||||
mail.show_about(name)
|
||||
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
66
ui/inbox.lua
Normal file
66
ui/inbox.lua
Normal file
|
@ -0,0 +1,66 @@
|
|||
local inbox_formspec = "size[8,10;]" .. mail.theme .. [[
|
||||
tabheader[0.3,1;boxtab;Inbox,Sent messages;1;false;false]
|
||||
|
||||
button[6,0.10;2,0.5;new;New]
|
||||
button[6,0.95;2,0.5;read;Read]
|
||||
button[6,1.70;2,0.5;reply;Reply]
|
||||
button[6,2.45;2,0.5;replyall;Reply All]
|
||||
button[6,3.20;2,0.5;forward;Forward]
|
||||
button[6,3.95;2,0.5;delete;Delete]
|
||||
button[6,4.82;2,0.5;markread;Mark Read]
|
||||
button[6,5.55;2,0.5;markunread;Mark Unread]
|
||||
button[6,6.8;2,0.5;contacts;Contacts]
|
||||
button[6,7.6;2,0.5;maillists;Mail lists]
|
||||
button[6,8.7;2,0.5;about;About]
|
||||
button_exit[6,9.5;2,0.5;quit;Close]
|
||||
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0.7;5.75,9.35;inbox;#999,From,Subject]]
|
||||
|
||||
|
||||
function mail.show_inbox(name)
|
||||
local formspec = { inbox_formspec }
|
||||
local entry = mail.get_storage_entry(name)
|
||||
local messages = entry.inbox
|
||||
|
||||
mail.message_drafts[name] = nil
|
||||
|
||||
if messages[1] then
|
||||
for _, message in ipairs(messages) do
|
||||
if not message.read then
|
||||
if not mail.player_in_list(name, message.to) then
|
||||
formspec[#formspec + 1] = ",#FFD788"
|
||||
else
|
||||
formspec[#formspec + 1] = ",#FFD700"
|
||||
end
|
||||
else
|
||||
if not mail.player_in_list(name, message.to) then
|
||||
formspec[#formspec + 1] = ",#CCCCDD"
|
||||
else
|
||||
formspec[#formspec + 1] = ","
|
||||
end
|
||||
end
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(message.from)
|
||||
formspec[#formspec + 1] = ","
|
||||
if message.subject ~= "" then
|
||||
if string.len(message.subject) > 30 then
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(string.sub(message.subject, 1, 27))
|
||||
formspec[#formspec + 1] = "..."
|
||||
else
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(message.subject)
|
||||
end
|
||||
else
|
||||
formspec[#formspec + 1] = "(No subject)"
|
||||
end
|
||||
end
|
||||
if mail.selected_idxs.inbox[name] then
|
||||
formspec[#formspec + 1] = ";"
|
||||
formspec[#formspec + 1] = tostring(mail.selected_idxs.inbox[name] + 1)
|
||||
end
|
||||
formspec[#formspec + 1] = "]"
|
||||
else
|
||||
formspec[#formspec + 1] = "]label[2.25,4.5;No mail]"
|
||||
end
|
||||
minetest.show_formspec(name, "mail:inbox", table.concat(formspec, ""))
|
||||
end
|
10
ui/mail.lua
Normal file
10
ui/mail.lua
Normal file
|
@ -0,0 +1,10 @@
|
|||
-- helper function for tabbed overview
|
||||
|
||||
function mail.show_mail_menu(playername)
|
||||
local index = mail.selected_idxs.boxtab[playername] or 1
|
||||
if index == 1 then
|
||||
mail.show_inbox(playername)
|
||||
elseif index == 2 then
|
||||
mail.show_sent(playername)
|
||||
end
|
||||
end
|
110
ui/maillists.lua
Normal file
110
ui/maillists.lua
Normal file
|
@ -0,0 +1,110 @@
|
|||
local FORMNAME = "mail:maillists"
|
||||
|
||||
local maillists_formspec = "size[8,9;]" .. mail.theme .. [[
|
||||
button[6,0.10;2,0.5;new;New]
|
||||
button[6,0.85;2,0.5;edit;Edit]
|
||||
button[6,1.60;2,0.5;delete;Delete]
|
||||
button[6,8.25;2,0.5;back;Back]
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0;5.75,9;maillists;#999,Name,Description]]
|
||||
|
||||
function mail.show_maillists(name)
|
||||
local formspec = { maillists_formspec }
|
||||
local maillists = mail.get_maillists(name)
|
||||
|
||||
if maillists[1] then
|
||||
for _, maillist in ipairs(maillists) do
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = "@" .. minetest.formspec_escape(maillist.name)
|
||||
formspec[#formspec + 1] = ","
|
||||
if maillist.desc ~= "" then
|
||||
if string.len(maillist.desc) > 30 then
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(string.sub(maillist.desc, 1, 27))
|
||||
formspec[#formspec + 1] = "..."
|
||||
else
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(maillist.desc)
|
||||
end
|
||||
else
|
||||
formspec[#formspec + 1] = "(No description)"
|
||||
end
|
||||
end
|
||||
if mail.selected_idxs.maillists[name] then
|
||||
formspec[#formspec + 1] = ";"
|
||||
formspec[#formspec + 1] = mail.selected_idxs.maillists[name]
|
||||
end
|
||||
formspec[#formspec + 1] = "]"
|
||||
else
|
||||
formspec[#formspec + 1] = "]label[2.25,4.5;No maillist]"
|
||||
end
|
||||
minetest.show_formspec(name, FORMNAME, table.concat(formspec, ""))
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local maillists = mail.get_maillists(name)
|
||||
|
||||
if fields.maillists then
|
||||
local evt = minetest.explode_table_event(fields.maillists)
|
||||
mail.selected_idxs.maillists[name] = evt.row - 1
|
||||
if evt.type == "DCL" and maillists[mail.selected_idxs.maillists[name]] then
|
||||
local maillist = mail.get_maillist_by_name(name, maillists[mail.selected_idxs.maillists[name]].name)
|
||||
local players_string = mail.concat_player_list(maillist.players)
|
||||
mail.show_edit_maillist(
|
||||
name,
|
||||
maillists[mail.selected_idxs.maillists[name]].name,
|
||||
maillists[mail.selected_idxs.maillists[name]].desc,
|
||||
players_string
|
||||
)
|
||||
end
|
||||
|
||||
elseif fields.new then
|
||||
mail.selected_idxs.maillists[name] = "#NEW#"
|
||||
mail.show_edit_maillist(name, "", "", "Player1, Player2, Player3")
|
||||
|
||||
elseif fields.edit and maillists[mail.selected_idxs.maillists[name]] then
|
||||
local maillist = mail.get_maillist_by_name(name, maillists[mail.selected_idxs.maillists[name]].name)
|
||||
local players_string = mail.concat_player_list(maillist.players)
|
||||
mail.show_edit_maillist(
|
||||
name,
|
||||
maillists[mail.selected_idxs.maillists[name]].name,
|
||||
maillists[mail.selected_idxs.maillists[name]].desc,
|
||||
players_string
|
||||
)
|
||||
|
||||
elseif fields.delete then
|
||||
if maillists[mail.selected_idxs.maillists[name]] then
|
||||
-- delete the maillist and set the selected to the next in the list,
|
||||
-- except if it was the last. Then determine the new last
|
||||
local found = false
|
||||
local last = nil
|
||||
for k in mail.pairsByKeys(maillists) do
|
||||
if found then
|
||||
mail.selected_idxs.maillists[name] = k
|
||||
break
|
||||
elseif k == mail.selected_idxs.maillists[name] then
|
||||
mail.delete_maillist(maillists[mail.selected_idxs.maillists[name]].name)
|
||||
mail.selected_idxs.maillists[name] = nil
|
||||
found = true
|
||||
else
|
||||
last = k
|
||||
end
|
||||
end
|
||||
if found and not mail.selected_idxs.maillists[name] then
|
||||
-- was the last in the list, so take the previous (new last)
|
||||
mail.selected_idxs.maillists[name] = last
|
||||
end
|
||||
end
|
||||
|
||||
mail.show_maillists(name)
|
||||
|
||||
elseif fields.back then
|
||||
mail.show_mail_menu(name)
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
136
ui/message.lua
Normal file
136
ui/message.lua
Normal file
|
@ -0,0 +1,136 @@
|
|||
local FORMNAME = "mail:message"
|
||||
|
||||
function mail.show_message(name, id)
|
||||
local message = mail.get_message(name, id)
|
||||
|
||||
local formspec = [[
|
||||
size[8,9]
|
||||
|
||||
box[0,0;7,1.9;#466432]
|
||||
|
||||
button[7.25,0.15;0.75,0.5;back;X]
|
||||
|
||||
label[0.2,0.1;From: %s]
|
||||
label[0.2,0.5;To: %s]
|
||||
label[0.2,0.9;CC: %s]
|
||||
label[0.2,1.3;Date: %s]
|
||||
|
||||
label[0,2.1;Subject: %s]
|
||||
textarea[0.25,2.6;8,7.0;;;%s]
|
||||
|
||||
button[0,8.5;2,1;reply;Reply]
|
||||
button[2,8.5;2,1;replyall;Reply All]
|
||||
button[4,8.5;2,1;forward;Forward]
|
||||
button[6,8.5;2,1;delete;Delete]
|
||||
]] .. mail.theme
|
||||
|
||||
local from = minetest.formspec_escape(message.from) or ""
|
||||
local to = minetest.formspec_escape(message.to) or ""
|
||||
local cc = minetest.formspec_escape(message.cc) or ""
|
||||
local date = type(message.time) == "number"
|
||||
and minetest.formspec_escape(os.date("%Y-%m-%d %X", message.time)) or ""
|
||||
local subject = minetest.formspec_escape(message.subject) or ""
|
||||
local body = minetest.formspec_escape(message.body) or ""
|
||||
formspec = string.format(formspec, from, to, cc, date, subject, body)
|
||||
|
||||
if not message.read then
|
||||
-- mark as read
|
||||
mail.mark_read(name, id)
|
||||
end
|
||||
|
||||
minetest.show_formspec(name, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
function mail.reply(name, message)
|
||||
local replyfooter = "Type your reply here.\n\n--Original message follows--\n" ..message.body
|
||||
mail.show_compose(name, message.from, "Re: "..message.subject, replyfooter)
|
||||
end
|
||||
|
||||
function mail.replyall(name, message)
|
||||
local replyfooter = "Type your reply here.\n\n--Original message follows--\n" ..message.body
|
||||
|
||||
-- new recipients are the sender plus the original recipients, minus ourselves
|
||||
local recipients = message.to or ""
|
||||
if message.from ~= nil then
|
||||
recipients = message.from .. ", " .. recipients
|
||||
end
|
||||
recipients = mail.parse_player_list(recipients)
|
||||
for k,v in pairs(recipients) do
|
||||
if v == name then
|
||||
table.remove(recipients, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
recipients = mail.concat_player_list(recipients)
|
||||
|
||||
-- new CC is old CC minus ourselves
|
||||
local cc = mail.parse_player_list(message.cc)
|
||||
for k,v in pairs(cc) do
|
||||
if v == name then
|
||||
table.remove(cc, k)
|
||||
break
|
||||
end
|
||||
end
|
||||
cc = mail.concat_player_list(cc)
|
||||
|
||||
mail.show_compose(name, recipients, "Re: "..message.subject, replyfooter, cc)
|
||||
end
|
||||
|
||||
function mail.forward(name, message)
|
||||
local fwfooter = "Type your message here.\n\n--Original message follows--\n" .. (message.body or "")
|
||||
mail.show_compose(name, "", "Fw: " .. (message.subject or ""), fwfooter)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local entry = mail.get_storage_entry(name)
|
||||
|
||||
local messagesInbox = entry.inbox
|
||||
local messagesSent = entry.outbox
|
||||
|
||||
if fields.back then
|
||||
mail.show_mail_menu(name)
|
||||
return true -- don't uselessly set messages
|
||||
|
||||
elseif fields.reply then
|
||||
local message = ""
|
||||
if messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
message = messagesInbox[mail.selected_idxs.inbox[name]]
|
||||
elseif messagesSent[mail.selected_idxs.sent[name]] then
|
||||
message = messagesSent[mail.selected_idxs.sent[name]]
|
||||
end
|
||||
mail.reply(name, message)
|
||||
|
||||
elseif fields.replyall then
|
||||
local message = ""
|
||||
if messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
message = messagesInbox[mail.selected_idxs.inbox[name]]
|
||||
elseif messagesSent[mail.selected_idxs.sent[name]] then
|
||||
message = messagesSent[mail.selected_idxs.sent[name]]
|
||||
end
|
||||
mail.replyall(name, message)
|
||||
|
||||
elseif fields.forward then
|
||||
local message = ""
|
||||
if messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
message = messagesInbox[mail.selected_idxs.inbox[name]]
|
||||
elseif messagesSent[mail.selected_idxs.sent[name]] then
|
||||
message = messagesSent[mail.selected_idxs.sent[name]]
|
||||
end
|
||||
mail.forward(name, message)
|
||||
|
||||
elseif fields.delete then
|
||||
if messagesInbox[mail.selected_idxs.inbox[name]] then
|
||||
mail.delete_mail(name, messagesInbox[mail.selected_idxs.inbox[name]].id)
|
||||
elseif messagesSent[mail.selected_idxs.sent[name]] then
|
||||
mail.delete_mail(name, messagesSent[mail.selected_idxs.sent[name]].id)
|
||||
end
|
||||
mail.show_mail_menu(name)
|
||||
end
|
||||
|
||||
return true
|
||||
end)
|
53
ui/outbox.lua
Normal file
53
ui/outbox.lua
Normal file
|
@ -0,0 +1,53 @@
|
|||
|
||||
local sent_formspec = "size[8,10;]" .. mail.theme .. [[
|
||||
tabheader[0.3,1;boxtab;Inbox,Sent messages;2;false;false]
|
||||
|
||||
button[6,0.10;2,0.5;new;New]
|
||||
button[6,0.95;2,0.5;read;Read]
|
||||
button[6,1.70;2,0.5;reply;Reply]
|
||||
button[6,2.45;2,0.5;replyall;Reply All]
|
||||
button[6,3.20;2,0.5;forward;Forward]
|
||||
button[6,3.95;2,0.5;delete;Delete]
|
||||
button[6,6.8;2,0.5;contacts;Contacts]
|
||||
button[6,7.6;2,0.5;maillists;Mail lists]
|
||||
button[6,8.7;2,0.5;about;About]
|
||||
button_exit[6,9.5;2,0.5;quit;Close]
|
||||
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0.7;5.75,9.35;sent;#999,To,Subject]]
|
||||
|
||||
|
||||
function mail.show_sent(name)
|
||||
local formspec = { sent_formspec }
|
||||
local entry = mail.get_storage_entry(name)
|
||||
local messages = entry.outbox
|
||||
|
||||
mail.message_drafts[name] = nil
|
||||
|
||||
if messages[1] then
|
||||
for _, message in ipairs(messages) do
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = ","
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(message.to)
|
||||
formspec[#formspec + 1] = ","
|
||||
if message.subject ~= "" then
|
||||
if string.len(message.subject) > 30 then
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(string.sub(message.subject, 1, 27))
|
||||
formspec[#formspec + 1] = "..."
|
||||
else
|
||||
formspec[#formspec + 1] = minetest.formspec_escape(message.subject)
|
||||
end
|
||||
else
|
||||
formspec[#formspec + 1] = "(No subject)"
|
||||
end
|
||||
end
|
||||
if mail.selected_idxs.sent[name] then
|
||||
formspec[#formspec + 1] = ";"
|
||||
formspec[#formspec + 1] = tostring(mail.selected_idxs.sent[name] + 1)
|
||||
end
|
||||
formspec[#formspec + 1] = "]"
|
||||
else
|
||||
formspec[#formspec + 1] = "]label[2.25,4.5;No mail]"
|
||||
end
|
||||
minetest.show_formspec(name, "mail:sent", table.concat(formspec, ""))
|
||||
end
|
115
ui/select_contact.lua
Normal file
115
ui/select_contact.lua
Normal file
|
@ -0,0 +1,115 @@
|
|||
local FORMNAME = "mail:selectcontact"
|
||||
|
||||
local select_contact_formspec = "size[8,9;]" .. mail.theme .. [[
|
||||
tablecolumns[color;text;text]
|
||||
table[0,0;3.5,9;contacts;#999,Name,Note%s]
|
||||
button[3.55,2.00;1.75,0.5;toadd;→ Add]
|
||||
button[3.55,2.75;1.75,0.5;toremove;← Remove]
|
||||
button[3.55,6.00;1.75,0.5;ccadd;→ Add]
|
||||
button[3.55,6.75;1.75,0.5;ccremove;← Remove]
|
||||
tablecolumns[color;text;text]
|
||||
table[5.15,0.0;2.75,4.5;to;#999,TO:,Note%s]
|
||||
tablecolumns[color;text;text]
|
||||
table[5.15,4.6;2.75,4.5;cc;#999,CC:,Note%s]
|
||||
button[3.55,8.25;1.75,0.5;back;Back]
|
||||
]]
|
||||
|
||||
|
||||
function mail.show_select_contact(name, to, cc)
|
||||
local formspec = select_contact_formspec
|
||||
local contacts = mail.compile_contact_list(name, mail.selected_idxs.contacts[name])
|
||||
|
||||
-- compile lists
|
||||
if to then
|
||||
to = mail.compile_contact_list(name, mail.selected_idxs.to[name], to)
|
||||
else
|
||||
to = ""
|
||||
end
|
||||
if cc then
|
||||
cc = mail.compile_contact_list(name, mail.selected_idxs.cc[name], cc)
|
||||
else
|
||||
cc = ""
|
||||
end
|
||||
--[[if bcc then
|
||||
bcc = table.concat(mail.compile_contact_list(name, mail.selected_idxs.bcc[name], bcc)
|
||||
else
|
||||
bcc = ""
|
||||
end]]--
|
||||
formspec = string.format(formspec, contacts, to, cc)--, bcc()
|
||||
minetest.show_formspec(name, FORMNAME, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= FORMNAME then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local contacts = mail.get_contacts(name)
|
||||
local draft = mail.message_drafts[name]
|
||||
|
||||
-- get indexes for fields with selected rows
|
||||
-- execute their default button's actions if double clicked
|
||||
for k,action in pairs({
|
||||
contacts = "toadd",
|
||||
to = "toremove",
|
||||
cc = "ccremove",
|
||||
bcc = "bccremove"
|
||||
}) do
|
||||
if fields[k] then
|
||||
local evt = minetest.explode_table_event(fields[k])
|
||||
mail.selected_idxs[k][name] = evt.row - 1
|
||||
if evt.type == "DCL" and mail.selected_idxs[k][name] then
|
||||
fields[action] = true
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local update = false
|
||||
-- add
|
||||
for _,v in pairs({"to","cc","bcc"}) do
|
||||
if fields[v.."add"] then
|
||||
update = true
|
||||
if mail.selected_idxs.contacts[name] then
|
||||
for k, contact, i in mail.pairsByKeys(contacts) do
|
||||
if k == mail.selected_idxs.contacts[name] or i == mail.selected_idxs.contacts[name] then
|
||||
local list = mail.parse_player_list(draft[v])
|
||||
list[#list+1] = contact.name
|
||||
mail.selected_idxs[v][name] = #list
|
||||
draft[v] = mail.concat_player_list(list)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- remove
|
||||
for _,v in pairs({"to","cc","bcc"}) do
|
||||
if fields[v.."remove"] then
|
||||
update = true
|
||||
if mail.selected_idxs[v][name] then
|
||||
local list = mail.parse_player_list(draft[v])
|
||||
table.remove(list, mail.selected_idxs[v][name])
|
||||
if #list < mail.selected_idxs[v][name] then
|
||||
mail.selected_idxs[v][name] = #list
|
||||
end
|
||||
draft[v] = mail.concat_player_list(list)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if update then
|
||||
mail.show_select_contact(name, draft.to, draft.cc, draft.bcc)
|
||||
return true
|
||||
end
|
||||
|
||||
-- delete old idxs
|
||||
for _,v in ipairs({"contacts","to","cc","bcc"}) do
|
||||
mail.selected_idxs[v][name] = nil
|
||||
end
|
||||
|
||||
mail.show_compose(name, draft.to, draft.subject, draft.body, draft.cc, draft.bcc)
|
||||
|
||||
return true
|
||||
end)
|
Loading…
Add table
Add a link
Reference in a new issue