orgs.lua 5.8 KB

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