2017-12-06 16:18:15 +01:00
|
|
|
-- based on https://github.com/cheapie/mail
|
|
|
|
|
|
|
|
laptop.register_app("mail", {
|
|
|
|
app_name = "Mail",
|
|
|
|
app_icon = "laptop_email_letter.png",
|
2017-12-24 10:42:58 -05:00
|
|
|
app_info = "Write Mail to Other Players",
|
2017-12-06 16:18:15 +01:00
|
|
|
formspec_func = function(app, mtos)
|
2017-12-15 00:30:35 +01:00
|
|
|
local cloud = mtos.bdev:get_app_storage('cloud', 'mail')
|
|
|
|
if not cloud then
|
|
|
|
mtos:set_app("mail:nonet")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if not mtos.sysram.last_player then
|
2017-12-06 16:18:15 +01:00
|
|
|
mtos:set_app() -- no player. Back to launcher
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-12-15 00:30:35 +01:00
|
|
|
if not cloud[mtos.sysram.last_player] then
|
2017-12-06 16:18:15 +01:00
|
|
|
mtos:set_app("mail:newplayer")
|
|
|
|
return false
|
|
|
|
end
|
2017-12-15 00:30:35 +01:00
|
|
|
local account = cloud[mtos.sysram.last_player]
|
2017-12-10 21:27:48 +01:00
|
|
|
account.selected_box = account.selected_box or "inbox"
|
|
|
|
account.selected_index = nil -- will be new determinated by selectedmessage
|
|
|
|
local box = account[account.selected_box] -- inbox or outbox
|
2017-12-10 19:46:35 +01:00
|
|
|
|
2018-02-22 15:46:33 -06:00
|
|
|
app.app_info = app.app_info.." - Welcome "..mtos.sysram.last_player
|
2017-12-06 16:18:15 +01:00
|
|
|
local formspec =
|
2017-12-10 19:46:35 +01:00
|
|
|
"tablecolumns[" ..
|
|
|
|
"image,align=center,1=laptop_mail.png,2=laptop_mail_read.png;".. --icon column
|
|
|
|
"color;".. -- subject and date color
|
|
|
|
"text;".. -- subject
|
|
|
|
"text,padding=1.5;".. -- sender
|
|
|
|
"text,padding=1.5,align=right]".. -- date
|
|
|
|
"table[0,0.5;7.5,8.2;message;"
|
|
|
|
|
2017-12-10 21:27:48 +01:00
|
|
|
if box and box[1] then
|
|
|
|
for idx,message in ipairs(box) do
|
2017-12-10 19:46:35 +01:00
|
|
|
if idx > 1 then
|
|
|
|
formspec = formspec..','
|
|
|
|
end
|
|
|
|
-- set read/unread status
|
2017-12-10 21:27:48 +01:00
|
|
|
if account.selected_box == "sentbox" then
|
|
|
|
formspec = formspec .. "1,#88FF88," -- unread
|
|
|
|
elseif not message.is_read then
|
2018-02-16 21:47:52 -06:00
|
|
|
formspec = formspec .. "1,#FFFFFF," -- unread
|
2017-12-10 19:46:35 +01:00
|
|
|
else
|
2018-02-16 21:47:52 -06:00
|
|
|
formspec = formspec .. "2,#888888," -- read
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
2017-12-10 19:46:35 +01:00
|
|
|
|
|
|
|
-- set subject
|
|
|
|
if not message.subject or message.subject == "" then
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. "(No Subject),"
|
2017-12-10 19:46:35 +01:00
|
|
|
elseif string.len(message.subject) > 30 then
|
|
|
|
formspec = formspec .. minetest.formspec_escape(string.sub(message.subject,1,27)) .. "...,"
|
2017-12-06 16:18:15 +01:00
|
|
|
else
|
2017-12-10 19:46:35 +01:00
|
|
|
formspec = formspec .. minetest.formspec_escape(message.subject) .. ","
|
|
|
|
end
|
|
|
|
|
2017-12-10 21:27:48 +01:00
|
|
|
-- set sender or receiver
|
|
|
|
if account.selected_box == "inbox" then
|
|
|
|
formspec = formspec..minetest.formspec_escape(message.sender or "") .."," -- body
|
|
|
|
else
|
|
|
|
formspec = formspec..minetest.formspec_escape(message.receiver or "") .."," -- body
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set date
|
|
|
|
formspec = formspec .. os.date("%c", message.time) -- timestamp
|
|
|
|
|
|
|
|
-- handle marked line
|
2017-12-10 19:46:35 +01:00
|
|
|
if account.selectedmessage and
|
|
|
|
message.sender == account.selectedmessage.sender and
|
|
|
|
message.subject == account.selectedmessage.subject and
|
|
|
|
message.time == account.selectedmessage.time and
|
|
|
|
message.body == account.selectedmessage.body then
|
2017-12-10 21:27:48 +01:00
|
|
|
account.selected_index = idx
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
|
|
|
end
|
2017-12-10 21:27:48 +01:00
|
|
|
formspec = formspec .. ";"..(account.selected_index or "").."]"
|
2017-12-06 16:18:15 +01:00
|
|
|
else
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. ",,No Mail :(]"
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
|
|
|
|
2017-12-10 21:27:48 +01:00
|
|
|
-- toggle inbox/sentbox
|
|
|
|
if account.selected_box == "inbox" then
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('0,9;1,1', 'minor', 'switch_sentbox', 'laptop_mail_sentbox.png', '', 'Show Sent Messages')
|
2017-12-10 21:27:48 +01:00
|
|
|
else
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('0,9;1,1', 'minor', 'switch_inbox', 'laptop_mail_received.png', '', 'Show Received Messages')
|
2017-12-10 21:27:48 +01:00
|
|
|
end
|
|
|
|
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('1.7,9;1,1', 'minor', 'new', 'laptop_email_new.png', '', 'New Message')
|
2017-12-10 20:18:46 +01:00
|
|
|
if account.newmessage then
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('2.7,9;1,1', 'minor', 'continue', 'laptop_email_edit.png', '', 'Continue Last Message')
|
2017-12-10 20:18:46 +01:00
|
|
|
end
|
|
|
|
|
2017-12-06 16:18:15 +01:00
|
|
|
if account.selectedmessage then
|
2017-12-10 20:18:46 +01:00
|
|
|
formspec = formspec ..
|
2017-12-18 20:04:56 +01:00
|
|
|
mtos.theme:get_image_button('3.7,9;1,1', 'minor', 'reply', 'laptop_email_reply.png', '', 'Reply')..
|
|
|
|
mtos.theme:get_image_button('4.7,9;1,1', 'minor', 'forward', 'laptop_email_forward.png', '', 'Forward')..
|
|
|
|
mtos.theme:get_image_button('5.7,9;1,1', 'minor', 'delete', 'laptop_email_trash.png', '', 'Delete')
|
2017-12-10 21:27:48 +01:00
|
|
|
if account.selected_box == "inbox" then
|
|
|
|
if not account.selectedmessage.is_read then
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('6.7,9;1,1', 'minor', 'markread', 'laptop_mail_read_button.png', '', 'Mark Message as Read')
|
2017-12-10 21:27:48 +01:00
|
|
|
else
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('6.7,9;1,1', 'minor', 'markunread', 'laptop_mail_button.png', '', 'Mark Message as Unread')
|
2017-12-10 21:27:48 +01:00
|
|
|
end
|
|
|
|
end
|
2017-12-24 10:42:58 -05:00
|
|
|
formspec = formspec .. mtos.theme:get_image_button('8,9;1,1', 'minor', 'print', 'printer_button.png', '', 'Print Email')
|
2017-12-10 21:27:48 +01:00
|
|
|
if account.selected_box == "inbox" then
|
2017-12-11 08:41:59 +01:00
|
|
|
formspec = formspec .. mtos.theme:get_label('8,0.5', "From: "..(account.selectedmessage.sender or ""))
|
2017-12-10 20:18:46 +01:00
|
|
|
else
|
2017-12-11 08:41:59 +01:00
|
|
|
formspec = formspec .. mtos.theme:get_label('8,0.5', "To: "..(account.selectedmessage.receiver or ""))
|
2017-12-10 20:18:46 +01:00
|
|
|
end
|
2017-12-10 21:27:48 +01:00
|
|
|
|
2017-12-11 08:41:59 +01:00
|
|
|
formspec = formspec .. mtos.theme:get_label('8,1', "Subject: "..(account.selectedmessage.subject or ""))..
|
2018-02-21 20:07:43 +01:00
|
|
|
"background[8,1.55;6.92,7.3;"..mtos.theme.contrast_background.."]"..
|
2017-12-22 14:32:07 +01:00
|
|
|
"textarea[8.35,1.6;6.8,8.25;;"..(minetest.formspec_escape(account.selectedmessage.body) or "")..";]"
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
|
|
|
return formspec
|
|
|
|
end,
|
2017-12-11 17:47:33 +01:00
|
|
|
receive_fields_func = function(app, mtos, sender, fields)
|
2017-12-15 00:30:35 +01:00
|
|
|
if sender:get_player_name() ~= mtos.sysram.last_player then
|
2017-12-06 16:18:15 +01:00
|
|
|
mtos:set_app() -- wrong player. Back to launcher
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-15 00:30:35 +01:00
|
|
|
local cloud = mtos.bdev:get_app_storage('cloud', 'mail')
|
|
|
|
local account = cloud[mtos.sysram.last_player]
|
2017-12-11 08:41:59 +01:00
|
|
|
if not account then
|
|
|
|
mtos:set_app() -- wrong player. Back to launcher
|
|
|
|
return
|
|
|
|
end
|
2017-12-10 21:27:48 +01:00
|
|
|
account.selected_box = account.selected_box or "inbox"
|
|
|
|
local box = account[account.selected_box] -- inbox or outbox
|
2017-12-06 16:18:15 +01:00
|
|
|
|
2017-12-10 19:46:35 +01:00
|
|
|
-- Set read status if 2 seconds selected
|
2017-12-10 21:27:48 +01:00
|
|
|
if account.selected_index and account.selectedmessage and account.selected_box == "inbox" and
|
|
|
|
account.selected_timestamp and (os.time() - account.selected_timestamp) > 1 then
|
2017-12-10 19:46:35 +01:00
|
|
|
account.selectedmessage.is_read = true
|
|
|
|
end
|
|
|
|
|
2017-12-10 21:27:48 +01:00
|
|
|
-- process input
|
2017-12-06 16:18:15 +01:00
|
|
|
if fields.message then
|
2017-12-10 19:46:35 +01:00
|
|
|
local event = minetest.explode_table_event(fields.message)
|
2017-12-10 21:27:48 +01:00
|
|
|
account.selectedmessage = box[event.row]
|
2017-12-10 19:46:35 +01:00
|
|
|
if account.selectedmessage then
|
2017-12-10 21:27:48 +01:00
|
|
|
account.selected_index = event.row
|
|
|
|
account.selected_timestamp = os.time()
|
2017-12-10 19:46:35 +01:00
|
|
|
else
|
2017-12-10 21:27:48 +01:00
|
|
|
account.selected_index = nil
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
|
|
|
elseif fields.new then
|
|
|
|
account.newmessage = {}
|
|
|
|
mtos:set_app("mail:compose")
|
|
|
|
elseif fields.continue then
|
|
|
|
mtos:set_app("mail:compose")
|
2017-12-10 21:27:48 +01:00
|
|
|
elseif fields.switch_sentbox then
|
|
|
|
account.selected_box = "sentbox"
|
|
|
|
account.selectedmessage = nil
|
|
|
|
elseif fields.switch_inbox then
|
|
|
|
account.selected_box = "inbox"
|
|
|
|
account.selectedmessage = nil
|
|
|
|
elseif account.selected_index then
|
2017-12-06 16:18:15 +01:00
|
|
|
if fields.delete then
|
2017-12-10 21:27:48 +01:00
|
|
|
table.remove(box, account.selected_index)
|
2017-12-10 20:18:46 +01:00
|
|
|
account.selectedmessage = nil
|
2017-12-06 16:18:15 +01:00
|
|
|
elseif fields.reply then
|
|
|
|
account.newmessage = {}
|
|
|
|
account.newmessage.receiver = account.selectedmessage.sender
|
|
|
|
account.newmessage.subject = "Re: "..(account.selectedmessage.subject or "")
|
|
|
|
account.newmessage.body = "Type your reply here."..string.char(10)..string.char(10).."--Original message follows--"..string.char(10)..(account.selectedmessage.body or "")
|
|
|
|
mtos:set_app("mail:compose")
|
|
|
|
elseif fields.forward then
|
|
|
|
account.newmessage = {}
|
|
|
|
account.newmessage.subject = "Fw: "..(account.selectedmessage.subject or "")
|
|
|
|
account.newmessage.body = "Type your reply here."..string.char(10)..string.char(10).."--Original message follows--"..string.char(10)..(account.selectedmessage.body or "")
|
|
|
|
mtos:set_app("mail:compose")
|
|
|
|
elseif fields.markread then
|
|
|
|
account.selectedmessage.is_read = true
|
|
|
|
elseif fields.markunread then
|
|
|
|
account.selectedmessage.is_read = false
|
2017-12-23 18:51:40 +01:00
|
|
|
account.selected_timestamp = nil
|
|
|
|
elseif fields.print then
|
|
|
|
mtos:print_file_dialog({
|
|
|
|
label = account.selectedmessage.subject,
|
|
|
|
author = account.selectedmessage.sender,
|
|
|
|
timestamp = account.selectedmessage.time,
|
|
|
|
text = account.selectedmessage.body,
|
|
|
|
})
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
|
|
|
laptop.register_view("mail:newplayer", {
|
|
|
|
formspec_func = function(app, mtos)
|
2017-12-15 00:30:35 +01:00
|
|
|
return mtos.theme:get_label('1,3', "No mail account for player "..mtos.sysram.last_player.. " found. Do you like to create a new account?")..
|
2017-12-24 10:42:58 -05:00
|
|
|
mtos.theme:get_button('1,4;3,1', 'major', 'create', 'Create Account')
|
2017-12-06 16:18:15 +01:00
|
|
|
end,
|
2017-12-11 17:47:33 +01:00
|
|
|
receive_fields_func = function(app, mtos, sender, fields)
|
2017-12-15 00:30:35 +01:00
|
|
|
if sender:get_player_name() ~= mtos.sysram.last_player then
|
2017-12-06 16:18:15 +01:00
|
|
|
mtos:set_app() -- wrong player. Back to launcher
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if fields.create then
|
2017-12-17 10:50:24 +01:00
|
|
|
local cloud = mtos.bdev:get_app_storage('cloud', 'mail')
|
2017-12-15 00:30:35 +01:00
|
|
|
cloud[mtos.sysram.last_player] = {
|
2017-12-06 16:18:15 +01:00
|
|
|
inbox = {},
|
2017-12-10 21:27:48 +01:00
|
|
|
sentbox = {}
|
2017-12-06 16:18:15 +01:00
|
|
|
}
|
|
|
|
app:back_app()
|
|
|
|
elseif fields.os_back then
|
|
|
|
app:exit_app()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2017-12-15 00:30:35 +01:00
|
|
|
laptop.register_view("mail:nonet", {
|
|
|
|
formspec_func = function(app, mtos)
|
2018-02-22 15:46:33 -06:00
|
|
|
return mtos.theme:get_label('1,3', "NO NETWORK CONNECTION")
|
2017-12-15 00:30:35 +01:00
|
|
|
end,
|
|
|
|
receive_fields_func = function(app, mtos, sender, fields)
|
|
|
|
app:exit_app()
|
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2017-12-06 16:18:15 +01:00
|
|
|
-- Write new mail
|
|
|
|
laptop.register_view("mail:compose", {
|
|
|
|
formspec_func = function(app, mtos)
|
2017-12-15 00:30:35 +01:00
|
|
|
local cloud = mtos.bdev:get_app_storage('cloud', 'mail')
|
|
|
|
local account = cloud[mtos.sysram.last_player]
|
2017-12-06 16:18:15 +01:00
|
|
|
account.newmessage = account.newmessage or {}
|
|
|
|
local message = account.newmessage
|
|
|
|
|
2018-02-21 20:07:43 +01:00
|
|
|
local formspec = "background[0,0.4;8,2.4;"..mtos.theme.contrast_background.."]"..
|
2017-12-14 12:14:15 +01:00
|
|
|
"label[0.25,2;Subject:]".."field[2.7,2;5,1;subject;;"..minetest.formspec_escape(message.subject or "").."]"..
|
2018-02-21 20:07:43 +01:00
|
|
|
"background[0,3.05;7.95,3.44;"..mtos.theme.contrast_background.."]"..
|
2017-12-14 07:37:16 +01:00
|
|
|
"textarea[0.25,3;8,4;body;;"..minetest.formspec_escape(message.body or "").."]"..
|
|
|
|
mtos.theme:get_button("0,8;2,1", "major", "send", "Send message")..
|
2017-12-14 12:14:15 +01:00
|
|
|
"label[0.25,0.75;Receiver:]".."dropdown[2.4,0.75;5.2,1;receiver;"
|
2017-12-14 07:37:16 +01:00
|
|
|
|
|
|
|
local sortedtab = {}
|
|
|
|
for playername,_ in pairs(cloud) do
|
|
|
|
table.insert(sortedtab, playername)
|
|
|
|
end
|
|
|
|
table.sort(sortedtab)
|
|
|
|
|
|
|
|
local selected_idx
|
|
|
|
for idx, playername in ipairs(sortedtab) do
|
|
|
|
formspec = formspec..',' .. minetest.formspec_escape(playername)
|
|
|
|
if playername == message.receiver then
|
|
|
|
selected_idx = idx+1 -- +1 because of empty entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
formspec = formspec .. ";"..(selected_idx or "").."]"
|
|
|
|
|
2017-12-06 16:18:15 +01:00
|
|
|
if message.receiver and not cloud[message.receiver] then
|
2017-12-11 11:33:09 +01:00
|
|
|
formspec = formspec..mtos.theme:get_label('2.3,8', "invalid receiver player")
|
2017-12-06 16:18:15 +01:00
|
|
|
end
|
|
|
|
return formspec
|
|
|
|
end,
|
2017-12-11 17:47:33 +01:00
|
|
|
receive_fields_func = function(app, mtos, sender, fields)
|
2017-12-15 00:30:35 +01:00
|
|
|
if sender:get_player_name() ~= mtos.sysram.last_player then
|
2017-12-06 16:18:15 +01:00
|
|
|
mtos:set_app() -- wrong player. Back to launcher
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-12-15 00:30:35 +01:00
|
|
|
local cloud = mtos.bdev:get_app_storage('cloud', 'mail')
|
|
|
|
local account = cloud[mtos.sysram.last_player]
|
2017-12-06 16:18:15 +01:00
|
|
|
account.newmessage = account.newmessage or {}
|
|
|
|
local message = account.newmessage
|
|
|
|
|
|
|
|
message.receiver = fields.receiver or message.receiver
|
2017-12-15 00:30:35 +01:00
|
|
|
message.sender = mtos.sysram.last_player
|
2017-12-06 16:18:15 +01:00
|
|
|
message.time = os.time()
|
|
|
|
message.subject = fields.subject or message.subject
|
|
|
|
message.body = fields.body or message.body
|
|
|
|
|
|
|
|
if fields.send and message.receiver and cloud[message.receiver] then
|
|
|
|
table.insert(cloud[message.receiver].inbox, message)
|
|
|
|
table.insert(account.sentbox, table.copy(message))
|
|
|
|
account.newmessage = nil
|
|
|
|
app:back_app()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|