iwords.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # -*- test-case-name: twisted.words.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. from zope.interface import Interface, Attribute
  5. class IProtocolPlugin(Interface):
  6. """Interface for plugins providing an interface to a Words service
  7. """
  8. name = Attribute("A single word describing what kind of interface this is (eg, irc or web)")
  9. def getFactory(realm, portal):
  10. """Retrieve a C{twisted.internet.interfaces.IServerFactory} provider
  11. @param realm: An object providing C{twisted.cred.portal.IRealm} and
  12. L{IChatService}, with which service information should be looked up.
  13. @param portal: An object providing C{twisted.cred.portal.IPortal},
  14. through which logins should be performed.
  15. """
  16. class IGroup(Interface):
  17. name = Attribute("A short string, unique among groups.")
  18. def add(user):
  19. """Include the given user in this group.
  20. @type user: L{IUser}
  21. """
  22. def remove(user, reason=None):
  23. """Remove the given user from this group.
  24. @type user: L{IUser}
  25. @type reason: C{unicode}
  26. """
  27. def size():
  28. """Return the number of participants in this group.
  29. @rtype: L{twisted.internet.defer.Deferred}
  30. @return: A Deferred which fires with an C{int} representing the
  31. number of participants in this group.
  32. """
  33. def receive(sender, recipient, message):
  34. """
  35. Broadcast the given message from the given sender to other
  36. users in group.
  37. The message is not re-transmitted to the sender.
  38. @param sender: L{IUser}
  39. @type recipient: L{IGroup}
  40. @param recipient: This is probably a wart. Maybe it will be removed
  41. in the future. For now, it should be the group object the message
  42. is being delivered to.
  43. @param message: C{dict}
  44. @rtype: L{twisted.internet.defer.Deferred}
  45. @return: A Deferred which fires with None when delivery has been
  46. attempted for all users.
  47. """
  48. def setMetadata(meta):
  49. """Change the metadata associated with this group.
  50. @type meta: C{dict}
  51. """
  52. def iterusers():
  53. """Return an iterator of all users in this group.
  54. """
  55. class IChatClient(Interface):
  56. """Interface through which IChatService interacts with clients.
  57. """
  58. name = Attribute("A short string, unique among users. This will be set by the L{IChatService} at login time.")
  59. def receive(sender, recipient, message):
  60. """
  61. Callback notifying this user of the given message sent by the
  62. given user.
  63. This will be invoked whenever another user sends a message to a
  64. group this user is participating in, or whenever another user sends
  65. a message directly to this user. In the former case, C{recipient}
  66. will be the group to which the message was sent; in the latter, it
  67. will be the same object as the user who is receiving the message.
  68. @type sender: L{IUser}
  69. @type recipient: L{IUser} or L{IGroup}
  70. @type message: C{dict}
  71. @rtype: L{twisted.internet.defer.Deferred}
  72. @return: A Deferred which fires when the message has been delivered,
  73. or which fails in some way. If the Deferred fails and the message
  74. was directed at a group, this user will be removed from that group.
  75. """
  76. def groupMetaUpdate(group, meta):
  77. """
  78. Callback notifying this user that the metadata for the given
  79. group has changed.
  80. @type group: L{IGroup}
  81. @type meta: C{dict}
  82. @rtype: L{twisted.internet.defer.Deferred}
  83. """
  84. def userJoined(group, user):
  85. """
  86. Callback notifying this user that the given user has joined
  87. the given group.
  88. @type group: L{IGroup}
  89. @type user: L{IUser}
  90. @rtype: L{twisted.internet.defer.Deferred}
  91. """
  92. def userLeft(group, user, reason=None):
  93. """
  94. Callback notifying this user that the given user has left the
  95. given group for the given reason.
  96. @type group: L{IGroup}
  97. @type user: L{IUser}
  98. @type reason: C{unicode}
  99. @rtype: L{twisted.internet.defer.Deferred}
  100. """
  101. class IUser(Interface):
  102. """Interface through which clients interact with IChatService.
  103. """
  104. realm = Attribute("A reference to the Realm to which this user belongs. Set if and only if the user is logged in.")
  105. mind = Attribute("A reference to the mind which logged in to this user. Set if and only if the user is logged in.")
  106. name = Attribute("A short string, unique among users.")
  107. lastMessage = Attribute("A POSIX timestamp indicating the time of the last message received from this user.")
  108. signOn = Attribute("A POSIX timestamp indicating this user's most recent sign on time.")
  109. def loggedIn(realm, mind):
  110. """Invoked by the associated L{IChatService} when login occurs.
  111. @param realm: The L{IChatService} through which login is occurring.
  112. @param mind: The mind object used for cred login.
  113. """
  114. def send(recipient, message):
  115. """Send the given message to the given user or group.
  116. @type recipient: Either L{IUser} or L{IGroup}
  117. @type message: C{dict}
  118. """
  119. def join(group):
  120. """Attempt to join the given group.
  121. @type group: L{IGroup}
  122. @rtype: L{twisted.internet.defer.Deferred}
  123. """
  124. def leave(group):
  125. """Discontinue participation in the given group.
  126. @type group: L{IGroup}
  127. @rtype: L{twisted.internet.defer.Deferred}
  128. """
  129. def itergroups():
  130. """
  131. Return an iterator of all groups of which this user is a
  132. member.
  133. """
  134. class IChatService(Interface):
  135. name = Attribute("A short string identifying this chat service (eg, a hostname)")
  136. createGroupOnRequest = Attribute(
  137. "A boolean indicating whether L{getGroup} should implicitly "
  138. "create groups which are requested but which do not yet exist.")
  139. createUserOnRequest = Attribute(
  140. "A boolean indicating whether L{getUser} should implicitly "
  141. "create users which are requested but which do not yet exist.")
  142. def itergroups():
  143. """Return all groups available on this service.
  144. @rtype: C{twisted.internet.defer.Deferred}
  145. @return: A Deferred which fires with a list of C{IGroup} providers.
  146. """
  147. def getGroup(name):
  148. """Retrieve the group by the given name.
  149. @type name: C{str}
  150. @rtype: L{twisted.internet.defer.Deferred}
  151. @return: A Deferred which fires with the group with the given
  152. name if one exists (or if one is created due to the setting of
  153. L{IChatService.createGroupOnRequest}, or which fails with
  154. L{twisted.words.ewords.NoSuchGroup} if no such group exists.
  155. """
  156. def createGroup(name):
  157. """Create a new group with the given name.
  158. @type name: C{str}
  159. @rtype: L{twisted.internet.defer.Deferred}
  160. @return: A Deferred which fires with the created group, or
  161. with fails with L{twisted.words.ewords.DuplicateGroup} if a
  162. group by that name exists already.
  163. """
  164. def lookupGroup(name):
  165. """Retrieve a group by name.
  166. Unlike C{getGroup}, this will never implicitly create a group.
  167. @type name: C{str}
  168. @rtype: L{twisted.internet.defer.Deferred}
  169. @return: A Deferred which fires with the group by the given
  170. name, or which fails with L{twisted.words.ewords.NoSuchGroup}.
  171. """
  172. def getUser(name):
  173. """Retrieve the user by the given name.
  174. @type name: C{str}
  175. @rtype: L{twisted.internet.defer.Deferred}
  176. @return: A Deferred which fires with the user with the given
  177. name if one exists (or if one is created due to the setting of
  178. L{IChatService.createUserOnRequest}, or which fails with
  179. L{twisted.words.ewords.NoSuchUser} if no such user exists.
  180. """
  181. def createUser(name):
  182. """Create a new user with the given name.
  183. @type name: C{str}
  184. @rtype: L{twisted.internet.defer.Deferred}
  185. @return: A Deferred which fires with the created user, or
  186. with fails with L{twisted.words.ewords.DuplicateUser} if a
  187. user by that name exists already.
  188. """
  189. __all__ = [
  190. 'IGroup', 'IChatClient', 'IUser', 'IChatService',
  191. ]