From ea0de708da76aaaaf3e5a4b4a24b219bd02ca3ec Mon Sep 17 00:00:00 2001 From: Peter Nerlich Date: Mon, 10 Aug 2020 11:43:10 +0200 Subject: [PATCH] fix some things, adjust GUI elements - messages are now actually sent (bug in parse player list) - no more crashes on sending mail (forgot to make variables local) - actually handle CC and BCC fields instead of leaving them empty, duh - make new functions be under the mail namespace - add util functions to ensure the new format, parse and player list as well as checking whether a player is in that list or not - rearrange some GUI elements (tighter spacing, grouping, increase window height to be consistent) - convert mails to new format only as needed (old mails stay intact in case someone reverts to old version) - mails are shaded differently in inbox, depending on whether the player is in the TO field - FROM, TO and CC fields are all displayed when reading a mail - add "Reply All" button (TO includes all original recipients plus the sender, but excluding the player himself, while adopting the CC field. To contrast: "Reply" just sets the original sender as TO and leaves the rest empty) - move reply, replyall and forward to their own functions in GUI (was duplicated for inbox and show mail) - don't needlessly set messages table when we do nothing but go back --- api.lua | 13 ++-- gui.lua | 147 ++++++++++++++++++++++++++++++++------------- util/normalize.lua | 44 +++++++++++--- 3 files changed, 150 insertions(+), 54 deletions(-) diff --git a/api.lua b/api.lua index 65d466f..1b3c3a8 100644 --- a/api.lua +++ b/api.lua @@ -28,6 +28,9 @@ function mail.send(src, dst, subject, body) m.body = body end + local cc + local bcc + local extra -- log mail send action if m.cc or m.bcc then if m.cc then @@ -43,22 +46,22 @@ function mail.send(src, dst, subject, body) else bcc = "" end - extra = "(" .. cc .. bcc .. ") " + extra = " (" .. cc .. bcc .. ")" else extra = "" end - minetest.log("action", "[mail] '" .. m.from .. "' sends mail to '" .. m.to .. + minetest.log("action", "[mail] '" .. m.from .. "' sends mail to '" .. m.to .. "'" .. extra .. "' with subject '" .. m.subject .. "' and body: '" .. m.body .. "'") -- normalize to, cc and bcc while compiling a list of all recipients local recipients = {} - m.to = normalize_players_and_add_recipients(m.to, recipients) + m.to = mail.normalize_players_and_add_recipients(m.to, recipients) if m.cc then - m.cc = normalize_players_and_add_recipients(m.cc, recipients) + m.cc = mail.normalize_players_and_add_recipients(m.cc, recipients) end if m.bcc then - m.bcc = normalize_players_and_add_recipients(m.bcc, recipients) + m.bcc = mail.normalize_players_and_add_recipients(m.bcc, recipients) end -- form the actual mail diff --git a/gui.lua b/gui.lua index 9ff2840..b7efc5e 100644 --- a/gui.lua +++ b/gui.lua @@ -8,15 +8,16 @@ else end mail.inbox_formspec = "size[8,9;]" .. theme .. [[ - button_exit[7.5,0;0.5,0.5;quit;X] + button_exit[7.25,0;0.75,0.5;quit;X] button[6,1;2,0.5;new;New Message] button[6,2;2,0.5;read;Read] button[6,3;2,0.5;reply;Reply] - button[6,4;2,0.5;forward;Forward] - button[6,5;2,0.5;delete;Delete] - button[6,6;2,0.5;markread;Mark Read] - button[6,7;2,0.5;markunread;Mark Unread] - button[6,8;2,0.5;about;About] + button[6,3.8;2,0.5;replyall;Reply All] + button[6,4.6;2,0.5;forward;Forward] + button[6,5.6;2,0.5;markread;Mark Read] + button[6,6.4;2,0.5;markunread;Mark Unread] + button[6,7.4;2,0.5;delete;Delete] + button[6,8.4;2,0.5;about;About] tablecolumns[color;text;text] table[0,0;5.75,9;messages;#999,From,Subject]] @@ -24,7 +25,7 @@ mail.inbox_formspec = "size[8,9;]" .. theme .. [[ function mail.show_about(name) local formspec = [[ size[8,5;] - button[7.5,0;0.5,0.5;back;X] + 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] @@ -44,13 +45,22 @@ function mail.show_inbox(name) if messages[1] then for _, message in ipairs(messages) do + mail.ensure_new_format(message) if message.unread then - formspec[#formspec + 1] = ",#FFD700" + if not mail.player_in_list(name, message.to) then + formspec[#formspec + 1] = ",#FFD788" + else + formspec[#formspec + 1] = ",#FFD700" + end else - formspec[#formspec + 1] = "," + 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.sender) + formspec[#formspec + 1] = minetest.formspec_escape(message.from) formspec[#formspec + 1] = "," if message.subject ~= "" then if string.len(message.subject) > 30 then @@ -79,47 +89,90 @@ function mail.show_message(name, msgnumber) local messages = mail.getMessages(name) local message = messages[msgnumber] local formspec = [[ - size[8,7.2] - button[7,0;1,0.5;back;X] + size[8,9] + button[7.25,0;0.75,0.5;back;X] label[0,0;From: %s] - label[0,0.5;Subject: %s] - textarea[0.25,1.25;8,6.25;body;;%s] - button[1,6.7;2,1;reply;Reply] - button[3,6.7;2,1;forward;Forward] - button[5,6.7;2,1;delete;Delete] + 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] ]] .. theme local from = minetest.formspec_escape(message.from) + local to = minetest.formspec_escape(message.to) + local cc = minetest.formspec_escape(message.cc) local subject = minetest.formspec_escape(message.subject) local body = minetest.formspec_escape(message.body) - formspec = string.format(formspec, from, subject, body) + formspec = string.format(formspec, from, to, cc, subject, body) minetest.show_formspec(name,"mail:message",formspec) end function mail.show_compose(name, defaultto, defaultsubj, defaultbody, defaultcc, defaultbcc) local formspec = [[ - size[8,7.2] - field[0.25,0.5;4,1;to;To:;%s] - field[0.25,0.5;4,1;to;CC:;%s] - field[0.25,0.5;4,1;to;BCC:;%s] - field[0.25,1.7;8,1;subject;Subject:;%s] - textarea[0.25,2.4;8,5;body;;%s] - button[0.5,6.7;3,1;cancel;Cancel] - button[7,0;1,0.5;cancel;X] - button[4.5,6.7;3,1;send;Send] + 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] ]] .. theme + defaultto = defaultto or "" + defaultsubj = defaultsubj or "" + defaultbody = defaultbody or "" + defaultcc = defaultcc or "" + defaultbcc = defaultbcc or "" + formspec = string.format(formspec, minetest.formspec_escape(defaultto), - minetest.formspec_escape(defaultsubj), - minetest.formspec_escape(defaultbody), minetest.formspec_escape(defaultcc), - minetest.formspec_escape(defaultbcc)) + minetest.formspec_escape(defaultbcc), + minetest.formspec_escape(defaultsubj), + minetest.formspec_escape(defaultbody)) 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 + print('parsing recipients: '..recipients) + 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) + print('resulting recipients: '..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 + 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") @@ -158,28 +211,36 @@ function mail.handle_receivefields(player, formname, fields) mail.show_inbox(name) elseif fields.reply and messages[selected_message_idxs[name]] then local message = messages[selected_message_idxs[name]] - local replyfooter = "Type your reply here.\n\n--Original message follows--\n" ..message.body - mail.show_compose(name, message.from, "Re: "..message.subject,replyfooter) + 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) elseif fields.forward and messages[selected_message_idxs[name]] then local message = messages[selected_message_idxs[name]] - local fwfooter = "Type your message here.\n\n--Original message follows--\n" ..message.body - mail.show_compose(name, "", "Fw: "..message.subject, fwfooter) + mail.forward(name, message) elseif fields.markread then if messages[selected_message_idxs[name]] then messages[selected_message_idxs[name]].unread = false end + -- set messages immediately, so it shows up already when updating the inbox + mail.setMessages(name, messages) mail.show_inbox(name) + return true elseif fields.markunread then if messages[selected_message_idxs[name]] then messages[selected_message_idxs[name]].unread = true end + -- set messages immediately, so it shows up already when updating the inbox + mail.setMessages(name, messages) mail.show_inbox(name) + return true elseif fields.new then - mail.show_compose(name,"","","Type your message here.") + mail.show_compose(name) elseif fields.quit then if minetest.get_modpath("unified_inventory") then @@ -199,14 +260,16 @@ function mail.handle_receivefields(player, formname, fields) if fields.back then mail.show_inbox(name) + return true -- don't uselessly set messages elseif fields.reply then local message = messages[selected_message_idxs[name]] - local replyfooter = "Type your reply here.\n\n--Original message follows--\n" ..message.body - mail.show_compose(name, message.sender, "Re: "..message.subject, replyfooter) + mail.reply(name, message) + elseif fields.replyall then + local message = messages[selected_message_idxs[name]] + mail.replyall(name, message) elseif fields.forward then local message = messages[selected_message_idxs[name]] - local fwfooter = "Type your message here.\n\n--Original message follows--\n" ..message.body - mail.show_compose(name, "", "Fw: "..message.subject, fwfooter) + mail.forward(name, message.subject) elseif fields.delete then if messages[selected_message_idxs[name]] then table.remove(messages,selected_message_idxs[name]) @@ -221,10 +284,10 @@ function mail.handle_receivefields(player, formname, fields) mail.send({ from = player:get_player_name(), to = fields.to, - cc = "", - bcc = "" + cc = fields.cc, + bcc = fields.bcc, subject = fields.subject, - body = fields.body + body = fields.body, }) end minetest.after(0.5, function() diff --git a/util/normalize.lua b/util/normalize.lua index 13f51b0..f91859f 100644 --- a/util/normalize.lua +++ b/util/normalize.lua @@ -2,7 +2,18 @@ return the field normalized (comma separated, single space) and add individual player names to recipient list --]] -function normalize_players_and_add_recipients(field, recipients) +function mail.normalize_players_and_add_recipients(field, recipients) + local order = mail.parse_player_list(field) + for i,c in ipairs(order) do + if recipients[string.lower(c)] == nil then + recipients[string.lower(c)] = c + end + end + return mail.concat_player_list(order) +end + + +function mail.parse_player_list(field) local separator = ", " local pattern = "([^" .. separator .. "]+)" @@ -10,17 +21,36 @@ function normalize_players_and_add_recipients(field, recipients) local player_set = {} local order = {} field:gsub(pattern, function(c) - if player_set[string.lower(c)] ~= nil then + if player_set[string.lower(c)] == nil then player_set[string.lower(c)] = c order[#order+1] = c - - -- also sort into recipients - if recipients[string.lower(c)] ~= nil then - recipients[string.lower(c)] = c - end end end) + return order +end + +function mail.concat_player_list(order) -- turn list of players back into normalized string return table.concat(order, ", ") end + +function mail.player_in_list(name, list) + if type(list) == "string" then + list = mail.parse_player_list(list) + end + for k,c in pairs(list) do + if name == c then + return true + end + end + return false +end + + +function mail.ensure_new_format(message) + if message.sender then + message.from = message.sender + message.to = name + end +end