Page 1 of 1

[Mod Feature]

PostPosted: Fri Mar 23, 2012 12:26
by jordan4ibanez
Mods that use commands should parse a file in the /minetest/ base folder...the file could be Mod.txt i guess? there could be a base code for all mod commands that parse the file looking if you are a moderator and then if you are it would just execute the command..but if you aren't then it would tell you "You don't have permission for that"

OR

There could be a mod called "Mod_parse" and it could have a function called "Mod_parse" and other mods with commands could actually use the simple moderator check function to check all the names and match yours up and do the same thing i said above

What do you guys think?

PostPosted: Fri Mar 23, 2012 13:28
by sfan5
+1
I think I'll make a Mod called MesePermissions

PostPosted: Fri Mar 23, 2012 15:28
by randomproof
You would want the file in the world path:
in lua_api.txt:
minetest.get_worldpath(modname) -> eg. "/home/user/.minetest/world"
^ Useful for storing custom data

PostPosted: Fri Mar 23, 2012 15:47
by randomproof
How about something like this. It allows any other mod that depends on it to set and check any privileges they want. So you could use this to allow admins to limit any commands based on privileges they set. Note: it is the other mods job to control what players can set these privileges.

I've added this to my admin_tools mod or you can use this stand alone.

"privileges/init.lua"
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
privileges = {} -- exported functions

local privileges_db_filename = minetest.get_worldpath("privileges") .. "/player_privileges.tbl"

local privileges_defs = table_load(privileges_db_filename)

if type(privileges_defs) ~= "table" then
    privileges_defs = {}
end

table_save( privileges_defs, privileges_db_filename )

function privileges.set_privilege(player_name, priv, value)
    -- sanity check
    if type(player_name) ~= "string" or type(priv) ~= "string" or type(value) ~= "boolean" then
        return false
    end
   
    if privileges_defs[player_name] == nil then
        privileges_defs[player_name] = {}
    end
    privileges_defs[player_name][priv] = value
   
    table_save( privileges_defs, privileges_db_filename )
end

function privileges.get_privileges(player_name, priv)
    -- sanity check
    if type(player_name) ~= "string" or type(priv) ~= "string" then
        return nil
    end
   
    if privileges_defs[player_name] == nil then
        return false
    elseif privileges_defs[player_name][priv] == nil then
        return false
    end
   
    return privileges_defs[player_name][priv]
end



-- Save table functions (functions made local so this file is portable)
-- http://lua-users.org/wiki/SaveTableToFile
--[[
   Save Table to File
   Load Table from File
   v 1.0
   
   Lua 5.2 compatible
   
   Only Saves Tables, Numbers and Strings
   Insides Table References are saved
   Does not save Userdata, Metatables, Functions and indices of these
   ----------------------------------------------------
   table.save( table , filename )
   
   on failure: returns an error msg
   
   ----------------------------------------------------
   table.load( filename or stringtable )
   
   Loads a table that has been saved via the table.save function
   
   on success: returns a previously saved table
   on failure: returns as second argument an error msg
   ----------------------------------------------------
   
   Licensed under the same terms as Lua itself.
]]--

local function exportstring( s )
  s = string.format( "%q",s )
  -- to replace
  s = string.gsub( s,"\\\n","\\n" )
  s = string.gsub( s,"\r","\\r" )
  s = string.gsub( s,string.char(26),""..string.char(26).."" )
  return s
end
--// The Save Function
local function table_save(  tbl,filename )
   local charS,charE = "   ","\n"
   local file,err
   -- create a pseudo file that writes to a string and return the string
   if not filename then
      file =  { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
      charS,charE = "",""
   -- write table to tmpfile
   elseif filename == true or filename == 1 then
      charS,charE,file = "","",io.tmpfile()
   -- write table to file
   -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
   else
      file,err = io.open( filename, "w" )
      if err then return _,err end
   end
   -- initiate variables for save procedure
   local tables,lookup = { tbl },{ [tbl] = 1 }
   file:write( "return {"..charE )
   for idx,t in ipairs( tables ) do
      if filename and filename ~= true and filename ~= 1 then
         file:write( "-- Table: {"..idx.."}"..charE )
      end
      file:write( "{"..charE )
      local thandled = {}
      for i,v in ipairs( t ) do
         thandled[i] = true
         -- escape functions and userdata
         if type( v ) ~= "userdata" then
            -- only handle value
            if type( v ) == "table" then
               if not lookup[v] then
                  table.insert( tables, v )
                  lookup[v] = #tables
               end
               file:write( charS.."{"..lookup[v].."},"..charE )
            elseif type( v ) == "function" then
               file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
            else
               local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
               file:write(  charS..value..","..charE )
            end
         end
      end
      for i,v in pairs( t ) do
         -- escape functions and userdata
         if (not thandled[i]) and type( v ) ~= "userdata" then
            -- handle index
            if type( i ) == "table" then
               if not lookup[i] then
                  table.insert( tables,i )
                  lookup[i] = #tables
               end
               file:write( charS.."[{"..lookup[i].."}]=" )
            else
               local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
               file:write( charS..index.."=" )
            end
            -- handle value
            if type( v ) == "table" then
               if not lookup[v] then
                  table.insert( tables,v )
                  lookup[v] = #tables
               end
               file:write( "{"..lookup[v].."},"..charE )
            elseif type( v ) == "function" then
               file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
            else
               local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
               file:write( value..","..charE )
            end
         end
      end
      file:write( "},"..charE )
   end
   file:write( "}" )
   -- Return Values
   -- return stringtable from string
   if not filename then
      -- set marker for stringtable
      return file.str.."--|"
   -- return stringttable from file
   elseif filename == true or filename == 1 then
      file:seek ( "set" )
      -- no need to close file, it gets closed and removed automatically
      -- set marker for stringtable
      return file:read( "*a" ).."--|"
   -- close file and return 1
   else
      file:close()
      return 1
   end
end

--// The Load Function
local function table_load( sfile )
   -- catch marker for stringtable
   if string.sub( sfile,-3,-1 ) == "--|" then
      tables,err = loadstring( sfile )
   else
      tables,err = loadfile( sfile )
   end
   if err then return _,err
   end
   tables = tables()
   for idx = 1,#tables do
      local tolinkv,tolinki = {},{}
      for i,v in pairs( tables[idx] ) do
         if type( v ) == "table" and tables[v[1]] then
            table.insert( tolinkv,{ i,tables[v[1]] } )
         end
         if type( i ) == "table" and tables[i[1]] then
            table.insert( tolinki,{ i,tables[i[1]] } )
         end
      end
      -- link values, first due to possible changes of indices
      for _,v in ipairs( tolinkv ) do
         tables[idx][v[1]] = v[2]
      end
      -- link indices
      for _,v in ipairs( tolinki ) do
         tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
      end
   end
   return tables[1]
end

PostPosted: Fri Mar 23, 2012 16:28
by sfan5
+1

PostPosted: Fri Mar 23, 2012 21:56
by jordan4ibanez
ooo that's nice
+1