]>
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
27 self
.ptype
= "unavailable"
30 """ Destroys this object. Does not remove the contact from the server's list. """
31 self
.contactList
= None
34 def syncContactGrantedAuth(self
):
35 """ Since last using the transport the user has been granted authorisation by this contact.
36 Call this to synchronise the user's Jabber list with their legacy list after logon. """
37 if(self
.sub
== "none"):
39 elif(self
.sub
== "from"):
43 self
.updateRoster("subscribe")
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"):
54 self
.updateRoster("unsubscribed")
56 def syncUserGrantedAuth(self
):
57 """ Since last using the transport the user has granted authorisation to this contact.
58 Call this to synchronise the user's Jabber list with their legacy list after logon. """
59 if(self
.sub
== "none"):
61 elif(self
.sub
== "to"):
65 self
.updateRoster("subscribe")
67 def syncUserRemovedAuth(self
):
68 """ Since last using the transport the user has removed this contact's authorisation.
69 Call this to synchronise the user's Jabber list with their legacy list after logon. """
70 if(self
.sub
== "from"):
72 elif(self
.sub
== "both"):
76 self
.updateRoster("unsubscribe")
78 def syncGroups(self
, groups
, push
=True):
79 """ Set the groups that this contact is in on the legacy service.
80 By default this pushes the groups out with a presence subscribed packet. """
82 if push
: self
.updateRoster("subscribed");
84 def contactGrantsAuth(self
):
85 """ Live roster event """
86 if(self
.sub
== "none"):
88 elif(self
.sub
== "from"):
90 self
.sendSub("subscribed")
93 def contactRemovesAuth(self
):
94 """ Live roster event """
97 elif(self
.sub
== "both"):
99 self
.sendSub("unsubscribed")
101 def contactRequestsAuth(self
):
102 """ Live roster event """
103 self
.sendSub("subscribe")
105 def contactDerequestsAuth(self
):
106 """ Live roster event """
107 self
.sendSub("unsubscribe")
109 def jabberSubscriptionReceived(self
, subtype
):
110 """ Updates the subscription state internally and pushes the update to the legacy server """
111 if subtype
== "subscribe":
112 if self
.sub
== "to" or self
.sub
== "both":
113 self
.sendSub("subscribed")
114 self
.contactList
.legacyList
.addContact(self
.jid
)
116 elif subtype
== "subscribed":
117 if self
.sub
== "none":
121 self
.contactList
.legacyList
.authContact(self
.jid
)
123 elif subtype
== "unsubscribe":
124 if self
.sub
== "none" or self
.sub
== "from":
125 self
.sendSub("unsubscribed")
126 if self
.sub
== "both":
130 self
.contactList
.legacyList
.removeContact(self
.jid
)
132 elif(subtype
== "unsubscribed"):
133 if(self
.sub
== "both"):
135 if(self
.sub
== "from"):
137 self
.contactList
.legacyList
.deauthContact(self
.jid
)
139 def updateNickname(self
, nickname
, push
=True):
140 if(self
.nickname
!= nickname
):
141 self
.nickname
= nickname
142 if(push
): self
.sendPresence()
144 def updatePresence(self
, show
, status
, ptype
, force
=False):
145 updateFlag
= (self
.show
!= show
or self
.status
!= status
or self
.ptype
!= ptype
or force
)
152 def updateAvatar(self
, avatar
=None, push
=True):
153 if(self
.avatar
== avatar
): return
155 if(push
): self
.sendPresence()
157 def sendSub(self
, ptype
):
158 self
.contactList
.session
.sendPresence(to
=self
.contactList
.session
.jabberID
, fro
=self
.jid
, ptype
=ptype
)
160 def sendPresence(self
, tojid
=""):
163 avatarHash
= self
.avatar
.getImageHash()
164 caps
= Element((None, "c"))
165 caps
.attributes
["xmlns"] = disco
.CAPS
166 caps
.attributes
["node"] = legacy
.url
+ "/protocol/caps"
167 caps
.attributes
["ver"] = legacy
.version
169 tojid
=self
.contactList
.session
.jabberID
170 self
.contactList
.session
.sendPresence(to
=tojid
, fro
=self
.jid
, ptype
=self
.ptype
, show
=self
.show
, status
=self
.status
, avatarHash
=avatarHash
, nickname
=self
.nickname
, payload
=[caps
])
172 def updateRoster(self
, ptype
):
173 self
.contactList
.session
.sendRosterImport(jid
=self
.jid
, ptype
=ptype
, sub
=self
.sub
, groups
=self
.groups
, name
=self
.nickname
)
177 """ Represents the Jabber contact list """
178 def __init__(self
, session
):
179 LogEvent(INFO
, session
.jabberID
)
180 self
.session
= session
184 """ Cleanly removes the object """
185 LogEvent(INFO
, self
.session
.jabberID
)
186 for jid
in self
.contacts
:
187 self
.contacts
[jid
].updatePresence("", "", "unavailable")
188 self
.contacts
[jid
].removeMe()
191 self
.legacyList
= None
193 def resendLists(self
, tojid
=""):
194 for jid
in self
.contacts
:
195 if(self
.contacts
[jid
].status
!= "unavailable"):
196 self
.contacts
[jid
].sendPresence(tojid
)
197 LogEvent(INFO
, self
.session
.jabberID
)
199 def createContact(self
, jid
, sub
):
200 """ Creates a contact object. Use this to initialise the contact list
201 Returns a Contact object which you can call sync* methods on to synchronise
202 the user's legacy contact list with their Jabber list """
203 LogEvent(INFO
, self
.session
.jabberID
)
204 c
= Contact(jid
, sub
, self
)
205 self
.contacts
[jid
] = c
208 def getContact(self
, jid
):
209 """ Finds the contact. If one doesn't exist then a new one is created, with sub set to "none" """
210 if(not self
.contacts
.has_key(jid
)):
211 self
.contacts
[jid
] = Contact(jid
, "none", self
)
212 return self
.contacts
[jid
]
214 def findContact(self
, jid
):
215 if(self
.contacts
.has_key(jid
)):
216 return self
.contacts
[jid
]
219 def jabberSubscriptionReceived(self
, jid
, subtype
):
220 self
.getContact(jid
).jabberSubscriptionReceived(subtype
)