orgs.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. -- ===================================================================
  2. -- /orgs.lua
  3. -- Org-related functions for Modular Politics
  4. -- Called by modpol/modpol/api.lua
  5. -- ===================================================================
  6. -- Function: modpol.record
  7. -- Params: strings msg, org
  8. -- Outputs:
  9. -- "msg" specifies an event and/or status message.
  10. -- "org" specifies an "org" name.
  11. -- This function adds the message to a global ledger and, if "org"
  12. -- specifies a valid "org", to an "org"-specific ledger. Both the mem-
  13. -- ory-resident and on-disk copies of the data structures used are up-
  14. -- dated.
  15. modpol.record = function (org_name, msg)
  16. -- Record in global ledger
  17. table.insert (modpol.ledger, msg)
  18. -- Record in "org"-specific ledger
  19. if modpol.orgs [org_name] ~= nil then
  20. local org_ledger = modpol.orgs [org_name]["ledger"]
  21. if org_ledger == nil then
  22. modpol.orgs [org_name]["ledger"] = { msg }
  23. else
  24. modpol.orgs [org_name]["ledger"] =
  25. table.insert (org_ledger, msg)
  26. end
  27. end
  28. modpol.store_data() -- Copy data to disk
  29. end
  30. -- ===================================================================
  31. -- Function: modpol.add_org
  32. -- Params: string name, table members
  33. -- Output:
  34. --
  35. -- This function creates an "org". It returns either an error message
  36. -- starting with "-!-" or a success message.
  37. --
  38. -- The string parameter specifies the "org" name.
  39. --
  40. -- The specified table should be an integer-indexed array of member
  41. -- names.
  42. modpol.add_org = function (org_name, members)
  43. local str
  44. if modpol.ocutil.str_empty (org_name) then
  45. return "Error: Org needs a name"
  46. end
  47. modpol.ocutil.log ("Adding org " .. org_name)
  48. if modpol.orgs [org_name] ~= nil then
  49. str = "Error: Org " .. org_name .. " already exists"
  50. modpol.ocutil.log (str)
  51. return str
  52. end
  53. modpol.orgs [org_name] = { members=members }
  54. local msg = "New org: " .. org_name ..
  55. " (" .. table.concat (members, ", ") .. ")"
  56. modpol.record (org_name, msg)
  57. return msg
  58. end
  59. -- ===================================================================
  60. -- Function: modpol.list_orgs
  61. -- Params: None
  62. -- Output:
  63. -- This function returns a text-format list of "orgs". The list shows
  64. -- one "org" per line in the following format:
  65. -- org_name (member, member, ...)
  66. -- If there are no "orgs", the output is an empty string.
  67. modpol.list_orgs = function()
  68. local outbuf = ""
  69. local str
  70. for org_name, org_data in pairs (modpol.orgs) do
  71. -- Process next "org"
  72. -- Build string version of member list
  73. local memcat = org_data ["members"]
  74. if modpol.ocutil.str_empty (memcat) then
  75. memcat = "(empty)"
  76. else
  77. memcat = "(" .. table.concat (memcat, ", ") .. ")"
  78. end
  79. -- Build output string
  80. outbuf = outbuf .. org_name .. " " .. memcat .. "\n"
  81. end
  82. return outbuf
  83. end
  84. -- ===================================================================
  85. -- Function: modpol.reset_orgs
  86. -- Params: None
  87. -- Removes all orgs and recreates blank org "instance" with all
  88. -- current users
  89. -- returns confirmation message
  90. modpol.reset_orgs = function()
  91. local users = modpol.list_users()
  92. modpol.orgs = {}
  93. local message = "Orgs purged"
  94. modpol.record(nil, message)
  95. modpol.add_org("instance", users)
  96. return message
  97. end
  98. -- ===================================================================
  99. -- Function: modpol.add_member
  100. -- Params: org (string), member (string)
  101. -- Output:
  102. -- Adds the specified member to the specified org
  103. -- Returns confirmation or error message
  104. modpol.add_member = function(org, member)
  105. if (modpol.orgs[org] == nil) then
  106. return "Error: No such org"
  107. else
  108. if modpol.is_member(org,member) then
  109. return "Error: " .. member .. " is already a member of " .. org
  110. else
  111. table.insert(modpol.orgs[org]["members"], member)
  112. local message = member .. " added to org " .. org
  113. modpol.record(message, org)
  114. return message
  115. end
  116. end
  117. end
  118. -- ===================================================================
  119. -- Function: modpol.is_member
  120. -- Params: org (string), member (string)
  121. -- Output: Boolean depending on membership or nil/error message
  122. modpol.is_member = function(org, member)
  123. if (modpol.orgs[org] == nil) then
  124. return nil, "Error: No such org"
  125. else
  126. local member_table = modpol.orgs[org]["members"]
  127. if member_table then
  128. for index, value in pairs(member_table) do
  129. if value == member then
  130. -- match found
  131. return true
  132. end
  133. end
  134. -- no match found
  135. return false
  136. end
  137. return false
  138. end
  139. end
  140. -- ===================================================================
  141. -- Function: modpol.remove_member
  142. -- Params: org (string), member (string)
  143. -- Output:
  144. -- Removes the specified member from the specified org
  145. -- Returns confirmation or error message
  146. function modpol.remove_member(org, member)
  147. local message = "Error: No such org"
  148. if (modpol.orgs[org] == nil) then
  149. return nil, message
  150. else
  151. local member_table = modpol.orgs[org]["members"]
  152. if member_table then
  153. for index, value in pairs(member_table) do
  154. if value == member then
  155. -- match found, set to nil
  156. value = nil
  157. message = member .. " removed from org " .. org
  158. return message
  159. end
  160. end
  161. end
  162. -- no match found (or org has no members)
  163. message = member .. " not found in org " .. org
  164. return nil, message
  165. end
  166. end
  167. -- ===================================================================
  168. -- TKTK:
  169. -- + rename_org(old_name, new_name)
  170. -- +
  171. -- ===================================================================
  172. -- End of file.