Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement non-player recipients #131

Merged
merged 8 commits into from
Feb 1, 2024
Merged

Conversation

y5nw
Copy link
Contributor

@y5nw y5nw commented Jan 5, 2024

This PR allows sending mail to non-player recipients.

There are some potential usecases for this PR:

  • Interaction with automated systems (e.g. remote control)
  • Mailing lists; in particular, it could allow (for example) sending messages to all members of a beerchat channel, which can be useful for (for public channels) announcements or (for private channels) internal communication without having to keep track of the members.

Checklist:

  • Implement recipient handlers
  • Properly handle sending mail to the sender:
    • The sender should not receive mail addressed to a list if the sender is not otherwise listed as a recipient.
    • The sender may send mail to an alias (and should also receive it)
  • Add a callback for a player receiving mail (as opposed to a mail being sent)
  • Add tests

(I hope I did not miss anything important.)

Advtrains

For people into Advtrains, I put together a simple patch for Advtrains as a proof-of-concept for what can be done with this PR. The patch below allows

  • sending ATC commands to a train (note the protection code - the mail is rejected if the sender does not have permission to control the train)
  • sending mail to subscribers of a LuaATC environment, which can be useful for communicating certain changes to the code.
diff --git a/advtrains/init.lua b/advtrains/init.lua
index a7e5764..d1a605c 100644
--- a/advtrains/init.lua
+++ b/advtrains/init.lua
@@ -222,6 +222,7 @@ dofile(advtrains.modpath.."/craft_items.lua")
 
 dofile(advtrains.modpath.."/log.lua")
 dofile(advtrains.modpath.."/passive.lua")
+dofile(advtrains.modpath.."/mail.lua")
 if mesecon then
 	dofile(advtrains.modpath.."/p_mesecon_iface.lua")
 end
diff --git a/advtrains/mail.lua b/advtrains/mail.lua
new file mode 100644
index 0000000..6c0c48b
--- /dev/null
+++ b/advtrains/mail.lua
@@ -0,0 +1,19 @@
+if mail and mail.register_recipient_handler then
+	mail.register_recipient_handler(function(sender, recipient)
+		local tid = string.match(recipient, "^advtrains/train/(.+)$")
+		if tid and advtrains.trains[tid] then
+			local train = advtrains.trains[tid]
+			for _, wid in ipairs(train.trainparts) do
+				local wagon = advtrains.wagons[wid]
+				if not advtrains.check_driving_couple_protection(sender, wagon.owner, wagon.whitelist) then
+					return false, attrans("You do not have permission to operate the train @1", tid)
+				end
+			end
+			return true, function(msg)
+				if msg.subject == "ATC" then
+					advtrains.atc.train_set_command(train, msg.body)
+				end
+			end
+		end
+	end)
+end
diff --git a/advtrains/mod.conf b/advtrains/mod.conf
index 5808d1a..34c5fad 100644
--- a/advtrains/mod.conf
+++ b/advtrains/mod.conf
@@ -4,4 +4,4 @@ description=Core system for realistic trains in Minetest
 author=orwell96
 
 depends=serialize_lib
-optional_depends=mesecons,mesecons_switch,digtron
+optional_depends=mesecons,mesecons_switch,digtron,mail
diff --git a/advtrains_luaautomation/environment.lua b/advtrains_luaautomation/environment.lua
index d85bedc..c1685df 100644
--- a/advtrains_luaautomation/environment.lua
+++ b/advtrains_luaautomation/environment.lua
@@ -376,6 +376,12 @@ function atlatc.run_initcode()
 	end
 end
 
-
-
-
+--- mail interface
+if mail and mail.register_recipient_handler then
+	mail.register_recipient_handler(function(_, pname)
+		local envname = string.match(pname, "atlatc/subscribers/(.+)")
+		if envname and atlatc.envs[envname] then
+			return true, atlatc.envs[envname].subscribers
+		end
+	end)
+end
diff --git a/advtrains_luaautomation/mod.conf b/advtrains_luaautomation/mod.conf
index a737603..2fc210d 100644
--- a/advtrains_luaautomation/mod.conf
+++ b/advtrains_luaautomation/mod.conf
@@ -4,4 +4,4 @@ description=Lua control interface to Advanced Trains
 author=orwell96
 
 depends=advtrains
-optional_depends=advtrains_interlocking,advtrains_line_automation,mesecons_switch
+optional_depends=advtrains_interlocking,advtrains_line_automation,mesecons_switch,mail

@Athozus
Copy link
Member

Athozus commented Jan 6, 2024

This PR allows sending mail to non-player recipients.

There are some potential usecases for this PR:

* Interaction with automated systems (e.g. remote control)

* Mailing lists; in particular, it could allow (for example) sending messages to all members of a beerchat channel, which can be useful for (for public channels) announcements or (for private channels) internal communication without having to keep track of the members.

Advtrains

Hi,

First, thanks for this PR and giving interest to mail. However, I have some problems with that PR. The concept itself is good, but some are already active, and some are planning for the upcoming version (release blockers PR have to be merged first) :

Waiting for my "colleagues", but it has at my first impression to be reworked a bit.

@BuckarooBanzay
Copy link
Member

Waiting for my "colleagues", but it has at my first impression to be reworked a bit.

base concept sounds good to me (haven't tested the code though)

i like the idea of the recipient handler, this might be useful for some other automated mods/things 😏

release blockers PR have to be merged first

feel free to ping me up if you need help (might take a few days but i'll try) the other PR's have all approvals on it btw 😉

@BuckarooBanzay BuckarooBanzay added the Enhancement New feature or request label Jan 12, 2024
@y5nw y5nw marked this pull request as ready for review January 21, 2024 18:03
Copy link
Member

@Athozus Athozus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show your changes since the last comment, and it actually looks good to me. I'll test it in few days, but it seems to be good. It offers new perspective for mail mod.

@Athozus
Copy link
Member

Athozus commented Jan 29, 2024

I'll wait for #128 merged, to have more likely merging conflicts, if you agree. Anyway will be rebased soon for the next release.

@Athozus Athozus added this to the 1.4.0 milestone Feb 1, 2024
@Athozus
Copy link
Member

Athozus commented Feb 1, 2024

All merges are now done, I'm gonna test a last time tonight.

Would you like I squash or I rebase (or I squash only some commits) ?

@Athozus Athozus merged commit 570cf78 into mt-mods:master Feb 1, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants