From 9e4de68a260b149c0c3a114ff1128f3b70792b0a Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Mon, 21 Dec 2015 17:42:58 +0100 Subject: [PATCH] [email] Add mod --- mods/email/README.md | 22 +++ mods/email/depends.txt | 1 + mods/email/hud.lua | 44 ++++++ mods/email/hudkit.lua | 48 +++++++ mods/email/init.lua | 215 +++++++++++++++++++++++++++++ mods/email/textures/email_mail.png | Bin 0 -> 270 bytes worlds/minetestforfun/world.mt | 1 + 7 files changed, 331 insertions(+) create mode 100644 mods/email/README.md create mode 100644 mods/email/depends.txt create mode 100644 mods/email/hud.lua create mode 100644 mods/email/hudkit.lua create mode 100644 mods/email/init.lua create mode 100644 mods/email/textures/email_mail.png diff --git a/mods/email/README.md b/mods/email/README.md new file mode 100644 index 00000000..1ebeaaa3 --- /dev/null +++ b/mods/email/README.md @@ -0,0 +1,22 @@ +# Email + +Created by rubenwardy. + +License: WTFPL. + +## Usage + +Send an email: + +* /mail username message to send + +View inbox: + +* /inbox +* /inbox text + +No formspec commands (good for IRC): + +* /mail username message to send +* /inbox text +* /inbox clear diff --git a/mods/email/depends.txt b/mods/email/depends.txt new file mode 100644 index 00000000..aa2dd225 --- /dev/null +++ b/mods/email/depends.txt @@ -0,0 +1 @@ +chatplus? diff --git a/mods/email/hud.lua b/mods/email/hud.lua new file mode 100644 index 00000000..f974e5fc --- /dev/null +++ b/mods/email/hud.lua @@ -0,0 +1,44 @@ +local hudkit = dofile(minetest.get_modpath("email") .. "/hudkit.lua") +email.hud = hudkit() +function email.update_hud(player) + local name = player:get_player_name() + local inbox = email.get_inbox(name) + + if inbox and #inbox > 0 then + if email.hud:exists(player, "email:text") then + email.hud:change(player, "email:text", "text", #inbox .. " /inbox") + else + email.hud:add(player, "email:icon", { + hud_elem_type = "image", + name = "MailIcon", + position = {x=0.52, y=0.52}, + text="email_mail.png", + scale = {x=1,y=1}, + alignment = {x=0.5, y=0.5}, + }) + + email.hud:add(player, "email:text", { + hud_elem_type = "text", + name = "MailText", + position = {x=0.55, y=0.52}, + text= #inbox .. " /inbox", + scale = {x=1,y=1}, + alignment = {x=0.5, y=0.5}, + }) + end + else + email.hud:remove(player, "email:icon") + email.hud:remove(player, "email:text") + end +end +minetest.register_on_leaveplayer(function(player) + email.hud.players[player:get_player_name()] = nil +end) +function email.update_all_hud() + local players = minetest.get_connected_players() + for _, player in pairs(players) do + email.update_hud(player) + end + minetest.after(5, email.update_all_hud) +end +minetest.after(5, email.update_all_hud) diff --git a/mods/email/hudkit.lua b/mods/email/hudkit.lua new file mode 100644 index 00000000..d7635577 --- /dev/null +++ b/mods/email/hudkit.lua @@ -0,0 +1,48 @@ +-- HudKit, by rubenwardy +-- License: Either WTFPL or CC0, you can choose. + +local function hudkit() + return { + players = {}, + + add = function(self, player, id, def) + local name = player:get_player_name() + local elements = self.players[name] + + if not elements then + self.players[name] = {} + elements = self.players[name] + end + + elements[id] = player:hud_add(def) + end, + + exists = function(self, player, id) + local elements = self.players[player:get_player_name()] + return elements and elements[id] + end, + + change = function(self, player, id, stat, value) + local elements = self.players[player:get_player_name()] + if not elements or not elements[id] then + return false + end + + player:hud_change(elements[id], stat, value) + return true + end, + + remove = function(self, player, id) + local elements = self.players[player:get_player_name()] + if not elements or not elements[id] then + return false + end + + player:hud_remove(elements[id]) + elements[id] = nil + return true + end + } +end + +return hudkit diff --git a/mods/email/init.lua b/mods/email/init.lua new file mode 100644 index 00000000..90e6ac47 --- /dev/null +++ b/mods/email/init.lua @@ -0,0 +1,215 @@ +email = { + log = function(msg) end +} +local _loading = true + +if minetest.global_exists("chatplus") then + email.log = chatplus.log + + function chatplus.on_old_inbox(inbox) + if _loading then + return + end + + email.inboxes = inbox + end +end + +function email.init() + email.inboxes = {} + email.load() + _loading = false + if minetest.global_exists("chatplus") and chatplus.old_inbox then + chatplus.on_old_inbox(chatplus.old_inbox) + end +end + +function email.load() + local file = io.open(minetest.get_worldpath() .. "/email.txt", "r") + if file then + local from_file = minetest.deserialize(file:read("*all")) + file:close() + if type(from_file) == "table" then + if from_file.mod == "email" and tonumber(from_file.ver_min) <= 1 then + email.inboxes = from_file.inboxes + else + error("[Email] Attempt to read incompatible email.txt file.") + end + end + end +end + +function email.save() + local file = io.open(minetest.get_worldpath() .. "/email.txt", "w") + if file then + file:write(minetest.serialize({ + mod = "email", version = 1, ver_min = 1, + inboxes = email.inboxes })) + file:close() + end +end +minetest.register_on_shutdown(email.save) + +function email.get_inbox(name) + return email.inboxes[name] or {} +end + +function email.clear_inbox(name) + email.inboxes[name] = nil + email.save() +end + +minetest.register_on_joinplayer(function(player) + local inbox = email.get_inbox(player:get_player_name()) + if #inbox > 0 then + minetest.after(10, function() + minetest.chat_send_player(player:get_player_name(), + "(" .. #inbox .. ") You have mail! Type /inbox to recieve") + end) + end +end) + +function email.get_formspec(name) + local inbox = email.get_inbox(name) + + local fs = "size[12,8]" + fs = fs .. "vertlabel[0,0;email Mail]" + + function row(fs, c1, date, from, msg) + date = minetest.formspec_escape(date) + from = minetest.formspec_escape(from) + msg = minetest.formspec_escape(msg) + return fs .. ",#d0d0d0," .. table.concat({date, c1, from, msg}, ",") + end + + fs = fs .. "tablecolumns[color;text;color;text;text]" + fs = fs .. "tableoptions[highlight=#ffffff33]" + fs = fs .. "table[0.5,0;11.25,7;inbox;" + fs = fs .. "#ffffff,Date,,From,Message" + if #inbox == 0 then + fs = row(fs, "#d0d0d0", "", ":)", "Well done! Your inbox is empty!") + else + for i = 1, #inbox do + local color = "#ffffff" + if minetest.check_player_privs(inbox[i].from, {kick = true, ban = true}) then + color = "#FFD700" + end + local msg = inbox[i].msg + fs = row(fs, color, inbox[i].date, inbox[i].from, msg:sub(1, 44)) + while #msg > 45 do + msg = msg:sub(45, #msg) + fs = row(fs, color, "", "", msg:sub(1, 44)) + end + end + end + fs = fs .. "]" + + fs = fs .. "button[0,7.25;2,1;clear;Delete All]" + --fs = fs .. "button[0,7.25;2,1;clear;Mark as read]" + fs = fs .. "button_exit[10.1,7.25;2,1;close;Close]" + fs = fs .. "label[2,7.4;Exit then type /mail username message to reply]" + + return fs +end + +function email.show_inbox(name, text_mode) + if text_mode then + local inbox = email.get_inbox(name) + if #inbox == 0 then + return true, "Your inbox is empty!" + else + minetest.chat_send_player(name, #inbox .. " items in your inbox:") + for i = 1, #inbox do + local item = inbox[i] + minetest.chat_send_player(name, i .. ") " ..item.date .. + " <" .. item.from .. "> " .. item.msg) + end + return true, "End of mail (" .. #inbox .. " items)" + end + else + local fs = email.get_formspec(name) + minetest.show_formspec(name, "email:inbox", fs) + + return true, "Opened inbox!" + end + + return true +end + +minetest.register_on_player_receive_fields(function(player,formname,fields) + if fields.clear then + local name = player:get_player_name() + email.clear_inbox(name) + minetest.chat_send_player(name, "Inbox cleared!") + email.show_inbox(name) + end + + --[[if fields.mark_all_read then + if player and player.inbox and #player.inbox > 0 then + for i = 1, #player.inbox do + player.inbox[i].read = true + end + minetest.chat_send_player(name, "Marked all as read!") + email.show_inbox(name) + end + end]]-- +end) + +function email.send_mail(name, to, msg) + minetest.log("Email - To: "..to..", From: "..name..", MSG: "..msg) + email.log("Email - To: "..to..", From: "..name..", MSG: "..msg) + if true then + local mail = { + date = os.date("%Y-%m-%d %H:%M:%S"), + from = name, + msg = msg} + + if email.inboxes[to] then + table.insert(email.inboxes[to], mail) + else + email.inboxes[to] = { mail } + end + + email.save() + + minetest.chat_send_player(to, name .. " sent you mail! Type /inbox to see it.") + + return true, "Message sent to " .. to + else + return false, "Player '" .. to .. "' does not exist" + end +end + +minetest.register_chatcommand("inbox", { + params = "[/clear/text]", + description = "Inbox: Blank to see inbox. Use 'clear' to empty inbox, 'text' for text only.", + func = function(name, param) + if param == "clear" then + email.clear_inbox(name) + + return true, "Inbox cleared" + elseif param == "text" or param == "txt" or param == "t" then + return email.show_inbox(name, true) + else + return email.show_inbox(name, false) + end + end +}) + +minetest.register_chatcommand("mail", { + params = "name msg", + description = "mail: add a message to a player's inbox", + func = function(name, param) + local to, msg = string.match(param, "^([%a%d_-]+) (.+)") + if to and msg then + return email.send_mail(name, to, msg) + else + return false, "Usage: mail " + end + + end +}) + +dofile(minetest.get_modpath("email") .. "/hud.lua") + +email.init() diff --git a/mods/email/textures/email_mail.png b/mods/email/textures/email_mail.png new file mode 100644 index 0000000000000000000000000000000000000000..b1e5553de1bd6ce724fffdde42e8fbeb4a8067c1 GIT binary patch literal 270 zcmeAS@N?(olHy`uVBq!ia0vp^qChOj!3HFa+MYK6DYhhUcNd2LAh=-f^2tCE&H|6f zVg?4j!ywFfJby(BP>{XE)7O>#E*n3uByWTrTOd$qwx^3@h{frvQylqP6?j}EAK!BI zfAH7;C}%`PV__((%+_P?ADJsEvc@fAW>vYoKG{S5i28+#>I`we9fWvGq9f)VHi(^a z{^<@yrZ=rTUYR0m8kUQTGR|jQy4*HG_RgoHt8zMRHJ3&(%kK$hPq@Xt`EHc4o6YN~ zhS7&&gf<_YY8EFpd8t*zyzl%=->tAdFXh1ct#8tW?YUk1fBp({>G$D`DHDzWI+MZE L)z4*}Q$iB}$xB~d literal 0 HcmV?d00001 diff --git a/worlds/minetestforfun/world.mt b/worlds/minetestforfun/world.mt index 348af096..d58a4379 100755 --- a/worlds/minetestforfun/world.mt +++ b/worlds/minetestforfun/world.mt @@ -219,6 +219,7 @@ load_mod_money = true load_mod_beds = true load_mod_builtin_item = true load_mod_chatplus = true +load_mod_email = true load_mod_snowdrift = false load_mod_snow = true