AddressbookModel.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from modules.Sogo import Sogo
  2. from models.BaseModel import BaseModel
  3. class AddressbookModel(BaseModel):
  4. parser_command = "addressbook"
  5. required_args = {
  6. "add": [["username", "name"]],
  7. "delete": [["username", "name"]],
  8. "get": [["username", "name"]],
  9. "set_acl": [["username", "name", "sharee_email", "acl"]],
  10. "get_acl": [["username", "name"]],
  11. "delete_acl": [["username", "name", "sharee_email"]],
  12. "add_contact": [["username", "name", "contact_name", "contact_email", "type"]],
  13. "delete_contact": [["username", "name", "contact_name"]],
  14. }
  15. def __init__(
  16. self,
  17. username=None,
  18. name=None,
  19. sharee_email=None,
  20. acl=None,
  21. subscribe=None,
  22. ics=None,
  23. contact_name=None,
  24. contact_email=None,
  25. type=None,
  26. **kwargs
  27. ):
  28. self.sogo = Sogo(username)
  29. self.name = name
  30. self.acl = acl
  31. self.sharee_email = sharee_email
  32. self.subscribe = subscribe
  33. self.ics = ics
  34. self.contact_name = contact_name
  35. self.contact_email = contact_email
  36. self.type = type
  37. def add(self):
  38. """
  39. Add a new addressbook.
  40. :return: Response from SOGo API.
  41. """
  42. return self.sogo.addAddressbook(self.name)
  43. def set_acl(self):
  44. """
  45. Set ACL for the addressbook.
  46. :return: Response from SOGo API.
  47. """
  48. addressbook_id = self.sogo.getAddressbookIdByName(self.name)
  49. if not addressbook_id:
  50. print(f"Addressbook '{self.name}' not found for user '{self.username}'.")
  51. return None
  52. return self.sogo.setAddressbookACL(addressbook_id, self.sharee_email, self.acl, self.subscribe)
  53. def delete_acl(self):
  54. """
  55. Delete the addressbook ACL.
  56. :return: Response from SOGo API.
  57. """
  58. addressbook_id = self.sogo.getAddressbookIdByName(self.name)
  59. if not addressbook_id:
  60. print(f"Addressbook '{self.name}' not found for user '{self.username}'.")
  61. return None
  62. return self.sogo.deleteAddressbookACL(addressbook_id, self.sharee_email)
  63. def get_acl(self):
  64. """
  65. Get the ACL for the addressbook.
  66. :return: Response from SOGo API.
  67. """
  68. addressbook_id = self.sogo.getAddressbookIdByName(self.name)
  69. if not addressbook_id:
  70. print(f"Addressbook '{self.name}' not found for user '{self.username}'.")
  71. return None
  72. return self.sogo.getAddressbookACL(addressbook_id)
  73. def add_contact(self):
  74. """
  75. Add a new contact to the addressbook.
  76. :return: Response from SOGo API.
  77. """
  78. addressbook_id = self.sogo.getAddressbookIdByName(self.name)
  79. if not addressbook_id:
  80. print(f"Addressbook '{self.name}' not found for user '{self.username}'.")
  81. return None
  82. if self.type == "card":
  83. return self.sogo.addAddressbookContact(addressbook_id, self.contact_name, self.contact_email)
  84. elif self.type == "list":
  85. return self.sogo.addAddressbookContactList(addressbook_id, self.contact_name, self.contact_email)
  86. def delete_contact(self):
  87. """
  88. Delete a contact or contactlist from the addressbook.
  89. :return: Response from SOGo API.
  90. """
  91. addressbook_id = self.sogo.getAddressbookIdByName(self.name)
  92. if not addressbook_id:
  93. print(f"Addressbook '{self.name}' not found for user '{self.username}'.")
  94. return None
  95. return self.sogo.deleteAddressbookItem(addressbook_id, self.contact_name)
  96. def get(self):
  97. """
  98. Retrieve addressbooks list.
  99. :return: Response from SOGo API.
  100. """
  101. return self.sogo.getAddressbookList()
  102. def delete(self):
  103. """
  104. Delete the addressbook.
  105. :return: Response from SOGo API.
  106. """
  107. addressbook_id = self.sogo.getAddressbookIdByName(self.name)
  108. if not addressbook_id:
  109. print(f"Addressbook '{self.name}' not found for user '{self.username}'.")
  110. return None
  111. return self.sogo.deleteAddressbook(addressbook_id)
  112. @classmethod
  113. def add_parser(cls, subparsers):
  114. parser = subparsers.add_parser(
  115. cls.parser_command,
  116. help="Manage addressbooks (add, delete, get, set_acl, get_acl, delete_acl, add_contact, delete_contact)"
  117. )
  118. parser.add_argument("object", choices=list(cls.required_args.keys()), help="Action to perform: add, delete, get, set_acl, get_acl, delete_acl, add_contact, delete_contact")
  119. parser.add_argument("--username", required=True, help="Username of the addressbook owner (e.g. user@example.com)")
  120. parser.add_argument("--name", help="Addressbook name")
  121. parser.add_argument("--sharee-email", help="Email address to share the addressbook with")
  122. parser.add_argument("--acl", help="ACL rights for the sharee (e.g. r, w, rw)")
  123. parser.add_argument("--subscribe", action='store_true', help="Subscribe the sharee to the addressbook")
  124. parser.add_argument("--contact-name", help="Name of the contact or contactlist to add or delete")
  125. parser.add_argument("--contact-email", help="Email address of the contact to add")
  126. parser.add_argument("--type", choices=["card", "list"], help="Type of contact to add: card (single contact) or list (distribution list)")