mail/gui.lua

503 lines
15 KiB
Lua
Raw Normal View History

2019-09-16 08:06:54 +02:00
selected_message_idxs = {}
selected_contact_idxs = {}
2019-09-16 08:06:54 +02:00
local theme
if minetest.get_modpath("default") then
theme = default.gui_bg .. default.gui_bg_img
else
theme = ""
end
mail.inbox_formspec = "size[8,9;]" .. theme .. [[
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.55;2,0.5;contacts;Contacts]
button[6,7.40;2,0.5;about;About]
button_exit[6,8.45;2,0.5;quit;Close]
2019-09-16 08:06:54 +02:00
tablecolumns[color;text;text]
table[0,0;5.75,9;messages;#999,From,Subject]]
mail.contacts_formspec = "size[8,9;]" .. 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]
2020-07-31 23:51:13 +02:00
button[6,8.25;2,0.5;back;Back]
tablecolumns[color;text;text]
table[0,0;5.75,9;contacts;#999,Name,Note]]
mail.select_contact_formspec = "size[8,9;]" .. theme .. [[
2020-07-31 23:51:13 +02:00
button[6,0.00;2,0.5;select;Select]
button[6,8.25;2,0.5;back;Back]
tablecolumns[color;text]
table[0,0;5.75,9;contacts;#999,Name]]
2019-09-16 08:06:54 +02:00
function mail.show_about(name)
local formspec = [[
size[8,5;]
button[7.25,0;0.75,0.5;back;X]
2019-09-16 08:06:54 +02:00
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.]
]] .. theme
minetest.show_formspec(name, "mail:about", formspec)
end
function mail.show_inbox(name)
local formspec = { mail.inbox_formspec }
local messages = mail.getMessages(name)
if messages[1] then
2020-01-06 12:58:26 +01:00
for _, message in ipairs(messages) do
mail.ensure_new_format(message)
2019-09-16 08:06:54 +02:00
if message.unread then
if not mail.player_in_list(name, message.to) then
formspec[#formspec + 1] = ",#FFD788"
else
formspec[#formspec + 1] = ",#FFD700"
end
2019-09-16 08:06:54 +02:00
else
if not mail.player_in_list(name, message.to) then
formspec[#formspec + 1] = ",#CCCCDD"
else
formspec[#formspec + 1] = ","
end
2019-09-16 08:06:54 +02:00
end
formspec[#formspec + 1] = ","
formspec[#formspec + 1] = minetest.formspec_escape(message.from)
2019-09-16 08:06:54 +02:00
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 selected_message_idxs[name] then
formspec[#formspec + 1] = ";"
formspec[#formspec + 1] = tostring(selected_message_idxs[name] + 1)
end
formspec[#formspec + 1] = "]"
else
formspec[#formspec + 1] = "]label[2.25,4.5;No mail]"
2019-09-16 08:06:54 +02:00
end
minetest.show_formspec(name, "mail:inbox", table.concat(formspec, ""))
end
2020-07-31 23:51:13 +02:00
function mail.show_contacts(name)
local formspec = { mail.contacts_formspec }
formspec = mail.compile_contact_list(name, formspec)
minetest.show_formspec(name, "mail:contacts", table.concat(formspec, ""))
end
function mail.show_edit_contact(name, contact_name, note, illegal_name_hint)
2020-07-31 23:51:13 +02:00
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 .. theme
formspec = string.format(formspec,
minetest.formspec_escape(contact_name or ""),
minetest.formspec_escape(note or ""))
minetest.show_formspec(name, "mail:editcontact", formspec)
2020-07-31 23:51:13 +02:00
end
function mail.show_select_contact(name)
local formspec = { mail.select_contact_formspec }
formspec = mail.compile_contact_list(name, formspec)
minetest.show_formspec(name, "mail:selectcontact", table.concat(formspec, ""))
end
function mail.compile_contact_list(name, formspec)
local contacts = mail.getContacts(name)
local i = 0
local selected = nil
for k, contact in pairs(contacts) do
formspec[#formspec + 1] = ","
formspec[#formspec + 1] = ","
formspec[#formspec + 1] = minetest.formspec_escape(contact.name)
formspec[#formspec + 1] = ","
local note = contact.note
-- display an ellipsis if the note spans multiple lines
local idx = string.find(note, '\n')
if idx ~= nil then
note = string.sub(note, 1, idx-1) .. ' ...'
2020-07-31 23:51:13 +02:00
end
formspec[#formspec + 1] = minetest.formspec_escape(note)
i = i + 1
if selected_contact_idxs[name] == k then
selected = i
end
end
if i > 0 then
if selected then
2020-07-31 23:51:13 +02:00
formspec[#formspec + 1] = ";"
formspec[#formspec + 1] = tostring(selected + 1)
2020-07-31 23:51:13 +02:00
end
formspec[#formspec + 1] = "]"
else
formspec[#formspec + 1] = "]label[2,4.5;No contacts]"
end
return formspec
end
2019-09-16 08:06:54 +02:00
function mail.show_message(name, msgnumber)
local messages = mail.getMessages(name)
local message = messages[msgnumber]
local formspec = [[
size[8,9]
button[7.25,0;0.75,0.5;back;X]
2019-09-16 08:06:54 +02:00
label[0,0;From: %s]
label[0,0.4;To: %s]
label[0,0.8;CC: %s]
label[0,1.3;Subject: %s]
textarea[0.25,1.8;8,7.8;body;;%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]
2019-09-16 08:06:54 +02:00
]] .. theme
local from = minetest.formspec_escape(message.from)
local to = minetest.formspec_escape(message.to)
local cc = minetest.formspec_escape(message.cc)
2019-09-16 08:06:54 +02:00
local subject = minetest.formspec_escape(message.subject)
local body = minetest.formspec_escape(message.body)
formspec = string.format(formspec, from, to, cc, subject, body)
2019-09-16 08:06:54 +02:00
if message.unread then
message.unread = false
mail.setMessages(name, messages)
end
2019-09-16 08:06:54 +02:00
minetest.show_formspec(name,"mail:message",formspec)
end
function mail.show_compose(name, defaultto, defaultsubj, defaultbody, defaultcc, defaultbcc)
2019-09-16 08:06:54 +02:00
local formspec = [[
size[8,9]
field[0.25,0.5;3.5,1;to;To:;%s]
field[3.75,0.5;3.75,1;cc;CC:;%s]
field[3.75,1.6;3.75,1;bcc;BCC:;%s]
field[0.25,2.5;8,1;subject;Subject:;%s]
textarea[0.25,3.2;8,6;body;;%s]
button[0.5,8.5;3,1;cancel;Cancel]
button[7.25,0;0.75,0.5;cancel;X]
button[4.5,8.5;3,1;send;Send]
2019-09-16 08:06:54 +02:00
]] .. theme
defaultto = defaultto or ""
defaultsubj = defaultsubj or ""
defaultbody = defaultbody or ""
defaultcc = defaultcc or ""
defaultbcc = defaultbcc or ""
2019-09-16 08:06:54 +02:00
formspec = string.format(formspec,
2020-07-31 23:53:49 +02:00
minetest.formspec_escape(defaultto),
minetest.formspec_escape(defaultcc),
minetest.formspec_escape(defaultbcc),
minetest.formspec_escape(defaultsubj),
minetest.formspec_escape(defaultbody))
2019-09-16 08:06:54 +02:00
minetest.show_formspec(name, "mail:compose", formspec)
end
function mail.reply(name, message)
mail.ensure_new_format(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)
mail.ensure_new_format(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
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)
mail.show_compose(name, recipients, "Re: "..message.subject, replyfooter, message.cc)
end
function mail.forward(name, message)
local fwfooter = "Type your message here.\n\n--Original message follows--\n" ..message.body
mail.show_compose(name, "", "Fw: "..message.subject, fwfooter)
end
2019-09-16 08:06:54 +02:00
function mail.handle_receivefields(player, formname, fields)
if formname == "" and fields and fields.quit and minetest.get_modpath("unified_inventory") then
unified_inventory.set_inventory_formspec(player, "craft")
end
if formname == "mail:about" then
minetest.after(0.5, function()
mail.show_inbox(player:get_player_name())
end)
elseif formname == "mail:inbox" then
local name = player:get_player_name()
local messages = mail.getMessages(name)
if fields.messages then
local evt = minetest.explode_table_event(fields.messages)
selected_message_idxs[name] = evt.row - 1
if evt.type == "DCL" and messages[selected_message_idxs[name]] then
mail.show_message(name, selected_message_idxs[name])
end
return true
end
if fields.read then
if messages[selected_message_idxs[name]] then
mail.show_message(name, selected_message_idxs[name])
end
elseif fields.delete then
if messages[selected_message_idxs[name]] then
table.remove(messages, selected_message_idxs[name])
mail.setMessages(name, messages)
2019-09-16 08:06:54 +02:00
end
mail.show_inbox(name)
elseif fields.reply and messages[selected_message_idxs[name]] then
local message = messages[selected_message_idxs[name]]
mail.reply(name, message)
elseif fields.replyall and messages[selected_message_idxs[name]] then
local message = messages[selected_message_idxs[name]]
mail.replyall(name, message)
2019-09-16 08:06:54 +02:00
elseif fields.forward and messages[selected_message_idxs[name]] then
local message = messages[selected_message_idxs[name]]
mail.forward(name, message)
2019-09-16 08:06:54 +02:00
elseif fields.markread then
if messages[selected_message_idxs[name]] then
messages[selected_message_idxs[name]].unread = false
-- set messages immediately, so it shows up already when updating the inbox
mail.setMessages(name, messages)
2019-09-16 08:06:54 +02:00
end
mail.show_inbox(name)
return true
2019-09-16 08:06:54 +02:00
elseif fields.markunread then
if messages[selected_message_idxs[name]] then
messages[selected_message_idxs[name]].unread = true
-- set messages immediately, so it shows up already when updating the inbox
mail.setMessages(name, messages)
2019-09-16 08:06:54 +02:00
end
mail.show_inbox(name)
return true
2019-09-16 08:06:54 +02:00
elseif fields.new then
mail.show_compose(name)
2019-09-16 08:06:54 +02:00
elseif fields.contacts then
mail.show_contacts(name)
2019-09-16 08:06:54 +02:00
elseif fields.quit then
if minetest.get_modpath("unified_inventory") then
unified_inventory.set_inventory_formspec(player, "craft")
end
elseif fields.about then
mail.show_about(name)
end
return true
elseif formname == "mail:message" then
local name = player:get_player_name()
local messages = mail.getMessages(name)
if fields.back then
mail.show_inbox(name)
return true -- don't uselessly set messages
2019-09-16 08:06:54 +02:00
elseif fields.reply then
local message = messages[selected_message_idxs[name]]
mail.reply(name, message)
elseif fields.replyall then
local message = messages[selected_message_idxs[name]]
mail.replyall(name, message)
2019-09-16 08:06:54 +02:00
elseif fields.forward then
local message = messages[selected_message_idxs[name]]
mail.forward(name, message.subject)
2019-09-16 08:06:54 +02:00
elseif fields.delete then
if messages[selected_message_idxs[name]] then
table.remove(messages,selected_message_idxs[name])
mail.setMessages(name, messages)
2019-09-16 08:06:54 +02:00
end
mail.show_inbox(name)
end
return true
2019-09-16 08:06:54 +02:00
elseif formname == "mail:compose" then
if fields.send then
local name = player:get_player_name()
2019-09-16 08:06:54 +02:00
mail.send({
from = name,
to = fields.to,
cc = fields.cc,
bcc = fields.bcc,
2019-09-16 08:06:54 +02:00
subject = fields.subject,
body = fields.body,
2019-09-16 08:06:54 +02:00
})
local contacts = mail.getContacts(name)
local recipients = mail.parse_player_list(fields.to)
local changed = false
for _,v in pairs(recipients) do
if contacts[string.lower(v)] == nil then
contacts[string.lower(v)] = {
name = v,
note = "",
}
changed = true
end
end
if changed then
mail.setContacts(name, contacts)
end
2019-09-16 08:06:54 +02:00
end
minetest.after(0.5, function()
mail.show_inbox(player:get_player_name())
end)
return true
2020-07-31 23:51:13 +02:00
elseif formname == "mail:contacts" then
local name = player:get_player_name()
local contacts = mail.getContacts(name)
if fields.contacts then
local evt = minetest.explode_table_event(fields.contacts)
--selected_contact_idxs[name] = evt.row - 1
local i = 0
for k,c in pairs(contacts) do
i = i + 1
if i == evt.row - 1 then
selected_contact_idxs[name] = k
break
end
end
2020-07-31 23:51:13 +02:00
if evt.type == "DCL" and contacts[selected_contact_idxs[name]] then
mail.show_edit_contact(name, contacts[selected_contact_idxs[name]].name, contacts[selected_contact_idxs[name]].note)
2020-07-31 23:51:13 +02:00
end
return true
elseif fields.new then
selected_contact_idxs[name] = "#NEW#"
mail.show_edit_contact(name, "", "")
2020-07-31 23:51:13 +02:00
elseif fields.edit then
mail.show_edit_contact(name, contacts[selected_contact_idxs[name]].name, contacts[selected_contact_idxs[name]].note)
2020-07-31 23:51:13 +02:00
elseif fields.delete then
if contacts[selected_contact_idxs[name]] then
contacts[selected_contact_idxs[name]] = nil
mail.setContacts(name, contacts)
2020-07-31 23:51:13 +02:00
end
mail.show_contacts(name)
2020-07-31 23:51:13 +02:00
elseif fields.back then
mail.show_inbox(name)
end
elseif formname == "mail:editcontact" then
local name = player:get_player_name()
local contacts = mail.getContacts(name)
2020-07-31 23:51:13 +02:00
if fields.save then
if selected_contact_idxs[name] and selected_contact_idxs[name] ~= "#NEW#" then
local contact = contacts[selected_contact_idxs[name]]
if selected_contact_idxs[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
contacts[string.lower(fields.name)] = contact
contacts[selected_contact_idxs[name]] = nil
end
end
contact.name = fields.name
contact.note = fields.note
else
local contact = {
name = fields.name,
note = fields.note,
}
contacts[string.lower(contact.name)] = contact
end
mail.setContacts(name, contacts)
mail.show_contacts(name)
elseif fields.back then
mail.show_contacts(name)
end
2019-09-16 08:06:54 +02:00
elseif fields.mail then
mail.show_inbox(player:get_player_name())
else
return false
end
end
minetest.register_on_player_receive_fields(mail.handle_receivefields)
if minetest.get_modpath("unified_inventory") then
mail.receive_mail_message = mail.receive_mail_message ..
" or use the mail button in the inventory"
mail.read_later_message = mail.read_later_message ..
" or by using the mail button in the inventory"
unified_inventory.register_button("mail", {
type = "image",
image = "mail_button.png",
tooltip = "Mail"
})
end