2022-03-12 18:09:23 -03:00
|
|
|
local server = nil
|
|
|
|
local data, ip, port
|
|
|
|
local wait_timer = 0
|
|
|
|
|
2022-03-13 14:30:50 -03:00
|
|
|
local queue = {}
|
2022-03-14 09:33:59 -03:00
|
|
|
local nametags = {}
|
2022-03-13 14:30:50 -03:00
|
|
|
local talk_form = "formspec_version[5]"..
|
|
|
|
"size[1.5,1]"..
|
|
|
|
"button[0.125,0.125;1.25,0.75;play;Talk]"
|
|
|
|
|
2022-04-18 11:03:02 -03:00
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
local modpath = minetest.get_modpath(modname)
|
2022-03-12 18:09:23 -03:00
|
|
|
|
2022-09-07 15:14:09 -03:00
|
|
|
local fopen, fremove
|
|
|
|
|
2022-03-12 18:09:23 -03:00
|
|
|
local function sanitizeNick(data_received)
|
|
|
|
return string.gsub(data_received, "%c", ""):sub( 1, 20 )
|
|
|
|
end
|
|
|
|
|
|
|
|
local function make_unique_id(player_name)
|
|
|
|
local id=player_name .. "_"
|
|
|
|
for i=0,5 do
|
|
|
|
id=id..(math.random(0,9))
|
|
|
|
end
|
|
|
|
return id
|
|
|
|
end
|
|
|
|
|
|
|
|
-- DS-minetest wrapper for require
|
|
|
|
-- the same as `IE.require(...)`, but sets the env to IE
|
|
|
|
local function require_with_IE_env(...)
|
|
|
|
local old_thread_env = insecure_environment.getfenv(0)
|
|
|
|
|
|
|
|
-- set env of thread
|
|
|
|
-- (the loader used by IE.require will probably use the thread env for
|
|
|
|
-- the loaded functions)
|
|
|
|
insecure_environment.setfenv(0, insecure_environment)
|
|
|
|
|
|
|
|
-- (IE.require's env is neither _G, nor IE. we need to leave it like this,
|
|
|
|
-- otherwise it won't find the loaders (it uses the global `loaders`, not
|
|
|
|
-- `package.loaders` btw. (see luajit/src/lib_package.c)))
|
|
|
|
|
|
|
|
-- we might be pcall()ed, so we need to pcall to make sure that we reset
|
|
|
|
-- the thread env afterwards
|
|
|
|
local ok, ret = insecure_environment.pcall(insecure_environment.require, ...)
|
|
|
|
|
|
|
|
-- reset env of thread
|
|
|
|
insecure_environment.setfenv(0, old_thread_env)
|
|
|
|
|
|
|
|
if not ok then
|
|
|
|
insecure_environment.error(ret)
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
-- require the socket and gets udp
|
|
|
|
if minetest.request_insecure_environment then
|
|
|
|
insecure_environment = minetest.request_insecure_environment()
|
|
|
|
if insecure_environment then
|
2022-09-07 15:14:09 -03:00
|
|
|
fopen = insecure_environment.io.open
|
|
|
|
fremove = insecure_environment.os.remove
|
2022-03-12 18:09:23 -03:00
|
|
|
local old_path = insecure_environment.package.path
|
|
|
|
local old_cpath = insecure_environment.package.cpath
|
|
|
|
|
|
|
|
insecure_environment.package.path = insecure_environment.package.path.. ";" .. "external/?.lua"
|
|
|
|
insecure_environment.package.cpath = insecure_environment.package.cpath.. ";" .. "external/?.so"
|
|
|
|
--overriding require to insecure require to allow modules to load dependencies
|
2022-03-12 19:16:34 -03:00
|
|
|
--local old_require = require
|
2022-03-12 18:54:59 -03:00
|
|
|
--require = insecure_environment.require
|
2022-03-12 18:09:23 -03:00
|
|
|
|
|
|
|
-- load namespace
|
|
|
|
local socket = require_with_IE_env("socket")
|
|
|
|
--reset changes
|
2022-03-12 18:54:59 -03:00
|
|
|
--require = old_require
|
2022-03-12 18:09:23 -03:00
|
|
|
insecure_environment.package.path = old_path
|
|
|
|
insecure_environment.package.cpath = old_cpath
|
|
|
|
|
|
|
|
-- create a TCP socket and bind it to the local host, at any port
|
|
|
|
server = insecure_environment.assert(socket.bind("*", 41000))
|
|
|
|
server:settimeout(0)
|
|
|
|
-- find out which port the OS chose for us
|
|
|
|
ip, port = server:getsockname()
|
2022-03-13 09:18:21 -03:00
|
|
|
--my_io = insecure_environment.io
|
2022-03-12 18:09:23 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- loop forever waiting for clients
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
if server then
|
|
|
|
--minetest.chat_send_all(dump("we have server"))
|
|
|
|
wait_timer = wait_timer + dtime
|
|
|
|
if wait_timer > 0.5 then wait_timer = 0.5 end
|
|
|
|
|
|
|
|
if wait_timer >= 0.5 then
|
|
|
|
wait_timer = 0
|
|
|
|
local uid = nil
|
|
|
|
|
|
|
|
-- wait for a connection from any client
|
|
|
|
local client = server:accept()
|
|
|
|
if client then
|
2022-03-14 09:33:59 -03:00
|
|
|
local peer = client:getpeername()
|
|
|
|
--minetest.chat_send_all(dump(peer));
|
2022-03-12 18:09:23 -03:00
|
|
|
-- make sure we don't block waiting for this client's line
|
|
|
|
--client:settimeout(10)
|
|
|
|
-- receive the line
|
|
|
|
local line, err = client:receive('*l')
|
|
|
|
-- if there was no error, send it back to the client
|
|
|
|
if not err then
|
|
|
|
--minetest.chat_send_all("recebeu: " .. line)
|
|
|
|
local nick = sanitizeNick(line)
|
|
|
|
local player = minetest.get_player_by_name(nick)
|
|
|
|
if player then
|
2022-03-18 09:06:55 -03:00
|
|
|
--minetest.chat_send_all("peer: " .. peer .. " - player ip: " .. minetest.get_player_ip(nick) )
|
2022-03-14 10:03:51 -03:00
|
|
|
if peer == minetest.get_player_ip(nick) and minetest.check_player_privs(player, {can_talk=true}) then
|
2022-03-14 09:33:59 -03:00
|
|
|
nametags[nick] = player:get_nametag_attributes()
|
|
|
|
--sets the nametag of the player to red
|
|
|
|
player:set_nametag_attributes({bgcolor = "red"})
|
|
|
|
--and sets to normal color again after 10 seconds
|
2022-03-14 09:45:32 -03:00
|
|
|
--[[minetest.after(10, function()
|
2022-03-14 09:33:59 -03:00
|
|
|
player:set_nametag_attributes(nametags[nick])
|
2022-03-14 09:45:32 -03:00
|
|
|
end)]]--
|
2022-03-14 09:33:59 -03:00
|
|
|
|
|
|
|
|
|
|
|
_G.pcall(client:send("file" .. "\n"))
|
|
|
|
--receive file
|
|
|
|
local uid = make_unique_id(nick)
|
|
|
|
local filename = "ptt_" .. uid .. ".ogg"
|
|
|
|
--local file_path = modpath .. DIR_DELIM .. "sounds" .. DIR_DELIM .. filename
|
|
|
|
local file_path = minetest.get_worldpath() .. DIR_DELIM .. filename
|
2022-03-19 09:30:41 -03:00
|
|
|
|
|
|
|
|
|
|
|
local data = ""
|
|
|
|
local rec_lenght = 4096
|
|
|
|
local count = 0
|
|
|
|
while true do
|
|
|
|
count = count + 1
|
|
|
|
local recv, e, part = client:receive(rec_lenght)
|
|
|
|
if recv ~= nil then
|
|
|
|
data = data..recv
|
|
|
|
if count > 25 then break end --lets limmit the size to 100k
|
|
|
|
else
|
|
|
|
data = data..part
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--local data, e, part = client:receive('*a')
|
2022-03-14 09:33:59 -03:00
|
|
|
client:close()
|
2022-09-07 15:14:09 -03:00
|
|
|
local file = fopen(file_path, "wb")
|
2022-03-14 09:33:59 -03:00
|
|
|
--minetest.chat_send_all(dump(file))
|
|
|
|
if file then
|
|
|
|
file:write(data)
|
|
|
|
file:close()
|
|
|
|
|
|
|
|
if minetest.dynamic_add_media then
|
|
|
|
local media_options = {filepath = file_path, ephemeral = true}
|
|
|
|
local media_to_play = filename:gsub("%.ogg", "")
|
|
|
|
minetest.dynamic_add_media(media_options, function(name)
|
|
|
|
--minetest.chat_send_all(media_to_play)
|
|
|
|
queue[nick] = media_to_play
|
|
|
|
minetest.show_formspec(nick, "ptt_talk:talk", talk_form)
|
2022-09-07 15:14:09 -03:00
|
|
|
fremove (file_path)
|
2022-03-14 09:33:59 -03:00
|
|
|
end)
|
|
|
|
end
|
2022-03-12 19:25:26 -03:00
|
|
|
end
|
2022-03-14 09:33:59 -03:00
|
|
|
else
|
2022-03-14 10:03:51 -03:00
|
|
|
_G.pcall(client:send("Error! You aren't allowed to talk." .. "\n"))
|
2022-03-12 19:25:26 -03:00
|
|
|
end
|
2022-03-12 18:09:23 -03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
--minetest.chat_send_all("deu erro: " .. err)
|
|
|
|
end
|
|
|
|
-- done with client, close the object
|
|
|
|
client:close()
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2022-03-13 14:30:50 -03:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
local form_name = "ptt_talk:talk"
|
|
|
|
local name = player:get_player_name()
|
2022-03-14 10:46:44 -03:00
|
|
|
if nametags[name] then
|
|
|
|
player:set_nametag_attributes(nametags[name])
|
|
|
|
end
|
2022-03-13 14:30:50 -03:00
|
|
|
if formname == form_name then
|
|
|
|
if fields.play then
|
2022-03-15 16:11:59 -03:00
|
|
|
minetest.log("action", name .. " used voice chat")
|
2022-03-13 14:30:50 -03:00
|
|
|
minetest.sound_play(queue[name], {
|
|
|
|
--to_player = nick,
|
|
|
|
object = player,
|
|
|
|
max_hear_distance = 30,
|
|
|
|
gain = 1.0,
|
|
|
|
fade = 0.0,
|
|
|
|
pitch = 1.0,
|
|
|
|
--exclude_player = nick,
|
|
|
|
}, true)
|
|
|
|
end
|
|
|
|
minetest.close_formspec(name, form_name)
|
|
|
|
end
|
|
|
|
end)
|
2022-03-14 10:03:51 -03:00
|
|
|
|
|
|
|
minetest.register_privilege("can_talk", {
|
|
|
|
description = "Gives the player the permission to use ptt_talk",
|
|
|
|
give_to_singleplayer = true
|
|
|
|
})
|