]>
code.delx.au - pymsnt/blob - src/contact.py
1 # Copyright 2005 James Bunton <james@delx.cjb.net>
2 # Licensed for distribution under the GPL version 2, check COPYING for details
4 from debug
import LogEvent
, INFO
, WARN
, ERROR
6 from twisted
.internet
import reactor
7 from tlib
.xmlw
import Element
14 """ Represents a Jabber contact """
15 def __init__(self
, jid
, sub
, contactList
):
17 self
.contactList
= contactList
25 self
.ptype
= "unavailable"
28 """ Destroys this object. Does not remove the contact from the server's list. """
29 self
.contactList
= None
32 def syncContactGrantedAuth(self
):
33 """ Since last using the transport the user has been granted authorisation by this contact.
34 Call this to synchronise the user's Jabber list with their legacy list after logon. """
35 if(self
.sub
== "none"):
37 elif(self
.sub
== "from"):
42 def syncContactRemovedAuth(self
):
43 """ Since last using the transport the user has been blocked by this contact.
44 Call this to synchronise the user's Jabber list with their legacy list after logon. """
47 elif(self
.sub
== "both"):
52 def syncUserGrantedAuth(self
):
53 """ Since last using the transport the user has granted authorisation to this contact.
54 Call this to synchronise the user's Jabber list with their legacy list after logon. """
55 if(self
.sub
== "none"):
57 elif(self
.sub
== "to"):
62 def syncUserRemovedAuth(self
):
63 """ Since last using the transport the user has removed this contact's authorisation.
64 Call this to synchronise the user's Jabber list with their legacy list after logon. """
65 if(self
.sub
== "from"):
67 elif(self
.sub
== "both"):
72 def syncGroups(self
, groups
, push
=True):
73 """ Set the groups that this contact is in on the legacy service.
74 By default this pushes the groups out with a presence subscribed packet. """
76 if push
: self
.syncRoster(ptype
="subscribed");
79 ("none", "none") : "", #
80 ("none", "to" ) : "subscribe", # User+ Contact
81 ("none", "from") : "subscribe", # User Contact+
82 ("none", "both") : "subscribe", # User+ Contact+
83 ("to" , "none") : "unsubscribed", # User- Contact
84 ("to" , "to" ) : "", #
85 ("to" , "from") : "unsubscribe", # User- Contact+ **
86 ("to" , "both") : "subscribe", # User Contact+
87 ("from", "none") : "unsubscribe", # User Contact-
88 ("from", "to" ) : "subscribe", # User+ Contact- *
89 ("from", "from") : "", #
90 ("from", "both") : "subscribe", # User+ Contact
91 ("both", "none") : "unsubscribed", # User- Contact- *
92 ("both", "to" ) : "unsubscribe", # User Contact-
93 ("both", "from") : "unsubscribed", # User- Contact
94 ("both", "both") : "" #
97 def syncRoster(self
, ptype
=""):
99 ptype
= self
.syncChoice
.get((self
.origsub
, self
.sub
))
101 self
.contactList
.session
.sendRosterImport(jid
=self
.jid
, ptype
=ptype
, sub
=self
.sub
, groups
=self
.groups
, name
=self
.nickname
)
103 def contactGrantsAuth(self
):
104 """ Live roster event """
105 if(self
.sub
== "none"):
107 elif(self
.sub
== "from"):
109 self
.sendSub("subscribed")
112 def contactRemovesAuth(self
):
113 """ Live roster event """
114 if(self
.sub
== "to"):
116 elif(self
.sub
== "both"):
118 self
.sendSub("unsubscribed")
120 def contactRequestsAuth(self
):
121 """ Live roster event """
122 self
.sendSub("subscribe")
124 def contactDerequestsAuth(self
):
125 """ Live roster event """
126 self
.sendSub("unsubscribe")
128 def jabberSubscriptionReceived(self
, subtype
):
129 """ Updates the subscription state internally and pushes the update to the legacy server """
130 if subtype
== "subscribe":
131 if self
.sub
== "to" or self
.sub
== "both":
132 self
.sendSub("subscribed")
134 self
.contactList
.legacyList
.addContact(self
.jid
)
136 elif subtype
== "subscribed":
137 if self
.sub
== "none":
141 self
.contactList
.legacyList
.authContact(self
.jid
)
143 elif subtype
== "unsubscribe":
144 if self
.sub
== "none" or self
.sub
== "from":
145 self
.sendSub("unsubscribed")
146 if self
.sub
== "both":
150 self
.contactList
.legacyList
.removeContact(self
.jid
)
152 elif(subtype
== "unsubscribed"):
153 if(self
.sub
== "both"):
155 if(self
.sub
== "from"):
157 self
.contactList
.legacyList
.deauthContact(self
.jid
)
159 def updateNickname(self
, nickname
, push
=True):
160 if(self
.nickname
!= nickname
):
161 self
.nickname
= nickname
162 if(push
): self
.sendPresence()
164 def updatePresence(self
, show
, status
, ptype
, force
=False):
165 updateFlag
= (self
.show
!= show
or self
.status
!= status
or self
.ptype
!= ptype
or force
)
172 def updateAvatar(self
, avatar
=None, push
=True):
173 if(self
.avatar
== avatar
): return
175 if(push
): self
.sendPresence()
177 def sendSub(self
, ptype
):
178 self
.contactList
.session
.sendPresence(to
=self
.contactList
.session
.jabberID
, fro
=self
.jid
+ "/" + legacy
.id, ptype
=ptype
)
180 def sendPresence(self
, tojid
=""):
183 avatarHash
= self
.avatar
.getImageHash()
184 caps
= Element((None, "c"))
185 caps
.attributes
["xmlns"] = disco
.CAPS
186 caps
.attributes
["node"] = legacy
.url
+ "/protocol/caps"
187 caps
.attributes
["ver"] = legacy
.version
189 tojid
=self
.contactList
.session
.jabberID
190 self
.contactList
.session
.sendPresence(to
=tojid
, fro
=self
.jid
+ "/" + legacy
.id, ptype
=self
.ptype
, show
=self
.show
, status
=self
.status
, avatarHash
=avatarHash
, nickname
=self
.nickname
, payload
=[caps
])
195 """ Represents the Jabber contact list """
196 def __init__(self
, session
):
197 LogEvent(INFO
, session
.jabberID
)
198 self
.session
= session
202 """ Cleanly removes the object """
203 LogEvent(INFO
, self
.session
.jabberID
)
204 for jid
in self
.contacts
:
205 self
.contacts
[jid
].updatePresence("", "", "unavailable")
206 self
.contacts
[jid
].removeMe()
209 self
.legacyList
= None
211 def resendLists(self
, tojid
=""):
212 for jid
in self
.contacts
:
213 if(self
.contacts
[jid
].status
!= "unavailable"):
214 self
.contacts
[jid
].sendPresence(tojid
)
215 LogEvent(INFO
, self
.session
.jabberID
)
217 def createContact(self
, jid
, sub
):
218 """ Creates a contact object. Use this to initialise the contact list
219 Returns a Contact object which you can call sync* methods on to synchronise
220 the user's legacy contact list with their Jabber list """
221 LogEvent(INFO
, self
.session
.jabberID
)
222 c
= Contact(jid
, sub
, self
)
223 self
.contacts
[jid
] = c
226 def getContact(self
, jid
):
227 """ Finds the contact. If one doesn't exist then a new one is created, with sub set to "none" """
228 if(not self
.contacts
.has_key(jid
)):
229 self
.contacts
[jid
] = Contact(jid
, "none", self
)
230 return self
.contacts
[jid
]
232 def findContact(self
, jid
):
233 if(self
.contacts
.has_key(jid
)):
234 return self
.contacts
[jid
]
237 def jabberSubscriptionReceived(self
, jid
, subtype
):
238 self
.getContact(jid
).jabberSubscriptionReceived(subtype
)