iwords.py 8.4 KB

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