]>
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
5 from twisted
.internet
import reactor
6 from tlib
.xmlw
import Element
7 from debug
import LogEvent
, INFO
, WARN
, ERROR
17 """ Represents a Jabber contact """
18 def __init__(self
, jid
, sub
, contactList
):
20 self
.contactList
= contactList
28 self
.ptype
= "unavailable"
31 """ Destroys this object. Does not remove the contact from the server's list. """
32 self
.contactList
= None
35 def syncContactGrantedAuth(self
):
36 """ Since last using the transport the user has been granted authorisation by this contact.
37 Call this to synchronise the user's Jabber list with their legacy list after logon. """
38 if(self
.sub
== "none"):
40 elif(self
.sub
== "from"):
45 def syncContactRemovedAuth(self
):
46 """ Since last using the transport the user has been blocked by this contact.
47 Call this to synchronise the user's Jabber list with their legacy list after logon. """
50 elif(self
.sub
== "both"):
55 def syncUserGrantedAuth(self
):
56 """ Since last using the transport the user has granted authorisation to this contact.
57 Call this to synchronise the user's Jabber list with their legacy list after logon. """
58 if(self
.sub
== "none"):
60 elif(self
.sub
== "to"):
65 def syncUserRemovedAuth(self
):
66 """ Since last using the transport the user has removed this contact's authorisation.
67 Call this to synchronise the user's Jabber list with their legacy list after logon. """
68 if(self
.sub
== "from"):
70 elif(self
.sub
== "both"):
75 def syncGroups(self
, groups
, push
=True):
76 """ Set the groups that this contact is in on the legacy service.
77 By default this pushes the groups out with a presence subscribed packet. """
79 if push
: self
.syncRoster(ptype
="subscribed");
82 ("none", "none") : "", #
83 ("none", "to" ) : "subscribe", # User+ Contact
84 ("none", "from") : "subscribe", # User Contact+
85 ("none", "both") : "subscribe", # User+ Contact+
86 ("to" , "none") : "unsubscribed", # User- Contact
87 ("to" , "to" ) : "", #
88 ("to" , "from") : "unsubscribe", # User- Contact+ **
89 ("to" , "both") : "subscribe", # User Contact+
90 ("from", "none") : "unsubscribe", # User Contact-
91 ("from", "to" ) : "subscribe", # User+ Contact- *
92 ("from", "from") : "", #
93 ("from", "both") : "subscribe", # User+ Contact
94 ("both", "none") : "unsubscribed", # User- Contact- *
95 ("both", "to" ) : "unsubscribe", # User Contact-
96 ("both", "from") : "unsubscribed", # User- Contact
97 ("both", "both") : "" #
100 def syncRoster(self
, ptype
=""):
102 ptype
= self
.syncChoice
.get((self
.origsub
, self
.sub
))
104 self
.contactList
.session
.sendRosterImport(jid
=self
.jid
, ptype
=ptype
, sub
=self
.sub
, groups
=self
.groups
, name
=self
.nickname
)
106 def contactGrantsAuth(self
):
107 """ Live roster event """
108 if(self
.sub
== "none"):
110 elif(self
.sub
== "from"):
112 self
.sendSub("subscribed")
115 def contactRemovesAuth(self
):
116 """ Live roster event """
117 if(self
.sub
== "to"):
119 elif(self
.sub
== "both"):
121 self
.sendSub("unsubscribed")
123 def contactRequestsAuth(self
):
124 """ Live roster event """
125 self
.sendSub("subscribe")
127 def contactDerequestsAuth(self
):
128 """ Live roster event """
129 self
.sendSub("unsubscribe")
131 def jabberSubscriptionReceived(self
, subtype
):
132 """ Updates the subscription state internally and pushes the update to the legacy server """
133 if subtype
== "subscribe":
134 if self
.sub
== "to" or self
.sub
== "both":
135 self
.sendSub("subscribed")
137 self
.contactList
.legacyList
.addContact(self
.jid
)
139 elif subtype
== "subscribed":
140 if self
.sub
== "none":
144 self
.contactList
.legacyList
.authContact(self
.jid
)
146 elif subtype
== "unsubscribe":
147 if self
.sub
== "none" or self
.sub
== "from":
148 self
.sendSub("unsubscribed")
149 if self
.sub
== "both":
153 self
.contactList
.legacyList
.removeContact(self
.jid
)
155 elif(subtype
== "unsubscribed"):
156 if(self
.sub
== "both"):
158 if(self
.sub
== "from"):
160 self
.contactList
.legacyList
.deauthContact(self
.jid
)
162 def updateNickname(self
, nickname
, push
=True):
163 if(self
.nickname
!= nickname
):
164 self
.nickname
= nickname
165 if(push
): self
.sendPresence()
167 def updatePresence(self
, show
, status
, ptype
, force
=False):
168 updateFlag
= (self
.show
!= show
or self
.status
!= status
or self
.ptype
!= ptype
or force
)
175 def updateAvatar(self
, avatar
=None, push
=True):
176 if(self
.avatar
== avatar
): return
178 if(push
): self
.sendPresence()
180 def sendSub(self
, ptype
):
181 self
.contactList
.session
.sendPresence(to
=self
.contactList
.session
.jabberID
, fro
=self
.jid
+ "/" + legacy
.id, ptype
=ptype
)
183 def sendPresence(self
, tojid
=""):
186 avatarHash
= self
.avatar
.getImageHash()
187 caps
= Element((None, "c"))
188 caps
.attributes
["xmlns"] = disco
.CAPS
189 caps
.attributes
["node"] = legacy
.url
+ "/protocol/caps"
190 caps
.attributes
["ver"] = legacy
.version
192 tojid
=self
.contactList
.session
.jabberID
193 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
])
198 """ Represents the Jabber contact list """
199 def __init__(self
, session
):
200 LogEvent(INFO
, session
.jabberID
)
201 self
.session
= session
205 """ Cleanly removes the object """
206 LogEvent(INFO
, self
.session
.jabberID
)
207 for jid
in self
.contacts
:
208 self
.contacts
[jid
].updatePresence("", "", "unavailable")
209 self
.contacts
[jid
].removeMe()
212 self
.legacyList
= None
214 def resendLists(self
, tojid
=""):
215 for jid
in self
.contacts
:
216 if(self
.contacts
[jid
].status
!= "unavailable"):
217 self
.contacts
[jid
].sendPresence(tojid
)
218 LogEvent(INFO
, self
.session
.jabberID
)
220 def createContact(self
, jid
, sub
):
221 """ Creates a contact object. Use this to initialise the contact list
222 Returns a Contact object which you can call sync* methods on to synchronise
223 the user's legacy contact list with their Jabber list """
224 LogEvent(INFO
, self
.session
.jabberID
)
225 c
= Contact(jid
, sub
, self
)
226 self
.contacts
[jid
] = c
229 def getContact(self
, jid
):
230 """ Finds the contact. If one doesn't exist then a new one is created, with sub set to "none" """
231 if(not self
.contacts
.has_key(jid
)):
232 self
.contacts
[jid
] = Contact(jid
, "none", self
)
233 return self
.contacts
[jid
]
235 def findContact(self
, jid
):
236 if(self
.contacts
.has_key(jid
)):
237 return self
.contacts
[jid
]
240 def jabberSubscriptionReceived(self
, jid
, subtype
):
241 self
.getContact(jid
).jabberSubscriptionReceived(subtype
)