commit 7d0aa3f5ea38ebb12797de7737c98a82f8432288 Author: rubenwardy Date: Fri Dec 18 18:39:26 2015 +0000 Initial Commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..1ebeaaa --- /dev/null +++ b/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/depends.txt b/depends.txt new file mode 100644 index 0000000..aa2dd22 --- /dev/null +++ b/depends.txt @@ -0,0 +1 @@ +chatplus? diff --git a/hud.lua b/hud.lua new file mode 100644 index 0000000..f974e5f --- /dev/null +++ b/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/hudkit.lua b/hudkit.lua new file mode 100644 index 0000000..d763557 --- /dev/null +++ b/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/init.lua b/init.lua new file mode 100644 index 0000000..90e6ac4 --- /dev/null +++ b/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/textures/email_mail.png b/textures/email_mail.png new file mode 100644 index 0000000..b1e5553 Binary files /dev/null and b/textures/email_mail.png differ