123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- -- ===================================================================
- -- /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)
- -- +
- -- ===================================================================
- -- End of file.
|