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 " ,
app_info = " Write mails to other players " ,
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
2017-12-06 16:18:15 +01:00
local formspec =
2017-12-15 00:30:35 +01:00
" label[4,-0.31;Welcome " .. mtos.sysram . last_player .. " ] " ..
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
2017-12-10 19:46:35 +01:00
formspec = formspec .. " 1,#FF8888, " -- unread
else
formspec = formspec .. " 2,#FFFFFF, " -- 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
formspec = formspec .. " (No subject), "
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-10 19:46:35 +01: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-13 22:23:01 -05:00
formspec = formspec .. " image_button[0,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_mail_sentbox.png;switch_sentbox;]tooltip[switch_sentbox;Show sent messages] "
2017-12-10 21:27:48 +01:00
else
2017-12-13 22:23:01 -05:00
formspec = formspec .. " image_button[0,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_mail_received.png;switch_inbox;]tooltip[switch_inbox;Show received messages] "
2017-12-10 21:27:48 +01:00
end
2017-12-11 08:41:59 +01:00
formspec = formspec .. " image_button[1.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_email_new.png;new;]tooltip[new;New message] "
2017-12-10 20:18:46 +01:00
if account.newmessage then
2017-12-11 08:41:59 +01:00
formspec = formspec .. " image_button[2.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_email_edit.png;continue;]tooltip[continue;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-11 08:41:59 +01:00
" image_button[3.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_email_reply.png;reply;]tooltip[reply;Reply] " ..
" image_button[4.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_email_forward.png;forward;]tooltip[forward;Forward] " ..
" image_button[5.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_email_trash.png;delete;]tooltip[delete;Delete] "
2017-12-10 21:27:48 +01:00
if account.selected_box == " inbox " then
if not account.selectedmessage . is_read then
2017-12-13 08:40:09 +01:00
formspec = formspec .. " image_button[6.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_mail_read_button.png;markread;]tooltip[markread;Mark message as read] "
2017-12-10 21:27:48 +01:00
else
2017-12-13 08:40:09 +01:00
formspec = formspec .. " image_button[6.7,9;1,1; " .. mtos.theme . minor_button .. " ^laptop_mail_button.png;markunread;]tooltip[markunread;Mark message as unread] "
2017-12-10 21:27:48 +01:00
end
end
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 " " ) ) ..
2017-12-13 20:32:54 +01:00
" background[8,1.55;6.92,7.3; " .. mtos.theme . contrast_bg .. " ] " ..
2017-12-11 11:33:09 +01:00
" textarea[8.25,1.5;7,8.35;body;; " .. ( 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-10 21:27:48 +01:00
account.selected_timestamp = nil -- Stop timer
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-11 08:41:59 +01: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 )
return mtos.theme : get_label ( ' 1,3 ' , " No internet available on this computer " )
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
2017-12-14 12:14:15 +01:00
local formspec = " background[0,0.4;8,2.4; " .. mtos.theme . contrast_bg .. " ] " ..
" label[0.25,2;Subject:] " .. " field[2.7,2;5,1;subject;; " .. minetest.formspec_escape ( message.subject or " " ) .. " ] " ..
2017-12-13 20:32:54 +01:00
" background[0,3.05;7.95,3.44; " .. mtos.theme . contrast_bg .. " ] " ..
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
} )