orgs.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. -- "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 ocutil.str_empty (org_name) then
  45. return "Error: Org needs a name"
  46. end
  47. ocutil.log ("Adding org " .. org_name)
  48. if modpol.orgs [org_name] ~= nil then
  49. str = "Error: Org " .. org_name .. " already exists"
  50. 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 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. table.insert(modpol.orgs[org]["members"], member)
  109. local message = member .. " added to org " .. org
  110. modpol.record(message, org)
  111. return message
  112. end
  113. end
  114. -- ===================================================================
  115. -- Function: modpol.is_member
  116. -- Params: org (string), member (string)
  117. -- Output: Boolean depending on membership or nil/error message
  118. modpol.is_member = function(org, member)
  119. if (modpol.orgs[org] == nil) then
  120. return nil, "Error: No such org"
  121. else
  122. local member_table = modpol.orgs[org]["members"]
  123. if member_table then
  124. for index, value in pairs(member_table) do
  125. if value == member then
  126. -- match found
  127. return true
  128. end
  129. end
  130. -- no match found
  131. return false
  132. end
  133. return false
  134. end
  135. end
  136. -- ===================================================================
  137. -- Function: modpol.remove_member
  138. -- Params: org (string), member (string)
  139. -- Output:
  140. -- Removes the specified member from the specified org
  141. -- Returns confirmation or error message
  142. function modpol.remove_member(org, member)
  143. local message = "Error: No such org"
  144. if (modpol.orgs[org] == nil) then
  145. return nil, message
  146. else
  147. local member_table = modpol.orgs[org]["members"]
  148. if member_table then
  149. for index, value in pairs(member_table) do
  150. if value == member then
  151. -- match found, set to nil
  152. value = nil
  153. message = member .. " removed from org " .. org
  154. return message
  155. end
  156. end
  157. end
  158. -- no match found (or org has no members)
  159. message = member .. " not found in org " .. org
  160. return nil, message
  161. end
  162. end
  163. -- ===================================================================
  164. -- TKTK:
  165. -- + rename_org(old_name, new_name)
  166. -- +
  167. -- ===================================================================
  168. -- End of file.