-- =================================================================== -- /orgs.lua -- Org-related functions for Modular Politics -- Called by modpol/modpol/api.lua -- =================================================================== -- Function: modpol.record -- Params: strings msg, org -- Outputs: -- "msg" specifies an event and/or status message. -- "org" specifies an "org" name. -- This function adds the message to a global ledger and, if "org" -- specifies a valid "org", to an "org"-specific ledger. Both the mem- -- ory-resident and on-disk copies of the data structures used are up- -- dated. modpol.record = function (org_name, msg) -- Record in global ledger table.insert (modpol.ledger, msg) -- Record in "org"-specific ledger if modpol.orgs [org_name] ~= nil then local org_ledger = modpol.orgs [org_name]["ledger"] if org_ledger == nil then modpol.orgs [org_name]["ledger"] = { msg } else modpol.orgs [org_name]["ledger"] = table.insert (org_ledger, msg) end end modpol.store_data() -- Copy data to disk end -- =================================================================== -- Function: modpol.add_org -- Params: string name, table members -- Output: -- -- This function creates an "org". It returns either an error message -- starting with "-!-" or a success message. -- -- The string parameter specifies the "org" name. -- -- The specified table should be an integer-indexed array of member -- names. modpol.add_org = function (org_name, members) local str if modpol.ocutil.str_empty (org_name) then return "Error: Org needs a name" end modpol.ocutil.log ("Adding org " .. org_name) if modpol.orgs [org_name] ~= nil then str = "Error: Org " .. org_name .. " already exists" modpol.ocutil.log (str) return str end modpol.orgs [org_name] = { members=members } local msg = "New org: " .. org_name .. " (" .. table.concat (members, ", ") .. ")" modpol.record (org_name, msg) return msg end -- =================================================================== -- Function: modpol.list_orgs -- Params: None -- Output: -- This function returns a text-format list of "orgs". The list shows -- one "org" per line in the following format: -- org_name (member, member, ...) -- If there are no "orgs", the output is an empty string. modpol.list_orgs = function() local outbuf = "" local str for org_name, org_data in pairs (modpol.orgs) do -- Process next "org" -- Build string version of member list local memcat = org_data ["members"] if modpol.ocutil.str_empty (memcat) then memcat = "(empty)" else memcat = "(" .. table.concat (memcat, ", ") .. ")" end -- Build output string outbuf = outbuf .. org_name .. " " .. memcat .. "\n" end return outbuf end -- =================================================================== -- Function: modpol.reset_orgs -- Params: None -- Removes all orgs and recreates blank org "instance" with all -- current users -- returns confirmation message modpol.reset_orgs = function() local users = modpol.list_users() modpol.orgs = {} local message = "Orgs purged" modpol.record(nil, message) modpol.add_org("instance", users) return message end -- =================================================================== -- Function: modpol.add_member -- Params: org (string), member (string) -- Output: -- Adds the specified member to the specified org -- Returns confirmation or error message modpol.add_member = function(org, member) if (modpol.orgs[org] == nil) then return "Error: No such org" else if modpol.is_member(org,member) then return "Error: " .. member .. " is already a member of " .. org else table.insert(modpol.orgs[org]["members"], member) local message = member .. " added to org " .. org modpol.record(message, org) return message end end end -- =================================================================== -- Function: modpol.is_member -- Params: org (string), member (string) -- Output: Boolean depending on membership or nil/error message modpol.is_member = function(org, member) if (modpol.orgs[org] == nil) then return nil, "Error: No such org" else local member_table = modpol.orgs[org]["members"] if member_table then for index, value in pairs(member_table) do if value == member then -- match found return true end end -- no match found return false end return false end end -- =================================================================== -- Function: modpol.remove_member -- Params: org (string), member (string) -- Output: -- Removes the specified member from the specified org -- Returns confirmation or error message function modpol.remove_member(org, member) local message = "Error: No such org" if (modpol.orgs[org] == nil) then return nil, message else local member_table = modpol.orgs[org]["members"] if member_table then for index, value in pairs(member_table) do if value == member then -- match found, set to nil value = nil message = member .. " removed from org " .. org return message end end end -- no match found (or org has no members) message = member .. " not found in org " .. org return nil, message end end -- =================================================================== -- TKTK -- rename_org(old_name, new_name) -- Renames an org -- =================================================================== -- POLICIES -- Each policy is a table with a string for a key. Contents: -- KEY - name of the policy (string) -- [member orgs]: external orgs allowed to call policy -- [function]: the function that initiates the policy -- =================================================================== -- =================================================================== -- modpol.add_policy -- Adds a policy to an org's [org].policies table -- Params: org_name (string), -- policy_name (string), -- policy_table (table) -- Output: true if successful, nil if error -- TKTK NEEDS TO BE REWRITTEN modpol.add_policy = function(org_name, policy_name, policy_table) -- first, basic checks local message = "Error: No such org" if (modpol.orgs[org_name] == nil) then return nil, message elseif (modpol[target_function] == nil) then message = "Error: No such target function" return nil, message elseif (modpol[policy_function]) then message = "Error: No such policy function" return nil, message else -- okay, proceed modpol.orgs[org_name].policies[target_function] = policy_function message = "In org " .. org_name .. ", policy for " .. target_function .. " set to " .. policy_function record(org_name, message) return true, message end end -- =================================================================== -- End of file.