1
# -*- test-case-name: twisted.words.test -*-
2
# Copyright (c) 2001-2005 Twisted Matrix Laboratories.
3
# See LICENSE for details.
5
from zope.interface import Interface, Attribute, implements
7
class IProtocolPlugin(Interface):
8
"""Interface for plugins providing an interface to a Words service
11
name = Attribute("A single word describing what kind of interface this is (eg, irc or web)")
13
def getFactory(realm, portal):
14
"""Retrieve a C{twisted.internet.interfaces.IServerFactory} provider
16
@param realm: An object providing C{twisted.cred.portal.IRealm} and
17
C{IChatService}, with which service information should be looked up.
19
@param portal: An object providing C{twisted.cred.portal.IPortal},
20
through which logins should be performed.
24
class IGroup(Interface):
25
name = Attribute("A short string, unique among groups.")
28
"""Include the given user in this group.
33
def remove(user, reason=None):
34
"""Remove the given user from this group.
37
@type reason: C{unicode}
41
"""Return the number of participants in this group.
43
@rtype: L{twisted.internet.defer.Deferred}
44
@return: A Deferred which fires with an C{int} representing the the
45
number of participants in this group.
48
def receive(sender, recipient, message):
50
Broadcast the given message from the given sender to other
53
The message is not re-transmitted to the sender.
55
@param sender: L{IUser}
57
@type recipient: L{IGroup}
58
@param recipient: This is probably a wart. Maybe it will be removed
59
in the future. For now, it should be the group object the message
60
is being delivered to.
62
@param message: C{dict}
64
@rtype: L{twisted.internet.defer.Deferred}
65
@return: A Deferred which fires with None when delivery has been
66
attempted for all users.
69
def setMetadata(meta):
70
"""Change the metadata associated with this group.
76
"""Return an iterator of all users in this group.
80
class IChatClient(Interface):
81
"""Interface through which IChatService interacts with clients.
84
name = Attribute("A short string, unique among users. This will be set by the L{IChatService} at login time.")
86
def receive(sender, recipient, message):
88
Callback notifying this user of the given message sent by the
91
This will be invoked whenever another user sends a message to a
92
group this user is participating in, or whenever another user sends
93
a message directly to this user. In the former case, C{recipient}
94
will be the group to which the message was sent; in the latter, it
95
will be the same object as the user who is receiving the message.
97
@type sender: L{IUser}
98
@type recipient: L{IUser} or L{IGroup}
99
@type message: C{dict}
101
@rtype: L{twisted.internet.defer.Deferred}
102
@return: A Deferred which fires when the message has been delivered,
103
or which fails in some way. If the Deferred fails and the message
104
was directed at a group, this user will be removed from that group.
107
def groupMetaUpdate(group, meta):
109
Callback notifying this user that the metadata for the given
112
@type group: L{IGroup}
115
@rtype: L{twisted.internet.defer.Deferred}
118
def userJoined(group, user):
120
Callback notifying this user that the given user has joined
123
@type group: L{IGroup}
126
@rtype: L{twisted.internet.defer.Deferred}
129
def userLeft(group, user, reason=None):
131
Callback notifying this user that the given user has left the
132
given group for the given reason.
134
@type group: L{IGroup}
136
@type reason: C{unicode}
138
@rtype: L{twisted.internet.defer.Deferred}
142
class IUser(Interface):
143
"""Interface through which clients interact with IChatService.
146
realm = Attribute("A reference to the Realm to which this user belongs. Set if and only if the user is logged in.")
147
mind = Attribute("A reference to the mind which logged in to this user. Set if and only if the user is logged in.")
148
name = Attribute("A short string, unique among users.")
150
lastMessage = Attribute("A POSIX timestamp indicating the time of the last message received from this user.")
151
signOn = Attribute("A POSIX timestamp indicating this user's most recent sign on time.")
153
def loggedIn(realm, mind):
154
"""Invoked by the associated L{IChatService} when login occurs.
156
@param realm: The L{IChatService} through which login is occurring.
157
@param mind: The mind object used for cred login.
160
def send(recipient, message):
161
"""Send the given message to the given user or group.
163
@type recipient: Either L{IUser} or L{IGroup}
164
@type message: C{dict}
168
"""Attempt to join the given group.
170
@type group: L{IGroup}
171
@rtype: L{twisted.internet.defer.Deferred}
175
"""Discontinue participation in the given group.
177
@type group: L{IGroup}
178
@rtype: L{twisted.internet.defer.Deferred}
183
Return an iterator of all groups of which this user is a
188
class IChatService(Interface):
189
name = Attribute("A short string identifying this chat service (eg, a hostname)")
191
createGroupOnRequest = Attribute(
192
"A boolean indicating whether L{getGroup} should implicitly "
193
"create groups which are requested but which do not yet exist.")
195
createUserOnRequest = Attribute(
196
"A boolean indicating whether L{getUser} should implicitly "
197
"create users which are requested but which do not yet exist.")
200
"""Return all groups available on this service.
202
@rtype: C{twisted.internet.defer.Deferred}
203
@return: A Deferred which fires with a list of C{IGroup} providers.
207
"""Retrieve the group by the given name.
211
@rtype: L{twisted.internet.defer.Deferred}
212
@return: A Deferred which fires with the group with the given
213
name if one exists (or if one is created due to the setting of
214
L{createGroupOnRequest}, or which fails with
215
L{twisted.words.ewords.NoSuchGroup} if no such group exists.
218
def createGroup(name):
219
"""Create a new group with the given name.
223
@rtype: L{twisted.internet.defer.Deferred}
224
@return: A Deferred which fires with the created group, or
225
with fails with L{twisted.words.ewords.DuplicateGroup} if a
226
group by that name exists already.
229
def lookupGroup(name):
230
"""Retrieve a group by name.
232
Unlike C{getGroup}, this will never implicitly create a group.
236
@rtype: L{twisted.internet.defer.Deferred}
237
@return: A Deferred which fires with the group by the given
238
name, or which fails with L{twisted.words.ewords.NoSuchGroup}.
242
"""Retrieve the user by the given name.
246
@rtype: L{twisted.internet.defer.Deferred}
247
@return: A Deferred which fires with the user with the given
248
name if one exists (or if one is created due to the setting of
249
L{createUserOnRequest}, or which fails with
250
L{twisted.words.ewords.NoSuchUser} if no such user exists.
253
def createUser(name):
254
"""Create a new user with the given name.
258
@rtype: L{twisted.internet.defer.Deferred}
259
@return: A Deferred which fires with the created user, or
260
with fails with L{twisted.words.ewords.DuplicateUser} if a
261
user by that name exists already.
265
'IChatInterface', 'IGroup', 'IChatClient', 'IUser', 'IChatService',